From: Marc Nieper-Wißkirchen <m...@nieper-wisskirchen.de>

* modules/libgmp-mpq-tests: New file.
* tests/test-libgmp-mpq.c: New file.
---
 ChangeLog                |  4 ++
 modules/libgmp-mpq-tests | 14 +++++++
 tests/test-libgmp-mpq.c  | 82 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 modules/libgmp-mpq-tests
 create mode 100644 tests/test-libgmp-mpq.c

diff --git a/ChangeLog b/ChangeLog
index 6dc01c81f6..97dc1f49b6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-08-29  Marc Nieper-Wißkirchen  <m...@nieper-wisskirchen.de>
 
+       libgmp-mpq: Add tests.
+       * modules/libgmp-mpq-tests: New file.
+       * tests/test-libgmp-mpq.c: New file.
+
        libgmp-mpq: New module.
        * MODULES.html.sh: Mention libgmp-mpz and
        libgmp-mpq.
diff --git a/modules/libgmp-mpq-tests b/modules/libgmp-mpq-tests
new file mode 100644
index 0000000000..0706222411
--- /dev/null
+++ b/modules/libgmp-mpq-tests
@@ -0,0 +1,14 @@
+Files:
+tests/macros.h
+tests/test-libgmp-mpq.c
+
+Depends-on:
+assert-h
+c99
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-libgmp-mpq
+check_PROGRAMS += test-libgmp-mpq
+test_libgmp_mpq_LDADD = $(LDADD) @LIBGMP@
diff --git a/tests/test-libgmp-mpq.c b/tests/test-libgmp-mpq.c
new file mode 100644
index 0000000000..7da7e63904
--- /dev/null
+++ b/tests/test-libgmp-mpq.c
@@ -0,0 +1,82 @@
+/* Test of libgmp or its mini-mpq substitute.
+   Copyright (C) 2024 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 <gmp.h>
+
+#include <limits.h>
+#include <string.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.  */
+static_assert (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/3 + 2/3 = 4/3.  */
+  static mp_limb_t const twobody[] = { 2 };
+  static mp_limb_t const threebody[] = { 3 };
+  static mpz_t const two = MPZ_ROINIT_N ((mp_limb_t *) twobody, 1);
+  static mpz_t const three = MPZ_ROINIT_N ((mp_limb_t *) threebody, 1);
+  ASSERT (mpz_fits_slong_p (two));
+  ASSERT (mpz_get_si (two) == 2);
+  ASSERT (mpz_fits_slong_p (three));
+  ASSERT (mpz_get_si (three) == 3);
+
+  mpq_t q;
+  mpq_init (q);
+  mpz_set (mpq_numref (q), two);
+  mpz_set (mpq_denref (q), three);
+  mpq_add (q, q, q);
+  ASSERT (mpz_fits_slong_p (mpq_numref (q)));
+  ASSERT (mpz_get_si (mpq_numref (q)) == 4);
+  ASSERT (mpz_fits_slong_p (mpq_denref (q)));
+  ASSERT (mpz_get_si (mpq_denref (q)) == 3);
+  mpq_clear (q);
+
+  return test_exit_status;
+}
-- 
2.34.1


Reply via email to