]> Savannah Git Hosting - gnulib.git/commitdiff
libgmp tests: Add some safety checks.
authorBruno Haible <bruno@clisp.org>
Sun, 12 Jul 2020 20:51:22 +0000 (22:51 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 12 Jul 2020 20:51:22 +0000 (22:51 +0200)
* modules/libgmp-tests (Depends-on): Add verify.
* tests/test-libgmp.c: Verify GMP_NUMB_BITS value.
(main): Verify that gmp.h and libgmp versions match.

ChangeLog
modules/libgmp-tests
tests/test-libgmp.c

index 3737892191a2ba5c07474d905a482e66afdd86b9..8d71029d30a88035f292e93e19c311739da44970 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2020-07-12  Bruno Haible  <bruno@clisp.org>
+
+       libgmp tests: Add some safety checks.
+       * modules/libgmp-tests (Depends-on): Add verify.
+       * tests/test-libgmp.c: Verify GMP_NUMB_BITS value.
+       (main): Verify that gmp.h and libgmp versions match.
+
 2020-07-10  Bruno Haible  <bruno@clisp.org>
 
        unicodeio: Fix wrong result on NetBSD.
index 50a18016ca93f96f6d248b2262ec39c6dab3db38..c01b876a10fcc3351b714afc91360adaf9dea502 100644 (file)
@@ -3,6 +3,7 @@ tests/macros.h
 tests/test-libgmp.c
 
 Depends-on:
+verify
 
 configure.ac:
 
index f72a74cd6235e4ef6e32be410b471801ade27719..58ccc664c96158ab0fd386351e54605b91a84914 100644 (file)
 
 #include <config.h>
 
+/* Specification.  */
 #include <gmp.h>
 
+#include <limits.h>
+#include <string.h>
+
+#include "verify.h"
+
 #include "macros.h"
 
+#ifndef MINI_GMP_LIMB_TYPE
+/* Verify that the gmp.h header file was generated for the same
+   machine word size as we are using.  */
+verify (GMP_NUMB_BITS == sizeof (mp_limb_t) * CHAR_BIT);
+#endif
+
 int
 main ()
 {
+#ifndef MINI_GMP_LIMB_TYPE
+  /* Verify that the gmp.h header file and the libgmp library come from
+     the same GMP version.  */
+  {
+    char gmp_header_version[32];
+    sprintf (gmp_header_version, "%d.%d.%d", __GNU_MP_VERSION,
+             __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL);
+    if (strcmp (gmp_version, gmp_header_version) != 0)
+      {
+        char gmp_header_version2[32];
+        if (__GNU_MP_VERSION_PATCHLEVEL > 0
+            || (sprintf (gmp_header_version2, "%d.%d", __GNU_MP_VERSION,
+                         __GNU_MP_VERSION_MINOR),
+                strcmp (gmp_version, gmp_header_version2) != 0))
+          {
+            fprintf (stderr,
+                     "gmp header version (%s) does not match gmp library version (%s).\n",
+                     gmp_header_version, gmp_version);
+            exit (1);
+          }
+      }
+  }
+#endif
+
   /* A simple sanity check that 2 + 2 = 4.  */
   static mp_limb_t const twobody[] = { 2 };
   static mpz_t const two = MPZ_ROINIT_N ((mp_limb_t *) twobody, 1);