Skip to content

Commit 57ccd9f

Browse files
authored
Merge pull request #431 from libtom/rework-tests
clean-up test.c
2 parents 8bf82f9 + 4500d06 commit 57ccd9f

File tree

5 files changed

+439
-726
lines changed

5 files changed

+439
-726
lines changed

demo/mtest_opponent.c

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,27 @@ static int mtest_opponent(void)
4646
switch (abs(rand()) % 7) {
4747
case 0:
4848
mp_clear(&a);
49-
mp_init(&a);
49+
DO(mp_init(&a));
5050
break;
5151
case 1:
5252
mp_clear(&b);
53-
mp_init(&b);
53+
DO(mp_init(&b));
5454
break;
5555
case 2:
5656
mp_clear(&c);
57-
mp_init(&c);
57+
DO(mp_init(&c));
5858
break;
5959
case 3:
6060
mp_clear(&d);
61-
mp_init(&d);
61+
DO(mp_init(&d));
6262
break;
6363
case 4:
6464
mp_clear(&e);
65-
mp_init(&e);
65+
DO(mp_init(&e));
6666
break;
6767
case 5:
6868
mp_clear(&f);
69-
mp_init(&f);
69+
DO(mp_init(&f));
7070
break;
7171
case 6:
7272
break; /* don't clear any */
@@ -83,13 +83,13 @@ static int mtest_opponent(void)
8383
if (strcmp(cmd, "mul2d") == 0) {
8484
++mul2d_n;
8585
FGETS(buf, 4095, stdin);
86-
mp_read_radix(&a, buf, 64);
86+
DO(mp_read_radix(&a, buf, 64));
8787
FGETS(buf, 4095, stdin);
8888
sscanf(buf, "%u", &rr);
8989
FGETS(buf, 4095, stdin);
90-
mp_read_radix(&b, buf, 64);
90+
DO(mp_read_radix(&b, buf, 64));
9191

92-
mp_mul_2d(&a, (int)rr, &a);
92+
DO(mp_mul_2d(&a, (int)rr, &a));
9393
a.sign = b.sign;
9494
if (mp_cmp(&a, &b) != MP_EQ) {
9595
printf("mul2d failed, rr == %u\n", rr);
@@ -100,13 +100,13 @@ static int mtest_opponent(void)
100100
} else if (strcmp(cmd, "div2d") == 0) {
101101
++div2d_n;
102102
FGETS(buf, 4095, stdin);
103-
mp_read_radix(&a, buf, 64);
103+
DO(mp_read_radix(&a, buf, 64));
104104
FGETS(buf, 4095, stdin);
105105
sscanf(buf, "%u", &rr);
106106
FGETS(buf, 4095, stdin);
107-
mp_read_radix(&b, buf, 64);
107+
DO(mp_read_radix(&b, buf, 64));
108108

109-
mp_div_2d(&a, (int)rr, &a, &e);
109+
DO(mp_div_2d(&a, (int)rr, &a, &e));
110110
a.sign = b.sign;
111111
if ((a.used == b.used) && (a.used == 0)) {
112112
a.sign = b.sign = MP_ZPOS;
@@ -120,13 +120,13 @@ static int mtest_opponent(void)
120120
} else if (strcmp(cmd, "add") == 0) {
121121
++add_n;
122122
FGETS(buf, 4095, stdin);
123-
mp_read_radix(&a, buf, 64);
123+
DO(mp_read_radix(&a, buf, 64));
124124
FGETS(buf, 4095, stdin);
125-
mp_read_radix(&b, buf, 64);
125+
DO(mp_read_radix(&b, buf, 64));
126126
FGETS(buf, 4095, stdin);
127-
mp_read_radix(&c, buf, 64);
128-
mp_copy(&a, &d);
129-
mp_add(&d, &b, &d);
127+
DO(mp_read_radix(&c, buf, 64));
128+
DO(mp_copy(&a, &d));
129+
DO(mp_add(&d, &b, &d));
130130
if (mp_cmp(&c, &d) != MP_EQ) {
131131
printf("add %lu failure!\n", add_n);
132132
draw(&a);
@@ -139,9 +139,9 @@ static int mtest_opponent(void)
139139
/* test the sign/unsigned storage functions */
140140

141141
rr = (unsigned)mp_sbin_size(&c);
142-
mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
142+
DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
143143
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
144-
mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr);
144+
DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
145145
if (mp_cmp(&c, &d) != MP_EQ) {
146146
printf("mp_signed_bin failure!\n");
147147
draw(&c);
@@ -150,9 +150,9 @@ static int mtest_opponent(void)
150150
}
151151

152152
rr = (unsigned)mp_ubin_size(&c);
153-
mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
153+
DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
154154
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
155-
mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr);
155+
DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
156156
if (mp_cmp_mag(&c, &d) != MP_EQ) {
157157
printf("mp_unsigned_bin failure!\n");
158158
draw(&c);
@@ -163,13 +163,13 @@ static int mtest_opponent(void)
163163
} else if (strcmp(cmd, "sub") == 0) {
164164
++sub_n;
165165
FGETS(buf, 4095, stdin);
166-
mp_read_radix(&a, buf, 64);
166+
DO(mp_read_radix(&a, buf, 64));
167167
FGETS(buf, 4095, stdin);
168-
mp_read_radix(&b, buf, 64);
168+
DO(mp_read_radix(&b, buf, 64));
169169
FGETS(buf, 4095, stdin);
170-
mp_read_radix(&c, buf, 64);
171-
mp_copy(&a, &d);
172-
mp_sub(&d, &b, &d);
170+
DO(mp_read_radix(&c, buf, 64));
171+
DO(mp_copy(&a, &d));
172+
DO(mp_sub(&d, &b, &d));
173173
if (mp_cmp(&c, &d) != MP_EQ) {
174174
printf("sub %lu failure!\n", sub_n);
175175
draw(&a);
@@ -181,13 +181,13 @@ static int mtest_opponent(void)
181181
} else if (strcmp(cmd, "mul") == 0) {
182182
++mul_n;
183183
FGETS(buf, 4095, stdin);
184-
mp_read_radix(&a, buf, 64);
184+
DO(mp_read_radix(&a, buf, 64));
185185
FGETS(buf, 4095, stdin);
186-
mp_read_radix(&b, buf, 64);
186+
DO(mp_read_radix(&b, buf, 64));
187187
FGETS(buf, 4095, stdin);
188-
mp_read_radix(&c, buf, 64);
189-
mp_copy(&a, &d);
190-
mp_mul(&d, &b, &d);
188+
DO(mp_read_radix(&c, buf, 64));
189+
DO(mp_copy(&a, &d));
190+
DO(mp_mul(&d, &b, &d));
191191
if (mp_cmp(&c, &d) != MP_EQ) {
192192
printf("mul %lu failure!\n", mul_n);
193193
draw(&a);
@@ -199,15 +199,15 @@ static int mtest_opponent(void)
199199
} else if (strcmp(cmd, "div") == 0) {
200200
++div_n;
201201
FGETS(buf, 4095, stdin);
202-
mp_read_radix(&a, buf, 64);
202+
DO(mp_read_radix(&a, buf, 64));
203203
FGETS(buf, 4095, stdin);
204-
mp_read_radix(&b, buf, 64);
204+
DO(mp_read_radix(&b, buf, 64));
205205
FGETS(buf, 4095, stdin);
206-
mp_read_radix(&c, buf, 64);
206+
DO(mp_read_radix(&c, buf, 64));
207207
FGETS(buf, 4095, stdin);
208-
mp_read_radix(&d, buf, 64);
208+
DO(mp_read_radix(&d, buf, 64));
209209

210-
mp_div(&a, &b, &e, &f);
210+
DO(mp_div(&a, &b, &e, &f));
211211
if ((mp_cmp(&c, &e) != MP_EQ) || (mp_cmp(&d, &f) != MP_EQ)) {
212212
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e),
213213
mp_cmp(&d, &f));
@@ -223,11 +223,11 @@ static int mtest_opponent(void)
223223
} else if (strcmp(cmd, "sqr") == 0) {
224224
++sqr_n;
225225
FGETS(buf, 4095, stdin);
226-
mp_read_radix(&a, buf, 64);
226+
DO(mp_read_radix(&a, buf, 64));
227227
FGETS(buf, 4095, stdin);
228-
mp_read_radix(&b, buf, 64);
229-
mp_copy(&a, &c);
230-
mp_sqr(&c, &c);
228+
DO(mp_read_radix(&b, buf, 64));
229+
DO(mp_copy(&a, &c));
230+
DO(mp_sqr(&c, &c));
231231
if (mp_cmp(&b, &c) != MP_EQ) {
232232
printf("sqr %lu failure!\n", sqr_n);
233233
draw(&a);
@@ -238,13 +238,13 @@ static int mtest_opponent(void)
238238
} else if (strcmp(cmd, "gcd") == 0) {
239239
++gcd_n;
240240
FGETS(buf, 4095, stdin);
241-
mp_read_radix(&a, buf, 64);
241+
DO(mp_read_radix(&a, buf, 64));
242242
FGETS(buf, 4095, stdin);
243-
mp_read_radix(&b, buf, 64);
243+
DO(mp_read_radix(&b, buf, 64));
244244
FGETS(buf, 4095, stdin);
245-
mp_read_radix(&c, buf, 64);
246-
mp_copy(&a, &d);
247-
mp_gcd(&d, &b, &d);
245+
DO(mp_read_radix(&c, buf, 64));
246+
DO(mp_copy(&a, &d));
247+
DO(mp_gcd(&d, &b, &d));
248248
d.sign = c.sign;
249249
if (mp_cmp(&c, &d) != MP_EQ) {
250250
printf("gcd %lu failure!\n", gcd_n);
@@ -257,13 +257,13 @@ static int mtest_opponent(void)
257257
} else if (strcmp(cmd, "lcm") == 0) {
258258
++lcm_n;
259259
FGETS(buf, 4095, stdin);
260-
mp_read_radix(&a, buf, 64);
260+
DO(mp_read_radix(&a, buf, 64));
261261
FGETS(buf, 4095, stdin);
262-
mp_read_radix(&b, buf, 64);
262+
DO(mp_read_radix(&b, buf, 64));
263263
FGETS(buf, 4095, stdin);
264-
mp_read_radix(&c, buf, 64);
265-
mp_copy(&a, &d);
266-
mp_lcm(&d, &b, &d);
264+
DO(mp_read_radix(&c, buf, 64));
265+
DO(mp_copy(&a, &d));
266+
DO(mp_lcm(&d, &b, &d));
267267
d.sign = c.sign;
268268
if (mp_cmp(&c, &d) != MP_EQ) {
269269
printf("lcm %lu failure!\n", lcm_n);
@@ -276,15 +276,15 @@ static int mtest_opponent(void)
276276
} else if (strcmp(cmd, "expt") == 0) {
277277
++expt_n;
278278
FGETS(buf, 4095, stdin);
279-
mp_read_radix(&a, buf, 64);
279+
DO(mp_read_radix(&a, buf, 64));
280280
FGETS(buf, 4095, stdin);
281-
mp_read_radix(&b, buf, 64);
281+
DO(mp_read_radix(&b, buf, 64));
282282
FGETS(buf, 4095, stdin);
283-
mp_read_radix(&c, buf, 64);
283+
DO(mp_read_radix(&c, buf, 64));
284284
FGETS(buf, 4095, stdin);
285-
mp_read_radix(&d, buf, 64);
286-
mp_copy(&a, &e);
287-
mp_exptmod(&e, &b, &c, &e);
285+
DO(mp_read_radix(&d, buf, 64));
286+
DO(mp_copy(&a, &e));
287+
DO(mp_exptmod(&e, &b, &c, &e));
288288
if (mp_cmp(&d, &e) != MP_EQ) {
289289
printf("expt %lu failure!\n", expt_n);
290290
draw(&a);
@@ -297,32 +297,32 @@ static int mtest_opponent(void)
297297
} else if (strcmp(cmd, "invmod") == 0) {
298298
++inv_n;
299299
FGETS(buf, 4095, stdin);
300-
mp_read_radix(&a, buf, 64);
300+
DO(mp_read_radix(&a, buf, 64));
301301
FGETS(buf, 4095, stdin);
302-
mp_read_radix(&b, buf, 64);
302+
DO(mp_read_radix(&b, buf, 64));
303303
FGETS(buf, 4095, stdin);
304-
mp_read_radix(&c, buf, 64);
305-
mp_invmod(&a, &b, &d);
306-
mp_mulmod(&d, &a, &b, &e);
304+
DO(mp_read_radix(&c, buf, 64));
305+
DO(mp_invmod(&a, &b, &d));
306+
DO(mp_mulmod(&d, &a, &b, &e));
307307
if (mp_cmp_d(&e, 1uL) != MP_EQ) {
308308
printf("inv [wrong value from MPI?!] failure\n");
309309
draw(&a);
310310
draw(&b);
311311
draw(&c);
312312
draw(&d);
313313
draw(&e);
314-
mp_gcd(&a, &b, &e);
314+
DO(mp_gcd(&a, &b, &e));
315315
draw(&e);
316316
goto LBL_ERR;
317317
}
318318

319319
} else if (strcmp(cmd, "div2") == 0) {
320320
++div2_n;
321321
FGETS(buf, 4095, stdin);
322-
mp_read_radix(&a, buf, 64);
322+
DO(mp_read_radix(&a, buf, 64));
323323
FGETS(buf, 4095, stdin);
324-
mp_read_radix(&b, buf, 64);
325-
mp_div_2(&a, &c);
324+
DO(mp_read_radix(&b, buf, 64));
325+
DO(mp_div_2(&a, &c));
326326
if (mp_cmp(&c, &b) != MP_EQ) {
327327
printf("div_2 %lu failure\n", div2_n);
328328
draw(&a);
@@ -333,10 +333,10 @@ static int mtest_opponent(void)
333333
} else if (strcmp(cmd, "mul2") == 0) {
334334
++mul2_n;
335335
FGETS(buf, 4095, stdin);
336-
mp_read_radix(&a, buf, 64);
336+
DO(mp_read_radix(&a, buf, 64));
337337
FGETS(buf, 4095, stdin);
338-
mp_read_radix(&b, buf, 64);
339-
mp_mul_2(&a, &c);
338+
DO(mp_read_radix(&b, buf, 64));
339+
DO(mp_mul_2(&a, &c));
340340
if (mp_cmp(&c, &b) != MP_EQ) {
341341
printf("mul_2 %lu failure\n", mul2_n);
342342
draw(&a);
@@ -347,12 +347,12 @@ static int mtest_opponent(void)
347347
} else if (strcmp(cmd, "add_d") == 0) {
348348
++add_d_n;
349349
FGETS(buf, 4095, stdin);
350-
mp_read_radix(&a, buf, 64);
350+
DO(mp_read_radix(&a, buf, 64));
351351
FGETS(buf, 4095, stdin);
352352
sscanf(buf, "%d", &ix);
353353
FGETS(buf, 4095, stdin);
354-
mp_read_radix(&b, buf, 64);
355-
mp_add_d(&a, (mp_digit)ix, &c);
354+
DO(mp_read_radix(&b, buf, 64));
355+
DO(mp_add_d(&a, (mp_digit)ix, &c));
356356
if (mp_cmp(&b, &c) != MP_EQ) {
357357
printf("add_d %lu failure\n", add_d_n);
358358
draw(&a);
@@ -364,12 +364,12 @@ static int mtest_opponent(void)
364364
} else if (strcmp(cmd, "sub_d") == 0) {
365365
++sub_d_n;
366366
FGETS(buf, 4095, stdin);
367-
mp_read_radix(&a, buf, 64);
367+
DO(mp_read_radix(&a, buf, 64));
368368
FGETS(buf, 4095, stdin);
369369
sscanf(buf, "%d", &ix);
370370
FGETS(buf, 4095, stdin);
371-
mp_read_radix(&b, buf, 64);
372-
mp_sub_d(&a, (mp_digit)ix, &c);
371+
DO(mp_read_radix(&b, buf, 64));
372+
DO(mp_sub_d(&a, (mp_digit)ix, &c));
373373
if (mp_cmp(&b, &c) != MP_EQ) {
374374
printf("sub_d %lu failure\n", sub_d_n);
375375
draw(&a);

demo/shared.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ void ndraw(const mp_int *a, const char *name)
44
{
55
char *buf;
66
size_t size;
7+
mp_err err;
78

8-
mp_radix_size(a, 10, &size);
9+
if ((err = mp_radix_size(a, 10, &size)) != MP_OKAY) {
10+
fprintf(stderr, "\nndraw: mp_radix_size(a, 10, %zu) failed - %s\n", size, mp_error_to_string(err));
11+
exit(EXIT_FAILURE);
12+
}
913
buf = (char *)malloc(size);
1014
if (buf == NULL) {
1115
fprintf(stderr, "\nndraw: malloc(%zu) failed\n", size);
1216
exit(EXIT_FAILURE);
1317
}
1418

1519
printf("%s: ", name);
16-
mp_to_decimal(a, buf, size);
20+
if ((err = mp_to_decimal(a, buf, size)) != MP_OKAY) {
21+
fprintf(stderr, "\nndraw: mp_to_decimal(a, buf, %zu) failed - %s\n", size, mp_error_to_string(err));
22+
exit(EXIT_FAILURE);
23+
}
1724
printf("%s\n", buf);
18-
mp_to_hex(a, buf, size);
25+
if ((err = mp_to_hex(a, buf, size)) != MP_OKAY) {
26+
fprintf(stderr, "\nndraw: mp_to_hex(a, buf, %zu) failed - %s\n", size, mp_error_to_string(err));
27+
exit(EXIT_FAILURE);
28+
}
1929
printf("0x%s\n", buf);
2030

2131
free(buf);

demo/shared.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
#define LTM_DEMO_TEST_REDUCE_2K_L 0
1515
#endif
1616

17-
#define MP_WUR /* TODO: result checks disabled for now */
1817
#include "tommath_private.h"
1918

19+
20+
#define EXPECT(a) do { if (!(a)) { fprintf(stderr, "%d: EXPECT(%s) failed\n", __LINE__, #a); goto LBL_ERR; } } while(0)
21+
#define DO_WHAT(a, what) do { mp_err err; if ((err = (a)) != MP_OKAY) { fprintf(stderr, "%d: DO(%s) failed: %s\n", __LINE__, #a, mp_error_to_string(err)); what; } } while(0)
22+
#define DO(a) DO_WHAT(a, goto LBL_ERR)
23+
#define DOR(a) DO_WHAT(a, return EXIT_FAILURE)
24+
25+
2026
extern void ndraw(const mp_int* a, const char* name);
2127
extern void print_header(void);

0 commit comments

Comments
 (0)