| 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/. */ |
| 7 |
|
| 8 |
#include "jsnum.h" |
| 9 |
|
| 10 |
#include "jsapi-tests/tests.h" |
| 11 |
#include "vm/JSContext.h" |
| 12 |
#include "vm/Realm.h" |
| 13 |
#include "vm/StaticStrings.h" |
| 14 |
|
| 15 |
#include "vm/StringType-inl.h" |
| 16 |
|
| 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"}, |
| 46 |
}; |
| 47 |
|
| 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); |
| 53 |
|
| 54 |
bool match = false; |
| 55 |
CHECK(JS_StringEqualsAscii(cx, str, test.expected, &match)); |
| 56 |
CHECK(match); |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
END_TEST(testIndexToString) |
| 62 |
|
| 63 |
BEGIN_TEST(testStringIsIndex) { |
| 64 |
for (const auto& test : tests) { |
| 65 |
uint32_t u = test.num; |
| 66 |
JSLinearString* str = js::IndexToString(cx, u); |
| 67 |
CHECK(str); |
| 68 |
|
| 69 |
uint32_t n; |
| 70 |
CHECK(str->isIndex(&n)); |
| 71 |
CHECK(u == n); |
| 72 |
} |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
END_TEST(testStringIsIndex) |
| 77 |
|
| 78 |
BEGIN_TEST(testStringToPropertyName) { |
| 79 |
uint32_t index; |
| 80 |
|
| 81 |
static const char16_t hiChars[] = {'h', 'i'}; |
| 82 |
JSLinearString* hiStr = NewString(cx, hiChars); |
| 83 |
CHECK(hiStr); |
| 84 |
CHECK(!hiStr->isIndex(&index)); |
| 85 |
CHECK(hiStr->toPropertyName(cx) != nullptr); |
| 86 |
|
| 87 |
static const char16_t maxChars[] = {'4', '2', '9', '4', '9', |
| 88 |
'6', '7', '2', '9', '4'}; |
| 89 |
JSLinearString* maxStr = NewString(cx, maxChars); |
| 90 |
CHECK(maxStr); |
| 91 |
CHECK(maxStr->isIndex(&index)); |
| 92 |
CHECK(index == UINT32_MAX - 1); |
| 93 |
|
| 94 |
static const char16_t maxPlusOneChars[] = {'4', '2', '9', '4', '9', |
| 95 |
'6', '7', '2', '9', '5'}; |
| 96 |
JSLinearString* maxPlusOneStr = NewString(cx, maxPlusOneChars); |
| 97 |
CHECK(maxPlusOneStr); |
| 98 |
CHECK(!maxPlusOneStr->isIndex(&index)); |
| 99 |
CHECK(maxPlusOneStr->toPropertyName(cx) != nullptr); |
| 100 |
|
| 101 |
static const char16_t maxNonUint32Chars[] = {'4', '2', '9', '4', '9', |
| 102 |
'6', '7', '2', '9', '6'}; |
| 103 |
JSLinearString* maxNonUint32Str = NewString(cx, maxNonUint32Chars); |
| 104 |
CHECK(maxNonUint32Str); |
| 105 |
CHECK(!maxNonUint32Str->isIndex(&index)); |
| 106 |
CHECK(maxNonUint32Str->toPropertyName(cx) != nullptr); |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
template <size_t N> |
| 112 |
static JSLinearString* NewString(JSContext* cx, const char16_t (&chars)[N]) { |
| 113 |
return js::NewStringCopyN<js::CanGC>(cx, chars, N); |
| 114 |
} |
| 115 |
|
| 116 |
END_TEST(testStringToPropertyName) |