-
Notifications
You must be signed in to change notification settings - Fork 0
/
default-store.js
90 lines (72 loc) · 2.78 KB
/
default-store.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
const test = require('tape');
const TwoD = require('../');
const {
singleSetTestUnset,
tKey,
ourRand,
setNRandomAndCheckInBounds
} = require('./shared');
test('construct with no options', function (t) {
t.plan(1);
const defaultInitBitmap = new TwoD.SparseBitmap();
t.equal(typeof defaultInitBitmap.backingStore, typeof new TwoD.InMemoryStore());
});
test('0,0 coord', async function (t) { await singleSetTestUnset('00', t, 0, 0); });
test('0,1 coord', async function (t) { await singleSetTestUnset('01', t, 0, 1); });
test('1,0 coord', async function (t) { await singleSetTestUnset('10', t, 1, 0); });
test('1,1 coord', async function (t) { await singleSetTestUnset('11', t, 1, 1); });
test('small chunk', async function (t) {
t.plan(4);
const scBitmap = new TwoD.SparseBitmap({
[TwoD.ChunkWidthKey]: 16
});
const key = tKey('small');
await scBitmap.set(key, 0, 17);
t.equal(await scBitmap.get(key, 0, 17), 1);
const inBounds = await scBitmap.inBounds(key, {
from: { x: 0, y: 0 },
to: { x: 32, y: 32 }
});
t.equal(inBounds.length, 1);
t.equal(inBounds[0][0], 0);
t.equal(inBounds[0][1], 17);
});
test('single simple coord', async function (t) {
const coord = TwoD.Defaults[TwoD.ChunkWidthKey] / 2;
await singleSetTestUnset('ssc', t, coord, coord);
});
test('single random coord', async function (t) {
t.plan(5);
const bitmap = new TwoD.SparseBitmap();
const key = tKey('src');
const [xRand, yRand] = [ourRand(), ourRand()];
console.log(`using random x,y: ${xRand},${yRand}`);
await bitmap.set(key, xRand, yRand);
t.equal(await bitmap.get(key, xRand, yRand), 1);
const boundsAdj = TwoD.Defaults[TwoD.ChunkWidthKey] / 2;
const inBounds = await bitmap.inBounds(key, {
from: { x: Math.max(xRand - boundsAdj, 0), y: Math.max(yRand - boundsAdj, 0) },
to: { x: xRand + boundsAdj, y: yRand + boundsAdj}
});
t.equal(inBounds.length, 1);
t.equal(inBounds[0][0], xRand);
t.equal(inBounds[0][1], yRand);
await bitmap.unset(key, xRand, yRand);
t.equal(await bitmap.get(key, xRand, yRand), 0);
});
test('42 random coords', async function (t) {
await setNRandomAndCheckInBounds('42rand', t, 42, new TwoD.SparseBitmap());
});
test('rand random coords', async function (t) {
const nRand = Math.floor(512 + Math.random() * 512);
console.log(`using n=${nRand}`);
await setNRandomAndCheckInBounds(`rand${nRand}rand`, t, nRand, new TwoD.SparseBitmap());
});
test('42 random coords - strict', async function (t) {
await setNRandomAndCheckInBounds('42rand', t, 42, new TwoD.SparseBitmap(), true);
});
test('rand random coords - strict', async function (t) {
const nRand = Math.floor(512 + Math.random() * 512);
console.log(`using n=${nRand}`);
await setNRandomAndCheckInBounds(`rand${nRand}rand`, t, nRand, new TwoD.SparseBitmap(), true);
});