mtest: I like predictable random for testing
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
diff --git a/mtest/mtest.c b/mtest/mtest.c
index a520643..0165acf 100644
--- a/mtest/mtest.c
+++ b/mtest/mtest.c
@@ -39,7 +39,12 @@ mulmod
#include <time.h>
#include "mpi.c"
+#ifdef LTM_MTEST_REAL_RAND
+#define getRandChar() fgetc(rng)
FILE *rng;
+#else
+#define getRandChar() (rand()&0xFF)
+#endif
void rand_num(mp_int *a)
{
@@ -47,13 +52,21 @@ void rand_num(mp_int *a)
unsigned char buf[2048];
size_t sz;
- size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
- buf[0] = (fgetc(rng)&1)?1:0;
+ size = 1 + ((getRandChar()<<8) + getRandChar()) % 101;
+ buf[0] = (getRandChar()&1)?1:0;
+#ifdef LTM_MTEST_REAL_RAND
sz = fread(buf+1, 1, size, rng);
+#else
+ sz = 1;
+ while (sz < (unsigned)size) {
+ buf[sz] = getRandChar();
+ ++sz;
+ }
+#endif
if (sz != (unsigned)size) {
fprintf(stderr, "\nWarning: fread failed\n\n");
}
- while (buf[1] == 0) buf[1] = fgetc(rng);
+ while (buf[1] == 0) buf[1] = getRandChar();
mp_read_raw(a, buf, 1+size);
}
@@ -63,13 +76,21 @@ void rand_num2(mp_int *a)
unsigned char buf[2048];
size_t sz;
- size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
- buf[0] = (fgetc(rng)&1)?1:0;
+ size = 10 + ((getRandChar()<<8) + getRandChar()) % 101;
+ buf[0] = (getRandChar()&1)?1:0;
+#ifdef LTM_MTEST_REAL_RAND
sz = fread(buf+1, 1, size, rng);
+#else
+ sz = 1;
+ while (sz < (unsigned)size) {
+ buf[sz] = getRandChar();
+ ++sz;
+ }
+#endif
if (sz != (unsigned)size) {
fprintf(stderr, "\nWarning: fread failed\n\n");
}
- while (buf[1] == 0) buf[1] = fgetc(rng);
+ while (buf[1] == 0) buf[1] = getRandChar();
mp_read_raw(a, buf, 1+size);
}
@@ -79,7 +100,9 @@ int main(void)
{
int n, tmp;
mp_int a, b, c, d, e;
+#ifdef MTEST_NO_FULLSPEED
clock_t t1;
+#endif
char buf[4096];
mp_init(&a);
@@ -106,6 +129,7 @@ int main(void)
}
*/
+#ifdef LTM_MTEST_REAL_RAND
rng = fopen("/dev/urandom", "rb");
if (rng == NULL) {
rng = fopen("/dev/random", "rb");
@@ -114,16 +138,21 @@ int main(void)
rng = stdin;
}
}
+#else
+ srand(23);
+#endif
+#ifdef MTEST_NO_FULLSPEED
t1 = clock();
+#endif
for (;;) {
-#if 0
+#ifdef MTEST_NO_FULLSPEED
if (clock() - t1 > CLOCKS_PER_SEC) {
sleep(2);
t1 = clock();
}
#endif
- n = fgetc(rng) % 15;
+ n = getRandChar() % 15;
if (n == 0) {
/* add tests */
@@ -188,7 +217,7 @@ int main(void)
/* mul_2d test */
rand_num(&a);
mp_copy(&a, &b);
- n = fgetc(rng) & 63;
+ n = getRandChar() & 63;
mp_mul_2d(&b, n, &b);
mp_to64(&a, buf);
printf("mul2d\n");
@@ -200,7 +229,7 @@ int main(void)
/* div_2d test */
rand_num(&a);
mp_copy(&a, &b);
- n = fgetc(rng) & 63;
+ n = getRandChar() & 63;
mp_div_2d(&b, n, &b, NULL);
mp_to64(&a, buf);
printf("div2d\n");
@@ -307,7 +336,9 @@ int main(void)
printf("%s\n", buf);
}
}
+#ifdef LTM_MTEST_REAL_RAND
fclose(rng);
+#endif
return 0;
}