xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpq/t-get_str.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpq_get_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 <string.h>
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 
27 void
check_one(mpq_srcptr q,int base,const char * want)28 check_one (mpq_srcptr q, int base, const char *want)
29 {
30   char    *str, *ret;
31   size_t  str_alloc;
32 
33   MPQ_CHECK_FORMAT (q);
34   mp_trace_base = base;
35 
36   str_alloc =
37     mpz_sizeinbase (mpq_numref(q), ABS(base)) +
38     mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3;
39 
40   str = mpq_get_str (NULL, base, q);
41   if (strlen(str)+1 > str_alloc)
42     {
43       printf ("mpq_get_str size bigger than should be (passing NULL)\n");
44       printf ("  base %d\n", base);
45       printf ("  got  size %lu \"%s\"\n", (unsigned long)  strlen(str)+1, str);
46       printf ("  want size %lu\n", (unsigned long) str_alloc);
47       abort ();
48     }
49   if (strcmp (str, want) != 0)
50     {
51       printf ("mpq_get_str wrong (passing NULL)\n");
52       printf ("  base %d\n", base);
53       printf ("  got  \"%s\"\n", str);
54       printf ("  want \"%s\"\n", want);
55       mpq_trace ("  q", q);
56       abort ();
57     }
58   (*__gmp_free_func) (str, strlen (str) + 1);
59 
60   str = (char *) (*__gmp_allocate_func) (str_alloc);
61 
62   ret = mpq_get_str (str, base, q);
63   if (str != ret)
64     {
65       printf ("mpq_get_str wrong return value (passing non-NULL)\n");
66       printf ("  base %d\n", base);
67       printf ("  got  %p\n", (void *) ret);
68       printf ("  want %p\n", (void *) str);
69       abort ();
70     }
71   if (strcmp (str, want) != 0)
72     {
73       printf ("mpq_get_str wrong (passing non-NULL)\n");
74       printf ("  base %d\n", base);
75       printf ("  got  \"%s\"\n", str);
76       printf ("  want \"%s\"\n", want);
77       abort ();
78     }
79   (*__gmp_free_func) (str, str_alloc);
80 }
81 
82 
83 void
check_all(mpq_srcptr q,int base,const char * want)84 check_all (mpq_srcptr q, int base, const char *want)
85 {
86   char  *s;
87 
88   check_one (q, base, want);
89 
90   s = __gmp_allocate_strdup (want);
91   strtoupper (s);
92   check_one (q, -base, s);
93   (*__gmp_free_func) (s, strlen(s)+1);
94 }
95 
96 void
check_data(void)97 check_data (void)
98 {
99   static const struct {
100     int         base;
101     const char  *num;
102     const char  *den;
103     const char  *want;
104   } data[] = {
105     { 10, "0", "1", "0" },
106     { 10, "1", "1", "1" },
107 
108     { 16, "ffffffff", "1", "ffffffff" },
109     { 16, "ffffffffffffffff", "1", "ffffffffffffffff" },
110 
111     { 16, "1", "ffffffff", "1/ffffffff" },
112     { 16, "1", "ffffffffffffffff", "1/ffffffffffffffff" },
113     { 16, "1", "10000000000000003", "1/10000000000000003" },
114 
115     { 10, "12345678901234567890", "9876543210987654323",
116       "12345678901234567890/9876543210987654323" },
117   };
118 
119   mpq_t  q;
120   int    i;
121 
122   mpq_init (q);
123   for (i = 0; i < numberof (data); i++)
124     {
125       mpz_set_str_or_abort (mpq_numref(q), data[i].num, data[i].base);
126       mpz_set_str_or_abort (mpq_denref(q), data[i].den, data[i].base);
127       check_all (q, data[i].base, data[i].want);
128     }
129   mpq_clear (q);
130 }
131 
132 
133 int
main(void)134 main (void)
135 {
136   tests_start ();
137 
138   check_data ();
139 
140   tests_end ();
141   exit (0);
142 }
143