Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jscntxt.h"
9 #include "jscompartment.h"
10 #include "jsnum.h"
11 #include "jsstr.h"
13 #include "jsapi-tests/tests.h"
15 #include "vm/String-inl.h"
17 using mozilla::ArrayLength;
19 static const struct TestPair {
20 uint32_t num;
21 const char *expected;
22 } tests[] = {
23 { 0, "0" },
24 { 1, "1" },
25 { 2, "2" },
26 { 9, "9" },
27 { 10, "10" },
28 { 15, "15" },
29 { 16, "16" },
30 { 17, "17" },
31 { 99, "99" },
32 { 100, "100" },
33 { 255, "255" },
34 { 256, "256" },
35 { 257, "257" },
36 { 999, "999" },
37 { 1000, "1000" },
38 { 4095, "4095" },
39 { 4096, "4096" },
40 { 9999, "9999" },
41 { 1073741823, "1073741823" },
42 { 1073741824, "1073741824" },
43 { 1073741825, "1073741825" },
44 { 2147483647, "2147483647" },
45 { 2147483648u, "2147483648" },
46 { 2147483649u, "2147483649" },
47 { 4294967294u, "4294967294" },
48 { 4294967295u, "4294967295" },
49 };
51 BEGIN_TEST(testIndexToString)
52 {
53 for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) {
54 uint32_t u = tests[i].num;
55 JSString *str = js::IndexToString(cx, u);
56 CHECK(str);
58 if (!js::StaticStrings::hasUint(u))
59 CHECK(cx->compartment()->dtoaCache.lookup(10, u) == str);
61 bool match = false;
62 CHECK(JS_StringEqualsAscii(cx, str, tests[i].expected, &match));
63 CHECK(match);
64 }
66 return true;
67 }
68 END_TEST(testIndexToString)
70 BEGIN_TEST(testStringIsIndex)
71 {
72 for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) {
73 uint32_t u = tests[i].num;
74 JSFlatString *str = js::IndexToString(cx, u);
75 CHECK(str);
77 uint32_t n;
78 CHECK(str->isIndex(&n));
79 CHECK(u == n);
80 }
82 return true;
83 }
84 END_TEST(testStringIsIndex)
86 BEGIN_TEST(testStringToPropertyName)
87 {
88 uint32_t index;
90 static const jschar hiChars[] = { 'h', 'i' };
91 JSFlatString *hiStr = NewString(cx, hiChars);
92 CHECK(hiStr);
93 CHECK(!hiStr->isIndex(&index));
94 CHECK(hiStr->toPropertyName(cx) != nullptr);
96 static const jschar maxChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '5' };
97 JSFlatString *maxStr = NewString(cx, maxChars);
98 CHECK(maxStr);
99 CHECK(maxStr->isIndex(&index));
100 CHECK(index == UINT32_MAX);
102 static const jschar maxPlusOneChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '6' };
103 JSFlatString *maxPlusOneStr = NewString(cx, maxPlusOneChars);
104 CHECK(maxPlusOneStr);
105 CHECK(!maxPlusOneStr->isIndex(&index));
106 CHECK(maxPlusOneStr->toPropertyName(cx) != nullptr);
108 return true;
109 }
111 template<size_t N> static JSFlatString *
112 NewString(JSContext *cx, const jschar (&chars)[N])
113 {
114 return js_NewStringCopyN<js::CanGC>(cx, chars, N);
115 }
117 END_TEST(testStringToPropertyName)