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