git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 4fe6af79637c7ada713ce5ddce656ae7a400a7d9

Files: 4fe6af79637c7ada713ce5ddce656ae7a400a7d9 / tests / index.js

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

Built with git-ssb-web