OLD | NEW |
---|---|
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // modification, are permitted provided that the following conditions are | 3 // found in the LICENSE file. |
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 | 4 |
28 /** | 5 // Flags: --harmony-arrays --harmony-classes |
Dmitry Lomov (no reviews)
2014/12/10 23:02:47
Add a test for sloppy arguments (install isConcatS
| |
29 * @fileoverview Test concat on small and large arrays | 6 |
30 */ | 7 "use strict"; |
8 | |
9 (function testArrayConcatArity() { | |
10 assertEquals(1, Array.prototype.concat.length); | |
11 })(); | |
12 | |
13 | |
14 (function testArrayConcatNoPrototype() { | |
15 assertEquals(void 0, Array.prototype.concat.prototype); | |
16 })(); | |
17 | |
18 | |
19 (function testArrayConcatDescriptor() { | |
20 var desc = Object.getOwnPropertyDescriptor(Array.prototype, 'concat'); | |
21 assertEquals(false, desc.enumerable); | |
22 })(); | |
23 | |
24 | |
25 (function testConcatArrayLike() { | |
26 var obj = { | |
27 "length": 6, | |
28 "1": "A", | |
29 "3": "B", | |
30 "5": "C" | |
31 }; | |
32 obj[Symbol.isConcatSpreadable] = true; | |
33 var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" }; | |
34 var arr = ["X", "Y", "Z"]; | |
35 assertEquals([void 0, "A", void 0, "B", void 0, "C", | |
36 { "length": 3, "0": "0", "1": "1", "2": "2" }, | |
37 "X", "Y", "Z"], Array.prototype.concat.call(obj, obj2, arr)); | |
38 })(); | |
39 | |
40 | |
41 (function testConcatHoleyArray() { | |
42 var arr = []; | |
43 arr[4] = "Item 4"; | |
44 arr[8] = "Item 8"; | |
45 var arr2 = [".", "!", "?"]; | |
46 assertEquals([void 0, void 0, void 0, void 0, "Item 4", void 0, void 0, | |
47 void 0, "Item 8", ".", "!", "?"], arr.concat(arr2)); | |
48 })(); | |
49 | |
50 | |
51 (function testIsConcatSpreadableGetterThrows() { | |
52 function MyError() {} | |
53 var obj = {}; | |
54 Object.defineProperty(obj, Symbol.isConcatSpreadable, { | |
55 get: function() { throw new MyError(); } | |
56 }); | |
57 | |
58 assertThrows(function() { | |
59 [].concat(obj); | |
60 }, MyError); | |
61 | |
62 assertThrows(function() { | |
63 Array.prototype.concat.call(obj, 1, 2, 3); | |
64 }, MyError); | |
65 })(); | |
66 | |
67 | |
68 (function testConcatLengthThrows() { | |
69 function MyError() {} | |
70 var obj = {}; | |
71 obj[Symbol.isConcatSpreadable] = true; | |
72 Object.defineProperty(obj, "length", { | |
73 get: function() { throw new MyError(); } | |
74 }); | |
75 | |
76 assertThrows(function() { | |
77 [].concat(obj); | |
78 }, MyError); | |
79 | |
80 assertThrows(function() { | |
81 Array.prototype.concat.call(obj, 1, 2, 3); | |
82 }, MyError); | |
83 })(); | |
84 | |
85 | |
86 (function testConcatArraySubclass() { | |
87 // TODO(caitp): when concat is called on instances of classes which extend | |
88 // Array, they should: | |
89 // | |
90 // - return an instance of the class, rather than an Array instance (if from | |
91 // same Realm) | |
92 // - always treat such classes as concat-spreadable | |
93 })(); | |
94 | |
95 | |
96 (function testConcatNonArray() { | |
97 class NonArray { | |
98 constructor() { Array.apply(this, arguments); } | |
99 }; | |
100 | |
101 var obj = new NonArray(1,2,3); | |
102 var result = Array.prototype.concat.call(obj, 4, 5, 6); | |
103 assertEquals(Array, result.constructor); | |
104 assertEquals([obj,4,5,6], result); | |
105 assertFalse(result instanceof NonArray); | |
106 })(); | |
107 | |
108 | |
109 function testConcatTypedArray(type, elems, modulo) { | |
110 var items = new Array(elems); | |
111 for (var i = 0; i < elems; ++i) { | |
112 items[i] = modulo === false ? i : elems % modulo; | |
113 } | |
114 var ta = new type(items); | |
115 assertEquals([ta, ta], [].concat(ta, ta)); | |
116 ta[Symbol.isConcatSpreadable] = true; | |
117 assertEquals(items, [].concat(ta)); | |
118 } | |
119 | |
120 (function testConcatSmallTypedArray() { | |
121 var max = [2^8, 2^16, 2^32, false, false]; | |
122 [ | |
123 Uint8Array, | |
124 Uint16Array, | |
125 Uint32Array, | |
126 Float32Array, | |
127 Float64Array | |
128 ].forEach(function(ctor, i) { | |
129 testConcatTypedArray(ctor, 150, max[i]); | |
Dmitry Lomov (no reviews)
2014/12/10 16:36:37
Make this smaller, like 2. Default value of FLAG_t
| |
130 }); | |
131 })(); | |
132 | |
133 | |
134 (function testConcatLargeTypedArray() { | |
135 var max = [2^8, 2^16, 2^32, false, false]; | |
136 [ | |
137 Uint8Array, | |
138 Uint16Array, | |
139 Uint32Array, | |
140 Float32Array, | |
141 Float64Array | |
142 ].forEach(function(ctor, i) { | |
143 testConcatTypedArray(ctor, 4000000, max[i]); | |
144 }); | |
145 })(); | |
146 | |
147 | |
148 // ES5 tests | |
31 | 149 |
32 var poses; | 150 var poses; |
151 var pos; | |
33 | 152 |
34 poses = [140, 4000000000]; | 153 poses = [140, 4000000000]; |
35 while (pos = poses.shift()) { | 154 while (pos = poses.shift()) { |
36 var a = new Array(pos); | 155 var a = new Array(pos); |
37 var array_proto = []; | 156 var array_proto = []; |
38 a.__proto__ = array_proto; | 157 a.__proto__ = array_proto; |
39 assertEquals(pos, a.length); | 158 assertEquals(pos, a.length); |
40 a.push('foo'); | 159 a.push('foo'); |
41 assertEquals(pos + 1, a.length); | 160 assertEquals(pos + 1, a.length); |
42 var b = ['bar']; | 161 var b = ['bar']; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 function mkGetter(i) { return function() { trace.push(i); }; } | 347 function mkGetter(i) { return function() { trace.push(i); }; } |
229 arr3.length = 10000; | 348 arr3.length = 10000; |
230 for (var i = 0; i < 100; i++) { | 349 for (var i = 0; i < 100; i++) { |
231 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); | 350 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); |
232 expectedTrace[i] = i; | 351 expectedTrace[i] = i; |
233 expectedTrace[100 + i] = i; | 352 expectedTrace[100 + i] = i; |
234 } | 353 } |
235 var r4 = [0].concat(arr3, arr3); | 354 var r4 = [0].concat(arr3, arr3); |
236 assertEquals(1 + arr3.length * 2, r4.length); | 355 assertEquals(1 + arr3.length * 2, r4.length); |
237 assertEquals(expectedTrace, trace); | 356 assertEquals(expectedTrace, trace); |
OLD | NEW |