-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
sort.test.js
190 lines (183 loc) · 4.78 KB
/
sort.test.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as prng from './prng.js'
import * as t from './testing.js'
import * as sort from './sort.js'
/**
* @template T
* @param {t.TestCase} tc
* @param {Array<T>} arr
* @param {function(T,T):number} compare
* @param {function(T):number} getVal
*/
const runSortTest = (tc, arr, compare, getVal) => {
const arrSort = arr
const arrQuicksort = arr.slice()
const arrInsertionsort = arr.slice()
t.measureTime('Array.constructor.sort', () => {
arrSort.sort(compare)
})
if (arrInsertionsort.length <= 10000) {
t.measureTime('Insertionsort', () => {
sort.insertionSort(arrInsertionsort, compare)
})
t.compareArrays(arrSort, arrInsertionsort, 'compare Insertionsort with expected result')
}
t.measureTime('Quicksort', () => {
sort.quicksort(arrQuicksort, compare)
})
// quickSort is not stable
t.compareArrays(arrSort.map(getVal), arrQuicksort.map(getVal), 'compare Quicksort with expected result')
}
/**
* @template T
* @param {t.TestCase} tc
* @param {function(number):Array<T>} createArray
* @param {function(T,T):number} compare 0 if equal, 1 if a<b, -1 otherwise
* @param {function(T):number} getVal
*/
const createSortTest = (tc, createArray, compare, getVal) => {
t.describe('sort 10 elements')
runSortTest(tc, createArray(10), compare, getVal)
t.describe('sort 10 elements')
runSortTest(tc, createArray(10), compare, getVal)
t.describe('sort 10 elements')
runSortTest(tc, createArray(10), compare, getVal)
t.describe('sort 50 elements')
runSortTest(tc, createArray(50), compare, getVal)
t.describe('sort 100 elements')
runSortTest(tc, createArray(100), compare, getVal)
t.describe('sort 500 elements')
runSortTest(tc, createArray(500), compare, getVal)
t.describe('sort 1k elements')
runSortTest(tc, createArray(1000), compare, getVal)
t.describe('sort 10k elements')
runSortTest(tc, createArray(10000), compare, getVal)
t.describe('sort 100k elements')
runSortTest(tc, createArray(100000), compare, getVal)
if (t.extensive) {
t.describe('sort 1M elements')
runSortTest(tc, createArray(1000000), compare, getVal)
t.describe('sort 10M elements')
runSortTest(tc, createArray(10000000), compare, getVal)
}
}
/**
* @param {t.TestCase} tc
*/
export const testSortUint8 = tc => {
t.skip(!t.production)
/**
* @param {number} i
* @return {number}
*/
const getVal = i => i
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
const compare = (a, b) => a - b
/**
* @param {number} len
* @return {Array<number>}
*/
const createArray = len => Array.from(prng.uint8Array(tc.prng, len * 2))
createSortTest(tc, createArray, compare, getVal)
}
/**
* @param {t.TestCase} tc
*/
export const testSortUint32 = tc => {
t.skip(!t.production)
/**
* @param {number} i
* @return {number}
*/
const getVal = i => i
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
const compare = (a, b) => a - b
/**
* @param {number} len
* @return {Array<number>}
*/
const createArray = len => Array.from(prng.uint32Array(tc.prng, len))
createSortTest(tc, createArray, compare, getVal)
}
/**
* @param {t.TestCase} tc
*/
export const testSortUint16 = tc => {
t.skip(!t.production)
/**
* @param {number} i
* @return {number}
*/
const getVal = i => i
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
const compare = (a, b) => a - b
/**
* @param {number} len
* @return {Array<number>}
*/
const createArray = len => Array.from(prng.uint16Array(tc.prng, len))
createSortTest(tc, createArray, compare, getVal)
}
/**
* @param {t.TestCase} tc
*/
export const testSortObjectUint32 = tc => {
/**
* @param {{index:number}} obj
* @return {number}
*/
const getVal = obj => obj.index
/**
* @param {{index:number}} a
* @param {{index:number}} b
* @return {number}
*/
const compare = (a, b) => a.index - b.index
/**
* @param {number} len
* @return {Array<{index:number}>}
*/
const createArray = len => Array.from(prng.uint32Array(tc.prng, len)).map(index => ({ index }))
createSortTest(tc, createArray, compare, getVal)
}
/**
* @param {t.TestCase} tc
*/
export const testListVsArrayPerformance = tc => {
/**
* @typedef {{ val: number }} Val
* @typedef {{ val: Val, next: item }|null} item
*/
const len = 100000
t.measureTime('array creation', () => {
/**
* @type {Array<Val>}
*/
const array = new Array(len)
for (let i = 0; i < len; i++) {
array[i] = { val: i }
}
})
t.measureTime('list creation', () => {
/**
* @type {item}
*/
const listStart = { val: { val: 0 }, next: null }
for (let i = 1, n = listStart; i < len; i++) {
const next = { val: { val: i }, next: null }
n.next = next
n = next
}
})
}