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
|
<html>
<head>
<script type="text/javascript">
function print(str) {
document.writeln(str+"<br/>");
}
if (window.testRunner)
testRunner.dumpAsText();
</script>
</head>
<body>
1.0 Single Argument Testing<br/>
The following tests forEach with one argument, the callback. It should print the contents of the array [2, 5, 9] alongside each index.<br/><br/>
<script>
function printElt(element, index, array) {
print("[" + index + "] is " + element);
}
[2, 5, 9].forEach(printElt);
</script>
<br/>
2.0 Two Argument Testing<br/>
The following tests forEach with two arguments, the callback and the applied "this" object. It should print the contents of the array.<br/><br/>
<script>
var writer = {
sb: [],
write: function (s) {
this.sb.push(s);
},
writeln: function (s) {
this.write(s+"<br/>");
},
toString: function () {
return this.sb.join("");
}
};
[2, 5, 9].forEach(writer.writeln, writer);
print(writer.toString());
</script>
3.0 Array Mutation Tests<br/>
These tests the affects of array mutation during execution of forEach.<br/><br/>
3.1 Array Element Removal<br/>
This test removes elements from the array, these elements should thus not appear since forEach doesn't iterate over non-existing properties.<br/><br/>
<script>
function printEltAndPop(element, index, array) {
print("[" + index + "] is " + element);
array.pop();
}
[2, 5, 9].forEach(printEltAndPop);
</script>
<br/>
3.2 Array Element Addition<br/>
This test adds elements to the array, these elements should not appear since forEach uses the original length to create the range it iterates over. It should be identical to 1.0.<br/><br/>
<script>
function printEltAndPush(element, index, array) {
print("[" + index + "] is " + element);
array.push(1);
}
[2, 5, 9].forEach(printEltAndPush);
</script>
<br/>
3.3 Array Element Changing<br/>
This test changes elements in the array, these elements should appear in their mutated form when reached by forEach.<br/><br/>
<script>
function printEltAndChange(element, index, array) {
print("[" + index + "] is " + element);
array[array.length - 1 - index] = "changed";
}
[2, 5, 9].forEach(printEltAndChange);
</script>
<br/>
4.0 Exception Test<br/>
This test uses a function that throws an exception, and thus halts the execution of forEach.<br/><br/>
<script>
function printEltAndException(element, index, array) {
print("[" + index + "] is " + element);
if (index == 1)
throw "Exception thrown, execution halted!";
}
try {
[2, 5, 9].forEach(printEltAndException);
} catch (e) {
print(e);
}
</script>
<br/>
5.0 Wrong Type for Callback Test
This test sends in incorrect types for the callback parameter of forEach. An exception should be thrown in each case. There should be 6 type errors (and no crashes!):<br/><br/>
<script>
try {
[2, 5, 9].forEach(5);
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach("wrong");
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(new Object());
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(null);
} catch (e) {
print(e);
}
try {
[2, 5, 9].forEach(undefined);
} catch (e) {
print(e)
}
try {
[2, 5, 9].forEach();
} catch (e) {
print(e);
}
</script>
<br/>
6.0 Behavior for Holes in Arrays<br/>
This test checks that holes in arrays (indexes which have been deleted or are not present) are not included in enumeration:<br/><br/>
<script>
function throwIfUndefined(element, index, array) {
if (typeof element === "undefined")
throw "undefined element enumerated!";
}
var arr;
try {
arr = [5, 5, 5, 5];
delete arr[1];
arr.forEach(throwIfUndefined);
print("Manually deleted index not enumerated");
} catch (e) {
print(e);
}
try {
arr = new Array(20);
arr.forEach(throwIfUndefined);
print("Array created using constructor has no properties, so no indexes enumerated");
} catch (e) {
print(e);
}
</script>
<br/>
7.0 Return Value<br/>
This test checks that the return value of Array.prototype.forEach is undefined:<br/><br/>
<script>
var wasUndefined = typeof ([1, 2, 3].forEach(function(){})) === "undefined";
print("Return value is " + (wasUndefined ? "" : "NOT ") + "undefined!");
</script>
</body>
</html>
|