-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAssertUintArray.sol
344 lines (265 loc) · 9.2 KB
/
AssertUintArray.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
334
335
336
337
338
339
340
341
342
343
344
//SPDX-License-Identifier: MIT
pragma solidity >= 0.4.15 < 0.8.0;
library AssertUintArray {
uint8 constant ZERO = uint8(byte('0'));
uint8 constant A = uint8(byte('a'));
/*
Event: TestEvent
Fired when an assertion is made.
Params:
result (bool) - Whether or not the assertion holds.
message (string) - A message to display if the assertion does not hold.
*/
event TestEvent(bool indexed result, string message);
// ************************************** uint[] **************************************
/*
Function: equal(uint[])
Assert that two 'uint[ ]' are equal.
: arrA.length == arrB.length
and, for all valid indices 'i'
: arrA[i] == arrB[i]
Params:
A (uint[]) - The first array.
B (uint[]) - The second array.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function equal(uint[] memory arrA, uint[] memory arrB, string memory message) public returns (bool result) {
result = arrA.length == arrB.length;
if (result) {
for (uint i = 0; i < arrA.length; i++) {
if (arrA[i] != arrB[i]) {
result = false;
break;
}
}
}
_report(result, message);
}
/*
Function: notEqual(uint[])
Assert that two 'uint[]' are not equal.
: arrA.length != arrB.length
or, for some valid index 'i'
: arrA[i] != arrB[i]
Params:
A (uint[]) - The first string.
B (uint[]) - The second string.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function notEqual(uint[] memory arrA, uint[] memory arrB, string memory message) public returns (bool result) {
result = arrA.length == arrB.length;
if (result) {
for (uint i = 0; i < arrA.length; i++) {
if (arrA[i] != arrB[i]) {
result = false;
break;
}
}
}
result = !result;
_report(result, message);
}
/*
Function: lengthEqual(uint[])
Assert that the length of a 'uint[]' is equal to a given value.
: arr.length == length
Params:
arr (uint[]) - The array.
length (uint) - The length.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function lengthEqual(uint[] memory arr, uint length, string memory message) public returns (bool result) {
uint arrLength = arr.length;
if (arrLength == length)
_report(result, "");
else
_report(result, _appendTagged(_tag(arrLength, "Tested"), _tag(length, "Against"), message));
}
/*
Function: lengthNotEqual(uint[])
Assert that the length of a 'uint[]' is not equal to a given value.
: arr.length != length
Params:
arr (uint[]) - The array.
length (uint) - The length.
message (string) - A message that is sent if the assertion fails.
Returns:
result (bool) - The result.
*/
function lengthNotEqual(uint[] memory arr, uint length, string memory message) public returns (bool result) {
uint arrLength = arr.length;
if (arrLength != arr.length)
_report(result, "");
else
_report(result, _appendTagged(_tag(arrLength, "Tested"), _tag(length, "Against"), message));
}
/******************************** internal ********************************/
/*
Function: _report
Internal function for triggering <TestEvent>.
Params:
result (bool) - The test result (true or false).
message (string) - The message that is sent if the assertion fails.
*/
function _report(bool result, string memory message) internal {
if(result)
emit TestEvent(true, "");
else
emit TestEvent(false, message);
}
/*
Function: _utoa(uint)
Convert an unsigned integer to a string.
Params:
n (uint) - The unsigned integer.
radix (uint8) - A number between 2 and 16 (inclusive). Characters used are 0-9,a-f
Returns:
result (string) - The resulting string.
*/
function _utoa(uint n, uint8 radix) internal pure returns (string memory) {
if (n == 0 || radix < 2 || radix > 16)
return '0';
bytes memory bts = new bytes(256);
uint i;
while (n > 0) {
bts[i++] = _utoa(uint8(n % radix)); // Turn it to ascii.
n /= radix;
}
// Reverse
bytes memory rev = new bytes(i);
for (uint j = 0; j < i; j++)
rev[j] = bts[i - j - 1];
return string(rev);
}
/*
Function: _utoa(uint8)
Convert an unsigned 8-bit integer to its ASCII byte representation. Numbers 0-9 are converted to '0'-'9',
numbers 10-16 to 'a'-'f'. Numbers larger then 16 return the null byte.
Params:
u (uint8) - The unsigned 8-bit integer.
Returns:
result (string) - The ASCII byte.
*/
function _utoa(uint8 u) internal pure returns (byte) {
if (u < 10)
return byte(u + ZERO);
else if (u < 16)
return byte(u - 10 + A);
else
return 0;
}
/*
function htoa(address addr) constant returns (string) {
bytes memory bts = new bytes(40);
bytes20 addrBts = bytes20(addr);
for (uint i = 0; i < 20; i++) {
bts[2*i] = addrBts[i] % 16;
bts[2*i + 1] = (addrBts[i] / 16) % 16;
}
return string(bts);
}
*/
/*
Function: _tag(string)
Add a tag to a string. The 'value' and 'tag' strings are returned on the form "tag: value".
Params:
value (string) - The value.
tag (string) - The tag.
Returns:
result (string) - "tag: value"
*/
function _tag(string memory value, string memory tag) internal pure returns (string memory) {
bytes memory valueB = bytes(value);
bytes memory tagB = bytes(tag);
uint vl = valueB.length;
uint tl = tagB.length;
bytes memory newB = new bytes(vl + tl + 2);
uint i;
uint j;
for (i = 0; i < tl; i++)
newB[j++] = tagB[i];
newB[j++] = ':';
newB[j++] = ' ';
for (i = 0; i < vl; i++)
newB[j++] = valueB[i];
return string(newB);
}
/*
Function: _tag(uint)
Add a tag to an uint.
Params:
value (uint) - The value.
tag (string) - The tag.
Returns:
result (string) - "tag: _utoa(value)"
*/
function _tag(uint value, string memory tag) internal pure returns (string memory) {
string memory nstr = _utoa(value, 10);
return _tag(nstr, tag);
}
/*
Function: _appendTagged(string)
Append a tagged value to a string.
Params:
tagged (string) - The tagged value.
str (string) - The string.
Returns:
result (string) - "str (tagged)"
*/
function _appendTagged(string memory tagged, string memory str) internal pure returns (string memory) {
bytes memory taggedB = bytes(tagged);
bytes memory strB = bytes(str);
uint sl = strB.length;
uint tl = taggedB.length;
bytes memory newB = new bytes(sl + tl + 3);
uint i;
uint j;
for (i = 0; i < sl; i++)
newB[j++] = strB[i];
newB[j++] = ' ';
newB[j++] = '(';
for (i = 0; i < tl; i++)
newB[j++] = taggedB[i];
newB[j++] = ')';
return string(newB);
}
/*
Function: _appendTagged(string, string)
Append two tagged values to a string.
Params:
tagged0 (string) - The first tagged value.
tagged1 (string) - The second tagged value.
str (string) - The string.
Returns:
result (string) - "str (tagged0, tagged1)"
*/
function _appendTagged(string memory tagged0, string memory tagged1, string memory str) internal pure returns (string memory) {
bytes memory tagged0B = bytes(tagged0);
bytes memory tagged1B = bytes(tagged1);
bytes memory strB = bytes(str);
uint sl = strB.length;
uint t0l = tagged0B.length;
uint t1l = tagged1B.length;
bytes memory newB = new bytes(sl + t0l + t1l + 5);
uint i;
uint j;
for (i = 0; i < sl; i++)
newB[j++] = strB[i];
newB[j++] = ' ';
newB[j++] = '(';
for (i = 0; i < t0l; i++)
newB[j++] = tagged0B[i];
newB[j++] = ',';
newB[j++] = ' ';
for (i = 0; i < t1l; i++)
newB[j++] = tagged1B[i];
newB[j++] = ')';
return string(newB);
}
}