forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_qsortr_random.js
More file actions
50 lines (41 loc) · 1.32 KB
/
array_qsortr_random.js
File metadata and controls
50 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
function getRandomArray(size)
{
const arr = [];
for (let i = 0; i < size; ++i)
{
arr[i] = Math.random() * 100 | 0;
}
return arr;
}
function testRandomSort(size)
{
const unsorted = getRandomArray(size);
// Copy the array and sort it
const sorted = unsorted.slice();
sorted.sort(function (a, b){ return a - b;});
// Verify that the array is sorted
for (let i = 1; i < size; ++i)
{
// Sort has not completed correctly
if (sorted[i] < sorted[i - 1])
{
WScript.Echo(`Unsorted: ${unsorted}`);
WScript.Echo(`Sorted: ${sorted}`);
throw new Error(`Array is not sorted correctly at index '${i}'`);
}
}
}
function stressTestSort(iterations, size = 1000)
{
for (let i = 0; i < iterations; ++i)
{
testRandomSort(size);
}
}
// Test 1000 random arrays of 1000 elements, print out the failures.
stressTestSort(1000, 1000);
WScript.Echo("PASS");