git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 6b34c4eb25f5a0d003d2f5a603b19ae91bf6b375

Files: 6b34c4eb25f5a0d003d2f5a603b19ae91bf6b375 / tests / index.js

3995 bytesRaw
1const tape = require('tape')
2const crypto = require('crypto')
3const level = require('level')
4const RadixTree = require('../')
5const db = level('./testdb')
6
7tape('set and get', async t => {
8 let tree = new RadixTree({
9 db: db
10 })
11
12 await tree.set('test', Buffer.from('cat'))
13 let val = await tree.get('test')
14 t.equals(val.toString(), 'cat')
15 await tree.set('te', Buffer.from('blop'))
16 val = await tree.get('test')
17 t.equals(val.toString(), 'cat')
18
19 val = await tree.get('te')
20 t.equals(val.toString(), 'blop')
21
22 await tree.set('rad', Buffer.from('cat2'))
23
24 val = await tree.get('rad')
25 t.equals(val.toString(), 'cat2')
26
27 await tree.set('test', Buffer.from('cat111'))
28 val = await tree.get('test')
29 t.equals(val.toString(), 'cat111')
30
31 const stateRoot = await tree.flush()
32
33 // try reteriving node from ipfs
34 tree = new RadixTree({
35 db: db,
36 root: stateRoot
37 })
38
39 val = await tree.get('te')
40 t.equals(val.toString(), 'blop')
41
42 val = await tree.get('rad')
43 t.equals(val.toString(), 'cat2')
44
45 val = await tree.get('test')
46 t.equals(val.toString(), 'cat111')
47 // console.log(JSON.stringify(tree.root, null, 2))
48 t.end()
49})
50
51tape('branch nodes', async t => {
52 const tree = new RadixTree({
53 db: db
54 })
55
56 let key0 = new RadixTree.ArrayConstructor([1, 1])
57 await tree.set(key0, Buffer.from('cat'))
58 let key1 = new RadixTree.ArrayConstructor([0, 1])
59 await tree.set(key1, Buffer.from('cat2'))
60
61 let key2 = new RadixTree.ArrayConstructor([1, 0])
62 await tree.set(key2, Buffer.from('cat'))
63 let key3 = new RadixTree.ArrayConstructor([0, 0])
64 await tree.set(key3, Buffer.from('cat3'))
65
66 let val = await tree.get(key0)
67 t.equals(val.toString(), 'cat')
68 val = await tree.get(key1)
69 t.equals(val.toString(), 'cat2')
70 val = await tree.get(key2)
71 t.equals(val.toString(), 'cat')
72 val = await tree.get(key3)
73 t.equals(val.toString(), 'cat3')
74
75 t.end()
76})
77
78tape('delete', async t => {
79 const tree = new RadixTree({
80 db: db
81 })
82 try {
83 await tree.set('test', Buffer.from('cat'))
84 await tree.set('ter', Buffer.from('cat3'))
85 await tree.delete('te')
86 await tree.delete('test')
87 await tree.delete('ter')
88 t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
89
90 // tests delete midle branchs
91 await tree.set('test', Buffer.from('cat'))
92 await tree.set('te', Buffer.from('cat2'))
93 await tree.set('ter', Buffer.from('cat3'))
94 await tree.delete('te')
95 let val = await tree.get('test')
96 t.equals(val.toString(), 'cat')
97
98 // tests delete end branchs
99 await tree.set('te', 'cat2')
100 await tree.delete('ter')
101 await tree.delete('te')
102 await tree.delete('test')
103 t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
104 } catch (e) {
105 console.log(e)
106 }
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 await tree.set('test', saved)
116 const value = await tree.get('test')
117 t.equals(value.toString(), saved.toString())
118 t.end()
119})
120
121tape('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
136tape('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 await 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[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