diff options
author | Kevin Ryde <user42@zip.com.au> | 2002-05-07 00:21:53 +0200 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2002-05-07 00:21:53 +0200 |
commit | f5bce99b4566933fb4443bb60a9974a40fc08cbc (patch) | |
tree | f3fb158bfc8e4e79e58f1b00ed1d9050810ceb6e | |
parent | edfd47b603a62ba43273060426ffaf44b8b8e858 (diff) | |
download | gmp-f5bce99b4566933fb4443bb60a9974a40fc08cbc.tar.gz |
* tests/mpz/t-import.c, tests/mpz/t-export.c, tests/mpz/t-get_d.c:
New tests.
* tests/mpz/Makefile.am: Add them.
-rw-r--r-- | tests/mpz/Makefile.am | 7 | ||||
-rw-r--r-- | tests/mpz/t-export.c | 174 | ||||
-rw-r--r-- | tests/mpz/t-get_d.c | 76 | ||||
-rw-r--r-- | tests/mpz/t-import.c | 147 |
4 files changed, 401 insertions, 3 deletions
diff --git a/tests/mpz/Makefile.am b/tests/mpz/Makefile.am index 36ed20643..b192d8cd9 100644 --- a/tests/mpz/Makefile.am +++ b/tests/mpz/Makefile.am @@ -1,6 +1,7 @@ ## Process this file with automake to generate Makefile.in -# Copyright 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc. +# Copyright 1996, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, +# Inc. # # This file is part of the GNU MP Library. # @@ -28,11 +29,11 @@ LDADD = $(top_builddir)/tests/libtests.la $(top_builddir)/libgmp.la check_PROGRAMS = t-cmp t-mul t-mul_i t-tdiv t-tdiv_ui t-fdiv t-fdiv_ui \ t-cdiv_ui t-gcd t-lcm dive dive_ui t-sqrtrem convert io t-inp_str \ logic bit t-powm t-powm_ui t-pow t-div_2exp reuse \ - t-root t-perfsqr t-jac t-bin t-get_si t-set_si \ + t-root t-perfsqr t-jac t-bin t-get_d t-get_si t-set_si \ t-fac_ui t-fib_ui t-lucnum_ui t-scan t-fits \ t-divis t-divis_2exp t-cong t-cong_2exp t-sizeinbase t-set_str \ t-aorsmul t-cmp_d t-cmp_si t-hamdist t-oddeven t-popcount t-set_f \ - t-io_raw + t-io_raw t-import t-export TESTS = $(check_PROGRAMS) diff --git a/tests/mpz/t-export.c b/tests/mpz/t-export.c new file mode 100644 index 000000000..7592c1994 --- /dev/null +++ b/tests/mpz/t-export.c @@ -0,0 +1,174 @@ +/* Test mpz_export. + +Copyright 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library 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 Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "tests.h" + + +void +check_data (void) +{ + static const struct { + const char *src; + size_t want_count; + int order; + size_t size; + int endian; + int nail; + char want_data[64]; + + } data[] = { + + { "0", 0,1, 1,1, 0 }, + { "0", 0,1, 2,1, 0 }, + { "0", 0,1, 3,1, 0 }, + + { "0x12345678", 4,1, 1,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + { "0x12345678", 1,1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + { "0x12345678", 1,-1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + + { "0x12345678", 4,-1, 1,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + { "0x12345678", 1,1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + { "0x12345678", 1,-1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + + { "0x15", 5,1, 1,1, 7, { 0x01, 0x00, 0x01, 0x00, 0x01 } }, + + { "0x1FFFFFFFFFFF", 3,1, 2,1, 1, { 0x7F,0xFF, 0x7F,0xFF, 0x7F,0xFF } }, + { "0x1FFFFFFFFFFF", 3,1, 2,-1, 1, { 0xFF,0x7F, 0xFF,0x7F, 0xFF,0x7F } }, + { "0x7", 3,1, 2,1, 15, { 0x00,0x01, 0x00,0x01, 0x00,0x01 } }, + { "0x7", 3,1, 2,-1, 15, { 0x01,0x00, 0x01,0x00, 0x01,0x00 } }, + + { "0x24", 3,1, 2,1, 14, { 0x00,0x02, 0x00,0x01, 0x00,0x00 } }, + { "0x24", 3,1, 2,-1, 14, { 0x02,0x00, 0x01,0x00, 0x00,0x00 } }, + { "0x24", 3,-1, 2,-1, 14, { 0x00,0x00, 0x01,0x00, 0x02,0x00 } }, + { "0x24", 3,-1, 2,1, 14, { 0x00,0x00, 0x00,0x01, 0x00,0x02 } }, + + { "0x123456789ABC", 3,1, 2,1, 0, { 0x12,0x34, 0x56,0x78, 0x9A,0xBC } }, + { "0x123456789ABC", 3,-1, 2,1, 0, { 0x9A,0xBC, 0x56,0x78, 0x12,0x34 } }, + { "0x123456789ABC", 3,1, 2,-1, 0, { 0x34,0x12, 0x78,0x56, 0xBC,0x9A } }, + { "0x123456789ABC", 3,-1, 2,-1, 0, { 0xBC,0x9A, 0x78,0x56, 0x34,0x12 } }, + + { "0x112233445566778899AABBCC", 3,1, 4,1, 0, + { 0x11,0x22,0x33,0x44, 0x55,0x66,0x77,0x88, 0x99,0xAA,0xBB,0xCC } }, + { "0x112233445566778899AABBCC", 3,-1, 4,1, 0, + { 0x99,0xAA,0xBB,0xCC, 0x55,0x66,0x77,0x88, 0x11,0x22,0x33,0x44 } }, + { "0x112233445566778899AABBCC", 3,1, 4,-1, 0, + { 0x44,0x33,0x22,0x11, 0x88,0x77,0x66,0x55, 0xCC,0xBB,0xAA,0x99 } }, + { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0, + { 0xCC,0xBB,0xAA,0x99, 0x88,0x77,0x66,0x55, 0x44,0x33,0x22,0x11 } }, + + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0, + { 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04, + 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, + 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0, + { 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C, + 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, + 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04 } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0, + { 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10, + 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, + 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90 } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0, + { 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90, + 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, + 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10 } }, + }; + + char buf[sizeof(data[0].src) + sizeof (mp_limb_t) + 128]; + char *got_data; + void *ret; + size_t align, got_count, j; + int i, error = 0; + mpz_t src; + + mpz_init (src); + + for (i = 0; i < numberof (data); i++) + { + for (align = 0; align < sizeof (mp_limb_t); align++) + { + mpz_set_str_or_abort (src, data[i].src, 0); + MPZ_CHECK_FORMAT (src); + got_data = buf + align; + + ASSERT_ALWAYS (data[i].want_count * data[i].size + align + <= sizeof (buf)); + + memset (got_data, '\0', data[i].want_count * data[i].size); + ret = mpz_export (got_data, &got_count, data[i].order, + data[i].size, data[i].endian, data[i].nail, src); + + if (ret != got_data) + { + printf ("return doesn't equal given pointer\n"); + error = 1; + } + if (got_count != data[i].want_count) + { + printf ("wrong count\n"); + error = 1; + } + if (memcmp (got_data, data[i].want_data, got_count * data[i].size) != 0) + { + printf ("wrong result data\n"); + error = 1; + } + if (error) + { + printf (" at data[%d]\n", i); + printf (" src \"%s\"\n", data[i].src); + mpz_trace (" src", src); + printf (" order=%d size=%u endian=%d nail=%u\n", + data[i].order, + data[i].size, data[i].endian, data[i].nail); + printf (" want count %u\n", data[i].want_count); + printf (" got count %u\n", got_count); + printf (" want"); + for (j = 0; j < data[i].want_count*data[i].size; j++) + printf (" 0x%02X,", (unsigned) (unsigned char) data[i].want_data[j]); + printf ("\n"); + printf (" got "); + for (j = 0; j < got_count*data[i].size; j++) + printf (" 0x%02X,", (unsigned) (unsigned char) got_data[j]); + printf ("\n"); + abort (); + } + } + } + mpz_clear (src); +} + + +int +main (void) +{ + tests_start (); + + mp_trace_base = -16; + check_data (); + + tests_end (); + exit (0); +} diff --git a/tests/mpz/t-get_d.c b/tests/mpz/t-get_d.c new file mode 100644 index 000000000..b606c8918 --- /dev/null +++ b/tests/mpz/t-get_d.c @@ -0,0 +1,76 @@ +/* Test mpz_get_d. + +Copyright 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library 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 Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "tests.h" + + +void +check_onebit (void) +{ + int i; + mpz_t z; + double got, want; + /* FIXME: It'd be better to base this on the float format. */ +#ifdef __vax + int limit = 127; /* vax fp numbers have limited range */ +#else + int limit = 512; +#endif + + mpz_init (z); + + mpz_set_ui (z, 1L); + want = 1.0; + + for (i = 0; i < limit; i++) + { + got = mpz_get_d (z); + + if (got != want) + { + printf ("mpz_get_d wrong on 2**%d\n", i); + mpz_trace (" z ", z); + printf (" want %.20g\n", want); + printf (" got %.20g\n", got); + abort(); + } + + mpz_mul_2exp (z, z, 1L); + want *= 2.0; + } + mpz_clear (z); +} + + +int +main (void) +{ + tests_start (); + + check_onebit (); + + tests_end (); + exit (0); +} diff --git a/tests/mpz/t-import.c b/tests/mpz/t-import.c new file mode 100644 index 000000000..2713b32f1 --- /dev/null +++ b/tests/mpz/t-import.c @@ -0,0 +1,147 @@ +/* Test mpz_import. + +Copyright 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library 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 Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "tests.h" + + +void +check_data (void) +{ + static const struct { + const char *want; + size_t count; + int order; + size_t size; + int endian; + int nail; + char src[64]; + + } data[] = { + { "0x123456789ABC", 3,1, 2,1, 0, { 0x12,0x34, 0x56,0x78, 0x9A,0xBC } }, + + { "0", 0,1, 1,1, 0 }, + { "0", 1,1, 0,1, 0 }, + + { "0x12345678", 4,1, 1,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + { "0x12345678", 1,1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + { "0x12345678", 1,-1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, + + { "0x12345678", 4,-1, 1,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + { "0x12345678", 1,1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + { "0x12345678", 1,-1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, + + { "0", 5,1, 1,1, 7, { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE } }, + { "0", 5,-1, 1,1, 7, { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE } }, + { "0x15", 5,1, 1,1, 7, { 0xFF, 0xFE, 0xFF, 0xFE, 0xFF } }, + + { "0", 3,1, 2,1, 1, { 0x80,0x00, 0x80,0x00, 0x80,0x00 } }, + { "0", 3,1, 2,-1, 1, { 0x00,0x80, 0x00,0x80, 0x00,0x80 } }, + { "0", 3,1, 2,1, 15, { 0xFF,0xFE, 0xFF,0xFE, 0xFF,0xFE } }, + + { "0x2A", 3,1, 2,1, 14, { 0xFF,0xFE, 0xFF,0xFE, 0xFF,0xFE } }, + { "0x06", 3,1, 2,1, 14, { 0xFF,0xFC, 0xFF,0xFD, 0xFF,0xFE } }, + { "0x24", 3,-1, 2,1, 14, { 0xFF,0xFC, 0xFF,0xFD, 0xFF,0xFE } }, + + { "0x123456789ABC", 3,1, 2,1, 0, { 0x12,0x34, 0x56,0x78, 0x9A,0xBC } }, + { "0x123456789ABC", 3,-1, 2,1, 0, { 0x9A,0xBC, 0x56,0x78, 0x12,0x34 } }, + { "0x123456789ABC", 3,1, 2,-1, 0, { 0x34,0x12, 0x78,0x56, 0xBC,0x9A } }, + { "0x123456789ABC", 3,-1, 2,-1, 0, { 0xBC,0x9A, 0x78,0x56, 0x34,0x12 } }, + + { "0x112233445566778899AABBCC", 3,1, 4,1, 0, + { 0x11,0x22,0x33,0x44, 0x55,0x66,0x77,0x88, 0x99,0xAA,0xBB,0xCC } }, + { "0x112233445566778899AABBCC", 3,-1, 4,1, 0, + { 0x99,0xAA,0xBB,0xCC, 0x55,0x66,0x77,0x88, 0x11,0x22,0x33,0x44 } }, + { "0x112233445566778899AABBCC", 3,1, 4,-1, 0, + { 0x44,0x33,0x22,0x11, 0x88,0x77,0x66,0x55, 0xCC,0xBB,0xAA,0x99 } }, + { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0, + { 0xCC,0xBB,0xAA,0x99, 0x88,0x77,0x66,0x55, 0x44,0x33,0x22,0x11 } }, + + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0, + { 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04, + 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, + 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0, + { 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C, + 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, + 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04 } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0, + { 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10, + 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, + 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90 } }, + { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0, + { 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90, + 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, + 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10 } }, + }; + + char buf[sizeof(data[0].src) + sizeof (mp_limb_t)]; + char *src; + size_t align; + int i; + mpz_t got, want; + + mpz_init (got); + mpz_init (want); + + for (i = 0; i < numberof (data); i++) + { + for (align = 0; align < sizeof (mp_limb_t); align++) + { + mpz_set_str_or_abort (want, data[i].want, 0); + src = buf + align; + memcpy (src, data[i].src, data[i].count * data[i].size); + + mpz_import (got, data[i].count, data[i].order, + data[i].size, data[i].endian, data[i].nail, src); + + MPZ_CHECK_FORMAT (got); + if (mpz_cmp (got, want) != 0) + { + printf ("wrong at data[%d]\n", i); + printf (" count=%u order=%d size=%u endian=%d nail=%u\n", + data[i].count, data[i].order, + data[i].size, data[i].endian, data[i].nail); + mpz_trace (" got ", got); + mpz_trace (" want", want); + abort (); + } + } + } + mpz_clear (got); + mpz_clear (want); +} + + +int +main (void) +{ + tests_start (); + + mp_trace_base = -16; + check_data (); + + tests_end (); + exit (0); +} |