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