forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sort_random.js
More file actions
171 lines (147 loc) · 4.4 KB
/
array_sort_random.js
File metadata and controls
171 lines (147 loc) · 4.4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z"];
function getRandomObjArray(size)
{
const arr = [];
for (let i = 0; i < size; ++i)
{
arr[i] = {
value : Math.random() * 100 | 0,
index : i
}
}
return arr;
}
function getRandomStringArray(size)
{
const arr = [];
for (let i = 0; i < size; ++i)
{
let value = "";
const len = Math.random() * 5 | 0;
for (let j = 0; j < len; ++j)
{
value += alphabet[Math.random() * 26 |0];
}
arr[i] = value;
}
return arr;
}
function getRandomIntArray(size)
{
const arr = [];
for (let i = 0; i < size; ++i)
{
arr[i] = Math.random() * 100 | 0;
}
return arr;
}
function testRandomIntSort(size)
{
const unsorted = getRandomIntArray(size);
// Copy the array and sort it
const sorted = unsorted.slice();
sorted.sort(function (a, b){ return 0;});
for (let i = 0; i < size; ++i)
{
if (sorted[i] !== unsorted[i])
{
print (`Unsorted: ${unsorted}`);
print (`Sorted: ${sorted}`);
throw new Error ("Sort not stable at index " + i);
}
}
sorted.sort();
for (let i = 1; i < size; ++i)
{
if (`${sorted[i-1]}` > `${sorted[i]}`)
{
print (`Unsorted: ${unsorted}`);
print (`Sorted: ${sorted}`);
throw new Error ("Default sort has not sorted by strings at " + i);
}
}
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])
{
print (`Unsorted: ${unsorted}`);
print (`Sorted: ${sorted}`);
throw new Error(`Array is not sorted correctly at index '${i}'`);
}
}
}
function testRandomStringSort(size)
{
const unsorted = getRandomStringArray(size);
// Copy the array and sort it
const sorted = unsorted.slice();
sorted.sort(function (a, b){ return 0;});
for (let i = 0; i < size; ++i)
{
if (sorted[i] !== unsorted[i])
{
print (`Unsorted: ${unsorted}`);
print (`Sorted: ${sorted}`);
throw new Error ("Sort not stable at index " + i);
}
}
sorted.sort();
for (let i = 1; i < size; ++i)
{
if (sorted[i-1] > sorted[i])
{
print (`Unsorted: ${unsorted}`);
print (`Sorted: ${sorted}`);
throw new Error ("Default sort has not sorted by strings");
}
}
}
function testRandomObjSort(size)
{
const unsorted = getRandomObjArray(size);
const sorted = unsorted.slice();
// stable default sort on objects without custom toString should not re-order
sorted.sort();
for (let i = 0; i < size; ++i)
{
if (sorted[i].index !== i)
{
print (`Unsorted: ${JSON.stringify(unsorted)}`);
print (`Sorted: ${JSON.stringify(sorted)}`);
throw new Error ("Sort not stable at index " + i);
}
}
sorted.sort(function (a, b) { return a.value - b.value; });
for (let i = 1; i < size; ++i)
{
if (sorted[i-1].value > sorted[i].value || (sorted[i-1] === sorted[i] && sorted[i-1].index > sorted[i].index))
{
print (`Unsorted: ${JSON.stringify(unsorted)}`);
print (`Sorted: ${JSON.stringify(sorted)}`);
throw new Error ("Numeric sort of objects has failed at " + i);
}
}
}
function stressTestSort(iterations, size = 1000)
{
for (let i = 0; i < iterations; ++i)
{
testRandomIntSort(size);
testRandomStringSort(size);
testRandomObjSort(size);
}
}
// test arrays with length < 2048 for insertion sort
stressTestSort(200, 512);
// test arrays with length > 2048 for merge sort
stressTestSort(200, 2050);
print("PASS");