| 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' | ||
| 3 | #------ Tests for revised version of phrtsd | ||
| 4 | |||
| 5 | ################# We start with some black magic to print on failure. | ||
| 6 | |||
| 7 | BEGIN { $| = 1; print "1..16\n"; } | ||
| 8 | END {print "not ok 1\n" unless $loaded;} | ||
| 9 | use Math::Random qw(:all); | ||
| 10 | $loaded = 1; | ||
| 11 | print "ok 1\n"; | ||
| 12 | |||
| 13 | ################# End of black magic. | ||
| 14 | |||
| 15 | #------ SUBROUTINES | ||
| 16 | #--- Compare two 3-element arrays for equality to 5 decimal places. | ||
| 17 | sub eq5a { | ||
| 18 | $a0 = sprintf("%.5f", $_[0]); | ||
| 19 | $a1 = sprintf("%.5f", $_[1]); | ||
| 20 | $a2 = sprintf("%.5f", $_[2]); | ||
| 21 | $b0 = sprintf("%.5f", $_[3]); | ||
| 22 | $b1 = sprintf("%.5f", $_[4]); | ||
| 23 | $b2 = sprintf("%.5f", $_[5]); | ||
| 24 | return ($a0 eq $b0) && ($a1 eq $b1) && ($a2 eq $b2); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub was_it_ok { | ||
| 28 | my ($num, $test) = @_; | ||
| 29 | if ($test) { print "ok $num\n"; } | ||
| 30 | else { print "not ok $num\n"; $failed++; } | ||
| 31 | } | ||
| 32 | |||
| 33 | #------ TESTS | ||
| 34 | # NOTE: Do not change the order of these tests!! Since at least | ||
| 35 | # one new variate is produced every time, the results will differ | ||
| 36 | # if the order is changed. If new tests have to be added, add them | ||
| 37 | # at the end. | ||
| 38 | |||
| 39 | $failed = 0; | ||
| 40 | random_set_seed_from_phrase("En arkhe en ho Logos"); | ||
| 41 | |||
| 42 | print "random_uniform.................."; | ||
| 43 | @result = random_uniform(3, 0, 1.5); | ||
| 44 | was_it_ok(2, eq5a(@result, 0.05617, 0.51721, 0.83203)); | ||
| 45 | |||
| 46 | print "random_uniform_integer.........."; | ||
| 47 | @result = random_uniform_integer(3, 1, 999999); | ||
| 48 | was_it_ok(3, eq5a(@result, 134416, 581232, 488982)); | ||
| 49 | |||
| 50 | print "random_permutation.............."; | ||
| 51 | @result = random_permutation(qw[A 2 c iv E 6 g viii]); | ||
| 52 | was_it_ok(4, "@result" eq "A g E 6 viii 2 c iv"); | ||
| 53 | |||
| 54 | print "random_permuted_index..........."; | ||
| 55 | @result = random_permuted_index(9); | ||
| 56 | was_it_ok(5, "@result" eq "3 7 6 8 1 0 2 5 4"); | ||
| 57 | |||
| 58 | print "random_normal..................."; | ||
| 59 | @result = random_normal(3, 50, 2.3); | ||
| 60 | was_it_ok(6, eq5a(@result, 51.32045, 52.86931, 51.42714)); | ||
| 61 | |||
| 62 | print "random_chi_square..............."; | ||
| 63 | @result = random_chi_square(3, 4); | ||
| 64 | was_it_ok(7, eq5a(@result, 3.06391, 2.69547, 3.06120)); | ||
| 65 | |||
| 66 | print "random_f........................"; | ||
| 67 | @result = random_f(3, 2, 5); | ||
| 68 | was_it_ok(8, eq5a(@result, 20.49306, 1.76842, 0.18747)); | ||
| 69 | |||
| 70 | print "random_beta....................."; | ||
| 71 | @result = random_beta(3, 17, 23); | ||
| 72 | was_it_ok(9, eq5a(@result, 0.42553, 0.39371, 0.35722)); | ||
| 73 | |||
| 74 | print "random_binomial................."; | ||
| 75 | @result = random_binomial(3, 31, 0.43); | ||
| 76 | was_it_ok(10, eq5a(@result, 14, 13, 10)); | ||
| 77 | |||
| 78 | print "random_poisson.................."; | ||
| 79 | @result = random_poisson(3, 555); | ||
| 80 | was_it_ok(11, eq5a(@result, 510, 557, 536)); | ||
| 81 | |||
| 82 | print "random_exponential.............."; | ||
| 83 | @result = random_exponential(3, 444); | ||
| 84 | was_it_ok(12, eq5a(@result, 127.98662, 8.24119, 397.19221)); | ||
| 85 | |||
| 86 | print "random_gamma...................."; | ||
| 87 | @result = random_gamma(3, 11, 4); | ||
| 88 | was_it_ok(13, eq5a(@result, 0.47858, 0.32865, 0.56708)); | ||
| 89 | |||
| 90 | print "random_multinomial.............."; | ||
| 91 | @result = random_multinomial(3, 0.1, 0.72, 0.18); | ||
| 92 | was_it_ok(14, eq5a(@result, 0, 2, 1)); | ||
| 93 | |||
| 94 | print "random_negative_binomial........"; | ||
| 95 | @result = random_negative_binomial(3, 10, 0.63); | ||
| 96 | was_it_ok(15, eq5a(@result, 0, 2, 5)); | ||
| 97 | |||
| 98 | print "random_multivariate_normal......"; | ||
| 99 | @result = random_multivariate_normal(2,1,1, | ||
| 100 | [0.1,0.0], | ||
| 101 | [0.0,0.1]); | ||
| 102 | @result = (map { @$_ } @result); | ||
| 103 | was_it_ok(16, eq5a(@result[0..2], -0.06076, 0.89337, 1.51428)); | ||
| 104 | |||
| 105 | |||
| 106 | if ($failed == 0) { print "All tests successful.\n" } | ||
| 107 | else { | ||
| 108 | $tt = ($failed == 1) ? "1 test" : "$failed tests"; | ||
| 109 | print "$tt failed! There is no joy in Mudville.\n"; | ||
| 110 | } |