Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / src / jsapi-tests / testArgumentsObject.cpp
blob8378de7e08cb60047a8537662feb93f8e7d3424b
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 "jsapi-tests/tests.h"
10 #include "vm/ArgumentsObject-inl.h"
11 #include "vm/JSObject-inl.h"
13 using namespace js;
15 static const char NORMAL_ZERO[] = "function f() { return arguments; }";
16 static const char NORMAL_ONE[] = "function f(a) { return arguments; }";
17 static const char NORMAL_TWO[] = "function f(a, b) { return arguments; }";
18 static const char NORMAL_THREE[] = "function f(a, b, c) { return arguments; }";
20 static const char STRICT_ZERO[] =
21 "function f() { 'use strict'; return arguments; }";
22 static const char STRICT_ONE[] =
23 "function f() { 'use strict'; return arguments; }";
24 static const char STRICT_TWO[] =
25 "function f() { 'use strict'; return arguments; }";
26 static const char STRICT_THREE[] =
27 "function f() { 'use strict'; return arguments; }";
29 static const char* const CALL_CODES[] = {"f()", "f(0)",
30 "f(0, 1)", "f(0, 1, 2)",
31 "f(0, 1, 2, 3)", "f(0, 1, 2, 3, 4)"};
33 BEGIN_TEST(testArgumentsObject) {
34 return ExhaustiveTest<0>(NORMAL_ZERO) && ExhaustiveTest<1>(NORMAL_ZERO) &&
35 ExhaustiveTest<2>(NORMAL_ZERO) && ExhaustiveTest<0>(NORMAL_ONE) &&
36 ExhaustiveTest<1>(NORMAL_ONE) && ExhaustiveTest<2>(NORMAL_ONE) &&
37 ExhaustiveTest<3>(NORMAL_ONE) && ExhaustiveTest<0>(NORMAL_TWO) &&
38 ExhaustiveTest<1>(NORMAL_TWO) && ExhaustiveTest<2>(NORMAL_TWO) &&
39 ExhaustiveTest<3>(NORMAL_TWO) && ExhaustiveTest<4>(NORMAL_TWO) &&
40 ExhaustiveTest<0>(NORMAL_THREE) && ExhaustiveTest<1>(NORMAL_THREE) &&
41 ExhaustiveTest<2>(NORMAL_THREE) && ExhaustiveTest<3>(NORMAL_THREE) &&
42 ExhaustiveTest<4>(NORMAL_THREE) && ExhaustiveTest<5>(NORMAL_THREE) &&
43 ExhaustiveTest<0>(STRICT_ZERO) && ExhaustiveTest<1>(STRICT_ZERO) &&
44 ExhaustiveTest<2>(STRICT_ZERO) && ExhaustiveTest<0>(STRICT_ONE) &&
45 ExhaustiveTest<1>(STRICT_ONE) && ExhaustiveTest<2>(STRICT_ONE) &&
46 ExhaustiveTest<3>(STRICT_ONE) && ExhaustiveTest<0>(STRICT_TWO) &&
47 ExhaustiveTest<1>(STRICT_TWO) && ExhaustiveTest<2>(STRICT_TWO) &&
48 ExhaustiveTest<3>(STRICT_TWO) && ExhaustiveTest<4>(STRICT_TWO) &&
49 ExhaustiveTest<0>(STRICT_THREE) && ExhaustiveTest<1>(STRICT_THREE) &&
50 ExhaustiveTest<2>(STRICT_THREE) && ExhaustiveTest<3>(STRICT_THREE) &&
51 ExhaustiveTest<4>(STRICT_THREE) && ExhaustiveTest<5>(STRICT_THREE);
54 static const size_t MAX_ELEMS = 6;
56 template <size_t ArgCount>
57 bool ExhaustiveTest(const char funcode[]) {
58 RootedValue v(cx);
59 EVAL(funcode, &v);
61 EVAL(CALL_CODES[ArgCount], &v);
62 Rooted<ArgumentsObject*> argsobj(cx,
63 &v.toObjectOrNull()->as<ArgumentsObject>());
65 JS::RootedValueArray<MAX_ELEMS> elems(cx);
67 for (size_t i = 0; i <= ArgCount; i++) {
68 for (size_t j = 0; j <= ArgCount - i; j++) {
69 ClearElements(elems);
70 CHECK(argsobj->maybeGetElements(i, j, elems.begin()));
71 for (size_t k = 0; k < j; k++) {
72 CHECK(elems[k].isInt32(i + k));
74 for (size_t k = j; k < MAX_ELEMS - 1; k++) {
75 CHECK(elems[k].isNull());
77 CHECK(elems[MAX_ELEMS - 1].isInt32(42));
81 return true;
84 template <size_t N>
85 static void ClearElements(JS::RootedValueArray<N>& elems) {
86 for (size_t i = 0; i < elems.length() - 1; i++) {
87 elems[i].setNull();
89 elems[elems.length() - 1].setInt32(42);
91 END_TEST(testArgumentsObject)