mirror of
https://github.com/cryb-to/cryb-to.git
synced 2025-01-24 12:41:11 +00:00
Add tests for the off-by-one bug we just fixed in mpi_cmp_abs().
This commit is contained in:
parent
1c024928f9
commit
2027ce33e7
1 changed files with 39 additions and 15 deletions
54
t/t_mpi.c
54
t/t_mpi.c
|
@ -686,20 +686,22 @@ t_mpi_large_load_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
|
||||||
|
|
||||||
static struct t_cmp_case {
|
static struct t_cmp_case {
|
||||||
const char *desc;
|
const char *desc;
|
||||||
uint8_t a[16];
|
int a, b, cmp;
|
||||||
size_t alen;
|
|
||||||
uint8_t b[16];
|
|
||||||
size_t blen;
|
|
||||||
int cmpabs, cmp;
|
|
||||||
} t_cmp_cases[] = {
|
} t_cmp_cases[] = {
|
||||||
{
|
{ "-1 == -1", -1, -1, 0 },
|
||||||
"0 == 0",
|
{ "-1 < 0", -1, 0, -1 },
|
||||||
{ 0x00, }, 1,
|
{ "-1 < 1", -1, 1, -1 },
|
||||||
{ 0x00, }, 1,
|
{ "0 > -1", 0, -1, 1 },
|
||||||
0, 0,
|
{ "0 == 0", 0, 0, 0 },
|
||||||
},
|
{ "0 < 1", 0, 1, -1 },
|
||||||
|
{ "1 > -1", 1, -1, 1 },
|
||||||
|
{ "1 > 0", 1, 0, 1 },
|
||||||
|
{ "1 == 1", 1, 1, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compare two MPIs
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
|
t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -707,9 +709,28 @@ t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
|
||||||
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
|
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
|
||||||
mpi_load(&a, tc->a, tc->alen);
|
mpi_set(&a, tc->a);
|
||||||
mpi_load(&b, tc->b, tc->blen);
|
mpi_set(&b, tc->b);
|
||||||
ret &= t_compare_i(tc->cmpabs, mpi_cmp_abs(&a, &b));
|
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
|
||||||
|
mpi_destroy(&a);
|
||||||
|
mpi_destroy(&b);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compare two MPIs with garbage past the msb
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
t_mpi_cmp_garbage(char **desc CRYB_UNUSED, void *arg)
|
||||||
|
{
|
||||||
|
struct t_cmp_case *tc = arg;
|
||||||
|
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
|
mpi_set(&a, tc->a);
|
||||||
|
a.words[1] = 2;
|
||||||
|
mpi_set(&b, tc->b);
|
||||||
|
b.words[1] = tc->cmp < 1 ? 1 : 3;
|
||||||
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
|
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
|
||||||
mpi_destroy(&a);
|
mpi_destroy(&a);
|
||||||
mpi_destroy(&b);
|
mpi_destroy(&b);
|
||||||
|
@ -1405,8 +1426,11 @@ t_prepare(int argc, char *argv[])
|
||||||
t_add_test(t_mpi_large_load_fail, NULL, "large load (failure)");
|
t_add_test(t_mpi_large_load_fail, NULL, "large load (failure)");
|
||||||
|
|
||||||
/* comparison */
|
/* comparison */
|
||||||
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i)
|
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i) {
|
||||||
t_add_test(t_mpi_cmp, &t_cmp_cases[i], t_cmp_cases[i].desc);
|
t_add_test(t_mpi_cmp, &t_cmp_cases[i], t_cmp_cases[i].desc);
|
||||||
|
t_add_test(t_mpi_cmp_garbage, &t_cmp_cases[i],
|
||||||
|
"%s (trailing garbage)", t_cmp_cases[i].desc);
|
||||||
|
}
|
||||||
|
|
||||||
/* left / right shift */
|
/* left / right shift */
|
||||||
for (i = 0; i < sizeof t_lsh_cases / sizeof t_lsh_cases[0]; ++i)
|
for (i = 0; i < sizeof t_lsh_cases / sizeof t_lsh_cases[0]; ++i)
|
||||||
|
|
Loading…
Reference in a new issue