blob: f7c52c7f7c92c3682148f59dcc081141af95a723 [file] [log] [blame]
<html>
<head>
<script src="../http/tests/inspector/inspector-test.js"></script>
<script>
function test()
{
InspectorTest.runTestSuite([
function binaryIndexOfTest(next)
{
var testArrays = [
[],
[1],
[1, 10],
[1, 10, 11, 12, 13, 14, 100],
[-100, -50, 0, 50, 100],
[-100, -14, -13, -12, -11, -10, -1]
];
function testArray(array)
{
function comparator(a, b)
{
return a < b ? -1 : (a > b ? 1 : 0);
}
for (var i = -100; i <= 100; ++i) {
var reference = array.indexOf(i);
var actual = array.binaryIndexOf(i, comparator);
InspectorTest.assertEquals(reference, actual, "binaryIndexOf");
}
return true;
}
for (var i = 0, l = testArrays.length; i < l; ++i)
testArray(testArrays[i]);
next();
},
function lowerBoundTest(next)
{
var testArrays = [
[],
[1],
[-1, -1, 0, 0, 0, 0, 2, 3, 4, 4, 4, 7, 9, 9, 9]
];
function testArray(array, useComparator)
{
function comparator(a, b)
{
return a < b ? -1 : (a > b ? 1 : 0);
}
for (var value = -2; value <= 12; ++value) {
var index = useComparator ? array.lowerBound(value, comparator) : array.lowerBound(value);
InspectorTest.assertTrue(0 <= index && index <= array.length, "index is within bounds");
InspectorTest.assertTrue(index === 0 || array[index - 1] < value, "array[index - 1] < value");
InspectorTest.assertTrue(index === array.length || array[index] >= value, "array[index] >= value");
}
}
for (var i = 0, l = testArrays.length; i < l; ++i) {
testArray(testArrays[i], false);
testArray(testArrays[i], true);
}
next();
},
function upperBoundTest(next)
{
var testArrays = [
[],
[1],
[-1, -1, 0, 0, 0, 0, 2, 3, 4, 4, 4, 7, 9, 9, 9]
];
function testArray(array, useComparator)
{
function comparator(a, b)
{
return a < b ? -1 : (a > b ? 1 : 0);
}
for (var value = -2; value <= 12; ++value) {
var index = useComparator ? array.upperBound(value, comparator) : array.upperBound(value);
InspectorTest.assertTrue(0 <= index && index <= array.length, "index is within bounds");
InspectorTest.assertTrue(index === 0 || array[index - 1] <= value, "array[index - 1] <= value");
InspectorTest.assertTrue(index === array.length || array[index] > value, "array[index] > value");
}
}
for (var i = 0, l = testArrays.length; i < l; ++i) {
testArray(testArrays[i], false);
testArray(testArrays[i], true);
}
next();
},
function qselectTest(next)
{
var testArrays = [
[],
[0],
[0, 0, 0, 0, 0, 0, 0, 0],
[4, 3, 2, 1],
[1, 2, 3, 4, 5],
[-1, 3, 2, 7, 7, 7, 10, 12, 3, 4, -1, 2]
];
function testArray(array)
{
function compare(a, b)
{
return a - b;
}
var sorted = array.slice(0).sort(compare);
var reference = {
min: sorted[0],
median: sorted[Math.floor(sorted.length / 2)],
max: sorted[sorted.length - 1]
}
var actual = {
min: array.slice(0).qselect(0),
median: array.slice(0).qselect(Math.floor(array.length / 2)),
max: array.slice(0).qselect(array.length - 1)
}
InspectorTest.addResult("Array: " + JSON.stringify(array));
InspectorTest.addResult("Reference: " + JSON.stringify(reference));
InspectorTest.addResult("Actual: " + JSON.stringify(actual));
}
for (var i = 0, l = testArrays.length; i < l; ++i)
testArray(testArrays[i]);
next();
},
function sortRangeTest(next)
{
var testArrays = [
[],
[1],
[2, 1],
[6, 4, 2, 7, 10, 15, 1],
[10, 44, 3, 6, 56, 66, 10, 55, 32, 56, 2, 5]
];
function testArray(array)
{
function comparator(a, b)
{
return a < b ? -1 : (a > b ? 1 : 0);
}
function compareArrays(a, b, message)
{
InspectorTest.assertEquals(JSON.stringify(a), JSON.stringify(b), message);
}
for (var left = 0, l = array.length - 1; left < l; ++left) {
for (var right = left + 1, r = array.length; right < r; ++right)
for (var count = 1, k = right - left + 1; count <= k; ++count) {
var actual = array.slice(0);
actual.sortRange(comparator, left, right, count);
compareArrays(array.slice(0, left), actual.slice(0, left), "left " + left + " " + right + " " + count);
compareArrays(array.slice(right + 1), actual.slice(right + 1), "right " + left + " " + right + " " + count);
var middle = array.slice(left, right + 1);
middle.sort(comparator);
compareArrays(middle.slice(0, count), actual.slice(left, left + count), "sorted " + left + " " + right + " " + count);
actualRest = actual.slice(left + count, right + 1);
actualRest.sort(comparator);
compareArrays(middle.slice(count), actualRest, "unsorted " + left + " " + right + " " + count);
}
}
}
for (var i = 0, len = testArrays.length; i < len; ++i)
testArray(testArrays[i]);
next();
},
function naturalOrderComparatorTest(next)
{
var testArray = [
"dup",
"a1",
"a4222",
"a91",
"a07",
"dup",
"a7",
"a007",
"abc00",
"abc0",
"abc",
"abcd",
"abc000",
"x10y20z30",
"x9y19z29",
"dup",
"x09y19z29",
"x10y22z23",
"x10y19z43",
"1",
"10",
"11",
"dup",
"2", "2", "2",
"555555",
"5",
"5555",
"dup",
];
for (var i = 0, n = testArray.length; i < n; ++i)
InspectorTest.assertEquals(0, String.naturalOrderComparator(testArray[i], testArray[i]), "comparing equal strings");
testArray.sort(String.naturalOrderComparator);
InspectorTest.addResult("Sorted in natural order: [" + testArray.join(", ") + "]");
// Check comparator's transitivity.
for (var i = 0, n = testArray.length; i < n; ++i) {
for (var j = 0; j < n; ++j) {
var a = testArray[i];
var b = testArray[j];
var diff = String.naturalOrderComparator(a, b);
if (diff === 0)
InspectorTest.assertEquals(a, b, "zero diff");
else if (diff < 0)
InspectorTest.assertTrue(i < j);
else
InspectorTest.assertTrue(i > j);
}
}
next();
}
]);
}
</script>
</head>
<body onload="runTest()">
<p>
This test checks Web Inspector utilities.
</p>
</body>
</html>