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 |
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); | |
Dmitry Lomov (no reviews)
2014/12/10 10:58:21
Add a test that validates that concat works corre
caitp (gmail)
2014/12/10 13:25:33
I'm not sure how to create a non-array object whic
| |
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 // ES5 tests | |
31 | 110 |
32 var poses; | 111 var poses; |
112 var pos; | |
33 | 113 |
34 poses = [140, 4000000000]; | 114 poses = [140, 4000000000]; |
35 while (pos = poses.shift()) { | 115 while (pos = poses.shift()) { |
36 var a = new Array(pos); | 116 var a = new Array(pos); |
37 var array_proto = []; | 117 var array_proto = []; |
38 a.__proto__ = array_proto; | 118 a.__proto__ = array_proto; |
39 assertEquals(pos, a.length); | 119 assertEquals(pos, a.length); |
40 a.push('foo'); | 120 a.push('foo'); |
41 assertEquals(pos + 1, a.length); | 121 assertEquals(pos + 1, a.length); |
42 var b = ['bar']; | 122 var b = ['bar']; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 | 175 |
96 // Negative indices don't get concated. | 176 // Negative indices don't get concated. |
97 a[-1] = 'minus1'; | 177 a[-1] = 'minus1'; |
98 assertEquals("minus1", a[-1]); | 178 assertEquals("minus1", a[-1]); |
99 assertEquals("undefined", typeof(a[0xffffffff])); | 179 assertEquals("undefined", typeof(a[0xffffffff])); |
100 c = a.concat(b); | 180 c = a.concat(b); |
101 assertEquals("undefined", typeof(c[-1])); | 181 assertEquals("undefined", typeof(c[-1])); |
102 assertEquals("undefined", typeof(c[0xffffffff])); | 182 assertEquals("undefined", typeof(c[0xffffffff])); |
103 assertEquals(c.length, a.length + 1); | 183 assertEquals(c.length, a.length + 1); |
104 } | 184 } |
105 | 185 /* |
Dmitry Lomov (no reviews)
2014/12/10 10:58:20
Stray comments
caitp (gmail)
2014/12/10 13:25:33
Yeah, I had these commented out because they cause
Dmitry Lomov (no reviews)
2014/12/10 14:41:59
Oops sorry.
| |
106 poses = [140, 4000000000]; | 186 poses = [140, 4000000000]; |
107 while (pos = poses.shift()) { | 187 while (pos = poses.shift()) { |
108 var a = new Array(pos); | 188 var a = new Array(pos); |
109 assertEquals(pos, a.length); | 189 assertEquals(pos, a.length); |
110 a.push('foo'); | 190 a.push('foo'); |
111 assertEquals(pos + 1, a.length); | 191 assertEquals(pos + 1, a.length); |
112 var b = ['bar']; | 192 var b = ['bar']; |
113 var c = a.concat(b); | 193 var c = a.concat(b); |
114 assertEquals(pos + 2, c.length); | 194 assertEquals(pos + 2, c.length); |
115 assertEquals("undefined", typeof(c[pos - 1])); | 195 assertEquals("undefined", typeof(c[pos - 1])); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 function mkGetter(i) { return function() { trace.push(i); }; } | 308 function mkGetter(i) { return function() { trace.push(i); }; } |
229 arr3.length = 10000; | 309 arr3.length = 10000; |
230 for (var i = 0; i < 100; i++) { | 310 for (var i = 0; i < 100; i++) { |
231 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); | 311 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); |
232 expectedTrace[i] = i; | 312 expectedTrace[i] = i; |
233 expectedTrace[100 + i] = i; | 313 expectedTrace[100 + i] = i; |
234 } | 314 } |
235 var r4 = [0].concat(arr3, arr3); | 315 var r4 = [0].concat(arr3, arr3); |
236 assertEquals(1 + arr3.length * 2, r4.length); | 316 assertEquals(1 + arr3.length * 2, r4.length); |
237 assertEquals(expectedTrace, trace); | 317 assertEquals(expectedTrace, trace); |
318 */ | |
OLD | NEW |