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

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

1.1       maekawa     1: /* Exercise various mpz 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: oddeven (void)
                     33: {
                     34:   static const struct {
                     35:     const char  *n;
                     36:     int          odd, even;
                     37:   } data[] = {
                     38:     {   "0", 0, 1 },
                     39:     {   "1", 1, 0 },
                     40:     {   "2", 0, 1 },
                     41:     {   "3", 1, 0 },
                     42:     {   "4", 0, 1 },
                     43:
                     44:     {  "-4", 0, 1 },
                     45:     {  "-3", 1, 0 },
                     46:     {  "-2", 0, 1 },
                     47:     {  "-1", 1, 0 },
                     48:
                     49:     {  "0x1000000000000000000000000000000000000000000000000000", 0, 1 },
                     50:     {  "0x1000000000000000000000000000000000000000000000000001", 1, 0 },
                     51:     {  "0x1000000000000000000000000000000000000000000000000002", 0, 1 },
                     52:     {  "0x1000000000000000000000000000000000000000000000000003", 1, 0 },
                     53:
                     54:     { "-0x1000000000000000000000000000000000000000000000000004", 0, 1 },
                     55:     { "-0x1000000000000000000000000000000000000000000000000003", 1, 0 },
                     56:     { "-0x1000000000000000000000000000000000000000000000000002", 0, 1 },
                     57:     { "-0x1000000000000000000000000000000000000000000000000001", 1, 0 },
                     58:   };
                     59:
                     60:   mpz_t  n;
                     61:   int    i;
                     62:
                     63:   mpz_init (n);
                     64:   for (i = 0; i < numberof (data); i++)
                     65:     {
                     66:       mpz_set_str (n, data[i].n, 0);
                     67:
                     68:       if ((mpz_odd_p (n) != 0) != data[i].odd)
                     69:         {
                     70:           printf ("mpz_odd_p wrong on data[%d]\n", i);
                     71:           abort();
                     72:         }
                     73:
                     74:       if ((mpz_even_p (n) != 0) != data[i].even)
                     75:         {
                     76:           printf ("mpz_even_p wrong on data[%d]\n", i);
                     77:           abort();
                     78:         }
                     79:     }
                     80:
                     81:   mpz_clear (n);
                     82: }
                     83:
                     84:
                     85: void
                     86: check_mpz_set_si (void)
                     87: {
                     88:   static const struct {
                     89:     long       n;
                     90:     mp_size_t  want_size;
                     91:     mp_limb_t  want_limb;
                     92:   } data[] = {
                     93:
                     94:     {  0L,  0 },
                     95:     {  1L,  1, 1 },
                     96:     { -1L, -1, 1 },
                     97:
                     98:     {  LONG_MAX,  1, LONG_MAX },
                     99:     { -LONG_MAX, -1, LONG_MAX },
                    100:
                    101:     { LONG_HIGHBIT, -1, ULONG_HIGHBIT },
                    102:   };
                    103:
                    104:   mpz_t  n;
                    105:   int    i;
                    106:
                    107:   mpz_init (n);
                    108:   for (i = 0; i < numberof (data); i++)
                    109:     {
                    110:       mpz_init (n);
                    111:       mpz_set_si (n, data[i].n);
                    112:       if (n->_mp_size != data[i].want_size
                    113:           || (n->_mp_size != 0 && n->_mp_d[0] != data[i].want_limb))
                    114:         {
                    115:           printf ("mpz_set_si wrong on data[%d]\n", i);
                    116:           abort();
                    117:         }
                    118:       mpz_clear (n);
                    119:
                    120:       mpz_init_set_si (n, data[i].n);
                    121:       if (n->_mp_size != data[i].want_size
                    122:           || (n->_mp_size != 0 && n->_mp_d[0] != data[i].want_limb))
                    123:         {
                    124:           printf ("mpz_init_set_si wrong on data[%d]\n", i);
                    125:           abort();
                    126:         }
                    127:       mpz_clear (n);
                    128:     }
                    129: }
                    130:
                    131:
                    132: void
                    133: check_mpz_cmp_si (void)
                    134: {
                    135:   static const struct {
                    136:     const char  *a, *b;
                    137:     int         want;
                    138:   } data[] = {
                    139:     { "0",  "1", -1 },
                    140:     { "0",  "0",  0 },
                    141:     { "0", "-1",  1 },
                    142:
                    143:     { "1",  "1", 0 },
                    144:     { "1",  "0", 1 },
                    145:     { "1", "-1", 1 },
                    146:
                    147:     { "-1",  "1", -1 },
                    148:     { "-1",  "0", -1 },
                    149:     { "-1", "-1", 0 },
                    150:
                    151:     {           "0", "-0x80000000",  1 },
                    152:     {  "0x80000000", "-0x80000000",  1 },
                    153:     {  "0x80000001", "-0x80000000",  1 },
                    154:     { "-0x80000000", "-0x80000000",  0 },
                    155:     { "-0x80000001", "-0x80000000", -1 },
                    156:
                    157:     {                   "0", "-0x8000000000000000",  1 },
                    158:     {  "0x8000000000000000", "-0x8000000000000000",  1 },
                    159:     {  "0x8000000000000001", "-0x8000000000000000",  1 },
                    160:     { "-0x8000000000000000", "-0x8000000000000000",  0 },
                    161:     { "-0x8000000000000001", "-0x8000000000000000", -1 },
                    162:   };
                    163:
                    164:   mpz_t  a, bz;
                    165:   long   b;
                    166:   int    got;
                    167:   int    i;
                    168:
                    169:   mpz_init (a);
                    170:   mpz_init (bz);
                    171:   for (i = 0; i < numberof (data); i++)
                    172:     {
                    173:       mpz_set_str (a, data[i].a, 0);
                    174:       mpz_set_str (bz, data[i].b, 0);
                    175:
                    176:       if (mpz_fits_slong_p (bz))
                    177:         {
                    178:           b = mpz_get_si (bz);
                    179:           got = mpz_cmp_si (a, b);
                    180:           if (SGN (got) != data[i].want)
                    181:             {
                    182:               printf ("mpz_cmp_si wrong on data[%d]\n", i);
                    183:               printf ("  a="); mpz_out_str (stdout, 10, a); printf ("\n");
                    184:               printf ("  b=%ld\n", b);
                    185:               printf ("  got=%d\n", got);
                    186:               printf ("  want=%d\n", data[i].want);
                    187:               abort();
                    188:             }
                    189:         }
                    190:     }
                    191:
                    192:   mpz_clear (a);
                    193:   mpz_clear (bz);
                    194: }
                    195:
                    196:
                    197: int
                    198: main (void)
                    199: {
                    200:   oddeven ();
                    201:   check_mpz_set_si ();
                    202:   check_mpz_cmp_si ();
                    203:
                    204:   exit (0);
                    205: }

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