Skip to content

Commit a510e56

Browse files
committed
fixup! improve tests
1 parent 6e5b7d5 commit a510e56

File tree

1 file changed

+74
-55
lines changed

1 file changed

+74
-55
lines changed

src/tests.c

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,6 +3675,77 @@ static void run_inverse_tests(void)
36753675
}
36763676
}
36773677

3678+
/***** HSORT TESTS *****/
3679+
3680+
static void test_heap_swap(void) {
3681+
unsigned char a[600];
3682+
unsigned char e[sizeof(a)];
3683+
memset(a, 21, 200);
3684+
memset(a + 200, 99, 200);
3685+
memset(a + 400, 42, 200);
3686+
memset(e, 42, 200);
3687+
memset(e + 200, 99, 200);
3688+
memset(e + 400, 21, 200);
3689+
secp256k1_heap_swap(a, 0, 2, 200);
3690+
CHECK(secp256k1_memcmp_var(a, e, sizeof(a)) == 0);
3691+
}
3692+
3693+
static void test_hsort_is_sorted(unsigned char *elements, size_t n, size_t len) {
3694+
size_t i;
3695+
for (i = 1; i < n; i++) {
3696+
CHECK(memcmp(&elements[(i-1) * len], &elements[i * len], len) <= 0);
3697+
}
3698+
}
3699+
3700+
struct test_hsort_cmp_data {
3701+
size_t counter;
3702+
size_t element_len;
3703+
};
3704+
3705+
static int test_hsort_cmp(const void *ele1, const void *ele2, void *data) {
3706+
struct test_hsort_cmp_data *d = (struct test_hsort_cmp_data *) data;
3707+
d->counter += 1;
3708+
return memcmp((unsigned char *)ele1, (unsigned char *)ele2, d->element_len);
3709+
}
3710+
3711+
#define NUM 65
3712+
#define MAX_ELEMENT_LEN 65
3713+
static void test_hsort(size_t element_len) {
3714+
unsigned char elements[NUM * MAX_ELEMENT_LEN] = { 0 };
3715+
struct test_hsort_cmp_data data;
3716+
int i;
3717+
3718+
VERIFY_CHECK(element_len <= MAX_ELEMENT_LEN);
3719+
data.counter = 0;
3720+
data.element_len = element_len;
3721+
3722+
secp256k1_hsort(elements, 0, element_len, test_hsort_cmp, &data);
3723+
CHECK(data.counter == 0);
3724+
secp256k1_hsort(elements, 1, element_len, test_hsort_cmp, &data);
3725+
CHECK(data.counter == 0);
3726+
secp256k1_hsort(elements, NUM, element_len, test_hsort_cmp, &data);
3727+
CHECK(data.counter >= NUM);
3728+
test_hsort_is_sorted(elements, NUM, element_len);
3729+
3730+
/* Test hsort with array of random length n */
3731+
for (i = 0; i < COUNT; i++) {
3732+
int n = secp256k1_testrand_int(NUM);
3733+
secp256k1_testrand_bytes_test(elements, n*element_len);
3734+
secp256k1_hsort(elements, n, element_len, test_hsort_cmp, &data);
3735+
test_hsort_is_sorted(elements, n, element_len);
3736+
}
3737+
}
3738+
#undef NUM
3739+
#undef MAX_ELEMENT_LEN
3740+
3741+
3742+
static void run_hsort_tests(void) {
3743+
test_heap_swap();
3744+
test_hsort(1);
3745+
test_hsort(64);
3746+
test_hsort(65);
3747+
}
3748+
36783749
/***** GROUP TESTS *****/
36793750

36803751
/* This compares jacobian points including their Z, not just their geometric meaning. */
@@ -6607,59 +6678,6 @@ static void run_pubkey_comparison(void) {
66076678
CHECK(secp256k1_ec_pubkey_cmp(CTX, &pk2, &pk1) > 0);
66086679
}
66096680

6610-
static void test_heap_swap(void) {
6611-
unsigned char a[600];
6612-
unsigned char e[sizeof(a)];
6613-
memset(a, 21, 200);
6614-
memset(a + 200, 99, 200);
6615-
memset(a + 400, 42, 200);
6616-
memset(e, 42, 200);
6617-
memset(e + 200, 99, 200);
6618-
memset(e + 400, 21, 200);
6619-
secp256k1_heap_swap(a, 0, 2, 200);
6620-
CHECK(secp256k1_memcmp_var(a, e, sizeof(a)) == 0);
6621-
}
6622-
6623-
static void test_hsort_is_sorted(int *ints, size_t n) {
6624-
size_t i;
6625-
for (i = 1; i < n; i++) {
6626-
CHECK(ints[i-1] <= ints[i]);
6627-
}
6628-
}
6629-
6630-
static int test_hsort_cmp(const void *i1, const void *i2, void *counter) {
6631-
*(size_t*)counter += 1;
6632-
return *(int*)i1 - *(int*)i2;
6633-
}
6634-
6635-
#define NUM 64
6636-
static void test_hsort(void) {
6637-
int ints[NUM] = { 0 };
6638-
size_t counter = 0;
6639-
int i, j;
6640-
6641-
secp256k1_hsort(ints, 0, sizeof(ints[0]), test_hsort_cmp, &counter);
6642-
CHECK(counter == 0);
6643-
secp256k1_hsort(ints, 1, sizeof(ints[0]), test_hsort_cmp, &counter);
6644-
CHECK(counter == 0);
6645-
secp256k1_hsort(ints, NUM, sizeof(ints[0]), test_hsort_cmp, &counter);
6646-
CHECK(counter > 0);
6647-
test_hsort_is_sorted(ints, NUM);
6648-
6649-
/* Test hsort with length n array and random elements in
6650-
* [-interval/2, interval/2] */
6651-
for (i = 0; i < COUNT; i++) {
6652-
int n = secp256k1_testrand_int(NUM);
6653-
int interval = secp256k1_testrand_int(63) + 1;
6654-
for (j = 0; j < n; j++) {
6655-
ints[j] = secp256k1_testrand_int(interval) - interval/2;
6656-
}
6657-
secp256k1_hsort(ints, n, sizeof(ints[0]), test_hsort_cmp, &counter);
6658-
test_hsort_is_sorted(ints, n);
6659-
}
6660-
}
6661-
#undef NUM
6662-
66636681
static void test_sort_helper(secp256k1_pubkey *pk, size_t *pk_order, size_t n_pk) {
66646682
size_t i;
66656683
const secp256k1_pubkey *pk_test[5];
@@ -6809,11 +6827,9 @@ static void test_sort_vectors(void) {
68096827
}
68106828

68116829
static void run_pubkey_sort(void) {
6812-
test_hsort();
68136830
test_sort_api();
68146831
test_sort();
68156832
test_sort_vectors();
6816-
test_heap_swap();
68176833
}
68186834

68196835

@@ -7776,6 +7792,9 @@ int main(int argc, char **argv) {
77767792
run_modinv_tests();
77777793
run_inverse_tests();
77787794

7795+
/* sorting tests */
7796+
run_hsort_tests();
7797+
77797798
/* hash tests */
77807799
run_sha256_known_output_tests();
77817800
run_sha256_counter_tests();

0 commit comments

Comments
 (0)