1 /*
2     Copyright (C) 2009 William Hart
3     Copyright (C) 2011 Sebastian Pancratz
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
11 */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <limits.h>
16 #include <gmp.h>
17 #include "flint.h"
18 #include "ulong_extras.h"
19 #include "fmpz.h"
20 
check(fmpz_t x,int expected)21 static void check(fmpz_t x, int expected)
22 {
23     if (fmpz_fits_si(x) != expected)
24     {
25         flint_printf("FAIL:\n\n");
26         flint_printf("x = "), fmpz_print(x), flint_printf("\n");
27         flint_printf("fmpz_fits_si(x) = %d\n", fmpz_fits_si(x));
28         flint_printf("WORD_MIN = %wd\n", WORD_MIN);
29         abort();
30     }
31 }
32 
33 int
main(void)34 main(void)
35 {
36     slong i;
37     fmpz_t x;
38 
39     FLINT_TEST_INIT(state);
40 
41     flint_printf("fits_si....");
42     fflush(stdout);
43 
44     fmpz_init(x);
45 
46     fmpz_set_si(x, COEFF_MIN);
47     check(x, 1);
48 
49     fmpz_set_si(x, COEFF_MAX);
50     check(x, 1);
51 
52     fmpz_set_si(x, WORD_MAX);
53     check(x, 1);
54 
55     fmpz_set_si(x, WORD_MIN);
56     check(x, 1);
57 
58     fmpz_set_ui(x, UWORD_MAX);
59     check(x, 0);
60 
61     fmpz_set_ui(x, UWORD_MAX);
62     fmpz_neg(x, x);
63     check(x, 0);
64 
65     fmpz_set_si(x, WORD_MAX);
66     fmpz_add_ui(x, x, 1);
67     check(x, 0);
68 
69     fmpz_set_si(x, WORD_MIN);
70     fmpz_sub_ui(x, x, 1);
71     check(x, 0);
72 
73     for (i = 0; i < 1000; i++)
74     {
75         fmpz_set_ui(x, 1);
76         fmpz_mul_2exp(x, x, i);
77         check(x, i < FLINT_BITS - 1);
78         fmpz_neg(x, x);
79         check(x, i < FLINT_BITS);  /* WORD_MIN fits */
80     }
81 
82     fmpz_clear(x);
83 
84     FLINT_TEST_CLEANUP(state);
85     flint_printf("PASS\n");
86     return EXIT_SUCCESS;
87 }
88