Files: 806b9f80cab0da81d7e98a62754b80167fe58296 / tests / index.js
4041 bytesRaw
1 | const tape = require('tape') |
2 | const crypto = require('crypto') |
3 | const level = require('level-browserify') |
4 | const RadixTree = require('../') |
5 | const db = level('./testdb') |
6 | |
7 | tape('set and get', async t => { |
8 | const r = await RadixTree.getMerkleLink(Buffer.from([0])) |
9 | |
10 | t.equal(r.toString('hex'), '6e340b9cffb37a989ca544e6bb780a2c78901d3f', 'should hash') |
11 | |
12 | let tree = new RadixTree({ |
13 | db: db |
14 | }) |
15 | |
16 | tree.set('test', Buffer.from('cat')) |
17 | let val = await tree.get('test') |
18 | t.equals(val.value.toString(), 'cat') |
19 | tree.set('te', Buffer.from('blop')) |
20 | val = await tree.get('test') |
21 | t.equals(val.value.toString(), 'cat') |
22 | |
23 | val = await tree.get('te') |
24 | t.equals(val.value.toString(), 'blop') |
25 | |
26 | tree.set('rad', Buffer.from('cat2')) |
27 | |
28 | val = await tree.get('rad') |
29 | t.equals(val.value.toString(), 'cat2') |
30 | |
31 | tree.set('test', Buffer.from('cat111')) |
32 | val = await tree.get('test') |
33 | t.equals(val.value.toString(), 'cat111') |
34 | |
35 | const stateRoot = await tree.flush() |
36 | |
37 | // try reteriving node from ipfs |
38 | tree = new RadixTree({ |
39 | db: db, |
40 | root: stateRoot |
41 | }) |
42 | |
43 | val = await tree.get('te') |
44 | t.equals(val.value.toString(), 'blop') |
45 | |
46 | val = await tree.get('rad') |
47 | t.equals(val.value.toString(), 'cat2') |
48 | |
49 | val = await tree.get('test') |
50 | t.equals(val.value.toString(), 'cat111') |
51 | // console.log(JSON.stringify(tree.root, null, 2)) |
52 | t.end() |
53 | }) |
54 | |
55 | tape('branch nodes', async t => { |
56 | const tree = new RadixTree({ |
57 | db: db |
58 | }) |
59 | |
60 | let key0 = new RadixTree.ArrayConstructor([1, 1]) |
61 | let key1 = new RadixTree.ArrayConstructor([0, 1]) |
62 | let key2 = new RadixTree.ArrayConstructor([1, 0]) |
63 | let key3 = new RadixTree.ArrayConstructor([0, 0]) |
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 | let val = await tree.get(key0) |
71 | t.equals(val.value.toString(), 'cat') |
72 | val = await tree.get(key1) |
73 | t.equals(val.value.toString(), 'cat2') |
74 | val = await tree.get(key2) |
75 | t.equals(val.value.toString(), 'cat') |
76 | val = await tree.get(key3) |
77 | t.equals(val.value.toString(), 'cat3') |
78 | |
79 | t.end() |
80 | }) |
81 | |
82 | tape('delete', async t => { |
83 | const tree = new RadixTree({ |
84 | db: db |
85 | }) |
86 | tree.set('test', Buffer.from('cat')) |
87 | tree.set('ter', Buffer.from('cat3')) |
88 | tree.delete('te') |
89 | tree.delete('test') |
90 | await tree.delete('ter') |
91 | t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) |
92 | |
93 | // tests delete midle branchs |
94 | tree.set('test', Buffer.from('cat')) |
95 | tree.set('te', Buffer.from('cat2')) |
96 | tree.set('ter', Buffer.from('cat3')) |
97 | await tree.delete('te') |
98 | let val = await tree.get('test') |
99 | t.equals(val.value.toString(), 'cat') |
100 | |
101 | // tests delete end branchs |
102 | tree.set('te', 'cat2') |
103 | tree.delete('ter') |
104 | tree.delete('te') |
105 | await tree.delete('test') |
106 | t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) |
107 | t.end() |
108 | }) |
109 | |
110 | tape('large values', async t => { |
111 | const tree = new RadixTree({ |
112 | db: db |
113 | }) |
114 | const saved = Buffer.alloc(33).fill(1) |
115 | tree.set('test', saved) |
116 | const value = await tree.get('test') |
117 | t.equals(value.value.toString(), saved.toString()) |
118 | t.end() |
119 | }) |
120 | |
121 | tape('errors', async t => { |
122 | const tree = new RadixTree({ |
123 | db: db, |
124 | root: { |
125 | '/': Buffer.alloc(20) |
126 | } |
127 | }) |
128 | |
129 | try { |
130 | await tree.get('test') |
131 | } catch (e) { |
132 | t.end() |
133 | } |
134 | }) |
135 | |
136 | tape('random', async t => { |
137 | const tree = new RadixTree({ |
138 | db: db |
139 | }) |
140 | const entries = 100 |
141 | for (let i = 0; i < entries; i++) { |
142 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
143 | tree.set(key, Buffer.from([i])) |
144 | } |
145 | // console.log(JSON.stringify(tree.root, null, 2)) |
146 | |
147 | await tree.flush() |
148 | |
149 | for (let i = 0; i < entries; i++) { |
150 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
151 | const value = await tree.get(key) |
152 | t.equals(value.value[0], i) |
153 | } |
154 | |
155 | await tree.flush() |
156 | for (let i = 0; i < entries; i++) { |
157 | const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20) |
158 | await tree.delete(key) |
159 | } |
160 | |
161 | t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) |
162 | |
163 | t.end() |
164 | }) |
165 |
Built with git-ssb-web