1 package Math
::GSL
::Histogram
::Test
;
2 use base
q{Test::Class};
4 use Math
::GSL qw
/:all/;
5 use Math
::GSL
::Histogram qw
/:all/;
6 use Math
::GSL
::Errno qw
/:all/;
10 BEGIN{ gsl_set_error_handler_off
(); }
12 sub make_fixture
: Test
(setup
) {
14 $self->{H
} = gsl_histogram_alloc
( 100 );
15 gsl_histogram_set_ranges_uniform
($self->{H
}, 0, 100);
18 sub teardown
: Test
(teardown
) {
19 unlink 'histogram' if -f
'histogram';
22 sub ALLOC_FREE
: Tests
{
23 my $H = gsl_histogram_alloc
( 100 );
24 isa_ok
($H, 'Math::GSL::Histogram' );
25 gsl_histogram_free
($H);
26 ok
(!$@
, 'gsl_histogram_free');
29 sub SET_RANGES_UNIFORM
: Tests
{
30 my $H = gsl_histogram_alloc
(100);
31 ok_status
(gsl_histogram_set_ranges_uniform
($H, 0, 100));
32 ok_status
(gsl_histogram_set_ranges_uniform
($H, 0, -100), $GSL_EINVAL);
35 sub SET_RANGES
: Tests
{
37 my $ranges = [ 0 .. 100 ];
39 ok_status
(gsl_histogram_set_ranges
($self->{H
}, $ranges, 100 + 1));
40 ok_status
(gsl_histogram_set_ranges
($self->{H
}, $ranges, 42), $GSL_EINVAL);
45 my $copy = gsl_histogram_clone
($self->{H
});
46 isa_ok
( $copy, 'Math::GSL::Histogram');
51 my $copy = gsl_histogram_alloc
(100);
53 ok_status
(gsl_histogram_memcpy
($copy, $self->{H
}));
55 my $bob = gsl_histogram_alloc
(50);
56 ok_status
(gsl_histogram_memcpy
($bob, $self->{H
}), $GSL_EINVAL);
60 sub INCREMENT
: Tests
{
63 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
64 ok_status
(gsl_histogram_increment
($self->{H
}, -150.5 ), $GSL_EDOM);
65 ok_status
(gsl_histogram_increment
($self->{H
}, 150.5 ), $GSL_EDOM);
71 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
72 cmp_ok
(1,'==', gsl_histogram_get
($self->{H
}, 50 ) );
77 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
79 cmp_ok
(100,'==', gsl_histogram_max
($self->{H
}));
80 cmp_ok
(0,'==', gsl_histogram_min
($self->{H
}));
83 sub MIN_VAL_MAX_VAL
: Tests
{
85 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
87 cmp_ok
(1,'==', gsl_histogram_max_val
($self->{H
}));
88 cmp_ok
(0,'==', gsl_histogram_min_val
($self->{H
}));
93 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
94 ok_status
(gsl_histogram_increment
($self->{H
}, 11.5 ));
96 ok_similar
(31, gsl_histogram_mean
($self->{H
}));
101 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
102 ok_status
(gsl_histogram_increment
($self->{H
}, 11.5 ));
103 ok_similar
(2, gsl_histogram_sum
($self->{H
}));
108 ok_status
(gsl_histogram_shift
($self->{H
}, 2));
109 is_deeply
( [ map { gsl_histogram_get
($self->{H
}, $_) } (0..99) ],
114 sub FWRITE_FREAD
: Tests
{
116 my $stream = fopen
("histogram", "w");
117 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
119 ok_status
(gsl_histogram_fwrite
($stream, $self->{H
}));
120 ok_status
(fclose
($stream));
122 $stream = fopen
("histogram", "r");
123 my $h = gsl_histogram_alloc
(100);
124 ok_status
(gsl_histogram_fread
($stream, $h));
125 is_deeply
( [ map { gsl_histogram_get
($h, $_) } (0..99) ],
126 [ (0) x
50, 1, (0) x
49 ]
128 ok_status
(fclose
($stream));
131 sub GET_RANGE
: Tests
{
133 my @got = gsl_histogram_get_range
($self->{H
}, 50);
135 is_deeply
( [ $got[1], $got[2]], [50, 51]);
140 my @got = gsl_histogram_find
($self->{H
}, 1);
142 cmp_ok
($got[1], '==', 1);
145 sub ACCUMULATE
: Tests
{
148 ok_status
(gsl_histogram_accumulate
($self->{H
}, 50.5, 3 ));
149 cmp_ok
(3,'==', gsl_histogram_get
($self->{H
}, 50 ) );
150 ok_status
(gsl_histogram_accumulate
($self->{H
}, -150.5, 3 ), $GSL_EDOM);
155 cmp_ok
(gsl_histogram_bins
($self->{H
}), '==', 100);
160 ok_status
(gsl_histogram_shift
($self->{H
}, 2));
161 ok_status
(gsl_histogram_reset
($self->{H
}));
162 is_deeply
( [ map { gsl_histogram_get
($self->{H
}, $_) } (0..99) ],
167 sub MIN_BIN_MAX_BIN
: Tests
{
169 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
170 cmp_ok
(gsl_histogram_min_bin
($self->{H
}), '==', 0);
171 cmp_ok
(gsl_histogram_max_bin
($self->{H
}), '==', 50);
174 sub GSL_HISTOGRAM_SIGMA
: Tests
{
176 map { gsl_histogram_increment
($self->{H
}, $_ ) } (0,1);
177 my $standard_deviation = gsl_histogram_sigma
($self->{H
});
178 ok_similar
( 0.5, $standard_deviation );
181 sub EQUAL_BINS_P
: Tests
{
183 my $h2 = gsl_histogram_alloc
(100);
184 gsl_histogram_set_ranges_uniform
($h2, 0, 100);
185 cmp_ok
(gsl_histogram_equal_bins_p
($self->{H
}, $h2), '==', 1);
186 gsl_histogram_set_ranges_uniform
($self->{H
}, 0, 50);
187 cmp_ok
(gsl_histogram_equal_bins_p
($self->{H
}, $h2), '==', 0);
192 my $h2 = gsl_histogram_alloc
(100);
193 gsl_histogram_set_ranges_uniform
($h2, 0, 100);
194 gsl_histogram_increment
($h2, 50.5 );
195 ok_status
(gsl_histogram_add
($self->{H
}, $h2));
196 cmp_ok
(gsl_histogram_get
($self->{H
}, 50), '==', 1);
201 my $h2 = gsl_histogram_alloc
(100);
202 gsl_histogram_set_ranges_uniform
($h2, 0, 100);
203 gsl_histogram_increment
($h2, 50.5 );
204 gsl_histogram_increment
($self->{H
}, 50.5 );
205 ok_status
(gsl_histogram_sub
($self->{H
}, $h2));
206 cmp_ok
(gsl_histogram_get
($self->{H
}, 50), '==', 0);
211 my $h2 = gsl_histogram_alloc
(100);
212 gsl_histogram_set_ranges_uniform
($h2, 0, 100);
213 gsl_histogram_accumulate
($h2, 50.5, 2);
214 gsl_histogram_accumulate
($self->{H
}, 50.5, 3);
215 ok_status
(gsl_histogram_mul
($self->{H
}, $h2));
216 cmp_ok
(gsl_histogram_get
($self->{H
}, 50), '==', 6);
221 my $h2 = gsl_histogram_alloc
(100);
222 ok_status
(gsl_histogram_set_ranges_uniform
($h2, 0, 100));
223 ok_status
(gsl_histogram_accumulate
($h2, 50.5, 2));
224 ok_status
(gsl_histogram_accumulate
($self->{H
}, 50.5, 4));
225 ok_status
(gsl_histogram_div
($self->{H
}, $h2));
226 cmp_ok
(gsl_histogram_get
($self->{H
}, 50), '==', 2);
231 gsl_histogram_accumulate
($self->{H
}, 50.5, 4);
232 gsl_histogram_increment
($self->{H
}, 33.5 );
233 ok_status
(gsl_histogram_scale
($self->{H
}, 2));
234 cmp_ok
(gsl_histogram_get
($self->{H
}, 50), '==', 8);
235 cmp_ok
(gsl_histogram_get
($self->{H
}, 33), '==', 2);
238 sub FPRINTF_FSCANF
: Tests
{
240 my $stream = fopen
("histogram", "w");
241 ok_status
(gsl_histogram_increment
($self->{H
}, 50.5 ));
243 ok_status
(gsl_histogram_fprintf
($stream, $self->{H
}, "%e", "%e"));
244 ok_status
(fclose
($stream));
246 $stream = fopen
("histogram", "r");
247 my $h = gsl_histogram_alloc
(100);
248 ok_status
(gsl_histogram_fscanf
($stream, $h));
249 is_deeply
( [ map { gsl_histogram_get
($h, $_) } (0..99) ],
250 [ (0) x
50, 1, (0) x
49 ]
252 ok_status
(fclose
($stream));
255 sub PDF_ALLOC
: Tests
{
256 my $pdf = gsl_histogram_pdf_alloc
(100);
257 isa_ok
($pdf, 'Math::GSL::Histogram' );
258 gsl_histogram_pdf_free
($pdf);
259 ok
(!$@
, 'gsl_histogram_free');
262 sub PDF_INIT
: Tests
{
264 my $p = gsl_histogram_pdf_alloc
(100);
265 ok_status
(gsl_histogram_pdf_init
($p, $self->{H
}));
266 gsl_histogram_accumulate
($self->{H
}, 50.5, -4);
267 ok_status
(gsl_histogram_pdf_init
($p, $self->{H
}), $GSL_EDOM);
270 sub GSL_HISTOGRAM_PDF_SAMPLE
: Tests
{
271 my $p = gsl_histogram_pdf_alloc
(100);
272 ok_status
(gsl_histogram_pdf_sample
( $p, 0.5 ));