3 <script type=
"text/javascript">
6 document
.writeln(str
+"<br/>");
10 testRunner
.dumpAsText();
14 1.0 Single Argument Testing
<br/>
15 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/>
17 function printElt(element
, index
, array
) {
18 print("[" + index
+ "] is " + element
);
21 [2, 5, 9].forEach(printElt
);
25 2.0 Two Argument Testing
<br/>
26 The following tests forEach with two arguments, the callback and the applied
"this" object. It should print the contents of the array.
<br/><br/>
33 writeln: function (s
) {
34 this.write(s
+"<br/>");
36 toString: function () {
37 return this.sb
.join("");
41 [2, 5, 9].forEach(writer
.writeln
, writer
);
42 print(writer
.toString());
46 3.0 Array Mutation Tests
<br/>
47 These tests the affects of array mutation during execution of forEach.
<br/><br/>
48 3.1 Array Element Removal
<br/>
49 This test removes elements from the array, these elements should thus not appear since forEach doesn't iterate over non-existing properties.
<br/><br/>
52 function printEltAndPop(element
, index
, array
) {
53 print("[" + index
+ "] is " + element
);
57 [2, 5, 9].forEach(printEltAndPop
);
61 3.2 Array Element Addition
<br/>
62 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/>
64 function printEltAndPush(element
, index
, array
) {
65 print("[" + index
+ "] is " + element
);
69 [2, 5, 9].forEach(printEltAndPush
);
73 3.3 Array Element Changing
<br/>
74 This test changes elements in the array, these elements should appear in their mutated form when reached by forEach.
<br/><br/>
76 function printEltAndChange(element
, index
, array
) {
77 print("[" + index
+ "] is " + element
);
78 array
[array
.length
- 1 - index
] = "changed";
81 [2, 5, 9].forEach(printEltAndChange
);
84 4.0 Exception Test
<br/>
85 This test uses a function that throws an exception, and thus halts the execution of forEach.
<br/><br/>
87 function printEltAndException(element
, index
, array
) {
88 print("[" + index
+ "] is " + element
);
90 throw "Exception thrown, execution halted!";
93 [2, 5, 9].forEach(printEltAndException
);
100 5.0 Wrong Type for Callback Test
101 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/>
105 [2, 5, 9].forEach(5);
111 [2, 5, 9].forEach("wrong");
117 [2, 5, 9].forEach(new Object());
123 [2, 5, 9].forEach(null);
129 [2, 5, 9].forEach(undefined);
143 6.0 Behavior for Holes in Arrays
<br/>
144 This test checks that holes in arrays (indexes which have been deleted or are not present) are not included in enumeration:
<br/><br/>
146 function throwIfUndefined(element
, index
, array
) {
147 if (typeof element
=== "undefined")
148 throw "undefined element enumerated!";
155 arr
.forEach(throwIfUndefined
);
156 print("Manually deleted index not enumerated");
163 arr
.forEach(throwIfUndefined
);
164 print("Array created using constructor has no properties, so no indexes enumerated");
172 7.0 Return Value
<br/>
173 This test checks that the return value of Array.prototype.forEach is undefined:
<br/><br/>
175 var wasUndefined
= typeof ([1, 2, 3].forEach(function(){})) === "undefined";
177 print("Return value is " + (wasUndefined
? "" : "NOT ") + "undefined!");