summaryrefslogtreecommitdiff
path: root/gmp/mini-gmp/tests/t-signed.c
blob: e2502d6e9fd4cdad0947cee59d7d01d6073b8766 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Exercise some mpz_..._si functions.

Copyright 2013 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

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

The GNU MP Library test suite 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 General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <stdio.h>
#include <stdlib.h>

#include "testutils.h"

int
check_si (mpz_t sz, mpz_t oz, long si, long oi, int c)
{
  mpz_t t;
  int fail;

  if (mpz_cmp_si (sz, oi) != c)
    {
      printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c);
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      abort ();
    }

  if ((si < oi ? -1 : si > oi) != c)
    return 1;

  mpz_init_set_si (t, si);

  if ((fail = mpz_cmp_si (sz, si)) != 0)
    printf ("mpz_cmp_si (sz, %ld) != 0.\n", si);
  if (mpz_cmp_si (oz, si) != -c)
    printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1;
  if (! mpz_fits_slong_p (sz))
    printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1;
  if (mpz_get_si (sz) != si)
    printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1;
  if (mpz_cmp (t, sz) != 0)
    {
      printf ("mpz_init_set_si (%ld) failed.\n", si);
      printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n");
      fail = 1;
    }

  mpz_clear (t);

  if (fail)
    {
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      printf (" si=%ld\n", si);
      abort ();
    }

  return 0;
}

void
try_op_si (int c)
{
  long  si, oi;
  mpz_t sz, oz;

  si = c;
  mpz_init_set_si (sz, si);

  oi = si;
  mpz_init_set (oz, sz);

  do {
    si *= 2; /* c * 2^k */
    mpz_mul_2exp (sz, sz, 1);

    if (check_si (sz, oz, si, oi, c))
      {
	mpz_set (oz, sz);
	break;
      }

    oi = si + c; /* c * (2^k + 1) */
    if (c == -1)
      mpz_sub_ui (oz, sz, 1);
    else
      mpz_add_ui (oz, sz, 1);

    if (check_si (oz, sz, oi, si, c))
      break;

    oi = (si - c) * 2 + c; /* c * (2^K - 1) */
    mpz_mul_si (oz, sz, 2*c);
    if (c == -1)
      mpz_ui_sub (oz, 1, oz); /* oz = sz * 2 + 1 */
    else
      mpz_sub_ui (oz, oz, 1); /* oz = sz * 2 - 1 */
  } while (check_si (oz, sz, oi, si, c) == 0);

  mpz_clear (sz);

  if (mpz_fits_slong_p (oz))
    {
      printf ("Should not fit a signed long any more.\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      abort ();
    }

  if (mpz_cmp_si (oz, -c) != c)
      {
	printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c);
	printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
	abort ();
      }

  mpz_mul_2exp (oz, oz, 1);
  if (mpz_cmp_si (oz, -c) != c)
      {
	printf ("mpz_cmp_si (oz, %i) != %i.\n", c, c);
	printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
	abort ();
      }

  mpz_clear (oz);
}

void
testmain (int argc, char *argv[])
{
  try_op_si (-1);
  try_op_si (1);
}