git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: a7ce27a859b2fdd6478c8cad7d7718dfc8591466

Files: a7ce27a859b2fdd6478c8cad7d7718dfc8591466 / tests / index.js

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

Built with git-ssb-web