Refactor inclusion of system.i to one place
[Math-GSL.git] / t / Interp.t
bloba27a8a7a1ada79af31f60b4c60b9f49e64aa5324
1 package Math::GSL::Interp::Test;
2 use base q{Test::Class};
3 use Test::More tests => 35;
4 use Math::GSL         qw/:all/;
5 use Math::GSL::Interp qw/:all/;
6 use Math::GSL::Errno  qw/:all/;
7 use Math::GSL::Test   qw/:all/;
8 use Data::Dumper;
9 use strict;
11 sub make_fixture : Test(setup) {
14 sub teardown : Test(teardown) {
17 sub GSL_INTERP_ALLOC : Tests {
18  my $I = gsl_interp_alloc($gsl_interp_linear, 2);
19  isa_ok($I, 'Math::GSL::Interp');
22 sub GSL_INTERP_INIT : Tests {
25 sub GSL_INTERP_BSEARCH : Tests {
26   my $x_array = [ 0.0, 1.0, 2.0, 3.0, 4.0 ];
27   
28   # check an interior point
29   my $index_result = gsl_interp_bsearch($x_array, 1.5, 0, 4);
30   is($index_result, 1);
32   # check that we get the last interval if x == last value 
33   $index_result = gsl_interp_bsearch($x_array, 4.0, 0, 4);
34   is($index_result, 3);
36   # check that we get the first interval if x == first value
37   $index_result = gsl_interp_bsearch($x_array, 0.0, 0, 4);
38   is($index_result, 0);  
40   # check that we get correct interior boundary behaviour 
41   $index_result = gsl_interp_bsearch($x_array, 2.0, 0, 4);
42   is($index_result, 2);
44   # check out of bounds above 
45   $index_result = gsl_interp_bsearch($x_array, 10.0, 0, 4);
46   is($index_result, 3);
48   # check out of bounds below
49   $index_result = gsl_interp_bsearch($x_array, -10.0, 0, 4);
50   is($index_result, 0);
53 sub MULTIPLE_TESTS : Tests {
54   my $a = gsl_interp_accel_alloc ();
55   my $interp = gsl_interp_alloc ($gsl_interp_polynomial, 4);
56   my $data_x = [ 0.0, 1.0, 2.0, 3.0 ];
57   my $data_y = [ 0.0, 1.0, 2.0, 3.0 ];
58   my $test_x = [ 0.0, 0.5, 1.0, 1.5, 2.5, 3.0 ];
59   my $test_y = [ 0.0, 0.5, 1.0, 1.5, 2.5, 3.0 ];
60   my $test_dy = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ];
61   my $test_iy = [ 0.0, 0.125, 0.5, 9.0/8.0, 25.0/8.0, 9.0/2.0 ];
63   gsl_interp_init ($interp, $data_x, $data_y, 4);
64   for my $i (0.. 3) {
65     my $x = $test_x->[$i];
66     my ($s1, $y) = gsl_interp_eval_e ($interp, $data_x, $data_y, $x, $a);
67     my ($s2, $deriv) = gsl_interp_eval_deriv_e ($interp, $data_x, $data_y, $x, $a);
68     my ($s3, $integ) = gsl_interp_eval_integ_e ($interp, $data_x, $data_y, $test_x->[0], $x, $a);
70     ok_status($s1);
71     ok_status($s2);
72     ok_status($s3);
74      ok_similar([$y, $deriv, $integ], [$test_y->[$i], $test_dy->[$i], $test_iy->[$i]], "eval_e, derive_e and integ_e",1e-10);
76      my $diff_y = $y - $test_y->[$i];
77      my $diff_deriv = $deriv - $test_dy->[$i];
78      my $diff_integ = $integ - $test_iy->[$i];
79      ok( abs($diff_y) < 1e-10, "diff_y");
80      ok( abs($diff_deriv) < 1e-10, "diff_deriv");
81      ok( abs($diff_integ) < 1e-10, "diff_integ");
82     }
83   gsl_interp_accel_free ($a);
84   gsl_interp_free ($interp);
86 Test::Class->runtests;