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

Annotation of OpenXM_contrib/gmp/mpf/tests/t-misc.c, Revision 1.1.1.1

1.1       maekawa     1: /* Exercise various mpf functions. */
                      2:
                      3: /*
                      4: Copyright (C) 2000 Free Software Foundation, Inc.
                      5:
                      6: This file is part of the GNU MP Library.
                      7:
                      8: The GNU MP Library is free software; you can redistribute it and/or modify
                      9: it under the terms of the GNU Lesser General Public License as published by
                     10: the Free Software Foundation; either version 2.1 of the License, or (at your
                     11: option) any later version.
                     12:
                     13: The GNU MP Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Lesser General Public License
                     19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA.
                     22: */
                     23:
                     24: #include <stdio.h>
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27:
                     28: #define SGN(x)       ((x) < 0 ? -1 : (x) == 0 ? 0 : 1)
                     29:
                     30:
                     31: void
                     32: check_mpf_set_si (void)
                     33: {
                     34:   static const struct {
                     35:     long       x;
                     36:     mp_size_t  want_size;
                     37:     mp_limb_t  want_limb;
                     38:   } data[] = {
                     39:
                     40:     {  0L,  0 },
                     41:     {  1L,  1, 1 },
                     42:     { -1L, -1, 1 },
                     43:
                     44:     {  LONG_MAX,  1, LONG_MAX },
                     45:     { -LONG_MAX, -1, LONG_MAX },
                     46:
                     47:     { LONG_HIGHBIT, -1, ULONG_HIGHBIT },
                     48:   };
                     49:
                     50:   mpf_t  x;
                     51:   int    i;
                     52:
                     53:   mpf_init (x);
                     54:   for (i = 0; i < numberof (data); i++)
                     55:     {
                     56:       mpf_init (x);
                     57:       mpf_set_si (x, data[i].x);
                     58:       if (x->_mp_size != data[i].want_size
                     59:           || (x->_mp_size != 0 && x->_mp_d[0] != data[i].want_limb)
                     60:           || (x->_mp_exp != (data[i].x != 0)))
                     61:         {
                     62:           printf ("mpf_set_si wrong on data[%d]\n", i);
                     63:           abort();
                     64:         }
                     65:       mpf_clear (x);
                     66:
                     67:       mpf_init_set_si (x, data[i].x);
                     68:       if (x->_mp_size != data[i].want_size
                     69:           || (x->_mp_size != 0 && x->_mp_d[0] != data[i].want_limb)
                     70:           || (x->_mp_exp != (data[i].x != 0)))
                     71:         {
                     72:           printf ("mpf_init_set_si wrong on data[%d]\n", i);
                     73:           abort();
                     74:         }
                     75:       mpf_clear (x);
                     76:     }
                     77: }
                     78:
                     79:
                     80: void
                     81: check_mpf_cmp_si (void)
                     82: {
                     83:   static const struct {
                     84:     int         a_base;
                     85:     const char  *a;
                     86:     const char  *b;
                     87:     int         want;
                     88:   } data[] = {
                     89:     { 10, "0",  "1", -1 },
                     90:     { 10, "0",  "0",  0 },
                     91:     { 10, "0", "-1",  1 },
                     92:
                     93:     { 10, "1",  "1", 0 },
                     94:     { 10, "1",  "0", 1 },
                     95:     { 10, "1", "-1", 1 },
                     96:
                     97:     { 10, "-1",  "1", -1 },
                     98:     { 10, "-1",  "0", -1 },
                     99:     { 10, "-1", "-1", 0 },
                    100:
                    101:     { 16,         "0", "-0x80000000",  1 },
                    102:     { 16,  "80000000", "-0x80000000",  1 },
                    103:     { 16,  "80000001", "-0x80000000",  1 },
                    104:     { 16, "-80000000", "-0x80000000",  0 },
                    105:     { 16, "-80000001", "-0x80000000", -1 },
                    106:
                    107:     { 16,                 "0", "-0x8000000000000000",  1 },
                    108:     { 16,  "8000000000000000", "-0x8000000000000000",  1 },
                    109:     { 16,  "8000000000000001", "-0x8000000000000000",  1 },
                    110:     { 16, "-8000000000000000", "-0x8000000000000000",  0 },
                    111:     { 16, "-8000000000000001", "-0x8000000000000000", -1 },
                    112:   };
                    113:
                    114:   mpf_t  a;
                    115:   mpz_t  bz;
                    116:   long   b;
                    117:   int    got;
                    118:   int    i;
                    119:
                    120:   mpf_init (a);
                    121:   mpz_init (bz);
                    122:   for (i = 0; i < numberof (data); i++)
                    123:     {
                    124:       mpf_set_str (a, data[i].a, data[i].a_base);
                    125:       mpz_set_str (bz, data[i].b, 0);
                    126:
                    127:       if (mpz_fits_slong_p (bz))
                    128:         {
                    129:           b = mpz_get_si (bz);
                    130:           got = mpf_cmp_si (a, b);
                    131:           if (SGN (got) != data[i].want)
                    132:             {
                    133:               printf ("mpf_cmp_si wrong on data[%d]\n", i);
                    134:               printf ("  a="); mpf_out_str (stdout, 10, 0, a);
                    135:               printf (" (%s)\n", data[i].a);
                    136:               printf ("  b=%ld (%s)\n", b, data[i].b);
                    137:               printf ("  got=%d\n", got);
                    138:               printf ("  want=%d\n", data[i].want);
                    139:               abort();
                    140:             }
                    141:         }
                    142:     }
                    143:
                    144:   mpf_clear (a);
                    145:   mpz_clear (bz);
                    146: }
                    147:
                    148:
                    149: int
                    150: main (void)
                    151: {
                    152:   check_mpf_set_si ();
                    153:   check_mpf_cmp_si ();
                    154:   exit (0);
                    155: }

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