Upload Tizen:Base source
[external/gmp.git] / tests / mpq / t-cmp_si.c
1 /* Test mpq_cmp_si.
2
3 Copyright 2001 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://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
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
110 main (int argc, char **argv)
111 {
112   tests_start ();
113
114   check_data ();
115
116   tests_end ();
117   exit (0);
118 }