Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 210 lines (179 sloc) 6.774 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
package Math::GSL::Permutation::Test;
use base q{Test::Class};
use Test::More tests => 85;
use Math::GSL qw/:all/;
use Math::GSL::Vector qw/:all/;
use Math::GSL::Test qw/:all/;
use Math::GSL::Errno qw/:all/;
use Math::GSL::Permutation qw/:all/;
use Test::Exception;
use Data::Dumper;
use strict;

BEGIN { gsl_set_error_handler_off(); }
sub make_fixture : Test(setup) {
    my $self = shift;
    $self->{permutation} = gsl_permutation_alloc(6);
}

sub teardown : Test(teardown) {
    my $self = shift;
    unlink 'permutation' if -f 'permutation';

    gsl_permutation_free($self->{permutation});
}

sub GSL_PERMUTATION_ALLOC : Tests {
    my $p = gsl_permutation_alloc(6);
    isa_ok($p, 'Math::GSL::Permutation');
}

sub GSL_PERMUTATION_GET_INIT : Tests {
    my $self = shift;
    gsl_permutation_init($self->{permutation});
    map { is(gsl_permutation_get($self->{permutation}, $_), $_) } (0..5);
}
    
sub GSL_PERMUTATION_CALLOC : Tests {
    my $p = gsl_permutation_calloc(6);
    isa_ok($p, 'Math::GSL::Permutation');
    map { is(gsl_permutation_get($p, $_), $_) } (0..5);
}
    
sub GSL_PERMUTATION_MEMCPY : Tests {
    my $self = shift;
    my $p = gsl_permutation_alloc(6);
    gsl_permutation_init($self->{permutation});
    gsl_permutation_memcpy($p, $self->{permutation});
    map { is(gsl_permutation_get($p, $_), $_) } (0..5);
}

sub GSL_PERMUTATION_SWAP : Tests {
    my $self=shift;
    gsl_permutation_init($self->{permutation});
    is(gsl_permutation_swap($self->{permutation}, 0, 5), 0);
    is(gsl_permutation_get($self->{permutation}, 0), 5);
    is(gsl_permutation_get($self->{permutation}, 5), 0);
    map { is(gsl_permutation_get($self->{permutation}, $_), $_) } (1..4);
}

sub GSL_PERMUTATION_SIZE : Tests {
    my $self=shift;
    gsl_permutation_init($self->{permutation});
    is(gsl_permutation_size($self->{permutation}), 6);
}

sub GSL_PERMUTATION_VALID : Tests {
    my $self=shift;
    gsl_permutation_init($self->{permutation});
    is(gsl_permutation_valid($self->{permutation}), 0);
}

sub GSL_PERMUTATION_REVERSE : Tests {
    my $self=shift;
    gsl_permutation_init($self->{permutation});
    gsl_permutation_reverse($self->{permutation});
    
    is(gsl_permutation_get($self->{permutation}, 0), 5);
    is(gsl_permutation_get($self->{permutation}, 1), 4);
    is(gsl_permutation_get($self->{permutation}, 2), 3);
    is(gsl_permutation_get($self->{permutation}, 3), 2);
    is(gsl_permutation_get($self->{permutation}, 4), 1);
    is(gsl_permutation_get($self->{permutation}, 5), 0);
}

sub GSL_PERMUTATION_INVERSE : Tests {
    my $self = shift;
    my $p = gsl_permutation_alloc(6);
    gsl_permutation_init($self->{permutation});

    gsl_permutation_inverse($p, $self->{permutation});
    map { is(gsl_permutation_get($p, $_), $_) } (0..5);
}

sub GSL_PERMUTATION_NEXT : Tests {
    my $self = shift;
    gsl_permutation_init($self->{permutation});
    is(gsl_permutation_next($self->{permutation}), 0);
    map { is(gsl_permutation_get($self->{permutation}, $_), $_) } (0..3);
    is(gsl_permutation_get($self->{permutation}, 4), 5);
    is(gsl_permutation_get($self->{permutation}, 5), 4);
}

sub GSL_PERMUTATION_PREV : Tests {
    my $self = shift;
    gsl_permutation_init($self->{permutation});
    gsl_permutation_swap($self->{permutation}, 4, 5);
    is(gsl_permutation_prev($self->{permutation}), 0);
    map { is(gsl_permutation_get($self->{permutation}, $_), $_) } (0..5);
}

#sub GSL_PERMUTE : Tests {
# my $self = shift;
# my @data = [5, 4, 3, 2, 1, 0];
# gsl_permutation_init($self->{permutation});
# gsl_permute($self->{permutation}, \@data, 1); # need a typemap to input and output an array of double
# map { is($data[$_], $_) } (0..5);
#}

#sub GSL_PERMUTE_INVERSE : Tests {
# my $self = shift;
# my @data = [5, 4, 3, 2, 1, 0];
# gsl_permutation_init($self->{permutation});
# gsl_permute_inverse($self->{permutation}, \@data, 1); # need a typemap to input and output an array of double
# map { is($data[$_], $_) } (0..5);
#}

sub GSL_PERMUTE_VECTOR : Tests {
     my $self = shift;
     gsl_permutation_init($self->{permutation});
     gsl_permutation_swap($self->{permutation}, 0, 1);
    
     my $vec = gsl_vector_alloc(6);
     map { gsl_vector_set($vec, $_, $_) } (0..5);
     gsl_permute_vector($self->{permutation}, $vec);
     is(gsl_vector_get($vec, 0), 1);
     is(gsl_vector_get($vec, 1), 0);
     map { is(gsl_vector_get($vec, $_), $_) } (2..5);
}

sub GSL_PERMUTE_VECTOR_INVERSE : Tests {
     my $self = shift;
     gsl_permutation_init($self->{permutation});
     gsl_permutation_swap($self->{permutation}, 0, 1);
    
     my $vec = gsl_vector_alloc(6);
     map { gsl_vector_set($vec, $_, $_) } (0..5);
     gsl_permute_vector_inverse($self->{permutation}, $vec);
     is(gsl_vector_get($vec, 0), 1);
     is(gsl_vector_get($vec, 1), 0);
     map { is(gsl_vector_get($vec, $_), $_) } (2..5);
}

sub GSL_PERMUTATION_MUL : Tests {
     my $self = shift;
     gsl_permutation_init($self->{permutation});
     gsl_permutation_swap($self->{permutation}, 0, 1);

     my $p2 = gsl_permutation_alloc(6);
     gsl_permutation_init($p2);
     gsl_permutation_swap($p2, 0, 5);

     my $p = gsl_permutation_alloc(6) ;
     gsl_permutation_mul ($p, $p2, $self->{permutation});
     is(gsl_permutation_get($p, 0), 5);
     is(gsl_permutation_get($p, 1), 0);
     is(gsl_permutation_get($p, 5), 1);
     map { is(gsl_permutation_get($p, $_), $_)} (2..4);
}

sub GSL_PERMUTATION_FWRITE_FREAD : Tests {
    my $self = shift;
    gsl_permutation_init($self->{permutation});
    my $fh = gsl_fopen("permutation", 'w');
    gsl_permutation_fwrite($fh, $self->{permutation});
    fclose($fh);

    my $p = gsl_permutation_alloc(6);
    $fh = gsl_fopen("permutation", 'r');
    gsl_permutation_fread($fh, $p);
    map { is(gsl_permutation_get($p, $_), $_) } (0..5);
    fclose($fh);
}

sub GSL_PERMUTATION_FPRINTF_FSCANF : Tests {
    my $self = shift;
    my $fh = gsl_fopen("permutation", 'w');
    gsl_permutation_init($self->{permutation});
    ok_status( gsl_permutation_fprintf($fh, $self->{permutation}, "%f"));
    ok_status(gsl_fclose($fh));

    local $TODO = "odd error with fscanf";
    $fh = gsl_fopen("permutation", 'r');
    my $p = gsl_permutation_alloc(6);
    ok_status(gsl_permutation_fscanf($fh, $p));
    is_deeply( [ map {gsl_permutation_get($p, $_) } (0..5) ],
               [ 0 .. 5 ],
    );
    #ok_status(gsl_fclose($fh));
}

sub NEW: Tests {
    my $perm = Math::GSL::Permutation->new(42);
    isa_ok($perm, 'Math::GSL::Permutation' );
}

sub AS_LIST: Tests {
    my $perm = Math::GSL::Permutation->new(5);
    is_deeply( [ $perm->as_list ] , [ 0 .. 4 ] );
}
Test::Class->runtests;
Something went wrong with that request. Please try again.