Updated Makefile.am for Latvian language help translation.
[gcalctool.git] / src / test-mp.c
bloba750f2a3f3edbea9301930c3d5ad673b0a2f3e7e
1 /*
2 * Copyright (C) 2008-2011 Robert Ancell.
3 *
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
8 * license.
9 */
11 #include <glib-object.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdarg.h>
15 #include <locale.h>
17 #include "mp.h"
18 #include "mp-private.h"
20 static int fails = 0;
21 static int passes = 0;
23 /* If we're not using GNU C, elide __attribute__ */
24 #ifndef __GNUC__
25 # define __attribute__(x) /*NOTHING*/
26 #endif
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, ...)
34 /* va_list args;
36 printf(" PASS: ");
37 va_start(args, format);
38 vprintf(format, args);
39 va_end(args);
40 printf("\n");
42 passes += 1;
45 static void fail(const char *format, ...)
47 va_list args;
49 printf("*FAIL: ");
50 va_start(args, format);
51 vprintf(format, args);
52 va_end(args);
53 printf("\n");
54 fails++;
58 static void
59 print_number(MPNumber *x)
61 int i, j;
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++);
66 if (j == MP_SIZE) {
67 printf(",...");
68 break;
70 printf(",%d", x->fraction[i]);
74 static void
75 test_string(const char *number)
77 MPNumber t;
79 mp_set_from_string(number, 10, &t);
81 printf("MPNumber(%s) -> {", number);
82 print_number(&t);
83 printf("}\n");
86 static void
87 test_integer(int number)
89 MPNumber t;
91 mp_set_from_integer(number, &t);
93 printf("MPNumber(%d) -> {", number);
94 print_number(&t);
95 printf("}\n");
99 static void
100 test_numbers()
102 printf("base=%d\n", MP_BASE);
103 test_integer(0);
104 test_integer(1);
105 test_integer(-1);
106 test_integer(2);
107 test_integer(9999);
108 test_integer(10000);
109 test_integer(10001);
110 test_integer(2147483647);
112 test_string("0");
113 test_string("1");
114 test_string("-1");
115 test_string("16383");
116 test_string("16384");
117 test_string("16385");
118 test_string("268435456");
120 test_string("0.1");
121 test_string("0.5");
122 test_string("0.25");
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");
133 static void
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");
138 else
139 pass("%s -> %s", string, result ? "true" : "false");
143 static void
144 test_mp()
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");
232 g_type_init ();
234 test_mp();
235 test_numbers();
236 if (fails == 0)
237 printf("Passed all %i tests\n", passes);
239 return fails;