This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
forked from rust-util-collections/vsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rs
220 lines (174 loc) · 6.46 KB
/
test.rs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![warn(warnings)]
use super::*;
use std::collections::BTreeMap;
#[test]
fn trie_db_destroy_and_prune() {
let mut s = MptStore::new();
let mut hdr = pnk!(s.trie_init(&[0]));
pnk!(hdr.insert(b"k", b"v0"));
assert_eq!(b"v0", pnk!(hdr.get(b"k")).unwrap().as_slice());
let mut hdr0 = hdr.commit().unwrap();
let root0 = hdr0.root();
pnk!(hdr0.insert(b"k", b"v1"));
assert_eq!(b"v1", pnk!(hdr0.get(b"k")).unwrap().as_slice());
let mut hdr1 = hdr0.commit().unwrap();
let root1 = hdr1.root();
pnk!(s.trie_rederive(&[0], root0));
pnk!(hdr1.insert(b"k", b"V2"));
assert_eq!(b"V2", pnk!(hdr1.get(b"k")).unwrap().as_slice());
let mut hdr2 = hdr1.commit().unwrap();
let root2 = hdr2.root();
assert_eq!(b"V2", pnk!(hdr2.get(b"k")).unwrap().as_slice());
pnk!(s.trie_rederive(&[0], root0));
pnk!(s.trie_rederive(&[0], root1));
pnk!(hdr2.insert(b"k", b"V3"));
assert_eq!(b"V3", pnk!(hdr2.get(b"k")).unwrap().as_slice());
let mut hdr3 = hdr2.commit().unwrap();
let root3 = hdr3.root();
assert_eq!(b"V3", pnk!(hdr3.get(b"k")).unwrap().as_slice());
pnk!(s.trie_rederive(&[0], root2));
pnk!(hdr3.insert(b"k", b"V4"));
assert_eq!(b"V4", pnk!(hdr3.get(b"k")).unwrap().as_slice());
let hdr4 = hdr3.commit().unwrap();
let root4 = hdr4.root();
assert_eq!(b"V4", pnk!(hdr4.get(b"k")).unwrap().as_slice());
assert_ne!(root3, root2);
assert_ne!(root3, root4);
pnk!(s.trie_rederive(&[0], root0));
pnk!(s.trie_rederive(&[0], root1));
pnk!(s.trie_rederive(&[0], root2));
pnk!(s.trie_rederive(&[0], root3));
pnk!(s.trie_rederive(&[0], root4));
let mut hdr = pnk!(s.trie_rederive(&[0], root3));
assert_eq!(b"V3", pnk!(hdr.get(b"k")).unwrap().as_slice());
pnk!(hdr.insert(b"k", b"v5"));
assert_eq!(b"v5", pnk!(hdr.get(b"k")).unwrap().as_slice());
let mut hdr5 = hdr.commit().unwrap();
let root5 = hdr5.root();
assert_eq!(b"v5", pnk!(hdr5.get(b"k")).unwrap().as_slice());
pnk!(hdr5.insert(b"k", b"v6"));
assert_eq!(b"v6", pnk!(hdr5.get(b"k")).unwrap().as_slice());
let hdr6 = hdr5.commit().unwrap();
let root6 = hdr6.root();
assert_eq!(b"v6", pnk!(hdr6.get(b"k")).unwrap().as_slice());
pnk!(s.trie_prune(&[0], root3));
assert!(s.trie_rederive(&[0], root0).is_err());
assert!(s.trie_rederive(&[0], root1).is_err());
assert!(s.trie_rederive(&[0], root2).is_err());
assert_eq!(
b"V3",
pnk!(pnk!(s.trie_rederive(&[0], root3)).get(b"k"))
.unwrap()
.as_slice()
);
assert_eq!(
b"V4",
pnk!(pnk!(s.trie_rederive(&[0], root4)).get(b"k"))
.unwrap()
.as_slice()
);
assert_eq!(
b"v5",
pnk!(pnk!(s.trie_rederive(&[0], root5)).get(b"k"))
.unwrap()
.as_slice()
);
assert_eq!(
b"v6",
pnk!(pnk!(s.trie_rederive(&[0], root6)).get(b"k"))
.unwrap()
.as_slice()
);
pnk!(s.trie_prune(&[0], root4));
assert_eq!(
b"V4",
pnk!(pnk!(s.trie_rederive(&[0], root4)).get(b"k"))
.unwrap()
.as_slice()
);
assert!(s.trie_rederive(&[0], root0).is_err());
assert!(s.trie_rederive(&[0], root1).is_err());
assert!(s.trie_rederive(&[0], root2).is_err());
assert!(s.trie_rederive(&[0], root3).is_err());
assert!(s.trie_rederive(&[0], root5).is_err());
assert!(s.trie_rederive(&[0], root6).is_err());
s.trie_destroy(&[0]);
assert!(s.trie_rederive(&[0], root0).is_err());
assert!(s.trie_rederive(&[0], root1).is_err());
assert!(s.trie_rederive(&[0], root2).is_err());
assert!(s.trie_rederive(&[0], root3).is_err());
assert!(s.trie_rederive(&[0], root4).is_err());
assert!(s.trie_rederive(&[0], root5).is_err());
assert!(s.trie_rederive(&[0], root6).is_err());
}
#[test]
fn trie_db_rederive() {
let mut s = MptStore::new();
let mut hdr = pnk!(s.trie_init(b""));
pnk!(hdr.insert(b"key", b"value"));
assert_eq!(b"value", pnk!(hdr.get(b"key")).unwrap().as_slice());
let hdr = hdr.commit().unwrap();
let root = hdr.root();
assert_eq!(root, hdr.root());
let hdr_encoded = hdr.encode();
drop(hdr);
let mut hdr = pnk!(MptOnce::decode(&hdr_encoded));
assert_eq!(b"value", pnk!(hdr.get(b"key")).unwrap().as_slice());
assert_eq!(root, hdr.root());
pnk!(hdr.insert(b"key1", b"value1"));
assert_eq!(b"value1", pnk!(hdr.get(b"key1")).unwrap().as_slice());
let old_hdr_ro = pnk!(hdr.ro_handle(root));
assert_eq!(root, old_hdr_ro.root());
assert_eq!(b"value", pnk!(old_hdr_ro.get(b"key")).unwrap().as_slice());
assert!(pnk!(old_hdr_ro.get(b"key1")).is_none());
let hdr = hdr.commit().unwrap();
let new_root = hdr.root();
assert_eq!(new_root, hdr.root());
}
#[test]
fn trie_db_iter() {
let mut s = MptStore::new();
let mut hdr = pnk!(s.trie_init(b"backend_key"));
assert!(hdr.is_empty());
/////////////////////////////////////////////
let samples = (0u8..200).map(|i| ([i], [i])).collect::<Vec<_>>();
samples.iter().for_each(|(k, v)| {
pnk!(hdr.insert(k, v));
});
let mut hdr = hdr.commit().unwrap();
let root = hdr.root();
let ro_hdr = hdr.ro_handle(root).unwrap();
let bt = ro_hdr
.iter()
.map(|i| i.unwrap())
.collect::<BTreeMap<_, _>>();
bt.iter().enumerate().for_each(|(i, (k, v))| {
assert_eq!(&[i as u8], k.as_slice());
assert_eq!(k, v);
});
let keylist = ro_hdr.key_iter().map(|i| i.unwrap()).collect::<Vec<_>>();
assert_eq!(keylist, bt.keys().cloned().collect::<Vec<_>>());
/////////////////////////////////////////////
let samples = (0u8..200).map(|i| ([i], [i + 1])).collect::<Vec<_>>();
samples.iter().for_each(|(k, v)| {
pnk!(hdr.insert(k, v));
});
let mut hdr = hdr.commit().unwrap();
let root = hdr.root();
let ro_hdr = hdr.ro_handle(root).unwrap();
let bt = ro_hdr
.iter()
.map(|i| i.unwrap())
.collect::<BTreeMap<_, _>>();
bt.iter().enumerate().for_each(|(i, (k, v))| {
assert_eq!(&[i as u8], k.as_slice());
assert_eq!(&[k[0] + 1], v.as_slice());
});
let keylist = ro_hdr.key_iter().map(|i| i.unwrap()).collect::<Vec<_>>();
assert_eq!(keylist, bt.keys().cloned().collect::<Vec<_>>());
assert!(!hdr.is_empty());
/////////////////////////////////////////////
assert!(!hdr.is_empty());
hdr.clear().unwrap();
assert!(hdr.is_empty());
}