-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtreeset.spec.ts
193 lines (192 loc) · 6.64 KB
/
treeset.spec.ts
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
191
192
193
import { TreeSet, TreeMultiSet } from '../src/treeset'
describe('treeset', () => {
describe('TreeSet', () => {
it('should construct from array', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect([...set.values()]).toEqual([0, 1, 2, 3, 4, 5])
})
it('should dedupe', () => {
const set = new TreeSet([0, 1, 1, 3, 4, 5])
expect([...set.values()]).toEqual([0, 1, 3, 4, 5])
})
it('should support has', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
for (let i = 0; i <= 5; i++) expect(set.has(i)).toEqual(true)
expect(set.has(10)).toEqual(false)
expect(set.has(-10)).toEqual(false)
})
it('should support ceil', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.ceil(1)).toEqual(1)
expect(set.ceil(0.5)).toEqual(1)
})
it('should support higher', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.higher(1)).toEqual(2)
expect(set.higher(0.5)).toEqual(1)
})
it('should support floor', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.floor(1)).toEqual(1)
expect(set.floor(1.5)).toEqual(1)
})
it('should support lower', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.lower(2)).toEqual(1)
expect(set.lower(2.5)).toEqual(2)
})
it('should support first', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.first()).toEqual(0)
})
it('should support last', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.last()).toEqual(5)
})
it('should support shift', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.shift()).toEqual(0)
expect(set.size()).toEqual(5)
})
it('should support pop', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5])
expect(set.pop()).toEqual(5)
expect(set.size()).toEqual(5)
})
it('should sort the values correctly', () => {
for (let i = 0; i < 20; i++) {
const arr = [...Array(100)].map(() => Math.random())
const set = new TreeSet(arr)
expect([...set.values()]).toEqual(arr.sort((a, b) => a - b))
expect([...set.rvalues()]).toEqual(arr.sort((a, b) => b - a))
}
})
it('should support empty data', () => {
const set = new TreeSet()
set.add(2)
set.add(3)
expect(set.lower(3)).toEqual(2)
expect(set.lower(2.5)).toEqual(2)
})
it('should support customize compare', () => {
const set = new TreeSet([0, 1, 2, 3, 4, 5], (l, r) => r - l)
expect(set.lower(2)).toEqual(3)
expect(set.lower(2.5)).toEqual(3)
})
it('should support customize compare without data', () => {
const set = new TreeSet((l, r) => r - l)
set.add(2)
set.add(3)
expect(set.lower(2)).toEqual(3)
expect(set.lower(2.5)).toEqual(3)
})
it('should treat items as equal ones when `compare` returns 0', () => {
const s = new TreeSet<[number, number]>([], (a: [number, number], b: [number, number]) => {
if (a[0] !== b[0]) return a[0] - b[0]
return a[1] - b[1]
})
s.add([1, 2])
s.add([1, 2])
expect(s.size()).toEqual(1)
})
})
describe('TreeMultiSet', () => {
it('should construct from array', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5])
expect([...set.values()]).toEqual([0, 1, 2, 3, 4, 5])
})
it('should not dedupe', () => {
const set = new TreeMultiSet([0, 1, 1, 3, 4, 5])
expect([...set.values()]).toEqual([0, 1, 1, 3, 4, 5])
})
it('should support count', () => {
const set = new TreeMultiSet([0, 1, 1, 3, 4, 5])
expect(set.count(3)).toEqual(1)
expect(set.count(1)).toEqual(2)
expect(set.count(10)).toEqual(0)
set.add(10)
expect(set.count(10)).toEqual(1)
set.add(10)
expect(set.count(10)).toEqual(2)
})
it('should support has', () => {
const set = new TreeMultiSet([0, 1, 1, 3, 4, 5])
expect(set.has(3)).toEqual(true)
expect(set.has(10)).toEqual(false)
})
it('delete should only erase the value when count is 0', () => {
const set = new TreeMultiSet([4, 4])
expect(set.size()).toEqual(2)
set.delete(4)
expect(set.size()).toEqual(1)
expect(set.values().next().value).toEqual(4)
set.delete(4)
expect(set.size()).toEqual(0)
set.add(4)
expect(set.size()).toEqual(1)
set.delete(3)
expect(set.size()).toEqual(1)
})
it('should sort the values correctly', () => {
for (let i = 0; i < 20; i++) {
let arr = [...Array(100)].map(() => Math.random())
arr = [...arr, ...arr]
const set = new TreeMultiSet(arr)
expect([...set.values()]).toEqual(arr.sort((a, b) => a - b))
expect([...set.rvalues()]).toEqual(arr.sort((a, b) => b - a))
expect(set.count(set.rvalues().next().value)).toEqual(2)
}
})
it('should support lower', () => {
const set = new TreeMultiSet([1, 2, 3, 4, 5])
expect(set.lower(2)).toEqual(1)
expect(set.lower(2.5)).toEqual(2)
})
it('should support optional data', () => {
const set = new TreeSet()
set.add(2)
set.add(3)
expect(set.lower(3)).toEqual(2)
expect(set.lower(2.5)).toEqual(2)
})
it('should support customize compare', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5], (l, r) => r - l)
expect(set.lower(2)).toEqual(3)
expect(set.lower(2.5)).toEqual(3)
})
it('should support customize compare without data', () => {
const set = new TreeMultiSet((l, r) => r - l)
set.add(2)
set.add(3)
expect(set.lower(2)).toEqual(3)
expect(set.lower(2.5)).toEqual(3)
})
it('should support first', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5])
expect(set.first()).toEqual(0)
})
it('should support last', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5])
expect(set.last()).toEqual(5)
})
it('should support shift', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5])
expect(set.shift()).toEqual(0)
expect(set.size()).toEqual(5)
})
it('should support pop', () => {
const set = new TreeMultiSet([0, 1, 2, 3, 4, 5])
expect(set.pop()).toEqual(5)
expect(set.size()).toEqual(5)
})
it('should treat items as equal ones when `compare` returns 0', () => {
const s = new TreeMultiSet<[number, number]>([], (a: [number, number], b: [number, number]) => {
if (a[0] !== b[0]) return a[0] - b[0]
return a[1] - b[1]
})
s.add([1, 2])
s.add([1, 2])
expect(s.count([1, 2])).toEqual(2)
})
})
})