source: trunk/third/gmp/tests/mpz/t-get_si.c @ 18191

Revision 18191, 3.2 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Exercise mpz_get_si.
2
3Copyright 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 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
29void
30check_data (void)
31{
32  static const struct {
33    const char  *n;
34    long        want;
35  } data[] = {
36    { "0",      0L },
37    { "1",      1L },
38    { "-1",     -1L },
39    { "2",      2L },
40    { "-2",     -2L },
41    { "12345",  12345L },
42    { "-12345", -12345L },
43  };
44
45  int    i;
46  mpz_t  n;
47  long   got;
48
49  mpz_init (n);
50  for (i = 0; i < numberof (data); i++)
51    {
52      mpz_set_str_or_abort (n, data[i].n, 0);
53
54      got = mpz_get_si (n);
55      if (got != data[i].want)
56        {
57          printf ("mpz_get_si wrong at data[%d]\n", i);
58          printf ("   n     \"%s\" (", data[i].n);
59          mpz_out_str (stdout, 10, n); printf (", hex ");
60          mpz_out_str (stdout, 16, n); printf (")\n");
61          printf ("   got   %ld (0x%lX)\n", got, got);
62          printf ("   want  %ld (0x%lX)\n", data[i].want, data[i].want);
63          abort();                                   
64        }
65    }
66  mpz_clear (n);
67}
68
69
70void
71check_max (void)
72{
73  mpz_t  n;
74  long   want;
75  long   got;
76
77  mpz_init (n);
78
79#define CHECK_MAX(name)                                 \
80  if (got != want)                                      \
81    {                                                   \
82      printf ("mpz_get_si wrong on %s\n", name);        \
83      printf ("   n    ");                              \
84      mpz_out_str (stdout, 10, n); printf (", hex ");   \
85      mpz_out_str (stdout, 16, n); printf ("\n");       \
86      printf ("   got  %ld, hex %lX\n", got, got);      \
87      printf ("   want %ld, hex %lX\n", want, want);    \
88      abort();                                          \
89    }
90
91  want = LONG_MAX;
92  mpz_set_si (n, want);
93  got = mpz_get_si (n);
94  CHECK_MAX ("LONG_MAX");
95
96  want = LONG_MIN;
97  mpz_set_si (n, want);
98  got = mpz_get_si (n);
99  CHECK_MAX ("LONG_MIN");
100
101  /* The following checks that -0x100000000 gives -0x80000000.  This doesn't
102     actually fit in a long and the result from mpz_get_si() is undefined,
103     but -0x80000000 is what comes out currently, and it should be that
104     value irrespective of the mp_limb_t size (long or long long).  */
105
106  want = LONG_MIN;
107  mpz_mul_2exp (n, n, 1);
108  CHECK_MAX ("-0x100...00");
109
110  mpz_clear (n);
111}
112
113
114int
115main (void)
116{
117  tests_start ();
118
119  check_data ();
120  check_max ();
121
122  tests_end ();
123  exit (0);
124}
Note: See TracBrowser for help on using the repository browser.