summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/js/array-some.html
blob: b51f59b524a11f1345cdd13f6bddde6de35212d1 (plain)
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
<html>
<head>
<script type="text/javascript">

function print(str) {
    document.writeln(str+"<br/>");
}

if (window.layoutTestController)
    layoutTestController.dumpAsText();
</script>
</head>
<body>
1.0 Single Argument Testing<br/>
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/>
<script>
function isBigEnough(element, index, array) {
    return (element >= 10);
}

print([2, 5, 8, 1, 4].some(isBigEnough));
print([12, 5, 8, 1, 44].some(isBigEnough));
</script>
<br/>

2.0 Two Argument Testing<br/>
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/>
<script>
    var predicate = {
        comparison:     8,
        isBigEnough:    function(s) {
            return (s >= this.comparison);
        }
    };
    
    print([2, 5, 8, 1, 4].some(predicate.isBigEnough, predicate));
    print([12, 5, 8, 1, 44].some(predicate.isBigEnough, predicate));

</script>
<br/>

3.0 Array Mutation Tests<br/>
These tests the affects of array mutation during execution of some.<br/><br/>
3.1 Array Element Removal<br/>
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/>

<script>
function isBigEnoughAndPop(element, index, array) {
    array.pop();
    return (element >= 44);
}

print([2, 5, 8, 1, 4].some(isBigEnoughAndPop));
print([12, 5, 8, 1, 44].some(isBigEnoughAndPop));
</script>
<br/>

3.2 Array Element Addition<br/>
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/>
<script>
function isBigEnoughAndPush(element, index, array) {
    array.push(11);
    return (element >= 10);
}

print([2, 5, 8, 1, 4].some(isBigEnoughAndPush));
print([12, 5, 8, 1, 44].some(isBigEnoughAndPush));
</script>
<br/>

3.3 Array Element Changing<br/>
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/>
<script>
function isBigEnoughAndChange(element, index, array) {
    array[array.length - 1 - index] = 10;
    return (element >= 10);
}

print([2, 5, 8, 1, 4].some(isBigEnoughAndChange));
print([12, 5, 8, 1, 44].some(isBigEnoughAndChange));
</script>
<br/>
4.0 Exception Test<br/>
This test uses a function that throws an exception, and thus halts the execution of some.  There should be 2 exceptions thrown.<br/><br/>
<script>
function isBigEnoughAndException(element, index, array) {
    throw "Exception thrown, execution halted!";
    return (element >= 10);
}

try {
    [2, 5, 8, 1, 4].some(isBigEnoughAndException);
} catch (e) {
    print(e);
}

try {
    [12, 5, 8, 1, 44].some(isBigEnoughAndException);
} catch (e) {
    print(e);
}

</script>
<br/>
5.0 Wrong Type for Callback Test<br/>
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/>
<script>

try {
    [12, 5, 8, 1, 44].some(5)
} catch (e) {
    print(e);
}

try {
    [12, 5, 8, 1, 44].some("wrong");
} catch (e) {
    print(e);
}

try {
    [12, 5, 8, 1, 44].some(new Object());
} catch (e) {
    print(e);
}

try {
    [12, 5, 8, 1, 44].some(null);
} catch (e) {
    print(e);
}

try {
    [12, 5, 8, 1, 44].some(undefined);
} catch (e) {
    print(e)
}

try {
    [12, 5, 8, 1, 44].some();
} catch (e) {
    print(e);
}

</script>
<br/>
6.0 Early Abortion ("Short Circuiting")
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/>
<script>
function isBigEnough(element, index, array) {
    print("Testing element " + element + "...");
    return (element >= 10);
}

[2, 5, 8, 1, 4].some(isBigEnough);
print("Done with first array.");
[12, 5, 8, 1, 44].some(isBigEnough);
print("Done with second array.");

</script>
<br/>
7.0 Behavior with Holes in Arrays<br/>
This test checks that the callback function is not invoked for holes in the array. Five arrays are tested:<br/><br/>
<script>
function isBigEnough(element, index, array) {
    print("Testing element " + element + "...");
    return (element >= 10);
}

var arr = [2, 5, 8, 1, 4];
delete arr[1];
arr.some(isBigEnough);
print("Done with first array.");

arr = [undefined];
arr.some(isBigEnough);
print("Done with second array.");

delete arr[0];
arr.some(isBigEnough);
print("Done with third array.");

arr = new Array(20);
arr.some(isBigEnough);
print("Done with fourth array.");

arr[17] = undefined;
arr.some(isBigEnough);
print("Done with fifth array.");

</script>
</body>
</html>