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