This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
bitfield.js
71 lines (58 loc) · 1.5 KB
/
bitfield.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
const test = require('brittle')
const ram = require('random-access-memory')
const Bitfield = require('../lib/bitfield')
test('bitfield - set and get', async function (t) {
const b = await Bitfield.open(ram())
t.absent(b.get(42))
b.set(42, true)
t.ok(b.get(42))
// bigger offsets
t.absent(b.get(42000000))
b.set(42000000, true)
t.ok(b.get(42000000))
b.set(42000000, false)
t.absent(b.get(42000000))
await b.flush()
})
test('bitfield - random set and gets', async function (t) {
const b = await Bitfield.open(ram())
const set = new Set()
for (let i = 0; i < 200; i++) {
const idx = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
b.set(idx, true)
set.add(idx)
}
for (let i = 0; i < 500; i++) {
const idx = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
const expected = set.has(idx)
const val = b.get(idx)
if (val !== expected) {
t.fail('expected ' + expected + ' but got ' + val + ' at ' + idx)
return
}
}
for (const idx of set) {
const val = b.get(idx)
if (val !== true) {
t.fail('expected true but got ' + val + ' at ' + idx)
return
}
}
t.pass('all random set and gets pass')
})
test('bitfield - reload', async function (t) {
const s = ram()
{
const b = await Bitfield.open(s)
b.set(142, true)
b.set(40000, true)
b.set(1424242424, true)
await b.flush()
}
{
const b = await Bitfield.open(s)
t.ok(b.get(142))
t.ok(b.get(40000))
t.ok(b.get(1424242424))
}
})