| 1 | package Math::GSL::Test; | ||
| 2 | 47 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 47 | ▶ | use base qw(DynaLoader); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 47 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 47 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 47 | ▶ | use Test::More; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Math::GSL::Errno qw/:all/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Math::GSL::Machine qw/:all/; | ||
| 9 | use Math::GSL::Const qw/:all/; | ||
| 10 | use Math::GSL::Sys qw/gsl_nan gsl_isnan gsl_isinf/; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Carp qw/croak/; | ||
| 13 | our @EXPORT = qw(); | ||
| 14 | our @EXPORT_OK = qw( | ||
| 15 | is_similar ok_similar | ||
| 16 | ok_similar_relative | ||
| 17 | is_similar_relative | ||
| 18 | verify verify_results | ||
| 19 | is_windows | ||
| 20 | ok_status is_status_ok | ||
| 21 | ); | ||
| 22 | use constant GSL_IS_WINDOWS => ($^O =~ /MSWin32/i) ? 1 : 0 ; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Math::GSL::Test - Assertions and such | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | |||
| 31 | use Math::GSL::Test qw/:all/; | ||
| 32 | ok_similar($x,$y, $msg, $eps); | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | our %EXPORT_TAGS = ( all => \@EXPORT_OK,); | ||
| 37 | |||
| 38 | sub _dump_result($) | ||
| 39 | { | ||
| 40 | my $r=shift; | ||
| 41 | diag sprintf( "result->err: %.18g\n", $r->{err}); | ||
| 42 | diag sprintf( "result->val: %.18g\n", $r->{val}); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub dump_lu_matrix | ||
| 46 | { | ||
| 47 | # Syntax: dump_lu_matrix($base->raw, $permutation, 4, 4); | ||
| 48 | |||
| 49 | my ($base, $perm, $m, $n) = @_; | ||
| 50 | # Dump the upper triangular matrix first: | ||
| 51 | my $U = "U = ["; | ||
| 52 | for my $i (0..$m-1) { | ||
| 53 | for my $j (0..$n-1) { | ||
| 54 | if ($j >= $i) { | ||
| 55 | $U .= sprintf "%g ", $base->[$i*$n + $j]; | ||
| 56 | } else { | ||
| 57 | $U .= "0 "; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | $U .= "\n"; | ||
| 61 | } | ||
| 62 | diag $U . "]\n"; | ||
| 63 | # Now dump the lower triangular matrix: | ||
| 64 | my $L = "L = ["; | ||
| 65 | for my $i (0..$m-1) { | ||
| 66 | for my $j (0..$n-1) { | ||
| 67 | if ($j < $i) { | ||
| 68 | $L .= sprintf "%g ", $base->[$i*$n + $j]; | ||
| 69 | } elsif ($j == $i) { | ||
| 70 | $L .= "1 "; | ||
| 71 | } else { | ||
| 72 | $L .= "0 "; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | $L .= "\n"; | ||
| 76 | } | ||
| 77 | diag $L . "]\n"; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 is_windows() | ||
| 81 | |||
| 82 | Returns true if current system is Windows-like. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub is_windows() { GSL_IS_WINDOWS } | ||
| 87 | |||
| 88 | =head2 is_similar($x,$y;$eps,$similarity_function) | ||
| 89 | |||
| 90 | is_similar($x,$y); | ||
| 91 | is_similar($x, $y, 1e-7); | ||
| 92 | is_similar($x,$y, 1e-3, sub { ... } ); | ||
| 93 | |||
| 94 | Return true if $x and $y are within $eps of each other, i.e. | ||
| 95 | |||
| 96 | abs($x-$y) <= $eps | ||
| 97 | |||
| 98 | If passed a code reference $similarity_function, it will pass $x and $y as parameters to it and | ||
| 99 | will check to see if | ||
| 100 | |||
| 101 | $similarity_function->($x,$y_) <= $eps | ||
| 102 | |||
| 103 | The default value of $eps is 1e-8. Don't try sending anything to the Moon with this value... | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub is_similar { | ||
| 108 | my ($x,$y, $eps, $similarity_function) = @_; | ||
| 109 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 110 | $eps ||= 1e-8; | ||
| 111 | if (ref $x eq 'ARRAY' && ref $y eq 'ARRAY') { | ||
| 112 | if ( $#$x != $#$y ){ | ||
| 113 | diag "is_similar(): items differ in length, $#$x != $#$y !!!"; | ||
| 114 | return 0; | ||
| 115 | } else { | ||
| 116 | map { | ||
| 117 | my $delta = (gsl_isnan($x->[$_]) or gsl_isnan($y->[$_])) ? gsl_nan() : abs($x->[$_] - $y->[$_]); | ||
| 118 | if($delta > $eps){ | ||
| 119 | diag "\n\tElements start differing at index $_, delta = $delta\n"; | ||
| 120 | diag qq{\t\t\$x->[$_] = } . $x->[$_] . "\n"; | ||
| 121 | diag qq{\t\t\$y->[$_] = } . $y->[$_] . "\n"; | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | } (0..$#$x); | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | } else { | ||
| 128 | if( ref $similarity_function eq 'CODE') { | ||
| 129 | $similarity_function->($x,$y) <= $eps ? return 1 : return 0; | ||
| 130 | } elsif( defined $x && defined $y) { | ||
| 131 | my $delta = (gsl_isnan($x) or gsl_isnan($y)) ? gsl_nan() : abs($x-$y); | ||
| 132 | $delta > $eps ? diag qq{\t\t\$x=$x\n\t\t\$y=$y\n\t\tdelta=$delta\n} && return 0 : return 1; | ||
| 133 | } else { | ||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | # this is a huge hack | ||
| 140 | sub verify_results | ||
| 141 | { | ||
| 142 | my ($results,$class, $eps) = @_; | ||
| 143 | # GSL uses a factor of 100 | ||
| 144 | my $factor = 100; | ||
| 145 | my ($x,$res); | ||
| 146 | |||
| 147 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 148 | |||
| 149 | # If no epsilon is passed in, default to TOL3 from the GSL test suite | ||
| 150 | $eps ||= 2048*$Math::GSL::Machine::GSL_DBL_EPSILON; # TOL3 | ||
| 151 | |||
| 152 | croak "Usage: verify_results(%results, \$class)" unless $class; | ||
| 153 | while (my($code,$expected)=each %$results){ | ||
| 154 | my $r = Math::GSL::SF::gsl_sf_result_struct->new; | ||
| 155 | my $status = eval qq{${class}::$code}; | ||
| 156 | |||
| 157 | ok(0, qq{'$code' died: $@} ) if !defined $status; | ||
| 158 | |||
| 159 | if ( defined $r && $code =~ /_e\(.*\$r/) { | ||
| 160 | $x = $r->{val}; | ||
| 161 | |||
| 162 | # if $eps=0, give some default | ||
| 163 | $eps = $factor*$r->{err} || 1e-8; | ||
| 164 | $res = abs($x-$expected); | ||
| 165 | |||
| 166 | if ($ENV{DEBUG} ){ | ||
| 167 | _dump_result($r); | ||
| 168 | #print "got $code = $x\n"; | ||
| 169 | diag sprintf("expected : %.18g\n", $expected); | ||
| 170 | diag sprintf("difference : %.18g\n", $res); | ||
| 171 | diag sprintf("unexpected error of %.18g\n", $res-$eps) if ($res-$eps>0); | ||
| 172 | } | ||
| 173 | if (gsl_isnan($x)) { | ||
| 174 | ok( gsl_isnan($expected), sprintf("'$expected'?='$x' (%16b ?= %16b)", $expected, $x) ); | ||
| 175 | } elsif(gsl_isinf($x)) { | ||
| 176 | ok( gsl_isinf($expected), sprintf("'$expected'?='$x' (%16b ?= %16b)", $expected, $x) ); | ||
| 177 | } else { | ||
| 178 | cmp_ok( $res,'<=', $eps, "$code ?= $x,\nres= +-$res, eps=$eps" ); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 verify( $results, $class) | ||
| 185 | |||
| 186 | Takes a hash reference of key/value pairs where the keys are bits of code, which when evaluated should | ||
| 187 | be within some tolerance of the value. For example: | ||
| 188 | |||
| 189 | my $results = { | ||
| 190 | 'gsl_cdf_ugaussian_P(2.0)' => [ 0.977250, 1e-5 ], | ||
| 191 | 'gsl_cdf_ugaussian_Q(2.0)' => [ 0.022750, 1e-7 ], | ||
| 192 | 'gsl_cdf_ugaussian_Pinv(0.977250)'=> [ 2.000000 ], | ||
| 193 | }; | ||
| 194 | verify($results, 'Math::GSL::CDF'); | ||
| 195 | |||
| 196 | When no tolerance is given, a value of 1e-8 = 0.00000001 is used. One | ||
| 197 | may use $GSL_NAN and $GSL_INF in comparisons and this routine will | ||
| 198 | use the gsl_isnan() and gsl_isinf() routines to compare them. | ||
| 199 | |||
| 200 | |||
| 201 | Note: Needing to pass in the class name is honky. This may change. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub verify | ||
| 206 | { | ||
| 207 | my ($results,$class) = @_; | ||
| 208 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 209 | croak "Usage: verify(%results, \$class)" unless $class; | ||
| 210 | while (my($code,$result)=each %$results){ | ||
| 211 | my $x = eval qq{${class}::$code}; | ||
| 212 | ok(0, $@) if $@; | ||
| 213 | |||
| 214 | croak(__PACKAGE__." : $result is not an array reference!") unless ref $result; | ||
| 215 | my($expected,$eps)=@$result; | ||
| 216 | $eps ||= 1e-8; | ||
| 217 | |||
| 218 | if (gsl_isnan($x)) { | ||
| 219 | ok( gsl_isnan($expected), "'$expected'?='$x'" ); | ||
| 220 | } elsif(gsl_isinf($x)) { | ||
| 221 | ok( gsl_isinf($expected), "'$expected'?='$x'" ); | ||
| 222 | } else { | ||
| 223 | my $res = abs($x - $expected); | ||
| 224 | ok( $res <= $eps, "$code ?= $x,\nres= +-$res, eps=$eps" ); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 ok_status( $got_status; $expected_status ) | ||
| 230 | |||
| 231 | ok_status( $status ); # defaults to checking for $GSL_SUCCESS | ||
| 232 | |||
| 233 | ok_status( $status, $GSL_ECONTINUE ); | ||
| 234 | |||
| 235 | Pass a test if the GSL status codes match, with a default expected status of $GSL_SUCCESS. This | ||
| 236 | function also stringifies the status codes into meaningful messages when it fails. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub ok_status { | ||
| 241 | my ($got, $expected, $msg ) = @_; | ||
| 242 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 243 | $expected ||= $GSL_SUCCESS; | ||
| 244 | my $strerr = $got ? gsl_strerror(int($got)) : ''; | ||
| 245 | |||
| 246 | ok( defined $got && $got == $expected, $msg ? "$msg: " .$strerr : $strerr ); | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 is_status_ok($status) | ||
| 250 | |||
| 251 | is_status_ok( $status ); | ||
| 252 | |||
| 253 | Return true if $status is $GSL_SUCCESS, false otherwise. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | sub is_status_ok { | ||
| 258 | my ($got) = shift; | ||
| 259 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 260 | return ( defined $got && $got == $GSL_SUCCESS ) ? 1 : 0 ; | ||
| 261 | } | ||
| 262 | |||
| 263 | =head2 ok_similar( $x, $y, $msg, $eps) | ||
| 264 | |||
| 265 | ok_similar( $x, $y); | ||
| 266 | ok_similar( $x, $y, 'reason'); | ||
| 267 | ok_similar( $x, $y, 'reason', 1e-4); | ||
| 268 | |||
| 269 | Pass a test if is_similar($x,$y,$msg,$eps) is true, otherwise fail. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub ok_similar { | ||
| 274 | my ($x,$y, $msg, $eps) = @_; | ||
| 275 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 276 | ok(is_similar($x,$y,$eps), $msg); | ||
| 277 | } | ||
| 278 | |||
| 279 | =head2 is_similar_relative( $x, $y, $msg, $eps ) | ||
| 280 | |||
| 281 | is_similar_relative($x, $y, $eps ); | ||
| 282 | |||
| 283 | Returns true if $x has a relative error with repect to $y less than $eps. The | ||
| 284 | current default for $eps is the same as is_similar(), i.e. 1e-8. This doesn't | ||
| 285 | seem very useful. What should the default be? | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub is_similar_relative { | ||
| 290 | my ($x,$y, $eps) = @_; | ||
| 291 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 292 | return is_similar($x,$y,$eps, sub { abs( ($_[0] - $_[1])/abs($_[1]) ) } ); | ||
| 293 | } | ||
| 294 | |||
| 295 | =head2 ok_similar_relative( $x, $y, $msg, $eps ) | ||
| 296 | |||
| 297 | ok_similar_relative($x, $y, $msg, $eps ); | ||
| 298 | |||
| 299 | Pass a test if $x has a relative error with repect to $y less than $eps. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub ok_similar_relative { | ||
| 304 | my ($x,$y, $msg, $eps,) = @_; | ||
| 305 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 306 | ok(is_similar_relative($x,$y,$eps), $msg ); | ||
| 307 | } | ||
| 308 | |||
| 309 | 1; | ||