-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion_test.c
314 lines (238 loc) · 7.93 KB
/
conversion_test.c
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "test_common.h"
#include "os.h"
#include "iota/iota_types.h"
// include the c-file to be able to test static functions
#include "iota/conversion.c"
#define NUM_RANDOM_TESTS 10000
static void test_s64_to_trits_zero(void **state)
{
UNUSED(state);
static const int64_t input_value = 0;
static const trit_t expected_trits[42] = {0};
trit_t trits_out[42];
bool result = s64_to_trits(input_value, trits_out, 42);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 42);
}
static void test_s64_to_trits_one(void **state)
{
UNUSED(state);
static const int64_t input_value = 6078832729528464400;
trit_t expected_trits[40];
memset(expected_trits, 1, 40);
trit_t trits_out[40];
bool result = s64_to_trits(input_value, trits_out, 40);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 40);
}
static void test_s64_to_trits_neg_one(void **state)
{
UNUSED(state);
static const int64_t input_value = -6078832729528464400;
trit_t expected_trits[40];
memset(expected_trits, -1, 40);
trit_t trits_out[40];
bool result = s64_to_trits(input_value, trits_out, 40);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 40);
}
static void test_s64_to_trits_overflow(void **state)
{
UNUSED(state);
static const int64_t input_value = 6078832729528464401;
trit_t trits_out[40];
bool result = s64_to_trits(input_value, trits_out, 40);
assert_true(result);
}
static void test_s64_to_trits_int_min(void **state)
{
UNUSED(state);
static const int64_t input_value = INT64_MIN;
static const trit_t expected_trits[41] = {
1, 0, 0, -1, 1, 0, 1, -1, -1, 0, 1, 0, 1, 0,
-1, 0, -1, 0, 1, -1, -1, 1, 1, -1, 0, -1, 1, 0,
0, -1, -1, -1, 0, 0, -1, -1, -1, 1, -1, 1, -1};
trit_t trits_out[41];
bool result = s64_to_trits(input_value, trits_out, 41);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 41);
}
static void test_s64_to_trits_iota_max(void **state)
{
UNUSED(state);
static const int64_t input_value = MAX_IOTA_VALUE;
trit_t expected_trits[33];
memset(expected_trits, 1, 33);
trit_t trits_out[33];
bool result = s64_to_trits(input_value, trits_out, 33);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 33);
}
static void test_s64_to_trits_iota_min(void **state)
{
UNUSED(state);
static const int64_t input_value = -MAX_IOTA_VALUE;
trit_t expected_trits[33];
memset(expected_trits, -1, 33);
trit_t trits_out[33];
bool result = s64_to_trits(input_value, trits_out, 33);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 33);
}
static void test_u32_to_trits_zero(void **state)
{
UNUSED(state);
static const uint32_t input_value = 0;
static const trit_t expected_trits[21] = {0};
trit_t trits_out[21];
bool result = u32_to_trits(input_value, trits_out, 21);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 21);
}
static void test_u32_to_trits_one(void **state)
{
UNUSED(state);
static const uint32_t input_value = 1743392200; // \sum_{i=0}^{19} 3^i
trit_t expected_trits[20];
memset(expected_trits, 1, 20);
trit_t trits_out[20];
bool result = u32_to_trits(input_value, trits_out, 20);
assert_false(result);
assert_memory_equal(trits_out, expected_trits, 20);
}
static void test_u32_to_trits_overflow(void **state)
{
UNUSED(state);
static const uint32_t input_value = 1743392201;
trit_t trits_out[20];
bool result = u32_to_trits(input_value, trits_out, 20);
assert_true(result);
}
static void random_bytes(unsigned char *bytes)
{
for (int i = 0; i < NUM_HASH_BYTES; i++) {
bytes[i] = rand() & 0xFF;
}
}
static void random_chars(char *chars)
{
for (int i = 0; i < NUM_HASH_TRYTES; i++) {
const int rn = rand() % 27;
if (rn == 26) {
chars[i] = '9';
}
else {
chars[i] = (char)('A' + rn);
}
}
}
static void assert_bytes_equal(unsigned char *actual, unsigned char *expected)
{
bytes_set_last_trit_zero(actual);
bytes_set_last_trit_zero(expected);
assert_memory_equal(actual, expected, NUM_HASH_BYTES);
}
static void test_random_bytes_via_chars(void **state)
{
UNUSED(state);
srand(2);
for (uint i = 0; i < NUM_RANDOM_TESTS; i++) {
unsigned char in_bytes[NUM_HASH_BYTES];
random_bytes(in_bytes);
char chars[82];
bytes_to_chars(in_bytes, chars, NUM_HASH_BYTES);
unsigned char out_bytes[NUM_HASH_BYTES];
chars_to_bytes(chars, out_bytes, NUM_HASH_TRYTES);
assert_bytes_equal(in_bytes, out_bytes);
}
}
static void assert_chars_equal(const char *actual, const char *expected)
{
trit_t actual_trits[NUM_HASH_TRITS];
chars_to_trits(actual, actual_trits, NUM_HASH_TRYTES);
trit_t expected_trits[NUM_HASH_TRITS];
chars_to_trits(expected, expected_trits, NUM_HASH_TRYTES);
// ignore 243th trit
assert_memory_equal(actual_trits, expected_trits, 242);
}
static void test_chars_via_bytes(const char *input)
{
unsigned char bytes[NUM_HASH_BYTES];
chars_to_bytes(input, bytes, NUM_HASH_TRYTES);
char chars[82];
bytes_to_chars(bytes, chars, NUM_HASH_BYTES);
assert_chars_equal(chars, input);
}
static void test_all_zero(void **state)
{
UNUSED(state);
static const char ZERO_CHARS[NUM_HASH_TRYTES] =
"9999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999";
static const unsigned char ZERO_BYTES[NUM_HASH_BYTES] = {0};
unsigned char bytes[NUM_HASH_BYTES];
chars_to_bytes(ZERO_CHARS, bytes, NUM_HASH_TRYTES);
assert_memory_equal(bytes, ZERO_BYTES, NUM_HASH_BYTES);
char chars[82];
bytes_to_chars(bytes, chars, NUM_HASH_BYTES);
assert_chars_equal(chars, ZERO_CHARS);
}
static void test_all_one(void **state)
{
UNUSED(state);
static const char ONE_CHARS[NUM_HASH_TRYTES] =
"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
"MMMMMMMMMMM";
test_chars_via_bytes(ONE_CHARS);
}
static void test_all_neg_one(void **state)
{
UNUSED(state);
static const char NEG_ONE_CHARS[NUM_HASH_TRYTES] =
"NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"
"NNNNNNNNNNN";
test_chars_via_bytes(NEG_ONE_CHARS);
}
static void test_overflow(void **state)
{
UNUSED(state);
// corresponds to 2^320 leading to additions with carry
static const char NEG_ONE_CHARS[NUM_HASH_TRYTES] =
"NNNNNNNNNNNNIPWAK9KOEYFFRZLJXRFLFLBRBFQATTA9TLIDNFNIEMCSPPUHKUGISALJSL"
"L9PSXBQXEPW";
test_chars_via_bytes(NEG_ONE_CHARS);
}
static void test_random_chars_via_bytes(void **state)
{
UNUSED(state);
srand(2);
for (uint i = 0; i < NUM_RANDOM_TESTS; i++) {
char in_chars[NUM_HASH_TRYTES];
random_chars(in_chars);
test_chars_via_bytes(in_chars);
}
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_s64_to_trits_zero),
cmocka_unit_test(test_s64_to_trits_one),
cmocka_unit_test(test_s64_to_trits_neg_one),
cmocka_unit_test(test_s64_to_trits_overflow),
cmocka_unit_test(test_s64_to_trits_int_min),
cmocka_unit_test(test_s64_to_trits_iota_max),
cmocka_unit_test(test_s64_to_trits_iota_min),
cmocka_unit_test(test_u32_to_trits_zero),
cmocka_unit_test(test_u32_to_trits_one),
cmocka_unit_test(test_u32_to_trits_overflow),
cmocka_unit_test(test_all_zero),
cmocka_unit_test(test_all_one),
cmocka_unit_test(test_all_neg_one),
cmocka_unit_test(test_overflow),
cmocka_unit_test(test_random_bytes_via_chars),
cmocka_unit_test(test_random_chars_via_bytes)};
return cmocka_run_group_tests(tests, NULL, NULL);
}