| 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 <string> |
| 9 |
|
| 10 |
#include "jsfriendapi.h" |
| 11 |
|
| 12 |
#include "jsapi-tests/tests.h" |
| 13 |
|
| 14 |
static const struct TestTuple { |
| 15 |
/* The string being tested. */ |
| 16 |
const char16_t* string; |
| 17 |
/* The number of characters from the string to use. */ |
| 18 |
size_t length; |
| 19 |
/* Whether this string is an index. */ |
| 20 |
bool isindex; |
| 21 |
/* If it's an index, what index it is. Ignored if not an index. */ |
| 22 |
uint32_t index; |
| 23 |
|
| 24 |
constexpr TestTuple(const char16_t* string, bool isindex, uint32_t index) |
| 25 |
: TestTuple(string, std::char_traits<char16_t>::length(string), isindex, |
| 26 |
index) {} |
| 27 |
|
| 28 |
constexpr TestTuple(const char16_t* string, size_t length, bool isindex, |
| 29 |
uint32_t index) |
| 30 |
: string(string), length(length), isindex(isindex), index(index) {} |
| 31 |
} tests[] = { |
| 32 |
{u"0", true, 0}, |
| 33 |
{u"1", true, 1}, |
| 34 |
{u"2", true, 2}, |
| 35 |
{u"9", true, 9}, |
| 36 |
{u"10", true, 10}, |
| 37 |
{u"15", true, 15}, |
| 38 |
{u"16", true, 16}, |
| 39 |
{u"17", true, 17}, |
| 40 |
{u"99", true, 99}, |
| 41 |
{u"100", true, 100}, |
| 42 |
{u"255", true, 255}, |
| 43 |
{u"256", true, 256}, |
| 44 |
{u"257", true, 257}, |
| 45 |
{u"999", true, 999}, |
| 46 |
{u"1000", true, 1000}, |
| 47 |
{u"4095", true, 4095}, |
| 48 |
{u"4096", true, 4096}, |
| 49 |
{u"9999", true, 9999}, |
| 50 |
{u"1073741823", true, 1073741823}, |
| 51 |
{u"1073741824", true, 1073741824}, |
| 52 |
{u"1073741825", true, 1073741825}, |
| 53 |
{u"2147483647", true, 2147483647}, |
| 54 |
{u"2147483648", true, 2147483648u}, |
| 55 |
{u"2147483649", true, 2147483649u}, |
| 56 |
{u"4294967294", true, 4294967294u}, |
| 57 |
{u"4294967295", false, |
| 58 |
0}, // Not an array index because need to be able to represent length |
| 59 |
{u"-1", false, 0}, |
| 60 |
{u"abc", false, 0}, |
| 61 |
{u" 0", false, 0}, |
| 62 |
{u"0 ", false, 0}, |
| 63 |
// Tests to make sure the passed-in length is taken into account |
| 64 |
{u"0 ", 1, true, 0}, |
| 65 |
{u"123abc", 3, true, 123}, |
| 66 |
{u"123abc", 2, true, 12}, |
| 67 |
}; |
| 68 |
|
| 69 |
BEGIN_TEST(testStringIsArrayIndex) { |
| 70 |
for (const auto& test : tests) { |
| 71 |
uint32_t index; |
| 72 |
bool isindex = js::StringIsArrayIndex(test.string, test.length, &index); |
| 73 |
CHECK_EQUAL(isindex, test.isindex); |
| 74 |
if (isindex) { |
| 75 |
CHECK_EQUAL(index, test.index); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
END_TEST(testStringIsArrayIndex) |