-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit.spec.ts
39 lines (30 loc) · 1.09 KB
/
bit.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
// tslint:disable:no-expression-statement
import test from 'ava';
import BinaryIndexedTree from './bit';
import RedisMockBackend from './redis-mock-backend';
test('tests', async t => {
const backend = new RedisMockBackend(null, 1e6, 'dummyKey');
await backend.flushDb();
const bit = new BinaryIndexedTree(backend);
t.deepEqual(await bit.rangeQuery(0, 0), 0);
t.deepEqual(await bit.rangeQuery(1, 0), 0);
await bit.update(0, 4);
await bit.update(1, 8);
await bit.update(11, 3);
t.deepEqual(await bit.query(1), 12);
await bit.update(13, 10);
t.deepEqual(await bit.rangeQuery(0, 14), 25);
t.deepEqual(await bit.rangeQuery(11, 14), 13);
// updating out of the range
await bit.update(1e6, 3);
t.deepEqual(await bit.rangeQuery(1e6, 1e6 + 1), 0);
// querying out of range
t.deepEqual(await bit.query(1e6), 25);
t.deepEqual(await bit.query(1e6 + 1), 25);
await bit.update(1e6 - 1, 20);
t.deepEqual(await bit.rangeQuery(1e6 - 1, 1e6), 20);
// TS-Lint helps preventing the next situation:
// bit.update = async function(ind: number, val: number) {
// return;
// }
});