-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patharray.html
More file actions
344 lines (300 loc) · 8.72 KB
/
Copy patharray.html
File metadata and controls
344 lines (300 loc) · 8.72 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>array tests</title>
<script>
curl = {
baseUrl: '../',
paths: {
curl: 'test/curl'
},
packages: [
{ name: 'poly', location: '.', main: 'poly' }
]
};
</script>
<script src="curl.js"></script>
<script>
var natives = {
forEach: Array.prototype.forEach,
map: Array.prototype.map,
every: Array.prototype.every,
some: Array.prototype.some,
indexOf: Array.prototype.indexOf,
lastIndexOf: Array.prototype.lastIndexOf,
reduce: Array.prototype.reduce,
reduceRight: Array.prototype.reduceRight
};
// First, test that poly/array doesn't clobber native methods
curl(['test/testutils', 'poly/array', 'domReady!'], function (tester) {
var test = [];
// Note: these tests won't run in evirons that don't support .forEach()
if (natives.forEach) {
for (var m in natives) {
tester.assertTrue('shim doesn\'t clobber ' + m, function () { return test[m] == natives[m]; });
}
}
}).next(['curl/_privileged'], function (priv) {
// remove poly/array from cache
delete priv.cache['poly/array'];
// For testing in an ES env, remove shimmed methods first
Array.prototype.forEach = null;
Array.prototype.map = null;
Array.prototype.every = null;
Array.prototype.some = null;
Array.prototype.indexOf = null;
Array.prototype.lastIndexOf = null;
Array.prototype.reduce = null;
Array.prototype.reduceRight = null;
delete Array.isArray;
}).next(['test/testutils', 'poly/array'], runTests);
function runTests (tester) {
var methodNames, testArray;
methodNames = {
forEach: 1,
map: 1,
every: 1,
some: 1,
indexOf: 1,
lastIndexOf: 1,
reduce: 1,
reduceRight: 1
};
testArray = [
0, 1, 2, 3,
"four",
{ five: 'five' },
0,
"seven"
];
// check for existence of array prototype methods
for (var m in methodNames) {
tester.assertTrue(m + ' exists', function () {
return typeof [][m] == 'function';
});
}
// test some/every
// some/every helper
function isGreaterThanFive(a) {
return a > 5;
}
tester.assertEquals('some handles empty array', function() {
return [].some(isGreaterThanFive);
}, false);
tester.assertEquals('some hits nothing', function() {
return [1, 2, 3].some(isGreaterThanFive);
}, false);
tester.assertEquals('some hits some items', function() {
return [1, 2, 7].some(isGreaterThanFive);
}, true);
tester.assertEquals('some hits all items', function() {
return [6, 7, 8].some(isGreaterThanFive);
}, true);
tester.assertEquals('every handles empty array', function() {
return [].every(isGreaterThanFive);
}, true);
tester.assertEquals('every hits nothing', function() {
return [1, 2, 3].every(isGreaterThanFive);
}, false);
tester.assertEquals('every hits some items', function() {
return [1, 2, 7].every(isGreaterThanFive);
}, false);
tester.assertEquals('every hits all items', function() {
return [6, 7, 8].every(isGreaterThanFive);
}, true);
// test forEach
tester.assertTrue('forEach hits all items', function () {
var expected, sum;
expected = 0;
sum = 0;
testArray.forEach(function (item, i) {
sum += i;
});
for (var i = 0; i < testArray.length; i++) {
expected += i;
}
return expected === sum;
});
tester.assertTrue('forEach applies context correctly', function () {
var context = { sum: 0 };
testArray.forEach(function (item, i) {
this.sum++;
}, context);
return context.sum == 8;
});
// test map and filter
tester.assertTrue('map skips missing items in "sparse array"', function () {
var obj = {};
testArray.forEach(function (item, i) {
obj[i] = item;
});
delete obj[6];
obj.length = testArray.length;
return [].map.call(obj, function (item) {
return item.toString();
})[6] === undefined;
});
tester.assertTrue('map processes all values', function () {
var strings = testArray.map(function (item) {
return item.toString();
});
return strings.every(function (item) {
return typeof item == 'string';
});
});
tester.assertEquals('filter removes items', function () {
var filtered = testArray.filter(function (item) {
return typeof item == 'number';
});
return filtered.length;
}, 5);
//
// reduce tests
//
// reduce helper
function sum(a, b) {
return a + b;
}
function sub(a, b) {
return a - b;
}
// tests
tester.assertEquals('reduce handles valid input', function() {
return [1,1,1].reduce(sum);
}, 3);
tester.assertEquals('reduce is left to right', function() {
return [3,2,1].reduce(sub);
}, 0);
tester.assertEquals('reduce handles valid input with initial value', function() {
return [1,1,1].reduce(sum, 1);
}, 4);
tester.assertEquals('reduce handles sparse arrays', function() {
return [,1,,1,1].reduce(sum);
}, 3);
tester.assertEquals('reduce returns initial value for empty array', function() {
return [].reduce(sum, 1);
}, 1);
tester.assertTrue('reduce throws TypeError for empty array and no initial value', function() {
try {
[].reduce(sum);
return false;
} catch(e) {
return e instanceof TypeError;
}
});
// tests
tester.assertEquals('reduceRight handles valid input', function() {
return [1,1,1].reduceRight(sum);
}, 3);
tester.assertEquals('reduceRight is right to left', function() {
return [1,2,3].reduceRight(sub);
}, 0);
tester.assertEquals('reduceRight handles valid input with initial value', function() {
return [1,1,1].reduceRight(sum, 1);
}, 4);
tester.assertEquals('reduceRight handles sparse arrays', function() {
return [,1,,1,1].reduceRight(sum);
}, 3);
tester.assertEquals('reduceRight returns initial value for empty array', function() {
return [].reduceRight(sum, 1);
}, 1);
tester.assertTrue('reduceRight throws TypeError for empty array and no initial value', function() {
try {
[].reduceRight(sum);
return false;
} catch(e) {
return e instanceof TypeError;
}
});
// indexOf and lastIndexOf tests
tester.assertEquals('indexOf finds item in first position', function () {
return testArray.indexOf(0);
}, 0);
tester.assertEquals('indexOf finds item in last position', function () {
return testArray.indexOf('seven');
}, 7);
tester.assertEquals('indexOf finds item from index', function () {
return testArray.indexOf(0, 3);
}, 6);
tester.assertEquals('lastIndexOf finds item in first position', function () {
return testArray.lastIndexOf(0);
}, 6);
tester.assertEquals('lastIndexOf finds item in last position', function () {
return testArray.lastIndexOf('seven');
}, 7);
tester.assertEquals('lastIndexOf finds item from index', function () {
return testArray.lastIndexOf(0, 3);
}, 0);
// test isArray
tester.assertTrue('Array.isArray on an array is true', function () {
return Array.isArray(testArray);
});
tester.assertFalse('Array.isArray on a string is false', function () {
return Array.isArray('foo');
});
tester.assertFalse('Array.isArray on an array-like object is false', function () {
var alo = {};
alo.length = 1;
alo['1'] = 1;
return Array.isArray(alo);
});
// test generics on array-like objects
tester.assertEquals('Array methods should apply correctly to strings', function () {
try {
return [].reduce.call('abcdefg', function (val, ch) { return val + 1; }, 0);
}
catch (ex) {
return ex.message;
}
}, 7);
tester.assertEquals('Array methods should apply correctly to array-like objects', function () {
// create a sparse array-like object that reduces to 7
var alo = {};
alo.length = 13;
alo[1] = alo[3] = alo[4] = alo[7] = alo[8] = alo[9] = alo[10] = 1;
try {
return [].reduce.call(alo, function (val, one) { return val + one; }, 0);
}
catch (ex) {
return ex.message;
}
}, 7);
tester.assertEquals('// Array methods should not skip over undefined items in array (fails in IE)', function () {
// create a normal array with undefined items
// reduce should count every item
var arr = [1, void 0, 1, void 0, 1];
try {
return arr.reduce(function (val, num) {
return val + 1;
}, 0);
}
catch (ex) {
return ex.message;
}
}, 5);
// // these won't run correctly on most browsers since .call(null) implies .call(window)
// tester.assertTrue('Array methods should throw TypeError when applied to null', function () {
// try {
// [].forEach.call(null, function (ch) {});
// return false;
// }
// catch (ex) {
// return ex instanceof TypeError;
// }
// });
// tester.assertTrue('Array methods should throw TypeError when applied to undefined', function () {
// try {
// [].forEach.call(void 0, function (ch) {});
// return false;
// }
// catch (ex) {
// return ex instanceof TypeError;
// }
// });
}
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>