-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy patharray.test.js
156 lines (145 loc) · 3.8 KB
/
array.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
import * as array from './array.js'
import * as t from './testing.js'
import * as prng from './prng.js'
/**
* @param {t.TestCase} _tc
*/
export const testIsarrayPerformance = _tc => {
const N = 100000
/**
* @type {Array<any>}
*/
const objects = []
for (let i = 0; i < N; i++) {
if (i % 2 === 0) {
objects.push([i])
} else {
objects.push({ i })
}
}
const timeConstructor = t.measureTime('constructor check', () => {
let collectedArrays = 0
objects.forEach(obj => {
if (obj.constructor === Array) {
collectedArrays++
}
})
t.assert(collectedArrays === N / 2)
})
const timeIsarray = t.measureTime('Array.isArray', () => {
let collectedArrays = 0
objects.forEach(obj => {
if (array.isArray(obj)) {
collectedArrays++
}
})
t.assert(collectedArrays === N / 2)
})
t.assert(timeIsarray < timeConstructor * 2, 'Expecting that isArray is not much worse than a constructor check')
}
/**
* @param {t.TestCase} _tc
*/
export const testAppend = _tc => {
const arr = [1, 2, 3]
array.appendTo(arr, array.copy(arr))
t.compareArrays(arr, [1, 2, 3, 1, 2, 3])
}
/**
* @param {t.TestCase} _tc
*/
export const testBasic = _tc => {
const arr = array.create()
array.appendTo(arr, array.from([1]))
t.assert(array.last(arr) === 1)
}
/**
* @param {t.TestCase} _tc
*/
export const testflatten = _tc => {
const arr = [[1, 2, 3], [4]]
t.compareArrays(array.flatten(arr), [1, 2, 3, 4])
}
/**
* @param {t.TestCase} _tc
*/
export const testFolding = _tc => {
/**
* @param {number} n
*/
const testcase = n => {
// We mess with the seed (+/-1) to catch some edge cases without changing the result
const result = -1 + array.fold(array.unfold(n, i => i), 1, (accumulator, item, index) => {
t.assert(accumulator === index + 1)
t.assert(accumulator === item + 1)
return accumulator + 1
})
t.assert(result === n)
}
testcase(0)
testcase(1)
testcase(100)
}
/**
* @param {t.TestCase} _tc
*/
export const testEvery = _tc => {
const arr = [1, 2, 3]
t.assert(array.every(arr, x => x <= 3))
t.assert(!array.every(arr, x => x < 3))
t.assert(array.some(arr, x => x === 2))
t.assert(!array.some(arr, x => x === 42))
}
/**
* @param {t.TestCase} _tc
*/
export const testIsArray = _tc => {
t.assert(array.isArray([]))
t.assert(array.isArray([1]))
t.assert(array.isArray(Array.from(new Set([3]))))
t.assert(!array.isArray(1))
t.assert(!array.isArray(0))
t.assert(!array.isArray(''))
}
/**
* @param {t.TestCase} _tc
*/
export const testUnique = _tc => {
t.compare([1, 2], array.unique([1, 2, 1, 2, 2, 1]))
t.compare([], array.unique([]))
t.compare([{ el: 1 }], array.uniqueBy([{ el: 1 }, { el: 1 }], o => o.el))
t.compare([], array.uniqueBy([], o => o))
}
/**
* @param {t.TestCase} tc
*/
export const testBubblesortItemEdgeCases = tc => {
// does not throw..
array.bubblesortItem([1], 0, (a, b) => a - b)
array.bubblesortItem([2, 1], 1, (a, b) => a - b)
array.bubblesortItem([2, 1], 0, (a, b) => a - b)
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatBubblesortItem = tc => {
const arr = Array.from(prng.uint8Array(tc.prng, 10))
arr.sort((a, b) => a - b)
const newItem = prng.uint32(tc.prng, 0, 256)
const pos = prng.uint32(tc.prng, 0, arr.length)
arr.splice(pos, newItem)
const arrCopySorted = arr.slice().sort((a, b) => a - b)
array.bubblesortItem(arr, pos, (a, b) => a - b)
t.compare(arr, arrCopySorted)
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatBubblesort = tc => {
const arr = Array.from(prng.uint8Array(tc.prng, 10))
const arrCopySorted = arr.slice().sort((a, b) => a - b)
for (let i = arr.length - 1; i >= 0; i--) {
while (array.bubblesortItem(arr, i, (a, b) => a - b) !== i) { /* nop */ }
}
t.compare(arr, arrCopySorted)
}