Here are three patches that enhance the unit tests of the totalorder* functions.
The third patch uses the 'union' workaround, to avoid test failures on i386 and x86_64 platforms. 2023-10-14 Bruno Haible <br...@clisp.org> totalorder* tests: Test also the signalling NaNs. * tests/test-totalorder.h: Include signed-snan.h. (main): Change the array initializer to contain memory_* objects rather than floating-point numbers. This is needed for i386 and x86_64 CPUs. * tests/test-totalorder.c (TOTALORDER_TYPE): Use memory_double. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * tests/test-totalorderf.c (TOTALORDER_TYPE): Use memory_float. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * tests/test-totalorderl.c (TOTALORDER_TYPE): Use memory_long_double. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * modules/totalorder-tests (Depends-on): Add signed-snan. * modules/totalorderf-tests (Depends-on): Likewise. * modules/totalorderl-tests (Depends-on): Likewise. 2023-10-14 Bruno Haible <br...@clisp.org> totalorder* tests: Make it easier to debug failures. * tests/test-totalorder.h: Include <stdio.h>. (main): Print array indices of all failures. 2023-10-14 Bruno Haible <br...@clisp.org> totalorder* tests: Verify also the function signatures. * tests/test-totalorder.h: New file, based on tests/test-totalorder.c. * tests/test-totalorder.c: Check the signature. Include test-totalorder.h for the rest. * tests/test-totalorderf.c: Include <config.h>, <math.h>. Check the signature. Include test-totalorder.h instead of test-totalorder.c. * tests/test-totalorderl.c: Likewise. * modules/totalorder-tests (Files): Add test-totalorder.h, signature.h. * modules/totalorderf-tests (Files): Add test-totalorder.h, signature.h. Remove test-totalorder.c. * modules/totalorderl-tests (Files): Likewise.
>From 2d1f993d259d7797deeeec862f7d29d4befcff28 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 14 Oct 2023 20:43:39 +0200 Subject: [PATCH 1/3] totalorder* tests: Verify also the function signatures. * tests/test-totalorder.h: New file, based on tests/test-totalorder.c. * tests/test-totalorder.c: Check the signature. Include test-totalorder.h for the rest. * tests/test-totalorderf.c: Include <config.h>, <math.h>. Check the signature. Include test-totalorder.h instead of test-totalorder.c. * tests/test-totalorderl.c: Likewise. * modules/totalorder-tests (Files): Add test-totalorder.h, signature.h. * modules/totalorderf-tests (Files): Add test-totalorder.h, signature.h. Remove test-totalorder.c. * modules/totalorderl-tests (Files): Likewise. --- ChangeLog | 14 ++++++++++++++ modules/totalorder-tests | 2 ++ modules/totalorderf-tests | 3 ++- modules/totalorderl-tests | 3 ++- tests/test-totalorder.c | 39 ++++++++++----------------------------- tests/test-totalorder.h | 37 +++++++++++++++++++++++++++++++++++++ tests/test-totalorderf.c | 28 ++++++++++++++++++++++++++-- tests/test-totalorderl.c | 28 ++++++++++++++++++++++++++-- 8 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 tests/test-totalorder.h diff --git a/ChangeLog b/ChangeLog index 40b2116a88..db5cd18148 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2023-10-14 Bruno Haible <br...@clisp.org> + + totalorder* tests: Verify also the function signatures. + * tests/test-totalorder.h: New file, based on tests/test-totalorder.c. + * tests/test-totalorder.c: Check the signature. Include + test-totalorder.h for the rest. + * tests/test-totalorderf.c: Include <config.h>, <math.h>. Check the + signature. Include test-totalorder.h instead of test-totalorder.c. + * tests/test-totalorderl.c: Likewise. + * modules/totalorder-tests (Files): Add test-totalorder.h, signature.h. + * modules/totalorderf-tests (Files): Add test-totalorder.h, signature.h. + Remove test-totalorder.c. + * modules/totalorderl-tests (Files): Likewise. + 2023-10-14 Bruno Haible <br...@clisp.org> totalorder*: Improve documentation. diff --git a/modules/totalorder-tests b/modules/totalorder-tests index 3741b88d4c..2035acd697 100644 --- a/modules/totalorder-tests +++ b/modules/totalorder-tests @@ -1,7 +1,9 @@ Files: tests/test-totalorder.c +tests/test-totalorder.h tests/minus-zero.h tests/infinity.h +tests/signature.h tests/macros.h Depends-on: diff --git a/modules/totalorderf-tests b/modules/totalorderf-tests index f2801f496f..5fb1cc8225 100644 --- a/modules/totalorderf-tests +++ b/modules/totalorderf-tests @@ -1,8 +1,9 @@ Files: tests/test-totalorderf.c -tests/test-totalorder.c +tests/test-totalorder.h tests/minus-zero.h tests/infinity.h +tests/signature.h tests/macros.h Depends-on: diff --git a/modules/totalorderl-tests b/modules/totalorderl-tests index fa25c3e301..85d1768dfc 100644 --- a/modules/totalorderl-tests +++ b/modules/totalorderl-tests @@ -1,8 +1,9 @@ Files: tests/test-totalorderl.c -tests/test-totalorder.c +tests/test-totalorder.h tests/minus-zero.h tests/infinity.h +tests/signature.h tests/macros.h Depends-on: diff --git a/tests/test-totalorder.c b/tests/test-totalorder.c index af903c68b5..2b50b8d437 100644 --- a/tests/test-totalorder.c +++ b/tests/test-totalorder.c @@ -16,35 +16,16 @@ #include <config.h> +/* Specification. */ #include <math.h> -#include "infinity.h" -#include "macros.h" -#include "minus-zero.h" -#include "signed-nan.h" +#include "signature.h" +SIGNATURE_CHECK (totalorderf, int, (const float *, const float *)); -#ifndef TOTALORDER -# define TOTALORDER totalorder -# define TOTALORDER_INF Infinityd -# define TOTALORDER_MINUS_ZERO minus_zerod -# define TOTALORDER_POSITIVE_NAN positive_NaNd -# define TOTALORDER_NEGATIVE_NAN negative_NaNd -# define TOTALORDER_TYPE double -#endif - -int -main () -{ - TOTALORDER_TYPE x[] = - { - TOTALORDER_NEGATIVE_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5, - TOTALORDER_MINUS_ZERO, 0, - 1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_POSITIVE_NAN () - }; - int n = sizeof x / sizeof *x; - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j)); - return 0; -} +#define TOTALORDER totalorder +#define TOTALORDER_TYPE double +#define TOTALORDER_INF Infinityd +#define TOTALORDER_MINUS_ZERO minus_zerod +#define TOTALORDER_POSITIVE_NAN positive_NaNd +#define TOTALORDER_NEGATIVE_NAN negative_NaNd +#include "test-totalorder.h" diff --git a/tests/test-totalorder.h b/tests/test-totalorder.h new file mode 100644 index 0000000000..2405e49edf --- /dev/null +++ b/tests/test-totalorder.h @@ -0,0 +1,37 @@ +/* Test a totalorder-like function. + Copyright 2023 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +#include "infinity.h" +#include "macros.h" +#include "minus-zero.h" +#include "signed-nan.h" + +int +main () +{ + TOTALORDER_TYPE x[] = + { + TOTALORDER_NEGATIVE_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5, + TOTALORDER_MINUS_ZERO, 0, + 1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_POSITIVE_NAN () + }; + int n = sizeof x / sizeof *x; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j)); + return 0; +} diff --git a/tests/test-totalorderf.c b/tests/test-totalorderf.c index 5fd27031c0..6d921dd103 100644 --- a/tests/test-totalorderf.c +++ b/tests/test-totalorderf.c @@ -1,7 +1,31 @@ +/* Test totalorderf. + Copyright 2023 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <math.h> + +#include "signature.h" +SIGNATURE_CHECK (totalorderf, int, (const float *, const float *)); + #define TOTALORDER totalorderf +#define TOTALORDER_TYPE float #define TOTALORDER_INF Infinityf #define TOTALORDER_MINUS_ZERO minus_zerof #define TOTALORDER_POSITIVE_NAN positive_NaNf #define TOTALORDER_NEGATIVE_NAN negative_NaNf -#define TOTALORDER_TYPE float -#include "test-totalorder.c" +#include "test-totalorder.h" diff --git a/tests/test-totalorderl.c b/tests/test-totalorderl.c index c8f1fbd4b6..89df547fc5 100644 --- a/tests/test-totalorderl.c +++ b/tests/test-totalorderl.c @@ -1,7 +1,31 @@ +/* Test totalorderl. + Copyright 2023 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <math.h> + +#include "signature.h" +SIGNATURE_CHECK (totalorderl, int, (const long double *, const long double *)); + #define TOTALORDER totalorderl +#define TOTALORDER_TYPE long double #define TOTALORDER_INF Infinityl #define TOTALORDER_MINUS_ZERO minus_zerol #define TOTALORDER_POSITIVE_NAN positive_NaNl #define TOTALORDER_NEGATIVE_NAN negative_NaNl -#define TOTALORDER_TYPE long double -#include "test-totalorder.c" +#include "test-totalorder.h" -- 2.34.1
>From 24bdd76dc1ed9a12791217da1e44389462092978 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 14 Oct 2023 20:43:43 +0200 Subject: [PATCH 2/3] totalorder* tests: Make it easier to debug failures. * tests/test-totalorder.h: Include <stdio.h>. (main): Print array indices of all failures. --- ChangeLog | 6 ++++++ tests/test-totalorder.h | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index db5cd18148..fadb0a9978 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2023-10-14 Bruno Haible <br...@clisp.org> + + totalorder* tests: Make it easier to debug failures. + * tests/test-totalorder.h: Include <stdio.h>. + (main): Print array indices of all failures. + 2023-10-14 Bruno Haible <br...@clisp.org> totalorder* tests: Verify also the function signatures. diff --git a/tests/test-totalorder.h b/tests/test-totalorder.h index 2405e49edf..b242ff72f3 100644 --- a/tests/test-totalorder.h +++ b/tests/test-totalorder.h @@ -14,6 +14,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include <stdio.h> + #include "infinity.h" #include "macros.h" #include "minus-zero.h" @@ -28,10 +30,16 @@ main () TOTALORDER_MINUS_ZERO, 0, 1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_POSITIVE_NAN () }; - int n = sizeof x / sizeof *x; + int n = SIZEOF (x); + int result = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) - ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j)); - return 0; + if (!(!!TOTALORDER (&x[i], &x[j]) == (i <= j))) + { + fprintf (stderr, "Failed: i=%d j=%d\n", i, j); + result = 1; + } + + return result; } -- 2.34.1
>From 57b9775f4a36aadb32bfcc445bfe8038190ca689 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sat, 14 Oct 2023 20:43:47 +0200 Subject: [PATCH 3/3] totalorder* tests: Test also the signalling NaNs. * tests/test-totalorder.h: Include signed-snan.h. (main): Change the array initializer to contain memory_* objects rather than floating-point numbers. This is needed for i386 and x86_64 CPUs. * tests/test-totalorder.c (TOTALORDER_TYPE): Use memory_double. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * tests/test-totalorderf.c (TOTALORDER_TYPE): Use memory_float. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * tests/test-totalorderl.c (TOTALORDER_TYPE): Use memory_long_double. (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, TOTALORDER_NEGATIVE_SNAN): New macros. * modules/totalorder-tests (Depends-on): Add signed-snan. * modules/totalorderf-tests (Depends-on): Likewise. * modules/totalorderl-tests (Depends-on): Likewise. --- ChangeLog | 19 +++++++++++++++++++ modules/totalorder-tests | 1 + modules/totalorderf-tests | 1 + modules/totalorderl-tests | 1 + tests/test-totalorder.c | 5 ++++- tests/test-totalorder.h | 24 ++++++++++++++++++++---- tests/test-totalorderf.c | 5 ++++- tests/test-totalorderl.c | 5 ++++- 8 files changed, 54 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index fadb0a9978..f4db2f61c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2023-10-14 Bruno Haible <br...@clisp.org> + + totalorder* tests: Test also the signalling NaNs. + * tests/test-totalorder.h: Include signed-snan.h. + (main): Change the array initializer to contain memory_* objects rather + than floating-point numbers. This is needed for i386 and x86_64 CPUs. + * tests/test-totalorder.c (TOTALORDER_TYPE): Use memory_double. + (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, + TOTALORDER_NEGATIVE_SNAN): New macros. + * tests/test-totalorderf.c (TOTALORDER_TYPE): Use memory_float. + (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, + TOTALORDER_NEGATIVE_SNAN): New macros. + * tests/test-totalorderl.c (TOTALORDER_TYPE): Use memory_long_double. + (TOTALORDER_HAVE_SNAN, TOTALORDER_POSITIVE_SNAN, + TOTALORDER_NEGATIVE_SNAN): New macros. + * modules/totalorder-tests (Depends-on): Add signed-snan. + * modules/totalorderf-tests (Depends-on): Likewise. + * modules/totalorderl-tests (Depends-on): Likewise. + 2023-10-14 Bruno Haible <br...@clisp.org> totalorder* tests: Make it easier to debug failures. diff --git a/modules/totalorder-tests b/modules/totalorder-tests index 2035acd697..ca6238f7cb 100644 --- a/modules/totalorder-tests +++ b/modules/totalorder-tests @@ -8,6 +8,7 @@ tests/macros.h Depends-on: signed-nan +signed-snan configure.ac: diff --git a/modules/totalorderf-tests b/modules/totalorderf-tests index 5fb1cc8225..dbca0add15 100644 --- a/modules/totalorderf-tests +++ b/modules/totalorderf-tests @@ -8,6 +8,7 @@ tests/macros.h Depends-on: signed-nan +signed-snan configure.ac: diff --git a/modules/totalorderl-tests b/modules/totalorderl-tests index 85d1768dfc..ab73b6a5fd 100644 --- a/modules/totalorderl-tests +++ b/modules/totalorderl-tests @@ -8,6 +8,7 @@ tests/macros.h Depends-on: signed-nan +signed-snan configure.ac: diff --git a/tests/test-totalorder.c b/tests/test-totalorder.c index 2b50b8d437..d921ab716c 100644 --- a/tests/test-totalorder.c +++ b/tests/test-totalorder.c @@ -23,9 +23,12 @@ SIGNATURE_CHECK (totalorderf, int, (const float *, const float *)); #define TOTALORDER totalorder -#define TOTALORDER_TYPE double +#define TOTALORDER_TYPE memory_double #define TOTALORDER_INF Infinityd #define TOTALORDER_MINUS_ZERO minus_zerod #define TOTALORDER_POSITIVE_NAN positive_NaNd #define TOTALORDER_NEGATIVE_NAN negative_NaNd +#define TOTALORDER_HAVE_SNAN HAVE_SNAND +#define TOTALORDER_POSITIVE_SNAN memory_positive_SNaNd +#define TOTALORDER_NEGATIVE_SNAN memory_negative_SNaNd #include "test-totalorder.h" diff --git a/tests/test-totalorder.h b/tests/test-totalorder.h index b242ff72f3..6292ea130a 100644 --- a/tests/test-totalorder.h +++ b/tests/test-totalorder.h @@ -20,22 +20,38 @@ #include "macros.h" #include "minus-zero.h" #include "signed-nan.h" +#include "signed-snan.h" int main () { TOTALORDER_TYPE x[] = { - TOTALORDER_NEGATIVE_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5, - TOTALORDER_MINUS_ZERO, 0, - 1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_POSITIVE_NAN () + { .value = TOTALORDER_NEGATIVE_NAN () }, +#if TOTALORDER_HAVE_SNAN + TOTALORDER_NEGATIVE_SNAN (), +#endif + { .value = -TOTALORDER_INF () }, + { .value = -1e37 }, + { .value = -1 }, + { .value = -1e-5 }, + { .value = TOTALORDER_MINUS_ZERO }, + { .value = 0 }, + { .value = 1e-5 }, + { .value = 1 }, + { .value = 1e37 }, + { .value = TOTALORDER_INF () }, +#if TOTALORDER_HAVE_SNAN + TOTALORDER_POSITIVE_SNAN (), +#endif + { .value = TOTALORDER_POSITIVE_NAN () } }; int n = SIZEOF (x); int result = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) - if (!(!!TOTALORDER (&x[i], &x[j]) == (i <= j))) + if (!(!!TOTALORDER (&x[i].value, &x[j].value) == (i <= j))) { fprintf (stderr, "Failed: i=%d j=%d\n", i, j); result = 1; diff --git a/tests/test-totalorderf.c b/tests/test-totalorderf.c index 6d921dd103..1aaf35004d 100644 --- a/tests/test-totalorderf.c +++ b/tests/test-totalorderf.c @@ -23,9 +23,12 @@ SIGNATURE_CHECK (totalorderf, int, (const float *, const float *)); #define TOTALORDER totalorderf -#define TOTALORDER_TYPE float +#define TOTALORDER_TYPE memory_float #define TOTALORDER_INF Infinityf #define TOTALORDER_MINUS_ZERO minus_zerof #define TOTALORDER_POSITIVE_NAN positive_NaNf #define TOTALORDER_NEGATIVE_NAN negative_NaNf +#define TOTALORDER_HAVE_SNAN HAVE_SNANF +#define TOTALORDER_POSITIVE_SNAN memory_positive_SNaNf +#define TOTALORDER_NEGATIVE_SNAN memory_negative_SNaNf #include "test-totalorder.h" diff --git a/tests/test-totalorderl.c b/tests/test-totalorderl.c index 89df547fc5..0344f04b18 100644 --- a/tests/test-totalorderl.c +++ b/tests/test-totalorderl.c @@ -23,9 +23,12 @@ SIGNATURE_CHECK (totalorderl, int, (const long double *, const long double *)); #define TOTALORDER totalorderl -#define TOTALORDER_TYPE long double +#define TOTALORDER_TYPE memory_long_double #define TOTALORDER_INF Infinityl #define TOTALORDER_MINUS_ZERO minus_zerol #define TOTALORDER_POSITIVE_NAN positive_NaNl #define TOTALORDER_NEGATIVE_NAN negative_NaNl +#define TOTALORDER_HAVE_SNAN HAVE_SNANL +#define TOTALORDER_POSITIVE_SNAN memory_positive_SNaNl +#define TOTALORDER_NEGATIVE_SNAN memory_negative_SNaNl #include "test-totalorder.h" -- 2.34.1