Refactor inclusion of system.i to one place
[Math-GSL.git] / t / Spline.t
bloba5c94d2340a4e815601376dd9381d0095c763ece
1 package Math::GSL::Spline::Test;
2 use base 'Test::Class';
3 use Test::More  tests => 38;
4 use Math::GSL::Errno  qw/:all/;
5 use Math::GSL::Test   qw/:all/;
6 use Math::GSL::Spline qw/:all/;
7 use Math::GSL::Interp qw/:all/;
8 use Test::Exception;
9 use Data::Dumper;
10 use strict;
11 use warnings;
13 BEGIN{ gsl_set_error_handler_off() };
15 sub make_fixture : Test(setup) {
16     my $self = shift;
17     $self->{spline} = gsl_spline_alloc($gsl_interp_linear,100);
20 sub teardown : Test(teardown) {
22 sub TEST_FREE : Test {
23     my $self = shift;
24     gsl_spline_free($self->{spline});
25     ok(!$@ && !$! && !$?,'gsl_spline_free');
27 sub TEST_MIN_SIZE : Test {
28     my $self = shift;
29     cmp_ok(2,'==',gsl_spline_min_size($self->{spline}),'min_size');
31 sub TEST_NAME : Test {
32         my $self = shift;
33         my $spline = $self->{spline};
34         cmp_ok('linear', 'eq' , gsl_spline_name($spline));
36 sub TEST_ALLOC : Tests {
37     isa_ok(gsl_spline_alloc($_,100), 'Math::GSL::Spline') for (
38                $gsl_interp_linear,
39                $gsl_interp_polynomial,
40                $gsl_interp_cspline,
41                $gsl_interp_cspline_periodic,
42                $gsl_interp_akima,
43                $gsl_interp_akima_periodic);
46 sub MULTIPLE_TESTS : Tests {
47   my $a = gsl_interp_accel_alloc ();
48   my $spline = gsl_spline_alloc ($gsl_interp_polynomial, 4);
49   my $data_x = [ 0.0, 1.0, 2.0, 3.0 ];
50   my $data_y = [ 0.0, 1.0, 2.0, 3.0 ];
51   ok_status(gsl_spline_init($spline, $data_x, $data_y, 4), $GSL_SUCCESS);
52   my $test_x = [ 0.0, 0.5, 1.0, 1.5, 2.5, 3.0 ];
53   my $test_y = [ 0.0, 0.5, 1.0, 1.5, 2.5, 3.0 ];
54   my $test_dy = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ];
55   my $test_iy = [ 0.0, 0.125, 0.5, 9.0/8.0, 25.0/8.0, 9.0/2.0 ];
57   for my $i (0.. 3) {
58     my $x = $test_x->[$i];
59     my ($s1, $y) = gsl_spline_eval_e ($spline,$x, $a);
60     my ($s2, $deriv) = gsl_spline_eval_deriv_e ($spline, $x, $a);
61     my ($s3, $integ) = gsl_spline_eval_integ_e ($spline, $test_x->[0], $x, $a);
63     ok_status($s1);
64     ok_status($s2);
65     ok_status($s3);
67      ok_similar([$y, $deriv, $integ], [$test_y->[$i], $test_dy->[$i], $test_iy->[$i]], "eval_e, derive_e and integ_e",1e-10);
69      my $diff_y = $y - $test_y->[$i];
70      my $diff_deriv = $deriv - $test_dy->[$i];
71      my $diff_integ = $integ - $test_iy->[$i];
72      ok( abs($diff_y) < 1e-10, "diff_y");
73      ok( abs($diff_deriv) < 1e-10, "diff_deriv");
74      ok( abs($diff_integ) < 1e-10, "diff_integ");
75     }
76   gsl_interp_accel_free ($a);
77   gsl_spline_free ($spline);
80 Test::Class->runtests;