1// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28var loop_count = 5
29
30
31for (var i = 0; i < loop_count; i++) {
32  var a = new Array();
33  var b = Array();
34  assertEquals(0, a.length);
35  assertEquals(0, b.length);
36  for (var k = 0; k < 10; k++) {
37    assertEquals('undefined', typeof a[k]);
38    assertEquals('undefined', typeof b[k]);
39  }
40}
41
42
43for (var i = 0; i < loop_count; i++) {
44  for (var j = 0; j < 100; j++) {
45    var a = new Array(j);
46    var b = Array(j);
47    assertEquals(j, a.length);
48    assertEquals(j, b.length);
49    for (var k = 0; k < j; k++) {
50      assertEquals('undefined', typeof a[k]);
51      assertEquals('undefined', typeof b[k]);
52    }
53  }
54}
55
56
57for (var i = 0; i < loop_count; i++) {
58  a = new Array(0, 1);
59  assertArrayEquals([0, 1], a);
60  a = new Array(0, 1, 2);
61  assertArrayEquals([0, 1, 2], a);
62  a = new Array(0, 1, 2, 3);
63  assertArrayEquals([0, 1, 2, 3], a);
64  a = new Array(0, 1, 2, 3, 4);
65  assertArrayEquals([0, 1, 2, 3, 4], a);
66  a = new Array(0, 1, 2, 3, 4, 5);
67  assertArrayEquals([0, 1, 2, 3, 4, 5], a);
68  a = new Array(0, 1, 2, 3, 4, 5, 6);
69  assertArrayEquals([0, 1, 2, 3, 4, 5, 6], a);
70  a = new Array(0, 1, 2, 3, 4, 5, 6, 7);
71  assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7], a);
72  a = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8);
73  assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8], a);
74  a = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
75  assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], a);
76}
77
78
79function innerArrayLiteral(n) {
80  var a = new Array(n);
81  for (var i = 0; i < n; i++) {
82    a[i] = i * 2 + 7;
83  }
84  return a.join();
85}
86
87
88function testConstructOfSizeSize(n) {
89  var str = innerArrayLiteral(n);
90  var a = eval('[' + str + ']');
91  var b = eval('new Array(' + str + ')')
92  var c = eval('Array(' + str + ')')
93  assertEquals(n, a.length);
94  assertArrayEquals(a, b);
95  assertArrayEquals(a, c);
96}
97
98
99for (var i = 0; i < loop_count; i++) {
100  // JSObject::kInitialMaxFastElementArray is 10000.
101  for (var j = 1000; j < 12000; j += 1000) {
102    testConstructOfSizeSize(j);
103  }
104}
105
106
107for (var i = 0; i < loop_count; i++) {
108  assertArrayEquals(['xxx'], new Array('xxx'));
109  assertArrayEquals(['xxx'], Array('xxx'));
110  assertArrayEquals([true], new Array(true));
111  assertArrayEquals([false], Array(false));
112  assertArrayEquals([{a:1}], new Array({a:1}));
113  assertArrayEquals([{b:2}], Array({b:2}));
114}
115
116
117assertThrows('new Array(3.14)');
118assertThrows('Array(2.72)');
119
120// Make sure that throws occur in the context of the Array function.
121var b = Realm.create();
122var bArray = Realm.eval(b, "Array");
123var bError = Realm.eval(b, "RangeError");
124
125function verifier(array, error) {
126  try {
127    new array(3.14);
128  } catch(e) {
129    return e.__proto__ === error.__proto__;
130  }
131  assertTrue(false);  // should never get here.
132}
133
134
135assertTrue(verifier(Array, RangeError()));
136assertTrue(verifier(bArray, bError()));
137assertFalse(verifier(Array, bError()));
138assertFalse(verifier(bArray, RangeError()));
139