/
Trie.spec.js
95 lines (75 loc) · 2.15 KB
/
Trie.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017-2018 @polkadot/client-db authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import toU8a from '@polkadot/util/u8a/toU8a';
import TrieDb from './Trie/Memory';
const EMPTY_ROOT = toU8a('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421');
const HELLO_ROOT = toU8a('0x0a915659b88f80bfa200570bd2767a6ab8cb0a4e44fd240cc5ed7a27728c4531');
const FOO_ROOT = toU8a('0xed4e32371288ee83be74f78fb492f01261b7c3dc9c422581bb705c4376492dc6');
describe('TrieDb', () => {
let memory = new TrieDb();
it('starts with the default root', () => {
expect(
memory.getRoot()
).toEqual(EMPTY_ROOT);
});
it('has the correct root after a single insertion', () => {
memory.put(toU8a('hello'), toU8a('world'));
expect(
memory.getRoot()
).toEqual(
HELLO_ROOT
);
});
it('has the correct root after a deletion', () => {
memory.del(toU8a('hello'));
expect(
memory.getRoot()
).toEqual(
EMPTY_ROOT
);
});
it('has the correct root after a re-insertion', () => {
memory.put(toU8a('hello'), toU8a('world'));
expect(
memory.getRoot()
).toEqual(
HELLO_ROOT
);
});
it('has the correct root after a second insertion', () => {
memory.put(toU8a('foo'), toU8a('bar'));
expect(
memory.getRoot()
).toEqual(
FOO_ROOT
);
});
it('has the correct root after a re-deletion', () => {
memory.del(toU8a('hello'));
expect(
memory.getRoot()
).toEqual(
toU8a('0x99650c730bbb99f6f58ce8b09bca2a8d90b36ac662e71bf81ec401ed23d199fb')
);
});
it('has the correct root after a re-re-insertion', () => {
memory.put(toU8a('hello'), toU8a('world'));
expect(
memory.getRoot()
).toEqual(
FOO_ROOT
);
});
it('has the correct root after an update', () => {
memory.put(toU8a('hello'), toU8a('mars'));
expect(
memory.getRoot()
).toEqual(
toU8a('0x116267e482ab94eb8824af425ea7f20ee84d10a8fa735f26204d80000e2a907e')
);
});
it('terminates', () => {
return memory.terminate();
});
});