blob: ae36de9e368efb2addd199e23533a0257d8e5874 [file] [log] [blame]
<!doctype html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
let suite = InspectorTest.createSyncSuite("ArrayUtilities");
suite.addTestCase({
name: "Array.prototype.lowerBound",
test() {
// Index: 0 1 2 3 4 5 6 7 8 9
let arr = [0, 1, 2, 2, 2, 2, 2, 2, 6, 7];
InspectorTest.expectEqual(arr.lowerBound(-100), 0, "lowerBound of a value before the first value should produce the first index.");
InspectorTest.expectEqual(arr.lowerBound(0), 0, "lowerBound of a value in the list should return the value's index.");
InspectorTest.expectEqual(arr.lowerBound(1), 1, "lowerBound of a value in the list should return the value's index.");
InspectorTest.expectEqual(arr.lowerBound(7), 9, "lowerBound of a value in the list should return the value's index.");
InspectorTest.expectEqual(arr.lowerBound(2), 2, "lowerBound of a duplicate value in the list should return the value's first index.");
InspectorTest.expectEqual(arr.lowerBound(5), 8, "lowerBound of a value in a gap in the list should return the index where the value would be.");
InspectorTest.expectEqual(arr.lowerBound(100), arr.length, "lowerBound of a value after the last value should produce the index after the last index (length).");
let objs = [{size: 100}, {size: 200}, {size: 300}];
let comparator = (value, object) => value - object.size;
InspectorTest.expectEqual(objs.lowerBound(150, comparator), 1, "lowerBound with a comparator should invoke the comparator with the search value and a value in the list.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.upperBound",
test() {
// Index: 0 1 2 3 4 5 6 7 8 9
let arr = [0, 1, 2, 2, 2, 2, 2, 2, 6, 7];
InspectorTest.expectEqual(arr.upperBound(-100), 0, "upperBound of a value before the first value should produce the first index.");
InspectorTest.expectEqual(arr.upperBound(0), 1, "upperBound of a value in the list should return the next index after the value.");
InspectorTest.expectEqual(arr.upperBound(1), 2, "upperBound of a value in the list should return the next index after the value.");
InspectorTest.expectEqual(arr.upperBound(7), 10, "upperBound of a value in the list should return the next index after the value.");
InspectorTest.expectEqual(arr.upperBound(2), 8, "upperBound of a duplicate value in the list should return the next index after the value's last index.");
InspectorTest.expectEqual(arr.upperBound(5), 8, "upperBound of a value in a gap in the list should return the index where the value would be.");
InspectorTest.expectEqual(arr.upperBound(100), arr.length, "upperBound of a value after the last value should produce the index after the last index (length).");
let objs = [{size: 100}, {size: 200}, {size: 300}];
let comparator = (value, object) => value - object.size;
InspectorTest.expectEqual(objs.upperBound(150, comparator), 1, "upperBound with a comparator should invoke the comparator with the search value and a value in the list.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.binaryIndexOf",
test() {
// Index: 0 1 2 3 4 5 6 7 8 9
let arr = [0, 1, 2, 2, 2, 2, 2, 2, 6, 7];
let defaultComparator = (a, b) => a - b;
InspectorTest.expectEqual(arr.binaryIndexOf(-100, defaultComparator), -1, "binaryIndexOf of a value not in the list should be -1.");
InspectorTest.expectEqual(arr.binaryIndexOf(100, defaultComparator), -1, "binaryIndexOf of a value not in the list should be -1.");
InspectorTest.expectEqual(arr.binaryIndexOf(0, defaultComparator), arr.lowerBound(0), "binaryIndexOf of a value in the list should return the index of the value.");
InspectorTest.expectEqual(arr.binaryIndexOf(2, defaultComparator), arr.lowerBound(2), "binaryIndexOf of a duplicate value in the list should return the first index of the value.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.partition",
test() {
let arr1 = [1, 2, 3, 4];
let [even, odd] = arr1.partition((x) => x % 2 === 0);
InspectorTest.expectEqual(even.length + odd.length, arr1.length, "partition should not lose any elements.");
InspectorTest.expectThat(Array.shallowEqual(even, [2, 4]) && Array.shallowEqual(odd, [1, 3]), "partition should keep the order of elements in the sublists.");
let arr2 = [1, 1, -1, -2, 5, -2, -6, 0];
let [positive, negative] = arr2.partition((x) => x >= 0);
InspectorTest.expectThat(Array.shallowEqual(positive, [1, 1, 5, 0]) && Array.shallowEqual(negative, [-1, -2, -2, -6]), "partition should handle duplicates.");
let arr3 = [1, 2];
let [full, empty] = arr3.partition((x) => true);
InspectorTest.expectThat(Array.shallowEqual(full, [1, 2]) && !empty.length, "partition should produce an empty list for the negative side.");
[empty, full] = arr3.partition((x) => false);
InspectorTest.expectThat(Array.shallowEqual(full, [1, 2]) && !empty.length, "partition should produce an empty list for the positive side.");
return true;
}
});
suite.addTestCase({
name: "Array.isTypedArray",
test() {
InspectorTest.expectFalse(Array.isTypedArray(null), "isTypedArray of non-array.");
InspectorTest.expectFalse(Array.isTypedArray([]), "isTypedArray of non-typed-array should be false.");
InspectorTest.expectThat(Array.isTypedArray(new Int8Array), "isTypedArray of Int8Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Int16Array), "isTypedArray of Int16Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Int32Array), "isTypedArray of Int32Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Uint8Array), "isTypedArray of Uint8Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Uint8ClampedArray), "isTypedArray of Uint8ClampedArray should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Uint16Array), "isTypedArray of Uint16Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Uint32Array), "isTypedArray of Uint32Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Float32Array), "isTypedArray of Float32Array should be true.");
InspectorTest.expectThat(Array.isTypedArray(new Float64Array), "isTypedArray of Float64Array should be true.");
return true;
}
});
suite.addTestCase({
name: "Array.shallowEqual",
test() {
InspectorTest.expectThat(Array.shallowEqual([], []), "shallowEqual of empty arrays should be true.");
let arr1 = [1, 2, 3, 4];
InspectorTest.expectThat(Array.shallowEqual(arr1, arr1), "shallowEqual of an array with itself should be true.");
let arr2 = [1, 2, 3, 4];
InspectorTest.expectThat(Array.shallowEqual(arr1, arr2), "shallowEqual of equal arrays should be true.");
InspectorTest.expectThat(Array.shallowEqual(arr2, arr1), "shallowEqual of equal arrays should be true.");
let arr3 = [1, 2, 3, 4, 5];
InspectorTest.expectFalse(Array.shallowEqual(arr1, arr3), "shallowEqual of unequal arrays should be false.");
InspectorTest.expectFalse(Array.shallowEqual(arr3, arr1), "shallowEqual of unequal arrays should be false.");
InspectorTest.expectFalse(Array.shallowEqual([], null), "shallowEqual of an array and null should be false.");
InspectorTest.expectFalse(Array.shallowEqual([], 1.23), "shallowEqual of an array and non-array should be false.");
let typedArray1 = Int8Array.from(arr1);
InspectorTest.expectThat(Array.shallowEqual(typedArray1, arr1), "shallowEqual of a typed-array and it's array counterpart should be true.");
InspectorTest.expectThat(Array.shallowEqual(typedArray1, typedArray1), "shallowEqual of a typed-array with itself should be true.");
let typedArray2 = Int8Array.from(arr2);
InspectorTest.expectThat(Array.shallowEqual(typedArray1, arr2), "shallowEqual of equal typed-array and it's array counterpart should be true.");
InspectorTest.expectThat(Array.shallowEqual(typedArray1, typedArray2), "shallowEqual of equal typed-arrays should be true.");
InspectorTest.expectThat(Array.shallowEqual(typedArray2, arr1), "shallowEqual of equal typed-array and it's array counterpart should be true.");
InspectorTest.expectThat(Array.shallowEqual(typedArray2, typedArray1), "shallowEqual of equal typed-arrays should be true.");
let typedArray3 = Int8Array.from(arr3);
InspectorTest.expectFalse(Array.shallowEqual(typedArray1, arr3), "shallowEqual of unequal typed-array and it's array counterpart should be false.");
InspectorTest.expectFalse(Array.shallowEqual(typedArray1, typedArray3), "shallowEqual of unequal typed-arrays should be false.");
InspectorTest.expectFalse(Array.shallowEqual(typedArray3, arr1), "shallowEqual of unequal typed-array and it's array counterpart should be false.");
InspectorTest.expectFalse(Array.shallowEqual(typedArray3, typedArray1), "shallowEqual of unequal typed-arrays should be false.");
InspectorTest.expectFalse(Array.shallowEqual(new Int8Array, null), "shallowEqual of a typed-array and null should be false.");
InspectorTest.expectFalse(Array.shallowEqual(new Int8Array, 1.23), "shallowEqual of a typed-array and non-array should be false.");
let str = "abc";
InspectorTest.expectFalse(Array.shallowEqual(str, str), "shallowEqual of a non-array with itself should be false.");
InspectorTest.expectFalse(Array.shallowEqual({}, {}), "shallowEqual of non-arrays should be false.");
return true;
}
});
suite.addTestCase({
name: "Array.diffArrays",
test() {
function diff(initial, current) {
let actual = [];
Array.diffArrays(initial, current, (value, changed) => {
actual.push([value, changed]);
});
InspectorTest.log(JSON.stringify(initial) + ", " + JSON.stringify(current) + " => " + JSON.stringify(actual));
}
diff(["a"], []);
diff([], ["a"]);
diff(["a"], ["b"]);
diff(["a"], ["a"]);
diff(["a"], ["a", "b"]);
diff(["a"], ["b", "a"]);
diff(["a", "b"], ["a"]);
diff(["b", "a"], ["a"]);
diff(["b", "a"], ["a", "c"]);
diff(["b", "a"], ["a", "c"]);
diff(["b", "a"], ["a", "b"]);
diff(["a", "b", "c"], ["a", "d", "c"]);
diff(["a", "b", "c"], ["c", "b", "a"]);
InspectorTest.log("\nRepeating items:");
diff(["a"], ["a", "a"]);
diff(["a", "a"], ["a"]);
diff(["a", "a"], ["a", "a"]);
diff(["b", "a", "b"], ["a", "b", "a"]);
diff(["a", "b", "b", "c"], ["c", "b", "b", "b", "a"]);
diff(["a", "b", "b", "b", "c"], ["c", "b", "b", "a"]);
diff(["a", "a", "b", "b", "c", "c"], ["b", "b", "c", "c", "a", "a"]);
return true;
}
});
suite.addTestCase({
name: "Array.prototype.lastValue",
test() {
let object1 = {};
let object2 = {};
InspectorTest.expectEqual([object1, object2].lastValue, object2, "lastValue of a nonempty array should be the last value.")
InspectorTest.expectEqual([].lastValue, undefined, "lastValue of an empty array should be undefined.")
return true;
}
});
suite.addTestCase({
name: "Array.prototype.adjacencies",
test() {
function logAdjacencies(array) {
InspectorTest.log(JSON.stringify(array) + " => " + JSON.stringify(Array.from(array.adjacencies())));
}
logAdjacencies([]);
logAdjacencies([1]);
logAdjacencies([1, 2]);
logAdjacencies([1, 2, 3]);
logAdjacencies([1, 2, 3, 4]);
return true;
}
});
suite.addTestCase({
name: "Array.prototype.remove",
test() {
let arr1 = [1, 2, 3, 1];
InspectorTest.expectThat(arr1.remove(1), "remove should return true when removing a value that exists.");
InspectorTest.expectShallowEqual(arr1, [2, 3, 1], "remove should only remove the first matching value.");
let arr2 = ["1", "2", 3, 1];
InspectorTest.expectThat(arr2.remove("1"), "remove should return true when removing a value that exists.");
InspectorTest.expectFalse(arr2.remove(2), "remove should return false when removing a value that does not exist.");
InspectorTest.expectShallowEqual(arr2, ["2", 3, 1], "remove should only remove values that strictly match.");
let arr3 = [1, 2, 3];
InspectorTest.expectFalse(arr3.remove("1"), "remove should return false when removing a value that does not exist.");
InspectorTest.expectFalse(arr3.remove(4), "remove should return false when removing a value that does not exist.");
InspectorTest.expectShallowEqual(arr3, [1, 2, 3], "remove should not affect the array if the value does not exist.");
}
});
suite.addTestCase({
name: "Array.prototype.removeAll",
test() {
let arr1 = [1, 2, 3, 1];
arr1.removeAll(1);
InspectorTest.expectShallowEqual(arr1, [2, 3], "removeAll should remove all matching values.");
let arr2 = ["1", "2", 3, 1];
arr2.removeAll("1");
arr2.removeAll(2);
InspectorTest.expectShallowEqual(arr2, ["2", 3, 1], "removeAll should only remove values that strictly match.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.toggleIncludes",
test() {
let arr1 = [1, 2, 3];
arr1.toggleIncludes(3, true);
InspectorTest.expectShallowEqual(arr1, [1, 2, 3], "toggleIncludes of an existing item with force true should have no effect.");
let arr2 = [1, 2, 3];
arr2.toggleIncludes(3, false);
InspectorTest.expectShallowEqual(arr2, [1, 2], "toggleIncludes of an existing item with force false should remove the item.");
let arr3 = [1, 2, 3];
arr3.toggleIncludes(4, true);
InspectorTest.expectShallowEqual(arr3, [1, 2, 3, 4], "toggleIncludes of a nonexistent item with force true should add the item.");
let arr4 = [1, 2, 3];
arr4.toggleIncludes(4, false);
InspectorTest.expectShallowEqual(arr4, [1, 2, 3], "toggleIncludes of a nonexistent item with force false should have no effect.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.insertAtIndex",
test() {
let arr1 = [1, 2, 3];
arr1.insertAtIndex("x");
InspectorTest.expectShallowEqual(arr1, ["x", 1, 2, 3], "insertAtIndex with index unspecified should insert at the beginning.");
let arr2 = [1, 2, 3];
arr2.insertAtIndex("x", 0);
InspectorTest.expectShallowEqual(arr2, ["x", 1, 2, 3], "insertAtIndex with index zero should insert at the beginning.");
let arr3 = [1, 2, 3];
arr3.insertAtIndex("x", 2);
InspectorTest.expectShallowEqual(arr3, [1, 2, "x", 3], "insertAtIndex with 0 < index < length should insert at the correct location.");
let arr4 = [1, 2, 3];
arr4.insertAtIndex("x", -1);
InspectorTest.expectShallowEqual(arr4, [1, 2, "x", 3], "insertAtIndex with negative index should insert from the end.");
let arr5 = [1, 2, 3];
arr5.insertAtIndex("x", 100);
InspectorTest.expectShallowEqual(arr5, [1, 2, 3, "x"], "insertAtIndex with index greater than array length should insert at the end.");
return true;
}
});
suite.addTestCase({
name: "Array.prototype.pushAll",
test() {
function test(iterable) {
let array = [1, 2];
function stringify(object)
{
let string = JSON.stringify(object, function(key, value) {
if (value instanceof Node)
return "<" + value.localName + ">";
return value;
});
return string.replace(/"(<[^>]*>)"/g, "$1");
}
let before = stringify(array);
array.pushAll(iterable);
let after = stringify(array);
InspectorTest.log(before + " => " + after);
}
InspectorTest.log("Array:");
test(["a1", "a2"]);
InspectorTest.newline();
InspectorTest.log("Set:");
const set = new Set(["s1", "s2"]);
test(set);
test(set.entries());
test(set.keys());
test(set.values());
InspectorTest.newline();
InspectorTest.log("Map:");
const map = new Map([["m1k", "m1v"], ["m2k", "m2v"]]);
test(map);
test(map.entries());
test(map.keys());
test(map.values());
InspectorTest.newline();
InspectorTest.log("Object:");
const object = {
o1k: "o1v",
o2k: "o2v",
};
test(Object.entries(object));
test(Object.keys(object));
test(Object.values(object));
InspectorTest.newline();
InspectorTest.log("Generator:");
function* generator() {
yield "g1";
yield "g2";
}
test(generator());
InspectorTest.newline();
InspectorTest.log("Node:");
const node = document.createElement("div");
node.appendChild(document.createElement("n1"));
node.appendChild(document.createElement("n2"));
test(node.children);
test(node.querySelectorAll("*"));
InspectorTest.newline();
InspectorTest.log("Object (doesn't have [Symbol.iterator]):");
InspectorTest.expectException(() => {
test(object);
});
},
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onLoad="runTest()">
</body>
</html>