1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --harmony-tolength
6
7// Test array functions do not cause infinite loops when length is negative,
8// max_value, etc.
9
10// ArrayToString
11
12var o = { length: Number.MIN_VALUE };
13var result = Array.prototype.toString.call(o);
14assertEquals("[object Object]", result);
15
16// ArrayToLocaleString
17
18var o = { length: Number.MIN_VALUE };
19var result = Array.prototype.toLocaleString.call(o);
20assertEquals("", result);
21
22// ArrayJoin
23
24var o = { length: Number.MIN_VALUE };
25var result = Array.prototype.join.call(o);
26assertEquals(0, result.length);
27
28// ArrayPush
29
30var o = { length: Number.MIN_VALUE };
31Array.prototype.push.call(o, 1);
32assertEquals(1, o.length);
33assertEquals(1, o[0]);
34
35var o = { length: Number.MAX_VALUE };
36assertThrows(() => Array.prototype.push.call(o, 1), TypeError);
37
38// ArrayPop
39
40var o = { length: 0 };
41Array.prototype.pop.call(o);
42assertEquals(0, o.length);
43
44var o = { length: Number.MIN_VALUE };
45Array.prototype.pop.call(o);
46assertEquals(0, o.length);
47
48var o = { length: Number.MAX_VALUE };
49Array.prototype.pop.call(o);
50assertEquals(o.length, Number.MAX_SAFE_INTEGER - 1);
51
52// ArrayReverse
53
54var o = { 0: 'foo', length: Number.MIN_VALUE }
55var result = Array.prototype.reverse.call(o);
56assertEquals('object', typeof(result));
57assertEquals(Number.MIN_VALUE, result.length);
58assertEquals(Number.MIN_VALUE, o.length);
59
60// ArrayShift
61
62var o = { 0: "foo", length: Number.MIN_VALUE }
63var result = Array.prototype.shift.call(o);
64assertEquals(undefined, result);
65assertEquals(0, o.length);
66
67// ArrayUnshift
68
69var o = { length: 0 };
70Array.prototype.unshift.call(o);
71assertEquals(0, o.length);
72
73var o = { length: 0 };
74Array.prototype.unshift.call(o, 'foo');
75assertEquals('foo', o[0]);
76assertEquals(1, o.length);
77
78var o = { length: Number.MIN_VALUE };
79Array.prototype.unshift.call(o);
80assertEquals(0, o.length);
81
82var o = { length: Number.MIN_VALUE };
83Array.prototype.unshift.call(o, 'foo');
84assertEquals('foo', o[0]);
85assertEquals(1, o.length);
86
87// ArraySplice
88
89var o = { length: Number.MIN_VALUE };
90Array.prototype.splice.call(o);
91assertEquals(0, o.length);
92
93var o = { length: Number.MIN_VALUE };
94Array.prototype.splice.call(o, 0, 10, ['foo']);
95assertEquals(['foo'], o[0]);
96assertEquals(1, o.length);
97
98var o = { length: Number.MIN_VALUE };
99Array.prototype.splice.call(o, -1);
100assertEquals(0, o.length);
101
102var o = { length: Number.MAX_SAFE_INTEGER };
103Array.prototype.splice.call(o, -1);
104assertEquals(Number.MAX_SAFE_INTEGER - 1, o.length);
105
106// ArraySlice
107
108var o = { length: Number.MIN_VALUE };
109var result = Array.prototype.slice.call(o);
110assertEquals(0, result.length);
111
112var o = { length: Number.MIN_VALUE };
113var result = Array.prototype.slice.call(o, Number.MAX_VALUE);
114assertEquals(0, result.length);
115
116var o = { length: Number.MAX_VALUE };
117var result = Array.prototype.slice.call(o, Number.MAX_VALUE - 1);
118assertEquals(0, result.length);
119
120// ArrayIndexOf
121
122var o = { length: Number.MIN_VALUE };
123var result = Array.prototype.indexOf.call(o);
124assertEquals(-1, result);
125
126var o = { length: Number.MAX_SAFE_INTEGER }
127o[Number.MAX_SAFE_INTEGER - 1] = "foo"
128var result = Array.prototype.indexOf.call(o,
129    "foo", Number.MAX_SAFE_INTEGER - 2);
130assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
131
132var o = { length: Number.MAX_SAFE_INTEGER };
133o[Number.MAX_SAFE_INTEGER - 1] = "foo";
134var result = Array.prototype.indexOf.call(o, "foo", -1);
135assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
136
137// ArrayLastIndexOf
138
139var o = { length: Number.MIN_VALUE };
140var result = Array.prototype.lastIndexOf.call(o);
141assertEquals(-1, result);
142
143var o = { length: Number.MAX_SAFE_INTEGER }
144o[Number.MAX_SAFE_INTEGER - 1] = "foo"
145var result = Array.prototype.lastIndexOf.call(o,
146    "foo", Number.MAX_SAFE_INTEGER);
147assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
148
149var o = { length: Number.MAX_SAFE_INTEGER };
150o[Number.MAX_SAFE_INTEGER - 1] = "foo";
151var result = Array.prototype.lastIndexOf.call(o, "foo", -1);
152assertEquals(Number.MAX_SAFE_INTEGER - 1, result);
153
154// ArrayFilter
155
156var func = function(v) { return v; }
157
158var o = { length: Number.MIN_VALUE };
159Array.prototype.filter.call(o, func);
160assertEquals(Number.MIN_VALUE, o.length);
161
162// ArrayForEach
163
164var o = { length: Number.MIN_VALUE };
165Array.prototype.forEach.call(o, func);
166assertEquals(Number.MIN_VALUE, o.length);
167
168// ArraySome
169
170var o = { length: Number.MIN_VALUE };
171Array.prototype.some.call(o, func);
172assertEquals(Number.MIN_VALUE, o.length);
173
174// ArrayEvery
175
176var o = { length: Number.MIN_VALUE };
177Array.prototype.every.call(o, func);
178assertEquals(Number.MIN_VALUE, o.length);
179
180// ArrayMap
181
182var o = { length: Number.MIN_VALUE };
183Array.prototype.map.call(o, func);
184assertEquals(Number.MIN_VALUE, o.length);
185
186// ArrayReduce
187
188var o = { length: Number.MIN_VALUE };
189Array.prototype.reduce.call(o, func, 0);
190assertEquals(Number.MIN_VALUE, o.length);
191
192// ArrayReduceRight
193
194var o = { length: Number.MIN_VALUE };
195Array.prototype.reduceRight.call(o, func, 0);
196assertEquals(Number.MIN_VALUE, o.length);
197
198// ArrayFill
199
200var o = { length: Number.MIN_VALUE };
201Array.prototype.fill(o, 0);
202assertEquals(Number.MIN_VALUE, o.length);
203