blob: a66cbe1700a9a40a15dca4663e92e6199c1662ae [file] [log] [blame]
<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 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>