forked from jerryscript-project/jerryscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-prototype-slice.js
More file actions
191 lines (158 loc) · 4.92 KB
/
array-prototype-slice.js
File metadata and controls
191 lines (158 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This test will not pass on FLOAT32 due to precision issues
var array = [54, undefined, "Lemon", -127];
var array1 = array.slice();
var array2 = array.slice("a", "3");
var array3 = array.slice(-2);
var array4 = array.slice(-12, undefined);
var array5 = array.slice(undefined, -3);
var array6 = array.slice(Infinity, NaN);
var array7 = array.slice(-Infinity, Infinity);
var array8 = array.slice(NaN, -Infinity);
assert (array1.length == 4);
assert (array1[0] == 54);
assert (array1[1] == undefined);
assert (array1[2] == "Lemon");
assert (array1[3] == -127);
assert (array2.length == 3);
assert (array2[0] == 54);
assert (array2[1] == undefined);
assert (array2[2] == "Lemon");
assert (array3.length == 2);
assert (array3[0] == "Lemon");
assert (array3[1] == -127);
assert (array4.length == 4);
assert (array4[0] == 54);
assert (array4[1] == undefined);
assert (array4[2] == "Lemon");
assert (array4[3] == -127);
assert (array5.length == 1);
assert (array5[0] == 54);
assert (array6.length == 0);
assert (array7.length == 4);
assert (array7[0] == 54);
assert (array7[1] == undefined);
assert (array7[2] == "Lemon");
assert (array7[3] == -127);
assert (array8.length == 0);
var array = [];
array[4294967293] = "foo";
array.length = 4294967295;
var result = array.slice(4294967293, -1)
assert(result.length === 1)
assert(result[0] === "foo")
array[0] = "bar";
var result = array.slice(-4294967295, -4294967294)
assert(result.length === 1)
assert(result[0] === "bar")
var array = [];
array[0] = "foo";
var result = array.slice(4294967296, 4294967297);
assert(result.length === 0);
array[4294967293] = "bar";
var result = array.slice(-4294967297, -4294967296);
assert(result.length === 0);
var arr = [1,2];
Array.prototype[0] = 3;
var newArr = arr.slice(0, 1);
delete Array.prototype[0];
assert(newArr.hasOwnProperty("0"));
assert(newArr[0] === 1);
// Checking behavior when unable to get length
var obj = { slice : Array.prototype.slice };
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
try {
obj.slice(1, 2);
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
// Checking behavior when unable to get element
var obj = { length : 1, slice : Array.prototype.slice };
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
try {
obj.slice(0, 1);
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
/* ES v5.1 15.4.4.10.5.
Checking behavior when start value throws exception */
var arg1 = { };
Object.defineProperty(arg1, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
var obj = { slice : Array.prototype.slice };
try {
obj.slice(arg1);
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}
/* ES v5.1 15.4.4.10.7.
Checking behavior when end value throws exception */
var arg2 = { };
Object.defineProperty(arg2, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
var obj = { slice : Array.prototype.slice };
try {
obj.slice(0, arg2);
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}
/* ES v5.1 15.4.4.10.10.
Checking behavior when unable to get element */
var obj = { length : 3, slice : Array.prototype.slice };
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("foo"); } });
try {
obj.slice(0, 3);
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
function array_check(result_array, expected_array) {
assert(result_array instanceof Array);
assert(result_array.length === expected_array.length);
for (var idx = 0; idx < expected_array.length; idx++) {
assert(result_array[idx] === expected_array[idx]);
}
}
// Remove the buffer
var array = [1, 2, 3, 4, 5];
var value = array.slice(4, {
valueOf: function() {
array.length = 0;
}
})
array_check(value, []);
// Extend the buffer
var array = [1, 2, 3, 4, 5];
var value = array.slice(6, {
valueOf: function() {
array.length = 10;
}
})
array_check(value, []);
// Reduce the buffer
var array = [1, 2, 3, 4, 5];
var value = array.slice(1, {
valueOf: function() {
array.length = 3;
}
})
array_check(value, []);