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)
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
25 #include "mp-equation.h"
26 #include "mp-serializer.h"
27 #include "unit-manager.h"
29 static MPEquationOptions options
;
32 static int passes
= 0;
34 /* If we're not using GNU C, elide __attribute__ */
36 # define __attribute__(x) /*NOTHING*/
39 static void pass(const char *format
, ...) __attribute__((format(printf
, 1, 2)));
40 static void fail(const char *format
, ...) __attribute__((format(printf
, 1, 2)));
43 static void pass(const char *format
, ...)
48 va_start(args, format);
49 vprintf(format, args);
56 static void fail(const char *format
, ...)
61 va_start(args
, format
);
62 vprintf(format
, args
);
70 error_code_to_string(MPErrorCode error
)
72 static char error_string
[1024];
74 if (error
!= PARSER_ERR_MP
)
75 return mp_error_code_to_string(error
);
77 snprintf(error_string
, 1024, "PARSER_ERR_MP(\"%s\")", mp_get_error());
83 test(char *expression
, char *expected
, int expected_error
)
88 error
= mp_equation_parse(expression
, &options
, &result
, NULL
);
92 MpSerializer
*serializer
;
94 serializer
= mp_serializer_new(MP_DISPLAY_FORMAT_FIXED
, options
.base
, 9);
95 result_str
= mp_serializer_to_string(serializer
, &result
);
96 g_object_unref(serializer
);
98 if(expected_error
!= PARSER_ERR_NONE
)
99 fail("'%s' -> %s, expected error %s", expression
, result_str
, error_code_to_string(expected_error
));
100 else if(strcmp(result_str
, expected
) != 0)
101 fail("'%s' -> '%s', expected '%s'", expression
, result_str
, expected
);
103 pass("'%s' -> '%s'", expression
, result_str
);
107 if(error
== expected_error
)
108 pass("'%s' -> error %s", expression
, error_code_to_string(error
));
109 else if(expected_error
== PARSER_ERR_NONE
)
110 fail("'%s' -> error %s, expected result %s", expression
,
111 error_code_to_string(error
), expected
);
113 fail("'%s' -> error %s, expected error %s", expression
,
114 error_code_to_string(error
), error_code_to_string(expected_error
));
120 do_convert(const MPNumber
*x
, const char *x_units
, const char *z_units
, MPNumber
*z
, void *data
)
122 return unit_manager_convert_by_symbol(unit_manager_get_default(), x
, x_units
, z_units
, z
);
129 memset(&options
, 0, sizeof(options
));
131 options
.wordlen
= 32;
132 options
.angle_units
= MP_DEGREES
;
133 options
.convert
= do_convert
;
136 //test("π radians in degrees", "180", 0);
137 test("100 gradians in degrees", "90", 0);
140 test("1 meter in mm", "1000", 0);
141 test("1m in mm", "1000", 0);
142 test("1 inch in cm", "2.54", 0);
145 test("1m² in mm²", "1000000", 0);
148 test("1m³ in mm³", "1000000000", 0);
151 test("1 kg in pounds", "2.204622622", 0);
154 test("1 minute in seconds", "60", 0);
155 test("1s in ms", "1000", 0);
158 //test("100˚C in ˚F", "", 0);
159 //test("0˚C in ˚F", "32", 0);
160 //test("0˚K in ˚C", "−273.15", 0);
161 test("100degC in degF", "212", 0);
162 test("0degC in degF", "32", 0);
163 test("0degK in degC", "−273.15", 0);
168 variable_is_defined(const char *name
, void *data
)
170 return strcmp (name
, "x") == 0 || strcmp (name
, "y") == 0;
175 get_variable(const char *name
, MPNumber
*z
, void *data
)
177 if (strcmp (name
, "x") == 0) {
178 mp_set_from_integer (2, z
);
181 if (strcmp (name
, "y") == 0) {
182 mp_set_from_integer (3, z
);
190 set_variable(const char *name
, const MPNumber
*x
, void *data
)
197 memset(&options
, 0, sizeof(options
));
199 options
.wordlen
= 32;
200 options
.angle_units
= MP_DEGREES
;
201 options
.variable_is_defined
= variable_is_defined
;
202 options
.get_variable
= get_variable
;
203 options
.set_variable
= set_variable
;
206 test("2₁₀", "10", 0);
209 test("16434824₁₀", "76543210", 0);
213 test("18364758544493064720₁₀", "FEDCBA9876543210", 0);
216 test("0₂", "0", 0); test("0₈", "0", 0); test("0", "0", 0); test("0₁₆", "0", 0);
217 test("1₂", "1", 0); test("1₈", "1", 0); test("1", "1", 0); test("1₁₆", "1", 0);
218 test("2₂", "", PARSER_ERR_INVALID
); test("2₈", "2", 0); test("2", "2", 0); test("2₁₆", "2", 0);
219 test("3₂", "", PARSER_ERR_INVALID
); test("3₈", "3", 0); test("3", "3", 0); test("3₁₆", "3", 0);
220 test("4₂", "", PARSER_ERR_INVALID
); test("4₈", "4", 0); test("4", "4", 0); test("4₁₆", "4", 0);
221 test("5₂", "", PARSER_ERR_INVALID
); test("5₈", "5", 0); test("5", "5", 0); test("5₁₆", "5", 0);
222 test("6₂", "", PARSER_ERR_INVALID
); test("6₈", "6", 0); test("6", "6", 0); test("6₁₆", "6", 0);
223 test("7₂", "", PARSER_ERR_INVALID
); test("7₈", "7", 0); test("7", "7", 0); test("7₁₆", "7", 0);
224 test("8₂", "", PARSER_ERR_INVALID
); test("8₈", "", PARSER_ERR_INVALID
); test("8", "8", 0); test("8₁₆", "8", 0);
225 test("9₂", "", PARSER_ERR_INVALID
); test("9₈", "", PARSER_ERR_INVALID
); test("9", "9", 0); test("9₁₆", "9", 0);
226 test("A₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("A₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("A", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("A₁₆", "10", 0);
227 test("B₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("B₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("B", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("B₁₆", "11", 0);
228 test("C₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("C₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("C", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("C₁₆", "12", 0);
229 test("D₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("D₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("D", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("D₁₆", "13", 0);
230 test("E₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("E₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("E", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("E₁₆", "14", 0);
231 test("F₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("F₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("F", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("F₁₆", "15", 0);
232 test("a₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("a₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("a", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("a₁₆", "10", 0);
233 test("b₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("b₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("b", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("b₁₆", "11", 0);
234 test("c₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("c₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("c", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("c₁₆", "12", 0);
235 test("d₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("d₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("d", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("d₁₆", "13", 0);
236 test("e₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("e₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); /* e is a built-in variable */ test("e₁₆", "14", 0);
237 test("f₂", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("f₈", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("f", "", PARSER_ERR_UNKNOWN_VARIABLE
); test("f₁₆", "15", 0);
241 test("+ 1", "1", 0); // FIXME: Should this be allowed?
242 test("− 1", "−1", 0); // FIXME: Should this be allowed?
243 test("++1", "1", PARSER_ERR_INVALID
);
245 test("255", "255", 0);
246 test("256", "256", 0);
248 test("1½", "1.5", 0);
251 test("0°30'", "0.5", 0);
252 //test("0°0.1'", "1", 0); // FIXME: Not yet supported
253 test("0°0'1\"", "0.000277778", 0);
254 test("0°0'0.1\"", "0.000027778", 0);
255 test("1.00", "1", 0);
256 test("1.01", "1.01", 0);
258 test("١٢٣٤٥٦٧٨٩٠", "1234567890", 0);
259 test("۱۲۳۴۵۶۷۸۹۰", "1234567890", 0);
262 //test("2A", "2000000000000000", 0);
263 test("2T", "2000000000000", 0);
264 test("2G", "2000000000", 0);
265 test("2M", "2000000", 0);
266 test("2k", "2000", 0);
267 test("2c", "0.02", 0);
268 test("2d", "0.2", 0);
269 test("2c", "0.02", 0);
270 test("2m", "0.002", 0);
271 test("2u", "0.000002", 0);
272 test("2µ", "0.000002", 0);
273 test("2n", "0.000000002", 0);
274 //test("2p", "0.000000000002", 0); // FIXME: Need to print out significant figures, not decimal places
275 //test("2f", "0.000000000000002", 0); // FIXME: Need to print out significant figures, not decimal places
276 //test("2A3", "2300000000000000", 0);
277 test("2T3", "2300000000000", 0);
278 test("2G3", "2300000000", 0);
279 test("2M3", "2300000", 0);
280 test("2k3", "2300", 0);
281 test("2c3", "0.023", 0);
282 test("2d3", "0.23", 0);
283 test("2c3", "0.023", 0);
284 test("2m3", "0.0023", 0);
285 test("2u3", "0.0000023", 0);
286 test("2µ3", "0.0000023", 0);
287 //test("2n3", "0.0000000023", 0); // FIXME: Need to print out significant figures, not decimal places
288 //test("2p3", "0.0000000000023", 0); // FIXME: Need to print out significant figures, not decimal places
289 //test("2f3", "0.0000000000000023", 0); // FIXME: Need to print out significant figures, not decimal places
292 test("2×10^3", "2000", 0);
293 test("2×10^−3", "0.002", 0);
297 test("z", "", PARSER_ERR_UNKNOWN_VARIABLE
);
299 test("y2", "", PARSER_ERR_INVALID
);
300 test("y 2", "", PARSER_ERR_INVALID
);
301 test("2z", "", PARSER_ERR_UNKNOWN_VARIABLE
);
302 test("z2", "", PARSER_ERR_UNKNOWN_VARIABLE
);
303 test("z 2", "", PARSER_ERR_UNKNOWN_VARIABLE
);
304 test("z(2)", "", PARSER_ERR_UNKNOWN_VARIABLE
);
306 test("2y²", "18", 0);
310 test("2xy", "12", 0);
311 test("x²y", "12", 0);
312 test("xy²", "18", 0);
313 test("(xy)²", "36", 0);
314 test("2x²y", "24", 0);
315 test("2xy²", "36", 0);
316 test("2x²y²", "72", 0);
317 test("x²yx²y", "144", 0);
318 test("x³+2x²−5", "11", 0);
319 test("2(x+3y)", "22", 0);
320 test("x(x+3y)", "22", 0);
321 test("(x+3y)(2x-4y)", "−88", 0);
322 test("2x²+2xy−12y²", "−88", 0);
324 test("π", "3.141592654", 0);
325 test("e", "2.718281828", 0);
327 test("z=99", "99", 0);
328 test("longname=99", "99", 0);
329 //test("e=99", "", PARSER_ERR_BUILTIN_VARIABLE);
335 test("40000+0.001", "40000.001", 0);
336 test("0.001+40000", "40000.001", 0);
337 test("2-3", "−1", 0);
338 test("2−3", "−1", 0);
340 test("40000−0.001", "39999.999", 0);
341 test("0.001−40000", "−39999.999", 0);
344 test("−2×3", "−6", 0);
345 test("2×−3", "−6", 0);
346 test("−2×−3", "6", 0);
349 test("1÷2", "0.5", 0);
350 test("−6÷3", "−2", 0);
351 test("6÷−3", "−2", 0);
352 test("−6÷−3", "2", 0);
353 test("(−3)÷(−6)", "0.5", 0);
355 test("1203÷1", "1203", 0);
356 test("−0÷32352.689", "0", 0);
357 test("1÷4", "0.25", 0);
358 test("1÷3", "0.333333333", 0);
359 test("2÷3", "0.666666667", 0);
360 test("1÷0", "", PARSER_ERR_MP
);
361 test("0÷0", "", PARSER_ERR_MP
);
364 test("1000000000000000−1000000000000000", "0", 0);
365 test("1000000000000000÷1000000000000000", "1", 0);
366 test("1000000000000000×0.000000000000001", "1", 0);
368 /* Order of operations */
369 test("1−0.9−0.1", "0", 0);
370 test("1+2×3", "7", 0);
371 test("1+(2×3)", "7", 0);
372 test("(1+2)×3", "9", 0);
373 test("(1+2×3)", "7", 0);
376 test("100%", "1", 0);
377 test("1%", "0.01", 0);
378 test("100+1%", "101", 0);
379 test("100−1%", "99", 0);
380 test("100×1%", "1", 0);
381 test("100÷1%", "10000", 0);
386 test("5!", "120", 0);
387 test("69!", "171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000", 0);
388 test("0.1!", "", PARSER_ERR_MP
);
389 test("−1!", "−1", 0);
390 test("(−1)!", "", PARSER_ERR_MP
);
391 test("−(1!)", "−1", 0);
396 test("2¹⁰", "1024", 0);
397 test("(1+2)²", "9", 0);
398 test("(x)²", "4", 0);
399 test("|1−3|²", "4", 0);
400 test("|x|²", "4", 0);
405 test("2⁻¹", "0.5", 0);
406 //test("2⁻", "", PARSER_ERR_MP); // FIXME: Maybe an error in bison?
407 test("2^−1", "0.5", 0);
408 test("2^(−1)", "0.5", 0);
409 test("x⁻¹", "0.5", 0);
410 test("−10^2", "−100", 0);
411 test("(−10)^2", "100", 0);
412 test("−(10^2)", "−100", 0);
413 test("2^100", "1267650600228229401496703205376", 0);
414 test("4^3^2", "262144", 0);
415 test("4^(3^2)", "262144", 0);
416 test("(4^3)^2", "4096", 0);
418 test("√4−2", "0", 0);
422 test("₁₀√1024", "2", 0);
423 test("√(2+2)", "2", 0);
425 test("2×√4", "4", 0);
426 test("Sqrt(4)", "2", 0);
427 test("Sqrt(2)", "1.414213562", 0);
428 test("4^0.5", "2", 0);
429 test("2^0.5", "1.414213562", 0);
430 test("₃√−8", "−2", 0);
431 test("(−8)^(1÷3)", "−2", 0);
433 test("0 mod 7", "0", 0);
434 test("6 mod 7", "6", 0);
435 test("7 mod 7", "0", 0);
436 test("8 mod 7", "1", 0);
437 test("−1 mod 7", "6", 0);
439 test("sgn 0", "0", 0);
440 test("sgn 3", "1", 0);
441 test("sgn −3", "−1", 0);
445 test("⌊−3⌋", "−3", 0);
446 test("⌈−3⌉", "−3", 0);
447 test("[−3]", "−3", 0);
448 test("⌊3.2⌋", "3", 0);
449 test("⌈3.2⌉", "4", 0);
450 test("[3.2]", "3", 0);
451 test("⌊−3.2⌋", "−4", 0);
452 test("⌈−3.2⌉", "−3", 0);
453 test("[−3.2]", "−3", 0);
454 test("⌊3.5⌋", "3", 0);
455 test("⌈3.5⌉", "4", 0);
456 test("[3.5]", "4", 0);
457 test("⌊−3.5⌋", "−4", 0);
458 test("⌈−3.5⌉", "−3", 0);
459 test("[−3.5]", "−4", 0);
460 test("⌊3.7⌋", "3", 0);
461 test("⌈3.7⌉", "4", 0);
462 test("[3.7]", "4", 0);
463 test("⌊−3.7⌋", "−4", 0);
464 test("⌈−3.7⌉", "−3", 0);
465 test("[−3.7]", "−4", 0);
466 test("{3.2}", "0.2", 0);
467 test("{−3.2}", "0.8", 0);
470 test("|−1|", "1", 0);
471 test("|3−5|", "2", 0);
473 test("abs 1", "1", 0);
474 test("abs (−1)", "1", 0);
476 test("log 0", "", PARSER_ERR_MP
);
477 test("log 1", "0", 0);
478 test("log 2", "0.301029996", 0);
479 test("log 10", "1", 0);
480 test("log₁₀ 10", "1", 0);
481 test("log₂ 2", "1", 0);
482 test("2 log 2", "0.602059991", 0);
484 test("ln 0", "", PARSER_ERR_MP
);
485 test("ln 1", "0", 0);
486 test("ln 2", "0.693147181", 0);
487 test("ln e", "1", 0);
488 test("2 ln 2", "1.386294361", 0);
490 options
.angle_units
= MP_DEGREES
;
491 test("sin 0", "0", 0);
492 test("sin 45 − 1÷√2", "0", 0);
493 test("sin 20 + sin(−20)", "0", 0);
494 test("sin 90", "1", 0);
495 test("sin 180", "0", 0);
496 test("2 sin 90", "2", 0);
497 test("sin²45", "0.5", 0);
499 test("cos 0", "1", 0);
500 test("cos 45 − 1÷√2", "0", 0);
501 test("cos 20 − cos (−20)", "0", 0);
502 test("cos 90", "0", 0);
503 test("cos 180", "−1", 0);
504 test("2 cos 0", "2", 0);
505 test("cos²45", "0.5", 0);
507 test("tan 0", "0", 0);
508 test("tan 10 − sin 10÷cos 10", "0", 0);
509 test("tan 90", "", PARSER_ERR_MP
);
510 test("tan 10", "0.176326981", 0);
511 test("tan²10", "0.031091204", 0);
513 test("cos⁻¹ 0", "90", 0);
514 test("cos⁻¹ 1", "0", 0);
515 test("cos⁻¹ (−1)", "180", 0);
516 test("cos⁻¹ (1÷√2)", "45", 0);
518 test("sin⁻¹ 0", "0", 0);
519 test("sin⁻¹ 1", "90", 0);
520 test("sin⁻¹ (−1)", "−90", 0);
521 test("sin⁻¹ (1÷√2)", "45", 0);
523 test("cosh 0", "1", 0);
524 test("cosh 10 − (e^10 + e^−10)÷2", "0", 0);
526 test("sinh 0", "0", 0);
527 test("sinh 10 − (e^10 − e^−10)÷2", "0", 0);
528 test("sinh (−10) + sinh 10", "0", 0);
530 test("cosh² (−5) − sinh² (−5)", "1", 0);
531 test("tanh 0", "0", 0);
532 test("tanh 10 − sinh 10 ÷ cosh 10", "0", 0);
534 test("atanh 0", "0", 0);
535 test("atanh (1÷10) − 0.5 ln(11÷9)", "0", 0);
537 options
.angle_units
= MP_DEGREES
;
538 test("sin 90", "1", 0);
540 options
.angle_units
= MP_RADIANS
;
541 test("sin (π÷2)", "1", 0); // FIXME: Shouldn't need brackets
543 options
.angle_units
= MP_GRADIANS
;
544 test("sin 100", "1", 0);
546 /* Complex numbers */
547 options
.angle_units
= MP_DEGREES
;
551 test("1+i", "1+i", 0);
552 test("i+1", "1+i", 0);
553 test("1−i", "1−i", 0);
554 test("i−1", "−1+i", 0);
555 test("i×i", "−1", 0);
557 test("1÷i", "−i", 0);
559 test("|3+4i|", "5", 0);
560 test("arg 0", "", PARSER_ERR_MP
);
561 test("arg 1", "0", 0);
562 test("arg (1+i)", "45", 0);
563 test("arg i", "90", 0);
564 test("arg (−1+i)", "135", 0);
565 test("arg −1", "180", 0);
566 test("arg (1+−i)", "−45", 0);
567 test("arg −i", "−90", 0);
568 test("arg (−1−i)", "−135", 0);
569 test("i⁻¹", "−i", 0);
571 test("(−1)^0.5", "i", 0);
572 test("√−4", "2i", 0);
573 test("e^iπ", "−1", 0);
574 test("log (−10) − (1 + πi÷ln(10))", "0", 0);
575 test("ln (−e) − (1 + πi)", "0", 0);
576 test("sin(iπ÷4) − i×sinh(π÷4)", "0", 0);
577 test("cos(iπ÷4) − cosh(π÷4)", "0", 0);
580 test("0 and 0", "0", 0);
581 test("1 and 0", "0", 0);
582 test("0 and 1", "0", 0);
583 test("1 and 1", "1", 0);
584 test("3 and 5", "1", 0);
586 test("0 or 0", "0", 0);
587 test("1 or 0", "1", 0);
588 test("0 or 1", "1", 0);
589 test("1 or 1", "1", 0);
590 test("3 or 5", "7", 0);
592 test("0 xor 0", "0", 0);
593 test("1 xor 0", "1", 0);
594 test("0 xor 1", "1", 0);
595 test("1 xor 1", "0", 0);
596 test("3 xor 5", "6", 0);
599 test("ones 1", "FFFFFFFE", 0);
600 test("ones 7FFFFFFF", "80000000", 0);
601 test("twos 1", "FFFFFFFF", 0);
602 test("twos 7FFFFFFF", "80000001", 0);
603 test("~7A₁₆", "FFFFFF85", 0);
607 test("1100∧1010", "1000", 0);
608 test("1100∨1010", "1110", 0);
609 test("1100⊻1010", "110", 0);
610 test("1100⊕1010", "110", 0);
611 //test("1100⊼1010", "0111", 0);
612 //test("1100⊽1010", "0001", 0);
613 //options.wordlen = 2;
614 //test("¬01₂", "10₂", 0);
615 //test("¬¬10₂", "10₂", 0);
620 main (int argc
, char **argv
)
622 setlocale(LC_ALL
, "C");
628 printf("Passed all %i tests\n", passes
);