1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99:
3 : */
4 :
5 : #include "tests.h"
6 :
7 : #include "jscntxt.h"
8 : #include "jscompartment.h"
9 : #include "jsnum.h"
10 : #include "jsstr.h"
11 :
12 : #include "vm/String-inl.h"
13 :
14 : using namespace mozilla;
15 :
16 : template<size_t N> JSFlatString *
17 3 : NewString(JSContext *cx, const jschar (&chars)[N])
18 : {
19 3 : return js_NewStringCopyN(cx, chars, N);
20 : }
21 :
22 : static const struct TestPair {
23 : uint32_t num;
24 : const char *expected;
25 : } tests[] = {
26 : { 0, "0" },
27 : { 1, "1" },
28 : { 2, "2" },
29 : { 9, "9" },
30 : { 10, "10" },
31 : { 15, "15" },
32 : { 16, "16" },
33 : { 17, "17" },
34 : { 99, "99" },
35 : { 100, "100" },
36 : { 255, "255" },
37 : { 256, "256" },
38 : { 257, "257" },
39 : { 999, "999" },
40 : { 1000, "1000" },
41 : { 4095, "4095" },
42 : { 4096, "4096" },
43 : { 9999, "9999" },
44 : { 1073741823, "1073741823" },
45 : { 1073741824, "1073741824" },
46 : { 1073741825, "1073741825" },
47 : { 2147483647, "2147483647" },
48 : { 2147483648u, "2147483648" },
49 : { 2147483649u, "2147483649" },
50 : { 4294967294u, "4294967294" },
51 : { 4294967295u, "4294967295" },
52 : };
53 :
54 4 : BEGIN_TEST(testIndexToString)
55 : {
56 27 : for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) {
57 26 : uint32_t u = tests[i].num;
58 26 : JSString *str = js::IndexToString(cx, u);
59 26 : CHECK(str);
60 :
61 26 : if (!js::StaticStrings::hasUint(u))
62 15 : CHECK(cx->compartment->dtoaCache.lookup(10, u) == str);
63 :
64 26 : JSBool match = JS_FALSE;
65 26 : CHECK(JS_StringEqualsAscii(cx, str, tests[i].expected, &match));
66 26 : CHECK(match);
67 : }
68 :
69 1 : return true;
70 : }
71 1 : END_TEST(testIndexToString)
72 :
73 4 : BEGIN_TEST(testStringIsIndex)
74 : {
75 27 : for (size_t i = 0, sz = ArrayLength(tests); i < sz; i++) {
76 26 : uint32_t u = tests[i].num;
77 26 : JSFlatString *str = js::IndexToString(cx, u);
78 26 : CHECK(str);
79 :
80 : uint32_t n;
81 26 : CHECK(str->isIndex(&n));
82 26 : CHECK(u == n);
83 : }
84 :
85 1 : return true;
86 : }
87 1 : END_TEST(testStringIsIndex)
88 :
89 4 : BEGIN_TEST(testStringToPropertyName)
90 : {
91 : uint32_t index;
92 :
93 : static const jschar hiChars[] = { 'h', 'i' };
94 1 : JSFlatString *hiStr = NewString(cx, hiChars);
95 1 : CHECK(hiStr);
96 1 : CHECK(!hiStr->isIndex(&index));
97 1 : CHECK(hiStr->toPropertyName(cx) != NULL);
98 :
99 : static const jschar maxChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '5' };
100 1 : JSFlatString *maxStr = NewString(cx, maxChars);
101 1 : CHECK(maxStr);
102 1 : CHECK(maxStr->isIndex(&index));
103 1 : CHECK(index == UINT32_MAX);
104 :
105 : static const jschar maxPlusOneChars[] = { '4', '2', '9', '4', '9', '6', '7', '2', '9', '6' };
106 1 : JSFlatString *maxPlusOneStr = NewString(cx, maxPlusOneChars);
107 1 : CHECK(maxPlusOneStr);
108 1 : CHECK(!maxPlusOneStr->isIndex(&index));
109 1 : CHECK(maxPlusOneStr->toPropertyName(cx) != NULL);
110 :
111 1 : return true;
112 : }
113 3 : END_TEST(testStringToPropertyName)
|