2 * Copyright (C) 2008-2011 Robert Ancell.
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 2 of the License, or (at your option) any later
7 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
11 #include <glib-object.h>
18 #include "mp-private.h"
21 static int passes
= 0;
23 /* If we're not using GNU C, elide __attribute__ */
25 # define __attribute__(x) /*NOTHING*/
28 static void pass(const char *format
, ...) __attribute__((format(printf
, 1, 2)));
29 static void fail(const char *format
, ...) __attribute__((format(printf
, 1, 2)));
32 static void pass(const char *format
, ...)
37 va_start(args, format);
38 vprintf(format, args);
45 static void fail(const char *format
, ...)
50 va_start(args
, format
);
51 vprintf(format
, args
);
59 print_number(MPNumber
*x
)
63 printf("sign=%d exponent=%d fraction=%d", x
->sign
, x
->exponent
, x
->fraction
[0]);
64 for (i
= 1; i
< MP_SIZE
; i
++) {
65 for (j
= i
; j
< MP_SIZE
&& x
->fraction
[j
] == 0; j
++);
70 printf(",%d", x
->fraction
[i
]);
75 test_string(const char *number
)
79 mp_set_from_string(number
, 10, &t
);
81 printf("MPNumber(%s) -> {", number
);
87 test_integer(int number
)
91 mp_set_from_integer(number
, &t
);
93 printf("MPNumber(%d) -> {", number
);
102 printf("base=%d\n", MP_BASE
);
110 test_integer(2147483647);
115 test_string("16383");
116 test_string("16384");
117 test_string("16385");
118 test_string("268435456");
123 test_string("0.125");
124 test_string("0.0625");
125 test_string("0.00006103515625");
126 test_string("0.000030517578125");
128 test_string("1.00006103515625");
129 test_string("16384.00006103515625");
134 try(const char *string
, bool result
, bool expected
)
136 if ((result
&& !expected
) || (!result
&& expected
))
137 fail("%s -> %s, expected %s", string
, expected
? "true" : "false", result
? "true" : "false");
139 pass("%s -> %s", string
, result
? "true" : "false");
146 MPNumber zero
, one
, minus_one
;
148 mp_set_from_integer(0, &zero
);
149 mp_set_from_integer(1, &one
);
150 mp_set_from_integer(-1, &minus_one
);
152 try("mp_is_zero(-1)", mp_is_zero(&minus_one
), false);
153 try("mp_is_zero(0)", mp_is_zero(&zero
), true);
154 try("mp_is_zero(1)", mp_is_zero(&one
), false);
156 try("mp_is_negative(-1)", mp_is_negative(&minus_one
), true);
157 try("mp_is_negative(0)", mp_is_negative(&zero
), false);
158 try("mp_is_negative(1)", mp_is_negative(&one
), false);
160 try("mp_is_integer(-1)", mp_is_integer(&minus_one
), true);
161 try("mp_is_integer(0)", mp_is_integer(&zero
), true);
162 try("mp_is_integer(1)", mp_is_integer(&one
), true);
164 try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one
), false);
165 try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero
), true);
166 try("mp_is_positive_integer(1)", mp_is_positive_integer(&one
), true);
168 try("mp_is_natural(-1)", mp_is_natural(&minus_one
), false);
169 try("mp_is_natural(0)", mp_is_natural(&zero
), false);
170 try("mp_is_natural(1)", mp_is_natural(&one
), true);
172 try("mp_is_complex(-1)", mp_is_complex(&minus_one
), false);
173 try("mp_is_complex(0)", mp_is_complex(&zero
), false);
174 try("mp_is_complex(1)", mp_is_complex(&one
), false);
176 try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one
, &minus_one
), true);
177 try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one
, &zero
), false);
178 try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one
, &one
), false);
179 try("mp_is_equal(0, -1)", mp_is_equal(&zero
, &minus_one
), false);
180 try("mp_is_equal(0, 0)", mp_is_equal(&zero
, &zero
), true);
181 try("mp_is_equal(0, 1)", mp_is_equal(&zero
, &one
), false);
182 try("mp_is_equal(1, -1)", mp_is_equal(&one
, &minus_one
), false);
183 try("mp_is_equal(1, 0)", mp_is_equal(&one
, &zero
), false);
184 try("mp_is_equal(1, 1)", mp_is_equal(&one
, &one
), true);
186 try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero
, &minus_one
), true);
187 try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero
, &zero
), false);
188 try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero
, &one
), false);
189 try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one
, &minus_one
), true);
190 try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one
, &zero
), true);
191 try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one
, &one
), false);
192 try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one
, &minus_one
), false);
193 try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one
, &zero
), false);
194 try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one
, &one
), false);
196 try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero
, &minus_one
), true);
197 try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero
, &zero
), true);
198 try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero
, &one
), false);
199 try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one
, &minus_one
), true);
200 try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one
, &zero
), true);
201 try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one
, &one
), true);
202 try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one
, &minus_one
), true);
203 try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one
, &zero
), false);
204 try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one
, &one
), false);
206 try("mp_is_less_than(0, -1)", mp_is_less_than (&zero
, &minus_one
), false);
207 try("mp_is_less_than(0, 0)", mp_is_less_than (&zero
, &zero
), false);
208 try("mp_is_less_than(0, 1)", mp_is_less_than (&zero
, &one
), true);
209 try("mp_is_less_than(1, -1)", mp_is_less_than (&one
, &minus_one
), false);
210 try("mp_is_less_than(1, 0)", mp_is_less_than (&one
, &zero
), false);
211 try("mp_is_less_than(1, 1)", mp_is_less_than (&one
, &one
), false);
212 try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one
, &minus_one
), false);
213 try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one
, &zero
), true);
214 try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one
, &one
), true);
216 try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero
, &minus_one
), false);
217 try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero
, &zero
), true);
218 try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero
, &one
), true);
219 try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one
, &minus_one
), false);
220 try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one
, &zero
), false);
221 try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one
, &one
), true);
222 try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one
, &minus_one
), true);
223 try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one
, &zero
), true);
224 try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one
, &one
), true);
229 main (int argc
, char **argv
)
231 setlocale(LC_ALL
, "C");
237 printf("Passed all %i tests\n", passes
);