git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 207329f92a735f4a1b82e9f1a2be7e11934e6257

Files: 207329f92a735f4a1b82e9f1a2be7e11934e6257 / tests / index.js

4337 bytesRaw
1const tape = require('tape')
2const crypto = require('crypto')
3const level = require('level-browserify')
4const RadixTree = require('../')
5const db = level('./testdb')
6
7tape('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
55tape('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
82tape('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
110tape('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
121tape('encoding / decoding', async t => {
122 // t.plan(3)
123 const tree = new RadixTree({
124 db: db
125 })
126
127 await tree.set('test', {
128 'something': 1
129 })
130 await tree.flush()
131
132 let r = await tree.get('test', true)
133 t.equals(r.value.something, 1, 'should correctly decode value')
134 t.end()
135})
136
137tape('errors', async t => {
138 const tree = new RadixTree({
139 db: db,
140 root: {
141 '/': Buffer.alloc(20)
142 }
143 })
144
145 try {
146 await tree.get('test')
147 } catch (e) {
148 t.end()
149 }
150})
151
152tape('random', async t => {
153 const tree = new RadixTree({
154 db: db
155 })
156 const entries = 100
157 for (let i = 0; i < entries; i++) {
158 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
159 tree.set(key, Buffer.from([i]))
160 }
161 // console.log(JSON.stringify(tree.root, null, 2))
162
163 await tree.flush()
164
165 for (let i = 0; i < entries; i++) {
166 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
167 const value = await tree.get(key)
168 t.equals(value.value[0], i)
169 }
170
171 await tree.flush()
172 for (let i = 0; i < entries; i++) {
173 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
174 await tree.delete(key)
175 }
176
177 t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
178
179 t.end()
180})
181

Built with git-ssb-web