2 * Copyright (C) 2008-2012 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 private int fail_count
= 0;
12 private int pass_count
= 0;
14 private void pass (string? text
= null)
16 //stdout.printf ("PASS: %s\n", text);
20 private void fail (string text
)
22 stdout
.printf ("*FAIL: %s\n", text
);
26 private void test_integer ()
28 for (var a
= -10; a
<= 10; a
++)
30 var z
= new Number
.integer (a
);
31 if (z
.to_integer () != a
)
33 fail ("Number.integer (%d).to_integer () -> %lli, expected %i".printf (a
, z
.to_integer (), a
));
41 private void test_unsigned_integer ()
43 for (var a
= 0; a
<= 10; a
++)
45 var z
= new Number
.unsigned_integer (a
);
46 if (z
.to_unsigned_integer () != a
)
48 fail ("Number.unsigned_integer (%d).to_unsigned_integer () -> %i, expected %i".printf (a
, (int) z
.to_unsigned_integer (), a
));
56 private void test_fraction ()
58 for (var a
= 0; a
<= 10; a
++)
60 for (var b
= 1; b
<= 10; b
++)
62 var z
= new Number
.fraction (a
, b
);
63 var expected
= (double) a
/ b
;
64 if (!double_matches (z
, expected
))
66 fail ("Number.fraction (%d, %d) -> %f, expected %f".printf (a
, b
, z
.to_double (), expected
));
75 private void test_float ()
77 for (var a
= -10.0f
; a
<= 10.0f
; a
+= 0.5f
)
79 var z
= new Number
.float (a
);
80 if (z
.to_float () != a
)
82 fail ("Number.float (%f).to_float () -> %f, expected %f".printf (a
, z
.to_float (), a
));
90 private void test_double ()
92 for (var a
= -10.0; a
<= 10.0; a
+= 0.5)
94 var z
= new Number
.double (a
);
95 if (z
.to_double () != a
)
97 fail ("Number.double (%f).to_double () -> %f, expected %f".printf (a
, z
.to_double (), a
));
105 private void test_complex ()
107 for (var a
= -10; a
<= 10; a
++)
109 for (var b
= -10; b
<= 10; b
++)
111 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
114 if (z
.real_component ().to_integer () != re_expected
|| z
.imaginary_component ().to_integer () != im_expected
)
116 fail ("Number.complex (%d%+di) -> %d%+di, expected %d%+di".printf (a
, b
, (int) z
.real_component ().to_integer (), (int) z
.imaginary_component ().to_integer (), re_expected
, im_expected
));
123 private void test_polar ()
125 for (var ri
= -10; ri
<= 10; ri
++)
127 for (var theta_i
= -10; theta_i
<= 10; theta_i
++)
130 var theta
= 2 * Math
.PI
* theta_i
/ 10.0;
131 var z
= new Number
.polar (new Number
.double (r
), new Number
.double (theta
));
132 var re_expected
= r
* Math
.cos (theta
);
133 var im_expected
= r
* Math
.sin (theta
);
134 if (!double_matches (z
.real_component (), re_expected
) || !double_matches (z
.imaginary_component (), im_expected
))
136 fail ("Number.polar (%f, %f) -> %f%+fi, expected %f%+fi".printf (r
, theta
, z
.real_component ().to_double (), z
.imaginary_component ().to_double (), re_expected
, im_expected
));
143 private void test_i ()
145 var z
= new Number
.i ();
146 if (z
.real_component ().to_integer () != 0 && z
.imaginary_component ().to_integer () != 1)
148 fail ("Number.i () -> %d%+di, expected i".printf ((int) z
.real_component ().to_integer (), (int) z
.imaginary_component ().to_integer ()));
155 private void test_pi ()
157 var z
= new Number
.pi ();
158 var expected
= Math
.PI
;
159 if (!double_matches (z
, expected
))
161 fail ("Number.pi () -> %f, expected %f".printf (z
.to_double (), expected
));
168 private void test_eulers ()
170 var z
= new Number
.eulers ();
171 var expected
= Math
.E
;
172 if (!double_matches (z
, expected
))
174 fail ("Number.eulers () -> %f, expected %f".printf (z
.to_double (), expected
));
181 private void test_string ()
183 for (var a
= -10; a
<= 10; a
++)
185 var s
= "%d".printf (a
);
186 var z
= mp_set_from_string (s
);
189 fail ("mp_set_from_string (\"%s\") -> null".printf (s
));
193 if (z
.to_integer () != a
)
195 fail ("mp_set_from_string (\"%s\").to_integer () -> %d, expected %d".printf (s
, (int) z
.to_integer (), a
));
203 private void test_sgn ()
205 for (var a
= -10; a
<= 10; a
++)
207 var z
= new Number
.integer (a
);
213 if (z
.sgn ().to_integer () != expected
)
215 fail ("(%d).sgn () -> %d, expected %d".printf (a
, (int) z
.sgn ().to_integer (), expected
));
223 private void test_invert_sign ()
225 for (var a
= -10; a
<= 10; a
++)
227 var z
= new Number
.integer (a
);
229 if (z
.invert_sign ().to_integer () != expected
)
231 fail ("(%d).invert_sign () -> %d, expected %d".printf (a
, (int) z
.invert_sign ().to_integer (), expected
));
239 private void test_abs ()
241 for (var a
= -10; a
<= 10; a
++)
243 var z
= new Number
.integer (a
);
244 var expected
= a
.abs ();
245 if (z
.abs ().to_integer () != expected
)
247 fail ("(%d).abs () -> %d, expected %d".printf (a
, (int) z
.abs ().to_integer (), expected
));
255 private void test_arg ()
257 for (var a
= -10; a
<= 10; a
++)
259 for (var b
= -10; b
<= 10; b
++)
261 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
263 var expected
= Math
.atan2 (b
, a
);
264 if (!double_matches (z
.real_component (), expected
) || !z
.imaginary_component ().is_zero ())
266 fail ("(%d%+di).arg () -> %f%+fi, expected %f".printf (a
, b
, z
.real_component ().to_double (), z
.imaginary_component ().to_double (), expected
));
273 private void test_conjugate ()
275 for (var a
= -10; a
<= 10; a
++)
277 for (var b
= -10; b
<= 10; b
++)
279 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
282 var im_expected
= -b
;
283 if (z
.real_component ().to_integer () != re_expected
|| z
.imaginary_component ().to_integer () != im_expected
)
285 fail ("(%d%+di).real_component () -> %d%+di, expected %d%+di".printf (a
, b
, (int) z
.real_component ().to_integer (), (int) z
.imaginary_component ().to_integer (), re_expected
, im_expected
));
292 private void test_real_component ()
294 for (var a
= -10; a
<= 10; a
++)
296 for (var b
= -10; b
<= 10; b
++)
298 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
300 if (z
.real_component ().to_integer () != expected
)
302 fail ("(%d+%di).real_component () -> %d, expected %d".printf (a
, b
, (int) z
.real_component ().to_integer (), expected
));
311 private void test_imaginary_component ()
313 for (var a
= -10; a
<= 10; a
++)
315 for (var b
= -10; b
<= 10; b
++)
317 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
319 if (z
.imaginary_component ().to_integer () != expected
)
321 fail ("(%d+%di).imaginary_component () -> %d, expected %d".printf (a
, b
, (int) z
.imaginary_component ().to_integer (), expected
));
330 private bool double_matches (Number a
, double b
)
332 return double_string (a
.to_double ()) == double_string (b
);
335 private string double_string (double x
)
337 var value
= "%.6f".printf (x
);
338 if (value
== "-0.000000")
344 private void test_integer_component ()
346 for (var ai
= -100; ai
<= 100; ai
++)
349 var z
= new Number
.double (a
).integer_component ();
350 var expected
= Math
.trunc (a
);
351 if (!double_matches (z
, expected
))
353 fail ("(%f).integer_component () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
361 private void test_fractional_component ()
363 for (var ai
= -100; ai
<= 100; ai
++)
366 var z
= new Number
.double (a
).fractional_component ();
367 var expected
= a
- Math
.trunc (a
);
368 if (!double_matches (z
, expected
))
370 fail ("(%f).fractional_component () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
378 private void test_fractional_part ()
380 for (var ai
= -100; ai
<= 100; ai
++)
383 var z
= new Number
.double (a
).fractional_part ();
384 var expected
= a
- Math
.floor (a
);
385 if (!double_matches (z
, expected
))
387 fail ("(%f).fractional_part () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
395 private void test_floor ()
397 for (var ai
= -100; ai
<= 100; ai
++)
400 var z
= new Number
.double (a
).floor ();
401 var expected
= Math
.floor (a
);
402 if (!double_matches (z
, expected
))
404 fail ("(%f).floor () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
412 private void test_ceiling ()
414 for (var ai
= -100; ai
<= 100; ai
++)
417 var z
= new Number
.double (a
).ceiling ();
418 var expected
= Math
.ceil (a
);
419 if (!double_matches (z
, expected
))
421 fail ("(%f).ceiling () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
429 private void test_round ()
431 for (var ai
= -100; ai
<= 100; ai
++)
434 var z
= new Number
.double (a
).round ();
435 var expected
= Math
.round (a
);
436 if (!double_matches (z
, expected
))
438 fail ("(%f).round () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
446 private void test_reciprocal ()
448 for (var a
= -10; a
<= 10; a
++)
453 var z
= new Number
.double (a
).reciprocal ();
454 var expected
= 1.0 / a
;
455 if (!double_matches (z
, expected
))
457 fail ("(%f).reciprocal () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
461 z
= new Number
.double (expected
).reciprocal ();
462 if (!double_matches (z
, a
))
464 fail ("(%f).reciprocal () -> %f, expected %f".printf (expected
, z
.to_double (), a
));
472 private void test_epowy ()
474 for (var ai
= -100; ai
<= 100; ai
++)
477 var z
= new Number
.double (a
).epowy ();
478 var expected
= Math
.exp (a
);
479 if (!double_matches (z
, expected
))
481 fail ("(%f).epowy () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
489 private void test_xpowy ()
491 for (var a
= -10; a
<= 10; a
++)
493 for (var b
= -10; b
<= 10; b
++)
495 var z
= new Number
.integer (a
).xpowy (new Number
.integer (b
));
505 for (var i
= 0; i
> b
; i
--)
508 for (var i
= 0; i
< b
; i
++)
512 if (!double_matches (z
, expected
))
514 fail ("(%d).xpowy (%d) -> %f, expected %f".printf (a
, b
, z
.to_double (), expected
));
523 private void test_xpowy_integer ()
525 for (var a
= -10; a
<= 10; a
++)
527 for (var b
= -10; b
<= 10; b
++)
529 var z
= new Number
.integer (a
).xpowy_integer (b
);
539 for (var i
= 0; i
> b
; i
--)
542 for (var i
= 0; i
< b
; i
++)
546 if (!double_matches (z
, expected
))
548 fail ("(%d).xpowy_integer (%d) -> %f, expected %f".printf (a
, b
, z
.to_double (), expected
));
557 private void test_root3 ()
559 for (var a
= -10; a
<= 10; a
++)
561 var z
= new Number
.double (a
).root (3);
562 var expected
= Math
.cbrt (a
);
563 if (!double_matches (z
, expected
))
565 fail ("(%f).root (3) -> %f, expected %f".printf (a
, z
.to_double (), expected
));
573 private void test_sqrt ()
575 for (var a
= 0; a
<= 10; a
++)
577 var z
= new Number
.double (a
).sqrt ();
578 var expected
= Math
.sqrt (a
);
579 if (!double_matches (z
, expected
))
581 fail ("(%f).sqrt () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
589 private void test_ln ()
591 for (var a
= 1; a
<= 10; a
++)
593 var z
= new Number
.double (a
).ln ();
594 var expected
= Math
.log (a
);
595 if (!double_matches (z
, expected
))
597 fail ("(%f).ln () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
605 private void test_logarithm2 ()
607 for (var a
= 1; a
<= 10; a
++)
609 var z
= new Number
.double (a
).logarithm (2);
610 var expected
= Math
.log2 (a
);
611 if (!double_matches (z
, expected
))
613 fail ("(%f).logarithm (2) -> %f, expected %f".printf (a
, z
.to_double (), expected
));
621 private void test_logarithm10 ()
623 for (var a
= 1; a
<= 10; a
++)
625 var z
= new Number
.double (a
).logarithm (10);
626 var expected
= Math
.log10 (a
);
627 if (!double_matches (z
, expected
))
629 fail ("(%f).logarithm (10) -> %f, expected %f".printf (a
, z
.to_double (), expected
));
637 private void test_is_zero ()
639 for (var a
= -10; a
<= 10; a
++)
641 var z
= new Number
.integer (a
);
642 var expected
= a
== 0;
643 if (z
.is_zero () != expected
)
645 fail ("(%d).is_zero () -> %s, expected %s".printf (a
, z
.is_zero () ?
"true" : "false", expected ?
"true" : "false"));
653 private void test_is_negative ()
655 for (var a
= -10; a
<= 10; a
++)
657 var z
= new Number
.integer (a
);
658 var expected
= a
< 0;
659 if (z
.is_negative () != expected
)
661 fail ("(%d).is_negative () -> %s, expected %s".printf (a
, z
.is_negative () ?
"true" : "false", expected ?
"true" : "false"));
669 private void test_is_integer ()
671 for (var a
= -10; a
<= 10; a
++)
673 var z
= new Number
.integer (a
);
675 if (z
.is_integer () != expected
)
677 fail ("(%d).is_integer () -> %s, expected %s".printf (a
, z
.is_integer () ?
"true" : "false", expected ?
"true" : "false"));
685 private void test_is_positive_integer ()
687 for (var a
= -10; a
<= 10; a
++)
689 var z
= new Number
.integer (a
);
690 var expected
= a
>= 0;
691 if (z
.is_positive_integer () != expected
)
693 fail ("(%d).is_positive_integer () -> %s, expected %s".printf (a
, z
.is_positive_integer () ?
"true" : "false", expected ?
"true" : "false"));
701 private void test_is_natural ()
703 for (var a
= -10; a
<= 10; a
++)
705 var z
= new Number
.integer (a
);
706 var expected
= a
> 0;
707 if (z
.is_natural () != expected
)
709 fail ("(%d).is_natural () -> %s, expected %s".printf (a
, z
.is_natural () ?
"true" : "false", expected ?
"true" : "false"));
717 private void test_is_complex ()
719 for (var a
= -10; a
<= 10; a
++)
721 for (var b
= -10; b
<= 10; b
++)
723 var z
= new Number
.complex (new Number
.integer (a
), new Number
.integer (b
));
724 var expected
= b
!= 0;
725 if (z
.is_complex () != expected
)
727 fail ("(%d+%di).is_complex () -> %s, expected %s".printf (a
, b
, z
.is_complex () ?
"true" : "false", expected ?
"true" : "false"));
736 private void test_factorial ()
738 for (var a
= 0; a
<= 10; a
++)
740 var z
= new Number
.integer (a
);
742 for (var i
= 2; i
<= a
; i
++)
744 if (z
.factorial ().to_integer () != expected
)
746 fail ("(%d).factorial () -> %lli, expected %lli".printf (a
, z
.factorial ().to_integer (), expected
));
754 private void test_add ()
756 for (var a
= -10; a
<= 10; a
++)
758 for (var b
= -10; b
<= 10; b
++)
760 var z
= (new Number
.integer (a
)).add (new Number
.integer (b
));
761 var expected
= a
+ b
;
762 if (z
.to_integer () != expected
)
764 fail ("(%d).add (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
773 private void test_subtract ()
775 for (var a
= -10; a
<= 10; a
++)
777 for (var b
= -10; b
<= 10; b
++)
779 var z
= (new Number
.integer (a
)).subtract (new Number
.integer (b
));
780 var expected
= a
- b
;
781 if (z
.to_integer () != expected
)
783 fail ("(%d).subtract (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
792 private void test_multiply ()
794 for (var a
= -10; a
<= 10; a
++)
796 for (var b
= -10; b
<= 10; b
++)
798 var z
= (new Number
.integer (a
)).multiply (new Number
.integer (b
));
799 var expected
= a
* b
;
800 if (z
.to_integer () != expected
)
802 fail ("(%d).multiply (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
811 private void test_multiply_integer ()
813 for (var a
= -10; a
<= 10; a
++)
815 for (var b
= -10; b
<= 10; b
++)
817 var z
= (new Number
.integer (a
)).multiply_integer (b
);
818 var expected
= a
* b
;
819 if (z
.to_integer () != expected
)
821 fail ("(%d).multiply_integer (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
830 private void test_divide ()
832 for (var a
= -10; a
<= 10; a
++)
834 for (var b
= -10; b
<= 10; b
++)
839 var z
= (new Number
.integer (a
* b
)).divide (new Number
.integer (b
));
841 if (z
.to_integer () != expected
)
843 fail ("(%d).divide (%d) -> %lli, expected %d".printf (a
* b
, b
, z
.to_integer (), expected
));
852 private void test_divide_integer ()
854 for (var a
= -10; a
<= 10; a
++)
856 for (var b
= -10; b
<= 10; b
++)
861 var z
= (new Number
.integer (a
* b
)).divide_integer (b
);
863 if (z
.to_integer () != expected
)
865 fail ("(%d).divide_integer (%d) -> %lli, expected %d".printf (a
* b
, b
, z
.to_integer (), expected
));
874 private void test_modulus_divide ()
876 for (var a
= 0; a
<= 10; a
++)
878 for (var b
= 1; b
<= 10; b
++)
880 var z
= (new Number
.integer (a
)).modulus_divide (new Number
.integer (b
));
881 var expected
= a
% b
;
882 if (z
.to_integer () != expected
)
884 fail ("(%d).modulus_divide (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
893 static void test_sin ()
895 for (var a
= -Math
.PI
; a
<= Math
.PI
; a
+= Math
.PI
/ 16)
897 var z
= new Number
.double (a
).sin ();
898 var expected
= Math
.sin (a
);
899 if (!double_matches (z
, expected
))
901 fail ("(%f).sin () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
909 static void test_cos ()
911 for (var a
= -Math
.PI
; a
<= Math
.PI
; a
+= Math
.PI
/ 16)
913 var z
= new Number
.double (a
).cos ();
914 var expected
= Math
.cos (a
);
915 if (!double_matches (z
, expected
))
917 fail ("(%f).cos () -> %f, expected %f".printf (a
, z
.to_double (), expected
));
925 private void test_and ()
927 for (var a
= 0; a
< 10; a
++)
929 for (var b
= 0; b
< 10; b
++)
931 var z
= (new Number
.integer (a
)).and (new Number
.integer (b
));
932 var expected
= a
& b
;
933 if (z
.to_integer () != expected
)
935 fail ("(%d).and (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
944 private void test_or ()
946 for (var a
= 0; a
< 10; a
++)
948 for (var b
= 0; b
< 10; b
++)
950 var z
= (new Number
.integer (a
)).or (new Number
.integer (b
));
951 var expected
= a
| b
;
952 if (z
.to_integer () != expected
)
954 fail ("(%d).or (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
963 private void test_xor ()
965 for (var a
= 0; a
< 10; a
++)
967 for (var b
= 0; b
< 10; b
++)
969 var z
= (new Number
.integer (a
)).xor (new Number
.integer (b
));
970 var expected
= a ^ b
;
971 if (z
.to_integer () != expected
)
973 fail ("(%d).xor (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
982 private void test_not ()
984 for (var a
= 0; a
< 10; a
++)
986 var z
= (new Number
.integer (a
)).not (8);
987 var expected
= ~a
& 0xFF;
988 if (z
.to_integer () != expected
)
990 fail ("(%d).not () -> %lli, expected %d".printf (a
, z
.to_integer (), expected
));
998 private void test_shift ()
1000 for (var a
= 0; a
< 10; a
++)
1002 for (var b
= -10; b
< 10; b
++)
1004 var z
= (new Number
.integer (a
)).shift (b
);
1005 var expected
= a
<< b
;
1008 if (z
.to_integer () != expected
)
1010 fail ("(%d).shift (%d) -> %lli, expected %d".printf (a
, b
, z
.to_integer (), expected
));
1019 private void test_factorize ()
1021 for (var a
= 0; a
< 100; a
++)
1023 var factors
= (new Number
.integer (a
)).factorize ();
1024 var expected
= factorize (a
);
1026 var matches
= false;
1027 if (factors
.length () == expected
.length ())
1030 for (var i
= 0 ; i
< factors
.length (); i
++)
1031 if (factors
.nth_data (i
).to_integer () != expected
.nth_data (i
))
1037 var factors_string
= "";
1038 foreach (var f
in factors
)
1040 if (factors_string
!= "")
1041 factors_string
+= ", ";
1042 factors_string
+= "%d".printf ((int) f
.to_integer ());
1044 var expected_string
= "";
1045 foreach (var f
in expected
)
1047 if (expected_string
!= "")
1048 expected_string
+= ", ";
1049 expected_string
+= "%d".printf (f
);
1051 fail ("(%d).factorize () -> (%s), expected (%s)".printf (a
, factors_string
, expected_string
));
1059 private List
<int> factorize (int number
)
1061 var factors
= new List
<int> ();
1064 factors
.append (number
);
1071 for (var factor
= 2; factor
<= n
; factor
++)
1073 if (n
% factor
== 0)
1075 factors
.append (factor
);
1085 static int main (string[] args
)
1087 Intl
.setlocale (LocaleCategory
.ALL
, "C");
1090 test_unsigned_integer ();
1102 test_is_negative ();
1104 test_is_positive_integer ();
1108 test_invert_sign ();
1112 test_real_component ();
1113 test_imaginary_component ();
1114 test_integer_component ();
1115 test_fractional_component ();
1116 test_fractional_part ();
1123 test_xpowy_integer ();
1124 test_root3 (); // FIXME: should check other roots
1127 test_logarithm2 (); // FIXME: Should check other bases
1128 test_logarithm10 (); // FIXME: Should check other bases
1133 test_multiply_integer ();
1135 test_divide_integer ();
1136 test_modulus_divide ();
1155 //test_ones_complement ();
1156 //test_twos_complement ();
1159 if (fail_count
== 0)
1160 stdout
.printf ("Passed all %i tests\n", pass_count
);
1162 stdout
.printf ("Failed %i/%d tests\n", fail_count
, pass_count
+ fail_count
);