Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / array-some.html
bloba66cbe1700a9a40a15dca4663e92e6199c1662ae
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 every with one argument, the callback. It should print whether the arrays [2, 5, 8, 1, 4] and [12, 5, 8, 1, 44] contain any numbers >= to 10 (false and true, respectively).<br/><br/>
16 <script>
17 function isBigEnough(element, index, array) {
18 return (element >= 10);
21 print([2, 5, 8, 1, 4].some(isBigEnough));
22 print([12, 5, 8, 1, 44].some(isBigEnough));
23 </script>
24 <br/>
26 2.0 Two Argument Testing<br/>
27 The following tests every with two arguments, the callback and the applied "this" object. It should print whether the arrays [2, 5, 8, 1, 4] and [12, 5, 8, 1, 44] contain any numbers >= 8. Both should yield "true".<br/><br/>
28 <script>
29 var predicate = {
30 comparison: 8,
31 isBigEnough: function(s) {
32 return (s >= this.comparison);
36 print([2, 5, 8, 1, 4].some(predicate.isBigEnough, predicate));
37 print([12, 5, 8, 1, 44].some(predicate.isBigEnough, predicate));
39 </script>
40 <br/>
42 3.0 Array Mutation Tests<br/>
43 These tests the affects of array mutation during execution of some.<br/><br/>
44 3.1 Array Element Removal<br/>
45 This test is equivalent to 1.0, with the exception that it tests whether elements are >= 44 instead of 10, and that it removes elements from the array on each visit. Both should thus yield "false" since undefined is not >= to 44.<br/><br/>
47 <script>
48 function isBigEnoughAndPop(element, index, array) {
49 array.pop();
50 return (element >= 44);
53 print([2, 5, 8, 1, 4].some(isBigEnoughAndPop));
54 print([12, 5, 8, 1, 44].some(isBigEnoughAndPop));
55 </script>
56 <br/>
58 3.2 Array Element Addition<br/>
59 This test is equivalent to 1.0, with the exception that it adds elements greater than 10 to the end of the list. However, the results should be the same as those in 1.0 since some uses the original length to create the range it iterates over.<br/><br/>
60 <script>
61 function isBigEnoughAndPush(element, index, array) {
62 array.push(11);
63 return (element >= 10);
66 print([2, 5, 8, 1, 4].some(isBigEnoughAndPush));
67 print([12, 5, 8, 1, 44].some(isBigEnoughAndPush));
68 </script>
69 <br/>
71 3.3 Array Element Changing<br/>
72 This test is equivalent to 1.0, with the exception that it changes elements in the array to be > 10 in reverse order. These elements should appear in their mutated form when reached by every, and thus both tests should result in "true".<br/><br/>
73 <script>
74 function isBigEnoughAndChange(element, index, array) {
75 array[array.length - 1 - index] = 10;
76 return (element >= 10);
79 print([2, 5, 8, 1, 4].some(isBigEnoughAndChange));
80 print([12, 5, 8, 1, 44].some(isBigEnoughAndChange));
81 </script>
82 <br/>
83 4.0 Exception Test<br/>
84 This test uses a function that throws an exception, and thus halts the execution of some. There should be 2 exceptions thrown.<br/><br/>
85 <script>
86 function isBigEnoughAndException(element, index, array) {
87 throw "Exception thrown, execution halted!";
88 return (element >= 10);
91 try {
92 [2, 5, 8, 1, 4].some(isBigEnoughAndException);
93 } catch (e) {
94 print(e);
97 try {
98 [12, 5, 8, 1, 44].some(isBigEnoughAndException);
99 } catch (e) {
100 print(e);
103 </script>
104 <br/>
105 5.0 Wrong Type for Callback Test<br/>
106 This test sends in incorrect types for the callback parameter of every. An exception should be thrown in each case. There should be 6 type errors (and no crashes!):<br/><br/>
107 <script>
109 try {
110 [12, 5, 8, 1, 44].some(5)
111 } catch (e) {
112 print(e);
115 try {
116 [12, 5, 8, 1, 44].some("wrong");
117 } catch (e) {
118 print(e);
121 try {
122 [12, 5, 8, 1, 44].some(new Object());
123 } catch (e) {
124 print(e);
127 try {
128 [12, 5, 8, 1, 44].some(null);
129 } catch (e) {
130 print(e);
133 try {
134 [12, 5, 8, 1, 44].some(undefined);
135 } catch (e) {
136 print(e)
139 try {
140 [12, 5, 8, 1, 44].some();
141 } catch (e) {
142 print(e);
145 </script>
146 <br/>
147 6.0 Early Abortion ("Short Circuiting")
148 This test is nearly identical to 1.0, except that it prints upon every call to the designated callback function. Since some aborts as soon as it finds one qualifying element, the first array should print 5 times, and the second only once.<br/><br/>
149 <script>
150 function isBigEnough(element, index, array) {
151 print("Testing element " + element + "...");
152 return (element >= 10);
155 [2, 5, 8, 1, 4].some(isBigEnough);
156 print("Done with first array.");
157 [12, 5, 8, 1, 44].some(isBigEnough);
158 print("Done with second array.");
160 </script>
161 <br/>
162 7.0 Behavior with Holes in Arrays<br/>
163 This test checks that the callback function is not invoked for holes in the array. Five arrays are tested:<br/><br/>
164 <script>
165 function isBigEnough(element, index, array) {
166 print("Testing element " + element + "...");
167 return (element >= 10);
170 var arr = [2, 5, 8, 1, 4];
171 delete arr[1];
172 arr.some(isBigEnough);
173 print("Done with first array.");
175 arr = [undefined];
176 arr.some(isBigEnough);
177 print("Done with second array.");
179 delete arr[0];
180 arr.some(isBigEnough);
181 print("Done with third array.");
183 arr = new Array(20);
184 arr.some(isBigEnough);
185 print("Done with fourth array.");
187 arr[17] = undefined;
188 arr.some(isBigEnough);
189 print("Done with fifth array.");
191 </script>
192 </body>
193 </html>