1 /* Test mpq_cmp_si.
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 <limits.h>
23 
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "tests.h"
27 
28 
29 #define SGN(x)   ((x)<0 ? -1 : (x) != 0)
30 
31 void
check_data(void)32 check_data (void)
33 {
34   static const struct {
35     const char     *q;
36     long           n;
37     unsigned long  d;
38     int            want;
39   } data[] = {
40     { "0", 0, 1, 0 },
41     { "0", 0, 123, 0 },
42     { "0", 0, ULONG_MAX, 0 },
43     { "1", 0, 1, 1 },
44     { "1", 0, 123, 1 },
45     { "1", 0, ULONG_MAX, 1 },
46     { "-1", 0, 1, -1 },
47     { "-1", 0, 123, -1 },
48     { "-1", 0, ULONG_MAX, -1 },
49 
50     { "123", 123, 1, 0 },
51     { "124", 123, 1, 1 },
52     { "122", 123, 1, -1 },
53 
54     { "-123", 123, 1, -1 },
55     { "-124", 123, 1, -1 },
56     { "-122", 123, 1, -1 },
57 
58     { "123", -123, 1, 1 },
59     { "124", -123, 1, 1 },
60     { "122", -123, 1, 1 },
61 
62     { "-123", -123, 1, 0 },
63     { "-124", -123, 1, -1 },
64     { "-122", -123, 1, 1 },
65 
66     { "5/7", 3,4, -1 },
67     { "5/7", -3,4, 1 },
68     { "-5/7", 3,4, -1 },
69     { "-5/7", -3,4, 1 },
70   };
71 
72   mpq_t  q;
73   int    i, got;
74 
75   mpq_init (q);
76 
77   for (i = 0; i < numberof (data); i++)
78     {
79       mpq_set_str_or_abort (q, data[i].q, 0);
80       MPQ_CHECK_FORMAT (q);
81 
82       got = mpq_cmp_si (q, data[i].n, data[i].d);
83       if (SGN(got) != data[i].want)
84         {
85           printf ("mpq_cmp_si wrong\n");
86         error:
87           mpq_trace ("  q", q);
88           printf ("  n=%ld\n", data[i].n);
89           printf ("  d=%lu\n", data[i].d);
90           printf ("  got=%d\n", got);
91           printf ("  want=%d\n", data[i].want);
92           abort ();
93         }
94 
95       if (data[i].n == 0)
96         {
97           got = mpq_cmp_si (q, 0L, data[i].d);
98           if (SGN(got) != data[i].want)
99             {
100               printf ("mpq_cmp_si wrong\n");
101               goto error;
102             }
103         }
104     }
105 
106   mpq_clear (q);
107 }
108 
109 int
main(int argc,char ** argv)110 main (int argc, char **argv)
111 {
112   tests_start ();
113 
114   check_data ();
115 
116   tests_end ();
117   exit (0);
118 }
119