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

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

1.1       ohara       1: /* Exercise mpz_get_si.
                      2:
                      3: Copyright 2000, 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 "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "tests.h"
                     27:
                     28:
                     29: void
                     30: check_data (void)
                     31: {
                     32:   static const struct {
                     33:     int         base;
                     34:     const char  *f;
                     35:     long        want;
                     36:   } data[] = {
                     37:     { 10, "0",      0L },
                     38:     { 10, "1",      1L },
                     39:     { 10, "-1",     -1L },
                     40:     { 10, "2",      2L },
                     41:     { 10, "-2",     -2L },
                     42:     { 10, "12345",  12345L },
                     43:     { 10, "-12345", -12345L },
                     44:
                     45:     /* fraction bits ignored */
                     46:     { 10, "0.5",    0L },
                     47:     { 10, "-0.5",   0L },
                     48:     { 10, "1.1",    1L },
                     49:     { 10, "-1.1",   -1L },
                     50:     { 10, "1.9",    1L },
                     51:     { 10, "-1.9",   -1L },
                     52:     { 16, "1.000000000000000000000000000000000000000000000000001", 1L },
                     53:     { 16, "-1.000000000000000000000000000000000000000000000000001", -1L },
                     54:
                     55:     /* low bits extracted (this is undocumented) */
                     56:     { 16, "1000000000000000000000000000000000000000000000000001", 1L },
                     57:     { 16, "-1000000000000000000000000000000000000000000000000001", -1L },
                     58:   };
                     59:
                     60:   int    i;
                     61:   mpf_t  f;
                     62:   long   got;
                     63:
                     64:   mpf_init2 (f, 2000L);
                     65:   for (i = 0; i < numberof (data); i++)
                     66:     {
                     67:       mpf_set_str_or_abort (f, data[i].f, data[i].base);
                     68:
                     69:       got = mpf_get_si (f);
                     70:       if (got != data[i].want)
                     71:         {
                     72:           printf ("mpf_get_si wrong at data[%d]\n", i);
                     73:           printf ("   f     \"%s\"\n", data[i].f);
                     74:           printf ("     dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n");
                     75:           printf ("     hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n");
                     76:           printf ("   got   %ld (0x%lX)\n", got, got);
                     77:           printf ("   want  %ld (0x%lX)\n", data[i].want, data[i].want);
                     78:           abort();
                     79:         }
                     80:     }
                     81:   mpf_clear (f);
                     82: }
                     83:
                     84:
                     85: void
                     86: check_max (void)
                     87: {
                     88:   mpf_t  f;
                     89:   long   want;
                     90:   long   got;
                     91:
                     92:   mpf_init2 (f, 200L);
                     93:
                     94: #define CHECK_MAX(name)                                         \
                     95:   if (got != want)                                              \
                     96:     {                                                           \
                     97:       printf ("mpf_get_si wrong on %s\n", name);                \
                     98:       printf ("   f    ");                                      \
                     99:       mpf_out_str (stdout, 10, 0, f); printf (", hex ");        \
                    100:       mpf_out_str (stdout, 16, 0, f); printf ("\n");            \
                    101:       printf ("   got  %ld, hex %lX\n", got, got);              \
                    102:       printf ("   want %ld, hex %lX\n", want, want);            \
                    103:       abort();                                                  \
                    104:     }
                    105:
                    106:   want = LONG_MAX;
                    107:   mpf_set_si (f, want);
                    108:   got = mpf_get_si (f);
                    109:   CHECK_MAX ("LONG_MAX");
                    110:
                    111:   want = LONG_MIN;
                    112:   mpf_set_si (f, want);
                    113:   got = mpf_get_si (f);
                    114:   CHECK_MAX ("LONG_MIN");
                    115:
                    116:   mpf_clear (f);
                    117: }
                    118:
                    119:
                    120: int
                    121: main (void)
                    122: {
                    123:   tests_start ();
                    124:   check_data ();
                    125:   check_max ();
                    126:   tests_end ();
                    127:   exit (0);
                    128: }

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