1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testIndexToString.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include "jscntxt.h" 1.12 +#include "jscompartment.h" 1.13 +#include "jsnum.h" 1.14 +#include "jsstr.h" 1.15 + 1.16 +#include "jsapi-tests/tests.h" 1.17 + 1.18 +#include "vm/String-inl.h" 1.19 + 1.20 +using mozilla::ArrayLength; 1.21 + 1.22 +static const struct TestPair { 1.23 + uint32_t num; 1.24 + const char *expected; 1.25 +} tests[] = { 1.26 + { 0, "0" }, 1.27 + { 1, "1" }, 1.28 + { 2, "2" }, 1.29 + { 9, "9" }, 1.30 + { 10, "10" }, 1.31 + { 15, "15" }, 1.32 + { 16, "16" }, 1.33 + { 17, "17" }, 1.34 + { 99, "99" }, 1.35 + { 100, "100" }, 1.36 + { 255, "255" }, 1.37 + { 256, "256" }, 1.38 + { 257, "257" }, 1.39 + { 999, "999" }, 1.40 + { 1000, "1000" }, 1.41 + { 4095, "4095" }, 1.42 + { 4096, "4096" }, 1.43 + { 9999, "9999" }, 1.44 + { 1073741823, "1073741823" }, 1.45 + { 1073741824, "1073741824" }, 1.46 + { 1073741825, "1073741825" }, 1.47 + { 2147483647, "2147483647" }, 1.48 + { 2147483648u, "2147483648" }, 1.49 + { 2147483649u, "2147483649" }, 1.50 + { 4294967294u, "4294967294" }, 1.51 + { 4294967295u, "4294967295" }, 1.52 +}; 1.53 + 1.54 +BEGIN_TEST(testIndexToString) 1.55 +{ 1.56 + for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) { 1.57 + uint32_t u = tests[i].num; 1.58 + JSString *str = js::IndexToString(cx, u); 1.59 + CHECK(str); 1.60 + 1.61 + if (!js::StaticStrings::hasUint(u)) 1.62 + CHECK(cx->compartment()->dtoaCache.lookup(10, u) == str); 1.63 + 1.64 + bool match = false; 1.65 + CHECK(JS_StringEqualsAscii(cx, str, tests[i].expected, &match)); 1.66 + CHECK(match); 1.67 + } 1.68 + 1.69 + return true; 1.70 +} 1.71 +END_TEST(testIndexToString) 1.72 + 1.73 +BEGIN_TEST(testStringIsIndex) 1.74 +{ 1.75 + for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) { 1.76 + uint32_t u = tests[i].num; 1.77 + JSFlatString *str = js::IndexToString(cx, u); 1.78 + CHECK(str); 1.79 + 1.80 + uint32_t n; 1.81 + CHECK(str->isIndex(&n)); 1.82 + CHECK(u == n); 1.83 + } 1.84 + 1.85 + return true; 1.86 +} 1.87 +END_TEST(testStringIsIndex) 1.88 + 1.89 +BEGIN_TEST(testStringToPropertyName) 1.90 +{ 1.91 + uint32_t index; 1.92 + 1.93 + static const jschar hiChars[] = { 'h', 'i' }; 1.94 + JSFlatString *hiStr = NewString(cx, hiChars); 1.95 + CHECK(hiStr); 1.96 + CHECK(!hiStr->isIndex(&index)); 1.97 + CHECK(hiStr->toPropertyName(cx) != nullptr); 1.98 + 1.99 + static const jschar maxChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '5' }; 1.100 + JSFlatString *maxStr = NewString(cx, maxChars); 1.101 + CHECK(maxStr); 1.102 + CHECK(maxStr->isIndex(&index)); 1.103 + CHECK(index == UINT32_MAX); 1.104 + 1.105 + static const jschar maxPlusOneChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '6' }; 1.106 + JSFlatString *maxPlusOneStr = NewString(cx, maxPlusOneChars); 1.107 + CHECK(maxPlusOneStr); 1.108 + CHECK(!maxPlusOneStr->isIndex(&index)); 1.109 + CHECK(maxPlusOneStr->toPropertyName(cx) != nullptr); 1.110 + 1.111 + return true; 1.112 +} 1.113 + 1.114 +template<size_t N> static JSFlatString * 1.115 +NewString(JSContext *cx, const jschar (&chars)[N]) 1.116 +{ 1.117 + return js_NewStringCopyN<js::CanGC>(cx, chars, N); 1.118 +} 1.119 + 1.120 +END_TEST(testStringToPropertyName)