Debian Codegraph @

js/src/tests/non262/Array/sort_basics.js

raw
1 // Note: failed runs should include their "SEED" value in error messages,
2 // setting "const SEED" to that value will recreate the data from any such run.
3 const SEED = (Math.random() * 10) + 1;
4
5 // Create an array filled with random values, 'size' is the desired length of
6 // the array and 'seed' is an initial value supplied to a pseudo-random number
7 // generator.
8 function genRandomArray(size, seed) {
9 return Array.from(XorShiftGenerator(seed, size));
10 }
11
12 function SortTest(size, seed) {
13 let arrOne = genRandomArray(size, seed);
14 let arrTwo = Array.from(arrOne);
15 let arrThree = Array.from(arrOne);
16 let typedArrays = [
17 new Uint8Array(arrOne),
18 new Int32Array(arrOne),
19 new Float32Array(arrOne)
20 ];
21
22 let sorted = Array.from((Int32Array.from(arrOne)).sort());
23
24 // Test numeric comparators against typed array sort.
25 assertDeepEq(sorted, arrTwo.sort((x, y) => (x - y)),
26 `The array is not properly sorted! seed: ${SEED}`);
27
28 // Use multiplication to kill comparator optimization and trigger
29 // self-hosted sorting.
30 assertDeepEq(sorted, arrThree.sort((x, y) => (1*x - 1*y)),
31 `The array is not properly sorted! seed: ${SEED}`);
32
33 // Ensure that typed arrays are also sorted property.
34 for (typedArr of typedArrays) {
35 let sortedTypedArray = Array.prototype.sort.call(typedArr, (x, y) => (1*x - 1*y))
36 assertDeepEq(sorted, Array.from(sortedTypedArray),
37 `The array is not properly sorted! seed: ${SEED}`);
38 }
39 }
40
41 SortTest(2048, SEED);
42 SortTest(16, SEED);
43 SortTest(0, SEED);
44
45 if (typeof reportCompare === "function")
46 reportCompare(true, true);