xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpq/t-set_str.c (revision 4b004442778f1201b2161e87fd65ba87aae6601a)
1 /* Test mpq_set_str.
2 
3 Copyright 2001 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
27 check_one (mpq_srcptr want, int base, const char *str)
28 {
29   mpq_t   got;
30 
31   MPQ_CHECK_FORMAT (want);
32   mp_trace_base = base;
33 
34   mpq_init (got);
35 
36   if (mpq_set_str (got, str, base) != 0)
37     {
38       printf ("mpq_set_str unexpectedly failed\n");
39       printf ("  base %d\n", base);
40       printf ("  str  \"%s\"\n", str);
41       abort ();
42     }
43   MPQ_CHECK_FORMAT (got);
44 
45   if (! mpq_equal (got, want))
46     {
47       printf ("mpq_set_str wrong\n");
48       printf ("  base %d\n", base);
49       printf ("  str  \"%s\"\n", str);
50       mpq_trace ("got ", got);
51       mpq_trace ("want", want);
52       abort ();
53     }
54 
55   mpq_clear (got);
56 }
57 
58 void
59 check_samples (void)
60 {
61   mpq_t  q;
62 
63   mpq_init (q);
64 
65   mpq_set_ui (q, 0L, 1L);
66   check_one (q, 10, "0");
67   check_one (q, 10, "0/1");
68   check_one (q, 10, "0  / 1");
69   check_one (q, 0, "0x0/ 1");
70   check_one (q, 0, "0x0/ 0x1");
71   check_one (q, 0, "0 / 0x1");
72 
73   check_one (q, 10, "-0");
74   check_one (q, 10, "-0/1");
75   check_one (q, 10, "-0  / 1");
76   check_one (q, 0, "-0x0/ 1");
77   check_one (q, 0, "-0x0/ 0x1");
78   check_one (q, 0, "-0 / 0x1");
79 
80   mpq_set_ui (q, 255L, 256L);
81   check_one (q, 10, "255/256");
82   check_one (q, 0,  "0xFF/0x100");
83   check_one (q, 16, "FF/100");
84 
85   mpq_neg (q, q);
86   check_one (q, 10, "-255/256");
87   check_one (q, 0,  "-0xFF/0x100");
88   check_one (q, 16, "-FF/100");
89 
90   mpq_clear (q);
91 }
92 
93 int
94 main (void)
95 {
96   tests_start ();
97 
98   check_samples ();
99 
100   tests_end ();
101   exit (0);
102 }
103