Merge pull request #268 from libtom/improve-coverage improve coverage
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
diff --git a/demo/test.c b/demo/test.c
index e7dd9ff..cd80252 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -29,9 +29,11 @@ static unsigned long ulabs(long x)
static int test_trivial_stuff(void)
{
mp_int a, b, c, d;
- if (mp_init_multi(&a, &b, &c, &d, NULL)!= MP_OKAY) {
+ mp_err e;
+ if ((e = mp_init_multi(&a, &b, &c, &d, NULL)) != MP_OKAY) {
return EXIT_FAILURE;
}
+ (void)mp_error_to_string(e);
/* a: 0->5 */
mp_set_int(&a, 5uL);
@@ -87,6 +89,10 @@ static int test_trivial_stuff(void)
mp_set_int(&c, 7uL);
mp_exptmod(&a, &b, &c, &d);
+ if (mp_iseven(&a) == mp_isodd(&a)) {
+ goto LBL_ERR;
+ }
+
mp_clear_multi(&a, &b, &c, &d, NULL);
return EXIT_SUCCESS;
LBL_ERR:
@@ -94,6 +100,37 @@ LBL_ERR:
return EXIT_FAILURE;
}
+static int test_mp_fread_fwrite(void)
+{
+ mp_int a, b;
+ mp_err e;
+ FILE *tmp = NULL;
+ if ((e = mp_init_multi(&a, &b, NULL)) != MP_OKAY) {
+ return EXIT_FAILURE;
+ }
+
+ mp_set_int(&a, 123456uL);
+ tmp = tmpfile();
+ if ((e = mp_fwrite(&a, 64, tmp)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ rewind(tmp);
+ if ((e = mp_fread(&b, 64, tmp)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ if (mp_get_int(&b) != 123456uL) {
+ goto LBL_ERR;
+ }
+ fclose(tmp);
+
+ mp_clear_multi(&a, &b, NULL);
+ return EXIT_SUCCESS;
+LBL_ERR:
+ if (tmp != NULL) fclose(tmp);
+ mp_clear_multi(&a, &b, NULL);
+ return EXIT_FAILURE;
+}
+
static mp_err very_random_source(void *out, size_t size)
{
memset(out, 0xff, size);
@@ -841,7 +878,7 @@ static int test_mp_prime_is_prime(void)
{
int ix;
mp_err err;
- mp_bool cnt;
+ mp_bool cnt, fu;
mp_int a, b;
if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
@@ -902,6 +939,10 @@ static int test_mp_prime_is_prime(void)
if (cnt == MP_NO) {
printf("\nsub is not prime!\n");
}
+ mp_prime_frobenius_underwood(&b, &fu);
+ if (fu == MP_NO) {
+ printf("\nfrobenius-underwood says sub is not prime!\n");
+ }
if ((err != MP_OKAY) || (cnt == MP_NO)) {
printf("prime tested was: ");
mp_fwrite(&a,16,stdout);
@@ -1823,7 +1864,7 @@ static int test_s_mp_balance_mul(void)
goto LTM_ERR;
}
- if ((e = mp_mul(&a, &b, &c)) != MP_OKAY) {
+ if ((e = s_mp_balance_mul(&a, &b, &c)) != MP_OKAY) {
goto LTM_ERR;
}
@@ -1855,6 +1896,7 @@ int unit_tests(int argc, char **argv)
T(mp_decr),
T(mp_div_3),
T(mp_dr_reduce),
+ T(mp_fread_fwrite),
T(mp_get_int),
T(mp_get_long),
T(mp_get_long_long),