From 5709353178f3d10123fbfbff249a0f745f4c694a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 11 Dec 2018 20:36:35 +0100 Subject: [PATCH] array-set, linkedhash-set, hash-set: Fix tests. * tests/test-array_set.c (cmp_objects_in_array): New function. (check_equals): Use it. * tests/test-hash_set.c: Likewise. * tests/test-linkedhash_set.c: Likewise. --- ChangeLog | 8 ++++++++ tests/test-array_set.c | 14 ++++++++++---- tests/test-hash_set.c | 14 ++++++++++---- tests/test-linkedhash_set.c | 14 ++++++++++---- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 70e269d884..019b8252e1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2018-12-11 Bruno Haible + + array-set, linkedhash-set, hash-set: Fix tests. + * tests/test-array_set.c (cmp_objects_in_array): New function. + (check_equals): Use it. + * tests/test-hash_set.c: Likewise. + * tests/test-linkedhash_set.c: Likewise. + 2018-12-08 Bruno Haible Fix comments. diff --git a/tests/test-array_set.c b/tests/test-array_set.c index 35465ac2a7..91f12943b9 100644 --- a/tests/test-array_set.c +++ b/tests/test-array_set.c @@ -36,6 +36,14 @@ static const char *objects[30] = #define RANDOM(n) (rand () % (n)) #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))] +static int +cmp_objects_in_array (const void *objptr1, const void *objptr2) +{ + const void *obj1 = *(const void * const *)objptr1; + const void *obj2 = *(const void * const *)objptr2; + return strcmp ((const char *) obj1, (const char *) obj2); +} + static void check_equals (gl_set_t set1, gl_oset_t set2) { @@ -65,10 +73,8 @@ check_equals (gl_set_t set1, gl_oset_t set2) if (n > 0) { - qsort (elements_of_set1, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); - qsort (elements_of_set2, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); + qsort (elements_of_set1, n, sizeof (const void *), cmp_objects_in_array); + qsort (elements_of_set2, n, sizeof (const void *), cmp_objects_in_array); } for (i = 0; i < n; i++) ASSERT (elements_of_set1[i] == elements_of_set2[i]); diff --git a/tests/test-hash_set.c b/tests/test-hash_set.c index d791074f4b..a2138effd4 100644 --- a/tests/test-hash_set.c +++ b/tests/test-hash_set.c @@ -35,6 +35,14 @@ static const char *objects[30] = #define RANDOM(n) (rand () % (n)) #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))] +static int +cmp_objects_in_array (const void *objptr1, const void *objptr2) +{ + const void *obj1 = *(const void * const *)objptr1; + const void *obj2 = *(const void * const *)objptr2; + return strcmp ((const char *) obj1, (const char *) obj2); +} + static void check_equals (gl_set_t set1, gl_set_t set2) { @@ -64,10 +72,8 @@ check_equals (gl_set_t set1, gl_set_t set2) if (n > 0) { - qsort (elements_of_set1, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); - qsort (elements_of_set2, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); + qsort (elements_of_set1, n, sizeof (const void *), cmp_objects_in_array); + qsort (elements_of_set2, n, sizeof (const void *), cmp_objects_in_array); } for (i = 0; i < n; i++) ASSERT (elements_of_set1[i] == elements_of_set2[i]); diff --git a/tests/test-linkedhash_set.c b/tests/test-linkedhash_set.c index 7ff92e8aaf..6ad33a52a5 100644 --- a/tests/test-linkedhash_set.c +++ b/tests/test-linkedhash_set.c @@ -35,6 +35,14 @@ static const char *objects[30] = #define RANDOM(n) (rand () % (n)) #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))] +static int +cmp_objects_in_array (const void *objptr1, const void *objptr2) +{ + const void *obj1 = *(const void * const *)objptr1; + const void *obj2 = *(const void * const *)objptr2; + return strcmp ((const char *) obj1, (const char *) obj2); +} + static void check_equals (gl_set_t set1, gl_set_t set2) { @@ -64,10 +72,8 @@ check_equals (gl_set_t set1, gl_set_t set2) if (n > 0) { - qsort (elements_of_set1, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); - qsort (elements_of_set2, n, sizeof (const void *), - (int (*) (const void *, const void *)) strcmp); + qsort (elements_of_set1, n, sizeof (const void *), cmp_objects_in_array); + qsort (elements_of_set2, n, sizeof (const void *), cmp_objects_in_array); } for (i = 0; i < n; i++) ASSERT (elements_of_set1[i] == elements_of_set2[i]); -- 2.11.4.GIT