tests/index.jsView |
---|
1 | 1 … | const tape = require('tape') |
2 | 2 … | const crypto = require('crypto') |
3 | | -const level = require('level') |
| 3 … | +const level = require('level-browserify') |
4 | 4 … | const RadixTree = require('../') |
5 | 5 … | const db = level('./testdb') |
6 | 6 … | |
7 | 7 … | tape('set and get', async t => { |
12 | 12 … | let tree = new RadixTree({ |
13 | 13 … | db: db |
14 | 14 … | }) |
15 | 15 … | |
16 | | - await tree.set('test', Buffer.from('cat')) |
| 16 … | + tree.set('test', Buffer.from('cat')) |
17 | 17 … | let val = await tree.get('test') |
18 | 18 … | t.equals(val.value.toString(), 'cat') |
19 | | - await tree.set('te', Buffer.from('blop')) |
| 19 … | + tree.set('te', Buffer.from('blop')) |
20 | 20 … | val = await tree.get('test') |
21 | 21 … | t.equals(val.value.toString(), 'cat') |
22 | 22 … | |
23 | 23 … | val = await tree.get('te') |
24 | 24 … | t.equals(val.value.toString(), 'blop') |
25 | 25 … | |
26 | | - await tree.set('rad', Buffer.from('cat2')) |
| 26 … | + tree.set('rad', Buffer.from('cat2')) |
27 | 27 … | |
28 | 28 … | val = await tree.get('rad') |
29 | 29 … | t.equals(val.value.toString(), 'cat2') |
30 | 30 … | |
31 | | - await tree.set('test', Buffer.from('cat111')) |
| 31 … | + tree.set('test', Buffer.from('cat111')) |
32 | 32 … | val = await tree.get('test') |
33 | 33 … | t.equals(val.value.toString(), 'cat111') |
34 | 34 … | |
35 | 35 … | const stateRoot = await tree.flush() |
57 | 57 … | db: db |
58 | 58 … | }) |
59 | 59 … | |
60 | 60 … | let key0 = new RadixTree.ArrayConstructor([1, 1]) |
61 | | - await tree.set(key0, Buffer.from('cat')) |
62 | 61 … | let key1 = new RadixTree.ArrayConstructor([0, 1]) |
63 | | - await tree.set(key1, Buffer.from('cat2')) |
64 | | - |
65 | 62 … | let key2 = new RadixTree.ArrayConstructor([1, 0]) |
66 | | - await tree.set(key2, Buffer.from('cat')) |
67 | 63 … | let key3 = new RadixTree.ArrayConstructor([0, 0]) |
68 | | - await tree.set(key3, Buffer.from('cat3')) |
69 | 64 … | |
| 65 … | + tree.set(key0, Buffer.from('cat')) |
| 66 … | + tree.set(key1, Buffer.from('cat2')) |
| 67 … | + tree.set(key2, Buffer.from('cat')) |
| 68 … | + tree.set(key3, Buffer.from('cat3')) |
| 69 … | + |
70 | 70 … | let val = await tree.get(key0) |
71 | 71 … | t.equals(val.value.toString(), 'cat') |
72 | 72 … | val = await tree.get(key1) |
73 | 73 … | t.equals(val.value.toString(), 'cat2') |
82 | 82 … | tape('delete', async t => { |
83 | 83 … | const tree = new RadixTree({ |
84 | 84 … | db: db |
85 | 85 … | }) |
86 | | - await tree.set('test', Buffer.from('cat')) |
87 | | - await tree.set('ter', Buffer.from('cat3')) |
88 | | - await tree.delete('te') |
89 | | - await tree.delete('test') |
| 86 … | + tree.set('test', Buffer.from('cat')) |
| 87 … | + tree.set('ter', Buffer.from('cat3')) |
| 88 … | + tree.delete('te') |
| 89 … | + tree.delete('test') |
90 | 90 … | await tree.delete('ter') |
91 | 91 … | t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) |
92 | 92 … | |
93 | 93 … | |
94 | | - await tree.set('test', Buffer.from('cat')) |
95 | | - await tree.set('te', Buffer.from('cat2')) |
96 | | - await tree.set('ter', Buffer.from('cat3')) |
| 94 … | + tree.set('test', Buffer.from('cat')) |
| 95 … | + tree.set('te', Buffer.from('cat2')) |
| 96 … | + tree.set('ter', Buffer.from('cat3')) |
97 | 97 … | await tree.delete('te') |
98 | 98 … | let val = await tree.get('test') |
99 | 99 … | t.equals(val.value.toString(), 'cat') |
100 | 100 … | |
101 | 101 … | |
102 | | - await tree.set('te', 'cat2') |
103 | | - await tree.delete('ter') |
104 | | - await tree.delete('te') |
| 102 … | + tree.set('te', 'cat2') |
| 103 … | + tree.delete('ter') |
| 104 … | + tree.delete('te') |
105 | 105 … | await tree.delete('test') |
106 | 106 … | t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) |
107 | 107 … | t.end() |
108 | 108 … | }) |
111 | 111 … | const tree = new RadixTree({ |
112 | 112 … | db: db |
113 | 113 … | }) |
114 | 114 … | const saved = Buffer.alloc(33).fill(1) |
115 | | - await tree.set('test', saved) |
| 115 … | + tree.set('test', saved) |
116 | 116 … | const value = await tree.get('test') |
117 | 117 … | t.equals(value.value.toString(), saved.toString()) |
118 | 118 … | t.end() |
119 | 119 … | }) |
155 | 155 … | }) |
156 | 156 … | const entries = 100 |
157 | 157 … | for (let i = 0; i < entries; i++) { |
158 | 158 … | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
159 | | - await tree.set(key, Buffer.from([i])) |
| 159 … | + tree.set(key, Buffer.from([i])) |
160 | 160 … | } |
|
161 | 161 … | |
162 | 162 … | |
163 | 163 … | await tree.flush() |