Add missing file
[gcalctool.git] / src / test-mp.c
blob569d5cb2bf8af351bca99fe8cf22c8a786930ff1
1 /* Copyright (c) 2008-2009 Robert Ancell
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 * 02111-1307, USA.
19 #include <glib-object.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <locale.h>
25 #include "mp.h"
26 #include "mp-private.h"
28 static int fails = 0;
29 static int passes = 0;
31 /* If we're not using GNU C, elide __attribute__ */
32 #ifndef __GNUC__
33 # define __attribute__(x) /*NOTHING*/
34 #endif
36 static void pass(const char *format, ...) __attribute__((format(printf, 1, 2)));
37 static void fail(const char *format, ...) __attribute__((format(printf, 1, 2)));
40 static void pass(const char *format, ...)
42 /* va_list args;
44 printf(" PASS: ");
45 va_start(args, format);
46 vprintf(format, args);
47 va_end(args);
48 printf("\n");
50 passes += 1;
53 static void fail(const char *format, ...)
55 va_list args;
57 printf("*FAIL: ");
58 va_start(args, format);
59 vprintf(format, args);
60 va_end(args);
61 printf("\n");
62 fails++;
66 static void
67 print_number(MPNumber *x)
69 int i, j;
71 printf("sign=%d exponent=%d fraction=%d", x->sign, x->exponent, x->fraction[0]);
72 for (i = 1; i < MP_SIZE; i++) {
73 for (j = i; j < MP_SIZE && x->fraction[j] == 0; j++);
74 if (j == MP_SIZE) {
75 printf(",...");
76 break;
78 printf(",%d", x->fraction[i]);
82 static void
83 test_string(const char *number)
85 MPNumber t;
87 mp_set_from_string(number, 10, &t);
89 printf("MPNumber(%s) -> {", number);
90 print_number(&t);
91 printf("}\n");
94 static void
95 test_integer(int number)
97 MPNumber t;
99 mp_set_from_integer(number, &t);
101 printf("MPNumber(%d) -> {", number);
102 print_number(&t);
103 printf("}\n");
107 static void
108 test_numbers()
110 printf("base=%d\n", MP_BASE);
111 test_integer(0);
112 test_integer(1);
113 test_integer(-1);
114 test_integer(2);
115 test_integer(9999);
116 test_integer(10000);
117 test_integer(10001);
118 test_integer(2147483647);
120 test_string("0");
121 test_string("1");
122 test_string("-1");
123 test_string("16383");
124 test_string("16384");
125 test_string("16385");
126 test_string("268435456");
128 test_string("0.1");
129 test_string("0.5");
130 test_string("0.25");
131 test_string("0.125");
132 test_string("0.0625");
133 test_string("0.00006103515625");
134 test_string("0.000030517578125");
136 test_string("1.00006103515625");
137 test_string("16384.00006103515625");
141 static void
142 try(const char *string, bool result, bool expected)
144 if ((result && !expected) || (!result && expected))
145 fail("%s -> %s, expected %s", string, expected ? "true" : "false", result ? "true" : "false");
146 else
147 pass("%s -> %s", string, result ? "true" : "false");
151 static void
152 test_mp()
154 MPNumber zero, one, minus_one;
156 mp_set_from_integer(0, &zero);
157 mp_set_from_integer(1, &one);
158 mp_set_from_integer(-1, &minus_one);
160 try("mp_is_zero(-1)", mp_is_zero(&minus_one), false);
161 try("mp_is_zero(0)", mp_is_zero(&zero), true);
162 try("mp_is_zero(1)", mp_is_zero(&one), false);
164 try("mp_is_negative(-1)", mp_is_negative(&minus_one), true);
165 try("mp_is_negative(0)", mp_is_negative(&zero), false);
166 try("mp_is_negative(1)", mp_is_negative(&one), false);
168 try("mp_is_integer(-1)", mp_is_integer(&minus_one), true);
169 try("mp_is_integer(0)", mp_is_integer(&zero), true);
170 try("mp_is_integer(1)", mp_is_integer(&one), true);
172 try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one), false);
173 try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero), true);
174 try("mp_is_positive_integer(1)", mp_is_positive_integer(&one), true);
176 try("mp_is_natural(-1)", mp_is_natural(&minus_one), false);
177 try("mp_is_natural(0)", mp_is_natural(&zero), false);
178 try("mp_is_natural(1)", mp_is_natural(&one), true);
180 try("mp_is_complex(-1)", mp_is_complex(&minus_one), false);
181 try("mp_is_complex(0)", mp_is_complex(&zero), false);
182 try("mp_is_complex(1)", mp_is_complex(&one), false);
184 try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one, &minus_one), true);
185 try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one, &zero), false);
186 try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one, &one), false);
187 try("mp_is_equal(0, -1)", mp_is_equal(&zero, &minus_one), false);
188 try("mp_is_equal(0, 0)", mp_is_equal(&zero, &zero), true);
189 try("mp_is_equal(0, 1)", mp_is_equal(&zero, &one), false);
190 try("mp_is_equal(1, -1)", mp_is_equal(&one, &minus_one), false);
191 try("mp_is_equal(1, 0)", mp_is_equal(&one, &zero), false);
192 try("mp_is_equal(1, 1)", mp_is_equal(&one, &one), true);
194 try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero, &minus_one), true);
195 try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero, &zero), false);
196 try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero, &one), false);
197 try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one, &minus_one), true);
198 try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one, &zero), true);
199 try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one, &one), false);
200 try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one, &minus_one), false);
201 try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one, &zero), false);
202 try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one, &one), false);
204 try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero, &minus_one), true);
205 try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero, &zero), true);
206 try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero, &one), false);
207 try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one, &minus_one), true);
208 try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one, &zero), true);
209 try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one, &one), true);
210 try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one, &minus_one), true);
211 try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one, &zero), false);
212 try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one, &one), false);
214 try("mp_is_less_than(0, -1)", mp_is_less_than (&zero, &minus_one), false);
215 try("mp_is_less_than(0, 0)", mp_is_less_than (&zero, &zero), false);
216 try("mp_is_less_than(0, 1)", mp_is_less_than (&zero, &one), true);
217 try("mp_is_less_than(1, -1)", mp_is_less_than (&one, &minus_one), false);
218 try("mp_is_less_than(1, 0)", mp_is_less_than (&one, &zero), false);
219 try("mp_is_less_than(1, 1)", mp_is_less_than (&one, &one), false);
220 try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one, &minus_one), false);
221 try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one, &zero), true);
222 try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one, &one), true);
224 try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero, &minus_one), false);
225 try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero, &zero), true);
226 try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero, &one), true);
227 try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one, &minus_one), false);
228 try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one, &zero), false);
229 try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one, &one), true);
230 try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one, &minus_one), true);
231 try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one, &zero), true);
232 try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one, &one), true);
237 main (int argc, char **argv)
239 setlocale(LC_ALL, "C");
240 g_type_init ();
242 test_mp();
243 test_numbers();
244 if (fails == 0)
245 printf("Passed all %i tests\n", passes);
247 return fails;