Bug 1931736. Add missing braces around if/loop statements in layout/xul/. r=layout...
[gecko.git] / js / src / jsapi-tests / testIndexToString.cpp
blob9074a4267cdd136544bf363431909cb3f5cc51a6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 "jsnum.h"
10 #include "jsapi-tests/tests.h"
11 #include "vm/JSContext.h"
12 #include "vm/Realm.h"
13 #include "vm/StaticStrings.h"
15 #include "vm/StringType-inl.h"
17 static const struct TestPair {
18 uint32_t num;
19 const char* expected;
20 } tests[] = {
21 {0, "0"},
22 {1, "1"},
23 {2, "2"},
24 {9, "9"},
25 {10, "10"},
26 {15, "15"},
27 {16, "16"},
28 {17, "17"},
29 {99, "99"},
30 {100, "100"},
31 {255, "255"},
32 {256, "256"},
33 {257, "257"},
34 {999, "999"},
35 {1000, "1000"},
36 {4095, "4095"},
37 {4096, "4096"},
38 {9999, "9999"},
39 {1073741823, "1073741823"},
40 {1073741824, "1073741824"},
41 {1073741825, "1073741825"},
42 {2147483647, "2147483647"},
43 {2147483648u, "2147483648"},
44 {2147483649u, "2147483649"},
45 {4294967294u, "4294967294"},
48 BEGIN_TEST(testIndexToString) {
49 for (const auto& test : tests) {
50 uint32_t u = test.num;
51 JSString* str = js::IndexToString(cx, u);
52 CHECK(str);
54 if (!js::StaticStrings::hasUint(u)) {
55 CHECK(cx->realm()->dtoaCache.lookup(10, u) == str);
58 bool match = false;
59 CHECK(JS_StringEqualsAscii(cx, str, test.expected, &match));
60 CHECK(match);
63 return true;
65 END_TEST(testIndexToString)
67 BEGIN_TEST(testStringIsIndex) {
68 for (const auto& test : tests) {
69 uint32_t u = test.num;
70 JSLinearString* str = js::IndexToString(cx, u);
71 CHECK(str);
73 uint32_t n;
74 CHECK(str->isIndex(&n));
75 CHECK(u == n);
78 return true;
80 END_TEST(testStringIsIndex)
82 BEGIN_TEST(testStringToPropertyName) {
83 uint32_t index;
85 static const char16_t hiChars[] = {'h', 'i'};
86 JSLinearString* hiStr = NewString(cx, hiChars);
87 CHECK(hiStr);
88 CHECK(!hiStr->isIndex(&index));
89 CHECK(hiStr->toPropertyName(cx) != nullptr);
91 static const char16_t maxChars[] = {'4', '2', '9', '4', '9',
92 '6', '7', '2', '9', '4'};
93 JSLinearString* maxStr = NewString(cx, maxChars);
94 CHECK(maxStr);
95 CHECK(maxStr->isIndex(&index));
96 CHECK(index == UINT32_MAX - 1);
98 static const char16_t maxPlusOneChars[] = {'4', '2', '9', '4', '9',
99 '6', '7', '2', '9', '5'};
100 JSLinearString* maxPlusOneStr = NewString(cx, maxPlusOneChars);
101 CHECK(maxPlusOneStr);
102 CHECK(!maxPlusOneStr->isIndex(&index));
103 CHECK(maxPlusOneStr->toPropertyName(cx) != nullptr);
105 static const char16_t maxNonUint32Chars[] = {'4', '2', '9', '4', '9',
106 '6', '7', '2', '9', '6'};
107 JSLinearString* maxNonUint32Str = NewString(cx, maxNonUint32Chars);
108 CHECK(maxNonUint32Str);
109 CHECK(!maxNonUint32Str->isIndex(&index));
110 CHECK(maxNonUint32Str->toPropertyName(cx) != nullptr);
112 return true;
115 template <size_t N>
116 static JSLinearString* NewString(JSContext* cx, const char16_t (&chars)[N]) {
117 return js::NewStringCopyN<js::CanGC>(cx, chars, N);
120 END_TEST(testStringToPropertyName)