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:
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/. */
10 #include "jsapi-tests/tests.h"
11 #include "vm/JSContext.h"
13 #include "vm/StaticStrings.h"
15 #include "vm/StringType-inl.h"
17 static const struct TestPair
{
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
);
54 if (!js::StaticStrings::hasUint(u
)) {
55 CHECK(cx
->realm()->dtoaCache
.lookup(10, u
) == str
);
59 CHECK(JS_StringEqualsAscii(cx
, str
, test
.expected
, &match
));
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
);
74 CHECK(str
->isIndex(&n
));
80 END_TEST(testStringIsIndex
)
82 BEGIN_TEST(testStringToPropertyName
) {
85 static const char16_t hiChars
[] = {'h', 'i'};
86 JSLinearString
* hiStr
= NewString(cx
, hiChars
);
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
);
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);
116 static JSLinearString
* NewString(JSContext
* cx
, const char16_t (&chars
)[N
]) {
117 return js::NewStringCopyN
<js::CanGC
>(cx
, chars
, N
);
120 END_TEST(testStringToPropertyName
)