Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / array-foreach.html
blob9e21996eb15d177fa043ed48d0d21cd65ac5fe9e
1 <html>
2 <head>
3 <script type="text/javascript">
5 function print(str) {
6 document.writeln(str+"<br/>");
9 if (window.testRunner)
10 testRunner.dumpAsText();
11 </script>
12 </head>
13 <body>
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/>
16 <script>
17 function printElt(element, index, array) {
18 print("[" + index + "] is " + element);
21 [2, 5, 9].forEach(printElt);
22 </script>
23 <br/>
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/>
27 <script>
28 var writer = {
29 sb: [],
30 write: function (s) {
31 this.sb.push(s);
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());
44 </script>
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/>
51 <script>
52 function printEltAndPop(element, index, array) {
53 print("[" + index + "] is " + element);
54 array.pop();
57 [2, 5, 9].forEach(printEltAndPop);
58 </script>
59 <br/>
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/>
63 <script>
64 function printEltAndPush(element, index, array) {
65 print("[" + index + "] is " + element);
66 array.push(1);
69 [2, 5, 9].forEach(printEltAndPush);
70 </script>
71 <br/>
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/>
75 <script>
76 function printEltAndChange(element, index, array) {
77 print("[" + index + "] is " + element);
78 array[array.length - 1 - index] = "changed";
81 [2, 5, 9].forEach(printEltAndChange);
82 </script>
83 <br/>
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/>
86 <script>
87 function printEltAndException(element, index, array) {
88 print("[" + index + "] is " + element);
89 if (index == 1)
90 throw "Exception thrown, execution halted!";
92 try {
93 [2, 5, 9].forEach(printEltAndException);
94 } catch (e) {
95 print(e);
97 </script>
99 <br/>
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/>
102 <script>
104 try {
105 [2, 5, 9].forEach(5);
106 } catch (e) {
107 print(e);
110 try {
111 [2, 5, 9].forEach("wrong");
112 } catch (e) {
113 print(e);
116 try {
117 [2, 5, 9].forEach(new Object());
118 } catch (e) {
119 print(e);
122 try {
123 [2, 5, 9].forEach(null);
124 } catch (e) {
125 print(e);
128 try {
129 [2, 5, 9].forEach(undefined);
130 } catch (e) {
131 print(e)
134 try {
135 [2, 5, 9].forEach();
136 } catch (e) {
137 print(e);
140 </script>
142 <br/>
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/>
145 <script>
146 function throwIfUndefined(element, index, array) {
147 if (typeof element === "undefined")
148 throw "undefined element enumerated!";
151 var arr;
152 try {
153 arr = [5, 5, 5, 5];
154 delete arr[1];
155 arr.forEach(throwIfUndefined);
156 print("Manually deleted index not enumerated");
157 } catch (e) {
158 print(e);
161 try {
162 arr = new Array(20);
163 arr.forEach(throwIfUndefined);
164 print("Array created using constructor has no properties, so no indexes enumerated");
165 } catch (e) {
166 print(e);
169 </script>
171 <br/>
172 7.0 Return Value<br/>
173 This test checks that the return value of Array.prototype.forEach is undefined:<br/><br/>
174 <script>
175 var wasUndefined = typeof ([1, 2, 3].forEach(function(){})) === "undefined";
177 print("Return value is " + (wasUndefined ? "" : "NOT ") + "undefined!");
178 </script>
180 </body>
181 </html>