-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryIndexTree.test.js
45 lines (40 loc) · 1.17 KB
/
binaryIndexTree.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
const { BinaryIndexTree } = require('./binaryIndexTree.js');
describe('BinaryIndexTree', () => {
describe('constructor', () => {
test('fromArray', () => {
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const t = BinaryIndexTree.fromArray(arr);
for (let i = 0; i < arr.length; i++) {
expect(t.query(i) - t.query(i - 1)).toBe(arr[i]);
}
expect(t.query(-10)).toBe(0);
expect(t.query(arr.length - 1)).toBe(55);
});
test('new', () => {
const t = new BinaryIndexTree(10);
expect(t).not.toBeNull();
expect(t.query(9)).toBe(0);
});
});
describe('query', () => {
test('all', () => {
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const t = BinaryIndexTree.fromArray(arr);
expect(t.query(10)).toBe(55);
});
test('negative index', () => {
expect(new BinaryIndexTree().query(-1)).toBe(0);
expect(new BinaryIndexTree().query(-2)).toBe(0);
expect(new BinaryIndexTree().query(-999)).toBe(0);
});
});
describe('udpate', () => {
test('update', () => {
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const t = BinaryIndexTree.fromArray(arr);
t.update(0, +4);
t.update(1, +5);
expect(t.query(10)).toBe(64);
});
});
});