xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpf/t-get_ui.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Exercise mpf_get_ui.
2 
3 Copyright 2004 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 
26 void
check_limbdata(void)27 check_limbdata (void)
28 {
29 #define M  GMP_NUMB_MAX
30 
31   static const struct {
32     mp_exp_t       exp;
33     mp_size_t      size;
34     mp_limb_t      d[10];
35     unsigned long  want;
36 
37   } data[] = {
38 
39     /* in the comments here, a "_" indicates a digit (ie. limb) position not
40        included in the d data, and therefore zero */
41 
42     { 0, 0, { 0 }, 0L },    /* 0 */
43 
44     { 1,  1, { 1 }, 1L },   /* 1 */
45     { 1, -1, { 1 }, 1L },   /* -1 */
46 
47     { 0,  1, { 1 }, 0L },   /* .1 */
48     { 0, -1, { 1 }, 0L },   /* -.1 */
49 
50     { -1,  1, { 1 }, 0L },  /* ._1 */
51     { -1, -1, { 1 }, 0L },  /* -._1 */
52 
53     { -999,          1, { 1 }, 0L },   /* .___1 small */
54     { MP_EXP_T_MIN,  1, { 1 }, 0L },   /* .____1 very small */
55 
56     { 999,          1, { 1 }, 0L },    /* 1____. big */
57     { MP_EXP_T_MAX, 1, { 1 }, 0L },    /* 1_____. very big */
58 
59     { 1, 2, { 999, 2 }, 2L },                  /* 2.9 */
60     { 5, 8, { 7, 8, 9, 3, 0, 0, 0, 1 }, 3L },  /* 10003.987 */
61 
62     { 2, 2, { M, M },    ULONG_MAX }, /* FF. */
63     { 2, 2, { M, M, M }, ULONG_MAX }, /* FF.F */
64     { 3, 3, { M, M, M }, ULONG_MAX }, /* FFF. */
65 
66 #if GMP_NUMB_BITS >= BITS_PER_ULONG
67     /* normal case, numb bigger than long */
68     { 2,  1, { 1 },    0L },      /* 1_. */
69     { 2,  2, { 0, 1 }, 0L },      /* 10. */
70     { 2,  2, { 999, 1 }, 999L },  /* 19. */
71     { 3,  2, { 999, 1 }, 0L },    /* 19_. */
72 
73 #else
74     /* nails case, numb smaller than long */
75     { 2,  1, { 1 }, 1L << GMP_NUMB_BITS },  /* 1_. */
76     { 3,  1, { 1 }, 0L },                   /* 1__. */
77 
78     { 2,  2, { 99, 1 },    99L + (1L << GMP_NUMB_BITS) },  /* 19. */
79     { 3,  2, { 1, 99 },    1L << GMP_NUMB_BITS },          /* 91_. */
80     { 3,  3, { 0, 1, 99 }, 1L << GMP_NUMB_BITS },          /* 910. */
81 
82 #endif
83   };
84 
85   mpf_t          f;
86   unsigned long  got;
87   int            i;
88   mp_limb_t      buf[20 + numberof(data[i].d)];
89 
90   for (i = 0; i < numberof (data); i++)
91     {
92       refmpn_fill (buf, 10, CNST_LIMB(0xDEADBEEF));
93       refmpn_copy (buf+10, data[i].d, ABS(data[i].size));
94       refmpn_fill (buf+10+ABS(data[i].size), 10, CNST_LIMB(0xDEADBEEF));
95 
96       PTR(f) = buf+10;
97       EXP(f) = data[i].exp;
98       SIZ(f) = data[i].size;
99       PREC(f) = numberof (data[i].d);
100       MPF_CHECK_FORMAT (f);
101 
102       got = mpf_get_ui (f);
103       if (got != data[i].want)
104 	{
105 	  printf    ("mpf_get_ui wrong at limb data[%d]\n", i);
106 	  mpf_trace ("  f", f);
107 	  mpn_trace ("  d", data[i].d, data[i].size);
108 	  printf    ("  size %ld\n", (long) data[i].size);
109 	  printf    ("  exp %ld\n", (long) data[i].exp);
110 	  printf    ("  got   %lu (0x%lX)\n", got, got);
111 	  printf    ("  want  %lu (0x%lX)\n", data[i].want, data[i].want);
112 	  abort();
113 	}
114     }
115 }
116 
117 int
main(void)118 main (void)
119 {
120   tests_start ();
121   mp_trace_base = 16;
122 
123   check_limbdata ();
124 
125   tests_end ();
126   exit (0);
127 }
128