[BACK]Return to t-get_str.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpq

Annotation of OpenXM_contrib/gmp/tests/mpq/t-get_str.c, Revision 1.1.1.1

1.1       ohara       1: /* Test mpq_get_str.
                      2:
                      3: Copyright 2001 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "tests.h"
                     28:
                     29:
                     30: void
                     31: check_one (mpq_srcptr q, int base, const char *want)
                     32: {
                     33:   char    *str, *ret;
                     34:   size_t  str_alloc;
                     35:
                     36:   MPQ_CHECK_FORMAT (q);
                     37:   mp_trace_base = base;
                     38:
                     39:   str_alloc =
                     40:     mpz_sizeinbase (mpq_numref(q), ABS(base)) +
                     41:     mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3;
                     42:
                     43:   str = mpq_get_str (NULL, base, q);
                     44:   if (strlen(str)+1 > str_alloc)
                     45:     {
                     46:       printf ("mpq_get_str size bigger than should be (passing NULL)\n");
                     47:       printf ("  base %d\n", base);
                     48:       printf ("  got  size %u \"%s\"\n", strlen(str)+1, str);
                     49:       printf ("  want size %u\n", str_alloc);
                     50:       abort ();
                     51:     }
                     52:   if (strcmp (str, want) != 0)
                     53:     {
                     54:       printf ("mpq_get_str wrong (passing NULL)\n");
                     55:       printf ("  base %d\n", base);
                     56:       printf ("  got  \"%s\"\n", str);
                     57:       printf ("  want \"%s\"\n", want);
                     58:       mpq_trace ("  q", q);
                     59:       abort ();
                     60:     }
                     61:   (*__gmp_free_func) (str, strlen (str) + 1);
                     62:
                     63:   str = (char *) (*__gmp_allocate_func) (str_alloc);
                     64:
                     65:   ret = mpq_get_str (str, base, q);
                     66:   if (str != ret)
                     67:     {
                     68:       printf ("mpq_get_str wrong return value (passing non-NULL)\n");
                     69:       printf ("  base %d\n", base);
                     70:       printf ("  got  0x%lX\n", (unsigned long) ret);
                     71:       printf ("  want 0x%lX\n", (unsigned long) want);
                     72:       abort ();
                     73:     }
                     74:   if (strcmp (str, want) != 0)
                     75:     {
                     76:       printf ("mpq_get_str wrong (passing non-NULL)\n");
                     77:       printf ("  base %d\n", base);
                     78:       printf ("  got  \"%s\"\n", str);
                     79:       printf ("  want \"%s\"\n", want);
                     80:       abort ();
                     81:     }
                     82:   (*__gmp_free_func) (str, str_alloc);
                     83: }
                     84:
                     85:
                     86: void
                     87: check_all (mpq_srcptr q, int base, const char *want)
                     88: {
                     89:   char  *s;
                     90:
                     91:   check_one (q, base, want);
                     92:
                     93:   s = __gmp_allocate_strdup (want);
                     94:   strtoupper (s);
                     95:   check_one (q, -base, s);
                     96:   (*__gmp_free_func) (s, strlen(s)+1);
                     97: }
                     98:
                     99: void
                    100: check_data (void)
                    101: {
                    102:   static const struct {
                    103:     int         base;
                    104:     const char  *num;
                    105:     const char  *den;
                    106:     const char  *want;
                    107:   } data[] = {
                    108:     { 10, "0", "1", "0" },
                    109:     { 10, "1", "1", "1" },
                    110:
                    111:     { 16, "ffffffff", "1", "ffffffff" },
                    112:     { 16, "ffffffffffffffff", "1", "ffffffffffffffff" },
                    113:
                    114:     { 16, "1", "ffffffff", "1/ffffffff" },
                    115:     { 16, "1", "ffffffffffffffff", "1/ffffffffffffffff" },
                    116:     { 16, "1", "10000000000000003", "1/10000000000000003" },
                    117:
                    118:     { 10, "12345678901234567890", "9876543210987654323",
                    119:       "12345678901234567890/9876543210987654323" },
                    120:   };
                    121:
                    122:   mpq_t  q;
                    123:   int    i;
                    124:
                    125:   mpq_init (q);
                    126:   for (i = 0; i < numberof (data); i++)
                    127:     {
                    128:       mpz_set_str_or_abort (mpq_numref(q), data[i].num, data[i].base);
                    129:       mpz_set_str_or_abort (mpq_denref(q), data[i].den, data[i].base);
                    130:       check_all (q, data[i].base, data[i].want);
                    131:     }
                    132:   mpq_clear (q);
                    133: }
                    134:
                    135:
                    136: int
                    137: main (void)
                    138: {
                    139:   tests_start ();
                    140:
                    141:   check_data ();
                    142:
                    143:   tests_end ();
                    144:   exit (0);
                    145: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>