3 <script src=
"../http/tests/inspector/inspector-test.js"></script>
8 InspectorTest
.runTestSuite([
14 [1, 2, 3, 4, 5, 4, 3, 2, 1], [1, 3, 4, 5, 4, 3, 2, 1], [1, 3, 4, 5, 4, 3, 1],
15 [2, 2, 2, 2, 2], [2, 2, 2, 2], [],
16 [2, 2, 2, 1, 2, 2, 3, 2], [2, 2, 1, 2, 2, 3, 2], [1, 3]
18 for (var i
= 0; i
< testArrays
.length
; i
+= 3) {
19 var actual
= testArrays
[i
].slice(0);
20 var expected
= testArrays
[i
+ 1];
21 actual
.remove(2, true);
22 InspectorTest
.assertEquals(JSON
.stringify(expected
), JSON
.stringify(actual
), "remove(2, true) passed");
23 actual
= testArrays
[i
].slice(0);
24 expected
= testArrays
[i
+ 2];
25 actual
.remove(2, false);
26 InspectorTest
.assertEquals(JSON
.stringify(expected
), JSON
.stringify(actual
), "remove(2, false) passed");
31 function orderedMergeIntersect(next
)
33 function comparator(a
, b
)
39 return a
.upperBound(x
) - a
.lowerBound(x
);
41 function testAll(a
, b
)
43 testOperation(a
, b
, a
.mergeOrdered(b
, comparator
), Math
.max
, "U");
44 testOperation(a
, b
, a
.intersectOrdered(b
, comparator
), Math
.min
, "x");
46 function testOperation(a
, b
, actual
, checkOperation
, opName
)
48 var allValues
= a
.concat(b
).concat(actual
);
49 for (var i
= 0; i
< allValues
.length
; ++i
) {
50 var value
= allValues
[i
];
51 expectedCount
= checkOperation(count(a
, value
), count(b
, value
));
52 actualCount
= count(actual
, value
);
53 InspectorTest
.assertEquals(expectedCount
, actualCount
,
54 "Incorrect result for value: " + value
+ " at [" + a
+ "] " + opName
+ " [" + b
+ "] -> [" + actual
+ "]");
56 InspectorTest
.assertEquals(JSON
.stringify(actual
.sort()), JSON
.stringify(actual
), "result array is ordered");
62 [4, 5, 5, 8, 8], [1, 1, 1, 2, 6],
63 [1, 2, 2, 2, 2, 3, 3, 4], [2, 2, 2, 3, 3, 3, 3],
64 [1, 2, 3, 4, 5], [1, 2, 3]
66 for (var i
= 0; i
< testArrays
.length
; i
+= 2) {
67 testAll(testArrays
[i
], testArrays
[i
+ 1]);
68 testAll(testArrays
[i
+ 1], testArrays
[i
]);
73 function binaryIndexOfTest(next
)
79 [1, 10, 11, 12, 13, 14, 100],
80 [-100, -50, 0, 50, 100],
81 [-100, -14, -13, -12, -11, -10, -1]
84 function testArray(array
)
86 function comparator(a
, b
)
88 return a
< b
? -1 : (a
> b
? 1 : 0);
91 for (var i
= -100; i
<= 100; ++i
) {
92 var reference
= array
.indexOf(i
);
93 var actual
= array
.binaryIndexOf(i
, comparator
);
94 InspectorTest
.assertEquals(reference
, actual
, "binaryIndexOf");
99 for (var i
= 0, l
= testArrays
.length
; i
< l
; ++i
)
100 testArray(testArrays
[i
]);
104 function lowerBoundTest(next
)
109 [-1, -1, 0, 0, 0, 0, 2, 3, 4, 4, 4, 7, 9, 9, 9]
112 function testArray(array
, useComparator
)
114 function comparator(a
, b
)
116 return a
< b
? -1 : (a
> b
? 1 : 0);
119 for (var value
= -2; value
<= 12; ++value
) {
120 var index
= useComparator
? array
.lowerBound(value
, comparator
) : array
.lowerBound(value
);
121 InspectorTest
.assertTrue(0 <= index
&& index
<= array
.length
, "index is within bounds");
122 InspectorTest
.assertTrue(index
=== 0 || array
[index
- 1] < value
, "array[index - 1] < value");
123 InspectorTest
.assertTrue(index
=== array
.length
|| array
[index
] >= value
, "array[index] >= value");
127 for (var i
= 0, l
= testArrays
.length
; i
< l
; ++i
) {
128 testArray(testArrays
[i
], false);
129 testArray(testArrays
[i
], true);
134 function upperBoundTest(next
)
139 [-1, -1, 0, 0, 0, 0, 2, 3, 4, 4, 4, 7, 9, 9, 9]
142 function testArray(array
, useComparator
)
144 function comparator(a
, b
)
146 return a
< b
? -1 : (a
> b
? 1 : 0);
149 for (var value
= -2; value
<= 12; ++value
) {
150 var index
= useComparator
? array
.upperBound(value
, comparator
) : array
.upperBound(value
);
151 InspectorTest
.assertTrue(0 <= index
&& index
<= array
.length
, "index is within bounds");
152 InspectorTest
.assertTrue(index
=== 0 || array
[index
- 1] <= value
, "array[index - 1] <= value");
153 InspectorTest
.assertTrue(index
=== array
.length
|| array
[index
] > value
, "array[index] > value");
157 for (var i
= 0, l
= testArrays
.length
; i
< l
; ++i
) {
158 testArray(testArrays
[i
], false);
159 testArray(testArrays
[i
], true);
164 function qselectTest(next
)
169 [0, 0, 0, 0, 0, 0, 0, 0],
172 [-1, 3, 2, 7, 7, 7, 10, 12, 3, 4, -1, 2]
175 function testArray(array
)
177 function compare(a
, b
)
181 var sorted
= array
.slice(0).sort(compare
);
185 median
: sorted
[Math
.floor(sorted
.length
/ 2)],
186 max
: sorted
[sorted
.length
- 1]
190 min
: array
.slice(0).qselect(0),
191 median
: array
.slice(0).qselect(Math
.floor(array
.length
/ 2)),
192 max
: array
.slice(0).qselect(array
.length
- 1)
194 InspectorTest
.addResult("Array: " + JSON
.stringify(array
));
195 InspectorTest
.addResult("Reference: " + JSON
.stringify(reference
));
196 InspectorTest
.addResult("Actual: " + JSON
.stringify(actual
));
198 for (var i
= 0, l
= testArrays
.length
; i
< l
; ++i
)
199 testArray(testArrays
[i
]);
203 function sortRangeTest(next
)
209 [6, 4, 2, 7, 10, 15, 1],
210 [10, 44, 3, 6, 56, 66, 10, 55, 32, 56, 2, 5]
213 function testArray(array
)
215 function comparator(a
, b
)
217 return a
< b
? -1 : (a
> b
? 1 : 0);
220 function compareArrays(a
, b
, message
)
222 InspectorTest
.assertEquals(JSON
.stringify(a
), JSON
.stringify(b
), message
);
225 for (var left
= 0, l
= array
.length
- 1; left
< l
; ++left
) {
226 for (var right
= left
, r
= array
.length
; right
< r
; ++right
) {
227 for (var first
= left
; first
<= right
; ++first
) {
228 for (var count
= 1, k
= right
- first
+ 1; count
<= k
; ++count
) {
229 var actual
= array
.slice(0);
230 actual
.sortRange(comparator
, left
, right
, first
, first
+ count
- 1);
231 compareArrays(array
.slice(0, left
), actual
.slice(0, left
), "left " + left
+ " " + right
+ " " + count
);
232 compareArrays(array
.slice(right
+ 1), actual
.slice(right
+ 1), "right " + left
+ " " + right
+ " " + count
);
233 var middle
= array
.slice(left
, right
+ 1);
234 middle
.sort(comparator
);
235 compareArrays(middle
.slice(first
- left
, first
- left
+ count
), actual
.slice(first
, first
+ count
), "sorted " + left
+ " " + right
+ " " + first
+ " " + count
);
236 actualRest
= actual
.slice(first
+ count
, right
+ 1);
237 actualRest
.sort(comparator
);
238 compareArrays(middle
.slice(first
- left
+ count
), actualRest
, "unsorted " + left
+ " " + right
+ " " + first
+ " " + count
);
245 for (var i
= 0, len
= testArrays
.length
; i
< len
; ++i
)
246 testArray(testArrays
[i
]);
250 function naturalOrderComparatorTest(next
)
283 for (var i
= 0, n
= testArray
.length
; i
< n
; ++i
)
284 InspectorTest
.assertEquals(0, String
.naturalOrderComparator(testArray
[i
], testArray
[i
]), "comparing equal strings");
286 testArray
.sort(String
.naturalOrderComparator
);
287 InspectorTest
.addResult("Sorted in natural order: [" + testArray
.join(", ") + "]");
289 // Check comparator's transitivity.
290 for (var i
= 0, n
= testArray
.length
; i
< n
; ++i
) {
291 for (var j
= 0; j
< n
; ++j
) {
292 var a
= testArray
[i
];
293 var b
= testArray
[j
];
294 var diff
= String
.naturalOrderComparator(a
, b
);
296 InspectorTest
.assertEquals(a
, b
, "zero diff");
298 InspectorTest
.assertTrue(i
< j
);
300 InspectorTest
.assertTrue(i
> j
);
306 function stableSortPrimitiveTest(next
)
308 InspectorTest
.assertEquals("", [].stableSort().join(","));
309 InspectorTest
.assertEquals("1", [1].stableSort().join(","));
310 InspectorTest
.assertEquals("1,2", [2,1].stableSort().join(","));
311 InspectorTest
.assertEquals("1,1,1", [1,1,1].stableSort().join(","));
312 InspectorTest
.assertEquals("1,2,3,4", [1,2,3,4].stableSort().join(","));
314 function defaultComparator(a
, b
)
316 return a
< b
? -1 : (a
> b
? 1 : 0);
319 function strictNumberCmp(a
, b
)
325 return 1/a === 1/b
; // Neg zero comparison.
328 // The following fails with standard Array.sort()
329 var original
= [0,-0,0,-0,-0,-0,0,0,0,-0,0,-0,-0,-0,0,0];
330 var sorted
= original
.slice(0).stableSort(defaultComparator
);
331 for (var i
= 0; i
< original
.length
; ++i
)
332 InspectorTest
.assertTrue(strictNumberCmp(original
[i
], sorted
[i
]), "Sorting negative zeros");
334 // Test sorted and backward-sorted arrays.
337 for (var i
= 0; i
< 100; ++i
) {
341 identity
.stableSort(defaultComparator
);
342 reverse
.stableSort(defaultComparator
);
343 for (var i
= 1; i
< identity
.length
; ++i
) {
344 InspectorTest
.assertTrue(identity
[i
- 1] < identity
[i
], "identity");
345 InspectorTest
.assertTrue(reverse
[i
- 1] < reverse
[i
], "reverse");
351 function stableSortTest(next
)
353 function Pair(key1
, key2
)
358 Pair
.prototype.toString = function() {
359 return "Pair(" + this.key1
+ ", " + this.key2
+ ")";
386 var testArrayKey2Values
= [];
387 for (var i
= 0, n
= testArray
.length
; i
< n
; ++i
) {
388 var value
= testArray
[i
].key2
;
389 InspectorTest
.assertTrue(testArrayKey2Values
.indexOf(value
) === -1, "FAIL: key2 values should be unique");
390 testArrayKey2Values
.push(value
);
393 function comparator(a
, b
)
395 return a
.key1
- b
.key1
;
397 testArray
.stableSort(comparator
);
398 InspectorTest
.addResult("Stable sort result:\n" + testArray
.join("\n"));
400 function assertHelper(condition
, message
, i
, a
, b
)
402 InspectorTest
.assertTrue(condition
, "FAIL: " + message
+ " at index " + i
+ ": " + a
+ " < " + b
);
405 for (var i
= 1, n
= testArray
.length
; i
< n
; ++i
) {
406 var a
= testArray
[i
- 1];
407 var b
= testArray
[i
];
408 assertHelper(a
.key1
<= b
.key1
, "primary key order", i
, a
, b
);
409 if (a
.key1
!== b
.key1
)
411 var key2IndexA
= testArrayKey2Values
.indexOf(a
.key2
);
412 InspectorTest
.assertTrue(key2IndexA
!== -1);
413 var key2IndexB
= testArrayKey2Values
.indexOf(b
.key2
);
414 InspectorTest
.assertTrue(key2IndexB
!== -1);
415 assertHelper(key2IndexA
< key2IndexB
, "stable order", i
, a
, b
);
420 function stringHashTest(next
)
422 var stringA
= " ".repeat(10000);
423 var stringB
= stringA
+ " ";
424 var hashA
= String
.hashCode(stringA
);
425 InspectorTest
.assertTrue(hashA
!== String
.hashCode(stringB
));
426 InspectorTest
.assertTrue(isFinite(hashA
));
427 InspectorTest
.assertTrue(hashA
+ 1 !== hashA
);
431 function stringTrimURLTest(next
)
433 var baseURLDomain
= "www.chromium.org";
435 "http://www.chromium.org/foo/bar", "/foo/bar",
436 "https://www.CHromium.ORG/BAZ/zoo", "/BAZ/zoo",
437 "https://example.com/foo[]", "example.com/foo[]",
439 for (var i
= 0; i
< testArray
.length
; i
+= 2) {
440 var url
= testArray
[i
];
441 var expected
= testArray
[i
+ 1];
442 InspectorTest
.assertEquals(expected
, url
.trimURL(baseURLDomain
), url
);
447 function stringToBase64Test(next
)
456 "pqrstu", "cHFyc3R1",
457 String
.fromCharCode(0x444, 0x5555, 0x66666, 0x777777), "0YTllZXmmabnnbc="
459 for (var i
= 0; i
< testArray
.length
; i
+= 2) {
460 var string
= testArray
[i
];
461 var encodedString
= testArray
[i
+ 1];
462 InspectorTest
.assertEquals(encodedString
, string
.toBase64());
472 <body onload=
"runTest()">
474 This test checks Web Inspector utilities.