forked from ethereum/solidity-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.sol
333 lines (263 loc) · 9.55 KB
/
data.sol
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
pragma solidity ^0.4.16;
pragma experimental "v0.5.0";
pragma experimental "ABIEncoderV2";
//import {Bits} from "../../bits/Bits.sol";
import {Data} from "../../src/patricia_tree/Data.sol";
import {STLTest} from "../STLTest.sol";
// TODO seems testing here is a bit over the top.
/*******************************************************/
contract PatriciaTreeDataTest is STLTest {
using Data for Data.Node;
using Data for Data.Edge;
using Data for Data.Label;
uint internal constant MAX_LENGTH = 256;
uint internal constant UINT256_ZEROES = 0;
uint internal constant UINT256_ONES = ~uint(0);
bytes32 internal constant B32_ZEROES = bytes32(UINT256_ZEROES);
bytes32 internal constant B32_ONES = bytes32(UINT256_ONES);
}
/*******************************************************/
contract TestPatriciaTreeDataChopFirstBitThrowsLengthIsZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl;
lbl.chopFirstBit();
}
}
contract TestPatriciaTreeDataChopFirstBitZeroes is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 256);
uint bit;
for (uint i = 1; i <= 256; i++) {
(bit, lbl) = lbl.chopFirstBit();
require(bit == 0);
require(lbl.data == B32_ZEROES);
require(lbl.length == MAX_LENGTH - i);
}
}
}
contract TestPatriciaTreeDataChopFirstBitOnes is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 256);
uint bit;
for (uint i = 1; i <= 256; i++) {
(bit, lbl) = lbl.chopFirstBit();
require(bit == 1);
require(lbl.data == B32_ONES << i);
require(lbl.length == MAX_LENGTH - i);
}
}
}
contract TestPatriciaTreeDataChopFirstBitDoesNotMutate is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 256);
lbl.chopFirstBit();
require(lbl.data == B32_ONES);
require(lbl.length == 256);
}
}
contract TestPatriciaTreeDataRemovePrefixThrowsLessLengthZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl;
lbl.removePrefix(1);
}
}
contract TestPatriciaTreeDataRemovePrefixThrowsLessLengthNonZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 5);
lbl.removePrefix(6);
}
}
contract TestPatriciaTreeDataRemoveZeroPrefixLengthZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 0);
lbl = lbl.removePrefix(0);
require(lbl.data == B32_ONES);
require(lbl.length == 0);
}
}
contract TestPatriciaTreeDataRemoveZeroPrefixLength256 is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 256);
lbl = lbl.removePrefix(0);
require(lbl.data == B32_ONES);
require(lbl.length == 256);
}
}
contract TestPatriciaTreeDataRemoveFullPrefixLength256 is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 256);
lbl = lbl.removePrefix(256);
require(lbl.data == B32_ZEROES);
require(lbl.length == 0);
}
}
contract TestPatriciaTreeDataRemovePrefix is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(hex"ef1230", 20);
lbl = lbl.removePrefix(4);
require(lbl.length == 16);
require(lbl.data == hex"f123");
lbl = lbl.removePrefix(15);
require(lbl.length == 1);
require(lbl.data == hex"80");
lbl = lbl.removePrefix(1);
require(lbl.length == 0);
require(lbl.data == 0);
}
}
contract TestPatriciaTreeDataRemovePrefixOnes is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 256);
for (uint i = 1; i <= 256; i++) {
lbl = lbl.removePrefix(1);
require(lbl.data == B32_ONES << i);
require(lbl.length == MAX_LENGTH - i);
}
}
}
contract TestPatriciaTreeDataRemovePrefixDoesNotMutate is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(hex"ef1230", 20);
lbl.removePrefix(4);
require(lbl.data == hex"ef1230");
require(lbl.length == 20);
}
}
contract TestPatriciaTreeDataCommonPrefixOfZeroLengthLabelsIsZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a;
Data.Label memory b;
require(a.commonPrefix(b) == 0);
}
}
contract TestPatriciaTreeDataCommonPrefixOfNonZeroAndZeroLabelIsZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(B32_ONES, 256);
Data.Label memory b;
require(a.commonPrefix(b) == 0);
}
}
contract TestPatriciaTreeDataCommonPrefixOfLabelWithItselfIsLabelLength is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(B32_ONES, 0);
require(a.commonPrefix(a) == 0);
a = Data.Label(B32_ONES, 164);
require(a.commonPrefix(a) == 164);
a = Data.Label(B32_ONES, 256);
require(a.commonPrefix(a) == 256);
}
}
contract TestPatriciaTreeDataSplitAtThrowsPosGreaterThanLength is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 0);
lbl.splitAt(1);
}
}
contract TestPatriciaTreeDataSplitAtThrowsPosGreaterThan256 is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 0);
lbl.splitAt(257);
}
}
contract TestPatriciaTreeDataSplitAtPosEqualToLengthDoesntFail is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 2);
lbl.splitAt(2);
}
}
contract TestPatriciaTreeDataSplitAtPosEqualTo256DoesntFail is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ZEROES, 256);
lbl.splitAt(256);
}
}
contract TestPatriciaTreeDataSplitAtDoesntMutate is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 55);
lbl.splitAt(43);
require(lbl.data == B32_ONES);
require(lbl.length == 55);
}
}
contract TestPatriciaTreeDataSplitAtZero is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory lbl = Data.Label(B32_ONES, 55);
var (pre, suf) = lbl.splitAt(0);
require(pre.data == B32_ZEROES);
require(pre.length == 0);
require(suf.data == B32_ONES);
require(suf.length == 55);
}
}
contract TestPatriciaTreeDataSplitAt is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(hex"abcd", 16);
var (x, y) = a.splitAt(0);
require(x.length == 0);
require(y.length == a.length);
require(y.data == a.data);
(x, y) = a.splitAt(4);
require(x.length == 4);
require(x.data == hex"a0");
require(y.length == 12);
require(y.data == hex"bcd0");
(x, y) = a.splitAt(16);
require(y.length == 0);
require(x.length == a.length);
require(x.data == a.data);
}
}
contract TestPatriciaTreeDataSplitCommonPrefixDoesNotMutate is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(hex"abcd", 16);
Data.Label memory b = Data.Label(hex"a0f570", 20);
a.splitCommonPrefix(b);
require(a.data == hex"abcd");
require(a.length == 16);
require(b.data == hex"a0f570");
require(b.length == 20);
}
}
contract TestPatriciaTreeDataSplitCommonPrefixWithItself is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(hex"abcd", 16);
var (pre, suf) = a.splitCommonPrefix(a);
require(pre.data == a.data);
require(pre.length == a.length);
require(suf.data == B32_ZEROES);
require(suf.length == 0);
}
}
contract TestPatriciaTreeDataSplitCommonPrefixWithZeroLabel is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(B32_ZEROES, 0);
Data.Label memory b = Data.Label(hex"a0f570", 20);
var (pre, suf) = a.splitCommonPrefix(b);
require(pre.data == B32_ZEROES);
require(pre.length == 0);
require(suf.data == a.data);
require(suf.length == a.length);
}
}
contract TestPatriciaTreeDataSplitCommonPrefixWithZeroCheck is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(B32_ZEROES, 0);
Data.Label memory b = Data.Label(hex"a0f570", 20);
var (pre, suf) = b.splitCommonPrefix(a);
require(pre.data == B32_ZEROES);
require(pre.length == 0);
require(suf.data == b.data);
require(suf.length == b.length);
}
}
contract TestPatriciaTreeDataSplitCommonPrefix is PatriciaTreeDataTest {
function testImpl() internal {
Data.Label memory a = Data.Label(hex"abcd", 16);
Data.Label memory b = Data.Label(hex"a0f570", 20);
var (prefix, suffix) = b.splitCommonPrefix(a);
require(prefix.length == 4);
require(prefix.data == hex"a0");
require(suffix.length == 16);
require(suffix.data == hex"0f57");
}
}