Add t_is_zero predicates.

This commit is contained in:
Dag-Erling Smørgrav 2021-10-21 14:03:11 +02:00
parent 7885f54152
commit d014682206
10 changed files with 65 additions and 42 deletions

View file

@ -103,7 +103,10 @@ int t_is_not_null(const void *);
int t_compare_mem(const void *, const void *, size_t);
int t_compare_str(const char *, const char *);
int t_compare_strn(const char *, const char *, size_t);
#define t_compare_num(n, t) int t_compare_##n(t, t);
#define t_compare_num(n, t) \
int t_compare_##n(t, t); \
int t_is_zero_##n(t); \
int t_is_not_zero_##n(t);
t_compare_num(i, int);
t_compare_num(u, unsigned int);
t_compare_num(il, long);

View file

@ -185,6 +185,26 @@ t_compare_##n(t expected, t received) \
return (0); \
} \
return (1); \
} \
int \
t_is_zero_##n(t received) \
{ \
\
if (received != 0) { \
t_printv("expected zero, received " pf "\n", received); \
return (0); \
} \
return (1); \
} \
int \
t_is_not_zero_##n(t received) \
{ \
\
if (received == 0) { \
t_printv("expected non-zero, received zero\n"); \
return (0); \
} \
return (1); \
}
t_compare_num(i, int, "%d");

View file

@ -53,7 +53,7 @@ t_ffs(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
ret = t_is_zero_i(cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("ffs(0x%08x) == %d\n", u, cryb_ffs(u));
ret &= t_compare_i(n, cryb_ffs(u));
@ -67,7 +67,7 @@ t_ffsl(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
long int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
ret = t_is_zero_i(cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++)
ret &= t_compare_i(n, cryb_ffsl(u));
return (ret);
@ -79,7 +79,7 @@ t_ffsll(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
long long int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
ret = t_is_zero_i(cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++)
ret &= t_compare_i(n, cryb_ffsll(u));
return (ret);
@ -91,7 +91,7 @@ t_fls(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
unsigned int u;
int n, ret;
ret = t_compare_i(0, cryb_fls(0));
ret = t_is_zero_i(cryb_fls(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("fls(0x%08x) == %d\n", u, cryb_fls(u));
ret &= t_compare_i(n, cryb_fls(u));
@ -105,7 +105,7 @@ t_flsl(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
unsigned long int u;
int n, ret;
ret = t_compare_i(0, cryb_flsl(0));
ret = t_is_zero_i(cryb_flsl(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("flsl(0x%08lx) == %d\n", u, cryb_flsl(u));
ret &= t_compare_i(n, cryb_flsl(u));
@ -119,7 +119,7 @@ t_flsll(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
unsigned long long int u;
int n, ret;
ret = t_compare_i(0, cryb_flsll(0));
ret = t_is_zero_i(cryb_flsll(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("flsll(0x%016llx) == %d\n", u, cryb_flsll(u));
ret &= t_compare_i(n, cryb_flsll(u));

View file

@ -62,7 +62,7 @@ t_malloc_fill(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
head = *next;
free(next);
}
ret &= t_compare_u(0, i);
ret &= t_is_zero_u(i);
t_malloc_fatal = stmf;
return (ret);
}

View file

@ -102,7 +102,7 @@ t_mpi_grow_ok(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&x, 1);
t_assert(x.words[0] == 1 && x.msb == 1);
ret &= t_compare_i(0, mpi_grow(&x, CRYB_MPI_SWORDS * 32 + 1));
ret &= t_is_zero_i(mpi_grow(&x, CRYB_MPI_SWORDS * 32 + 1));
t_assert(x.words[0] == 1 && x.msb == 1);
ret &= t_mpi_grown(&x);
mpi_destroy(&x);
@ -136,10 +136,10 @@ t_mpi_grow_twice(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
uint32_t *p;
int ret = 1;
ret &= t_compare_i(0, mpi_grow(&x, CRYB_MPI_SWORDS * 32 + 1));
ret &= t_is_zero_i(mpi_grow(&x, CRYB_MPI_SWORDS * 32 + 1));
ret &= t_mpi_grown(&x);
p = x.words;
ret &= t_compare_i(0, mpi_grow(&x, CRYB_MPI_SWORDS * 32 * 2 + 1));
ret &= t_is_zero_i(mpi_grow(&x, CRYB_MPI_SWORDS * 32 * 2 + 1));
/* XXX we need inequality predicates */
if (x.words == p) {
t_printv("pointer was expected to change\n");
@ -360,7 +360,7 @@ t_mpi_set_positive(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
ret &= t_mpi_not_grown(&x);
ret &= t_compare_x32(0x19700101, x.words[0]);
ret &= t_compare_u(29, x.msb);
ret &= t_compare_i(0, x.neg);
ret &= t_is_zero_i(x.neg);
mpi_destroy(&x);
return (ret);
}
@ -416,7 +416,7 @@ t_mpi_negate_nonzero(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
ret &= t_mpi_not_grown(&x);
ret &= t_compare_x32(0x19700101, x.words[0]);
ret &= t_compare_u(29, x.msb);
ret &= t_compare_u(0, x.neg);
ret &= t_is_zero_u(x.neg);
mpi_negate(&x);
ret &= t_mpi_not_grown(&x);
ret &= t_compare_x32(0x19700101, x.words[0]);
@ -475,7 +475,7 @@ t_mpi_copy_zero(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
int ret = 1;
t_assert(x.words == NULL && y.words == NULL);
ret &= t_compare_i(0, mpi_copy(&y, &x));
ret &= t_is_zero_i(mpi_copy(&y, &x));
ret &= t_compare_mem(&z, &x, sizeof x);
ret &= t_mpi_not_grown(&y);
ret &= t_mpi_is_zero(&y);
@ -675,7 +675,7 @@ t_mpi_lsh(char **desc CRYB_UNUSED, void *arg)
mpi_load(&x, tc->v, tc->vlen);
mpi_load(&e, tc->e, tc->elen);
ret &= t_compare_i(0, mpi_lshift(&x, tc->n));
ret &= t_is_zero_i(mpi_lshift(&x, tc->n));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&x);
mpi_destroy(&e);
@ -694,7 +694,7 @@ t_mpi_large_lsh(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_load(&x, small_v, sizeof small_v);
t_assert(t_mpi_not_grown(&x));
ret &= t_compare_i(0, mpi_lshift(&x, 32));
ret &= t_is_zero_i(mpi_lshift(&x, 32));
/* XXX we need inequality predicates */
if (x.words == x.swords) {
t_printv("reallocation failed to occur\n");

View file

@ -121,8 +121,8 @@ t_mpi_is_zero(const cryb_mpi *x)
ret &= t_mpi_not_grown(x);
ret &= t_compare_mem(t_zero, x->words, CRYB_MPI_SWORDS);
ret &= t_compare_u(0, x->msb);
ret &= t_compare_i(0, x->neg);
ret &= t_is_zero_u(x->msb);
ret &= t_is_zero_i(x->neg);
return (ret);
}

View file

@ -153,7 +153,7 @@ t_mpi_add_tc(char **desc CRYB_UNUSED, void *arg)
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_add_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
@ -175,7 +175,7 @@ t_mpi_add(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
@ -246,7 +246,7 @@ t_mpi_add_b_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
@ -288,7 +288,7 @@ t_mpi_add_b_to_a_equal(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
@ -308,7 +308,7 @@ t_mpi_add_a_to_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
@ -350,7 +350,7 @@ t_mpi_add_a_to_b_equal(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
@ -369,7 +369,7 @@ t_mpi_add_a_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &a));
ret &= t_is_zero_i(mpi_add_abs(&a, &a, &a));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
@ -407,7 +407,7 @@ t_mpi_add_a_and_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &a));
ret &= t_is_zero_i(mpi_add_abs(&b, &a, &a));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
@ -448,7 +448,7 @@ t_mpi_add_zero_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_zero(&b);
mpi_copy(&e, &a);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
@ -468,7 +468,7 @@ t_mpi_add_zero_to_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_zero(&a);
mpi_set(&b, 0x20140901);
mpi_copy(&e, &b);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
@ -627,7 +627,7 @@ t_mpi_sub_tc(char **desc CRYB_UNUSED, void *arg)
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
@ -649,7 +649,7 @@ t_mpi_sub(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
@ -696,7 +696,7 @@ t_mpi_sub_b_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x20140901);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
@ -738,7 +738,7 @@ t_mpi_sub_a_from_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
@ -779,7 +779,7 @@ t_mpi_sub_a_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x19700101);
mpi_set(&e, 0);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &a));
ret &= t_is_zero_i(mpi_sub_abs(&a, &a, &a));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
@ -797,7 +797,7 @@ t_mpi_sub_zero_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x20140901);
mpi_set(&e, 0x20140901);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
@ -815,7 +815,7 @@ t_mpi_sub_b_from_zero(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901);
ret &= t_compare_i(0, mpi_sub_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&b);
mpi_destroy(&e);
@ -836,7 +836,7 @@ t_mpi_sub_neg_target(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x20140901 - 0x19700101);
mpi_set(&x, -1);
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);

View file

@ -154,7 +154,7 @@ t_mpi_cmp_ident(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
int ret = 1;
mpi_set(&a, CRYB_TO);
ret &= t_compare_i(0, mpi_cmp(&a, &a));
ret &= t_is_zero_i(mpi_cmp(&a, &a));
mpi_destroy(&a);
return (ret);
}

View file

@ -161,7 +161,7 @@ t_mpi_gcd_tc(char **desc CRYB_UNUSED, void *arg)
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_gcd_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_gcd_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
@ -177,7 +177,7 @@ t_mpi_gcd_ident_ab(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
int ret = 1;
mpi_set(&a, 0x20140901);
ret &= t_compare_i(0, mpi_gcd_abs(&x, &a, &a));
ret &= t_is_zero_i(mpi_gcd_abs(&x, &a, &a));
ret &= t_compare_mpi_u32(0x20140901, &x);
mpi_destroy(&a);
mpi_destroy(&x);
@ -192,7 +192,7 @@ t_mpi_gcd_ident_xa(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x20140901);
mpi_set(&b, 0x20140901);
ret &= t_compare_i(0, mpi_gcd_abs(&a, &a, &b));
ret &= t_is_zero_i(mpi_gcd_abs(&a, &a, &b));
ret &= t_compare_mpi_u32(0x20140901, &a);
mpi_destroy(&a);
mpi_destroy(&b);
@ -207,7 +207,7 @@ t_mpi_gcd_ident_xb(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_set(&a, 0x20140901);
mpi_set(&b, 0x20140901);
ret &= t_compare_i(0, mpi_gcd_abs(&b, &a, &b));
ret &= t_is_zero_i(mpi_gcd_abs(&b, &a, &b));
ret &= t_compare_mpi_u32(0x20140901, &b);
mpi_destroy(&a);
mpi_destroy(&b);

View file

@ -122,7 +122,7 @@ t_mpi_mul_tc(char **desc CRYB_UNUSED, void *arg)
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_mul_abs(&x, &a, &b));
ret &= t_is_zero_i(mpi_mul_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);