summaryrefslogtreecommitdiff
path: root/tests/mpz/t-get_si.c
blob: 08726fc53b56a29c50c267f28ebedc733a8bcf80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Exercise mpz_get_si.

Copyright 2000, 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"


void
check_data (void)
{
  static const struct {
    const char  *n;
    long        want;
  } data[] = {
    { "0",      0L },
    { "1",      1L },
    { "-1",     -1L },
    { "2",      2L },
    { "-2",     -2L },
    { "12345",  12345L },
    { "-12345", -12345L },
  };

  int    i;
  mpz_t  n;
  long   got;

  mpz_init (n);
  for (i = 0; i < numberof (data); i++)
    {
      mpz_set_str_or_abort (n, data[i].n, 0);

      got = mpz_get_si (n);
      if (got != data[i].want)
        {
          printf ("mpz_get_si wrong at data[%d]\n", i); 
          printf ("   n     \"%s\" (", data[i].n);
          mpz_out_str (stdout, 10, n); printf (", hex ");
          mpz_out_str (stdout, 16, n); printf (")\n");
          printf ("   got   %ld (0x%lX)\n", got, got);
          printf ("   want  %ld (0x%lX)\n", data[i].want, data[i].want);
          abort();                                    
        }
    }
  mpz_clear (n);
}


void
check_max (void)
{
  mpz_t  n;
  long   want;
  long   got;

  mpz_init (n);

#define CHECK_MAX(name)                                 \
  if (got != want)                                      \
    {                                                   \
      printf ("mpz_get_si wrong on %s\n", name);        \
      printf ("   n    ");                              \
      mpz_out_str (stdout, 10, n); printf (", hex ");   \
      mpz_out_str (stdout, 16, n); printf ("\n");       \
      printf ("   got  %ld, hex %lX\n", got, got);      \
      printf ("   want %ld, hex %lX\n", want, want);    \
      abort();                                          \
    }

  want = LONG_MAX;
  mpz_set_si (n, want);
  got = mpz_get_si (n);
  CHECK_MAX ("LONG_MAX");

  want = LONG_MIN;
  mpz_set_si (n, want);
  got = mpz_get_si (n);
  CHECK_MAX ("LONG_MIN");

  /* The following checks that -0x100000000 gives -0x80000000.  This doesn't
     actually fit in a long and the result from mpz_get_si() is undefined,
     but -0x80000000 is what comes out currently, and it should be that
     value irrespective of the mp_limb_t size (long or long long).  */

  want = LONG_MIN;
  mpz_mul_2exp (n, n, 1);
  CHECK_MAX ("-0x100...00");

  mpz_clear (n);
}


int
main (void)
{
  tests_start ();

  check_data ();
  check_max ();

  tests_end ();
  exit (0);
}