Cleanup of old attributes. And addition of "top_tool" which is necessary for -clean...
[PsN.git] / modules / Math-Random / test.pl
blob7925ce1ae185600df7935f439815f7a886a297d7
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
4 ################# We start with some black magic to print on failure.
6 BEGIN { $| = 1; print "1..16\n"; }
7 END {print "not ok 1\n" unless $loaded;}
8 use Math::Random qw(:all);
9 $loaded = 1;
10 print "ok 1\n";
12 ################# End of black magic.
14 #------ SUBROUTINES
15 #--- Compare two 3-element arrays for equality to 5 decimal places.
16 sub eq5a {
17 $a0 = sprintf("%.5f", $_[0]);
18 $a1 = sprintf("%.5f", $_[1]);
19 $a2 = sprintf("%.5f", $_[2]);
20 $b0 = sprintf("%.5f", $_[3]);
21 $b1 = sprintf("%.5f", $_[4]);
22 $b2 = sprintf("%.5f", $_[5]);
23 return ($a0 eq $b0) && ($a1 eq $b1) && ($a2 eq $b2);
26 sub was_it_ok {
27 my ($num, $test) = @_;
28 if ($test) { print "ok $num\n"; }
29 else { print "not ok $num\n"; $failed++; }
32 #------ TESTS
33 # NOTE: Do not change the order of these tests!! Since at least
34 # one new variate is produced every time, the results will differ
35 # if the order is changed. If new tests have to be added, add them
36 # at the end.
38 $failed = 0;
39 random_set_seed_from_phrase("En arkhe en ho Logos");
41 print "random_uniform..................";
42 @result = random_uniform(3, 0, 1.5);
43 was_it_ok(2, eq5a(@result, 0.02021, 0.72981, 0.28370));
45 print "random_uniform_integer..........";
46 @result = random_uniform_integer(3, 1, 999999);
47 was_it_ok(3, eq5a(@result, 877502, 385465, 712626));
49 print "random_permutation..............";
50 @result = random_permutation(qw[A 2 c iv E 6 g viii]);
51 was_it_ok(4, "@result" eq "iv c 6 2 E A g viii");
53 print "random_permuted_index...........";
54 @result = random_permuted_index(9);
55 was_it_ok(5, "@result" eq "8 5 3 7 0 6 2 1 4");
57 print "random_normal...................";
58 @result = random_normal(3, 50, 2.3);
59 was_it_ok(6, eq5a(@result, 49.96739, 52.24146, 47.73983));
61 print "random_chi_square...............";
62 @result = random_chi_square(3, 4);
63 was_it_ok(7, eq5a(@result, 2.53270, 1.71850, 5.85347));
65 print "random_f........................";
66 @result = random_f(3, 2, 5);
67 was_it_ok(8, eq5a(@result, 1.14801, 2.97847, 0.01688));
69 print "random_beta.....................";
70 @result = random_beta(3, 17, 23);
71 was_it_ok(9, eq5a(@result, 0.49457, 0.55133, 0.37236));
73 print "random_binomial.................";
74 @result = random_binomial(3, 31, 0.43);
75 was_it_ok(10, eq5a(@result, 17, 17, 18));
77 print "random_poisson..................";
78 @result = random_poisson(3, 555);
79 was_it_ok(11, eq5a(@result, 571, 560, 579));
81 print "random_exponential..............";
82 @result = random_exponential(3, 444);
83 was_it_ok(12, eq5a(@result, 1037.53566, 20.06634, 1063.81861));
85 print "random_gamma....................";
86 @result = random_gamma(3, 11, 4);
87 was_it_ok(13, eq5a(@result, 0.22119, 0.60820, 0.27125));
89 print "random_multinomial..............";
90 @result = random_multinomial(3, 0.1, 0.72, 0.18);
91 was_it_ok(14, eq5a(@result, 0, 2, 1));
93 print "random_negative_binomial........";
94 @result = random_negative_binomial(3, 10, 0.63);
95 was_it_ok(15, eq5a(@result, 7, 8, 2));
97 print "random_multivariate_normal......";
98 @result = random_multivariate_normal(2,1,1,
99 [0.1,0.0],
100 [0.0,0.1]);
101 @result = (map { @$_ } @result);
102 was_it_ok(16,eq5a(@result[0..2],0.61592,0.016556,1.27741));
105 if ($failed == 0) { print "All tests successful.\n" }
106 else {
107 $tt = ($failed == 1) ? "1 test" : "$failed tests";
108 print "$tt failed! There is no joy in Mudville.\n";