1 package Math::GSL::Histogram::Test;
2 use base q{Test::Class};
3 use Test::More tests => 74;
4 use Math::GSL qw/:all/;
5 use Math::GSL::Histogram qw/:all/;
6 use Math::GSL::Test qw/:all/;
7 use Math::GSL::Errno qw/:all/;
11 BEGIN{ gsl_set_error_handler_off(); }
13 sub make_fixture : Test(setup) {
15 $self->{H} = gsl_histogram_alloc( 100 );
16 gsl_histogram_set_ranges_uniform($self->{H}, 0, 100);
19 sub teardown : Test(teardown) {
20 unlink 'histogram' if -f 'histogram';
23 sub ALLOC_FREE : Tests {
24 my $H = gsl_histogram_alloc( 100 );
25 isa_ok($H, 'Math::GSL::Histogram' );
26 gsl_histogram_free($H);
27 ok(!$@, 'gsl_histogram_free');
30 sub SET_RANGES_UNIFORM : Tests {
31 my $H = gsl_histogram_alloc(100);
32 ok_status(gsl_histogram_set_ranges_uniform($H, 0, 100));
33 ok_status(gsl_histogram_set_ranges_uniform($H, 0, -100), $GSL_EINVAL);
36 sub SET_RANGES : Tests {
38 my $ranges = [ 0 .. 100 ];
40 ok_status(gsl_histogram_set_ranges($self->{H}, $ranges, 100 + 1));
41 ok_status(gsl_histogram_set_ranges($self->{H}, $ranges, 42), $GSL_EINVAL);
46 my $copy = gsl_histogram_clone($self->{H});
47 isa_ok( $copy, 'Math::GSL::Histogram');
52 my $copy = gsl_histogram_alloc(100);
54 ok_status(gsl_histogram_memcpy($copy, $self->{H}));
56 my $bob = gsl_histogram_alloc(50);
57 ok_status(gsl_histogram_memcpy($bob, $self->{H}), $GSL_EINVAL);
61 sub INCREMENT : Tests {
64 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
65 ok_status(gsl_histogram_increment($self->{H}, -150.5 ), $GSL_EDOM);
66 ok_status(gsl_histogram_increment($self->{H}, 150.5 ), $GSL_EDOM);
72 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
73 cmp_ok(1,'==', gsl_histogram_get($self->{H}, 50 ) );
78 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
80 cmp_ok(100,'==', gsl_histogram_max($self->{H}));
81 cmp_ok(0,'==', gsl_histogram_min($self->{H}));
84 sub MIN_VAL_MAX_VAL : Tests {
86 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
88 cmp_ok(1,'==', gsl_histogram_max_val($self->{H}));
89 cmp_ok(0,'==', gsl_histogram_min_val($self->{H}));
94 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
95 ok_status(gsl_histogram_increment($self->{H}, 11.5 ));
97 ok_similar(31, gsl_histogram_mean($self->{H}));
102 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
103 ok_status(gsl_histogram_increment($self->{H}, 11.5 ));
104 ok_similar(2, gsl_histogram_sum($self->{H}));
109 ok_status(gsl_histogram_shift($self->{H}, 2));
110 is_deeply( [ map { gsl_histogram_get($self->{H}, $_) } (0..99) ],
115 sub FWRITE_FREAD : Tests {
117 my $stream = gsl_fopen("histogram", 'w');
118 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
120 ok_status(gsl_histogram_fwrite($stream, $self->{H}));
121 ok_status(gsl_fclose($stream));
123 $stream = gsl_fopen("histogram", 'r');
124 my $h = gsl_histogram_alloc(100);
125 ok_status(gsl_histogram_fread($stream, $h));
126 is_deeply( [ map { gsl_histogram_get($h, $_) } (0..99) ],
127 [ (0) x 50, 1, (0) x 49 ]
129 ok_status(gsl_fclose($stream));
132 sub GET_RANGE : Tests {
134 my @got = gsl_histogram_get_range($self->{H}, 50);
136 is_deeply( [ $got[1], $got[2]], [50, 51]);
141 my @got = gsl_histogram_find($self->{H}, 1);
143 cmp_ok($got[1], '==', 1);
146 sub ACCUMULATE : Tests {
149 ok_status(gsl_histogram_accumulate($self->{H}, 50.5, 3 ));
150 cmp_ok(3,'==', gsl_histogram_get($self->{H}, 50 ) );
151 ok_status(gsl_histogram_accumulate($self->{H}, -150.5, 3 ), $GSL_EDOM);
156 cmp_ok(gsl_histogram_bins($self->{H}), '==', 100);
161 gsl_histogram_shift($self->{H}, 2);
162 gsl_histogram_reset($self->{H});
163 is_deeply( [ map { gsl_histogram_get($self->{H}, $_) } (0..99) ],
168 sub MIN_BIN_MAX_BIN : Tests {
170 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
171 cmp_ok(gsl_histogram_min_bin($self->{H}), '==', 0);
172 cmp_ok(gsl_histogram_max_bin($self->{H}), '==', 50);
175 sub GSL_HISTOGRAM_SIGMA : Tests {
177 map { gsl_histogram_increment($self->{H}, $_ ) } (0,1);
178 my $standard_deviation = gsl_histogram_sigma($self->{H});
179 ok_similar ( 0.5, $standard_deviation );
182 sub EQUAL_BINS_P : Tests {
184 my $h2 = gsl_histogram_alloc(100);
185 gsl_histogram_set_ranges_uniform($h2, 0, 100);
186 cmp_ok(gsl_histogram_equal_bins_p($self->{H}, $h2), '==', 1);
187 gsl_histogram_set_ranges_uniform($self->{H}, 0, 50);
188 cmp_ok(gsl_histogram_equal_bins_p($self->{H}, $h2), '==', 0);
193 my $h2 = gsl_histogram_alloc(100);
194 gsl_histogram_set_ranges_uniform($h2, 0, 100);
195 gsl_histogram_increment($h2, 50.5 );
196 ok_status(gsl_histogram_add($self->{H}, $h2));
197 cmp_ok(gsl_histogram_get($self->{H}, 50), '==', 1);
202 my $h2 = gsl_histogram_alloc(100);
203 gsl_histogram_set_ranges_uniform($h2, 0, 100);
204 gsl_histogram_increment($h2, 50.5 );
205 gsl_histogram_increment($self->{H}, 50.5 );
206 ok_status(gsl_histogram_sub($self->{H}, $h2));
207 cmp_ok(gsl_histogram_get($self->{H}, 50), '==', 0);
212 my $h2 = gsl_histogram_alloc(100);
213 gsl_histogram_set_ranges_uniform($h2, 0, 100);
214 gsl_histogram_accumulate($h2, 50.5, 2);
215 gsl_histogram_accumulate($self->{H}, 50.5, 3);
216 ok_status(gsl_histogram_mul($self->{H}, $h2));
217 cmp_ok(gsl_histogram_get($self->{H}, 50), '==', 6);
222 my $h2 = gsl_histogram_alloc(100);
223 ok_status(gsl_histogram_set_ranges_uniform($h2, 0, 100));
224 ok_status(gsl_histogram_accumulate($h2, 50.5, 2));
225 ok_status(gsl_histogram_accumulate($self->{H}, 50.5, 4));
226 ok_status(gsl_histogram_div($self->{H}, $h2));
227 cmp_ok(gsl_histogram_get($self->{H}, 50), '==', 2);
232 gsl_histogram_accumulate($self->{H}, 50.5, 4);
233 gsl_histogram_increment($self->{H}, 33.5 );
234 ok_status(gsl_histogram_scale($self->{H}, 2));
235 cmp_ok(gsl_histogram_get($self->{H}, 50), '==', 8);
236 cmp_ok(gsl_histogram_get($self->{H}, 33), '==', 2);
239 sub FPRINTF_FSCANF : Tests {
241 my $stream = gsl_fopen("histogram", 'w');
242 ok_status(gsl_histogram_increment($self->{H}, 50.5 ));
244 ok_status(gsl_histogram_fprintf($stream, $self->{H}, "%e", "%e"));
245 ok_status(gsl_fclose($stream));
247 $stream = gsl_fopen("histogram", 'r');
248 my $h = gsl_histogram_alloc(100);
249 ok_status(gsl_histogram_fscanf($stream, $h));
250 is_deeply( [ map { gsl_histogram_get($h, $_) } (0..99) ],
251 [ (0) x 50, 1, (0) x 49 ]
253 ok_status(gsl_fclose($stream));
256 sub PDF_ALLOC : Tests {
257 my $pdf = gsl_histogram_pdf_alloc(100);
258 isa_ok($pdf, 'Math::GSL::Histogram' );
259 gsl_histogram_pdf_free($pdf);
260 ok(!$@, 'gsl_histogram_free');
263 sub PDF_INIT : Tests {
265 my $p = gsl_histogram_pdf_alloc(100);
266 ok_status(gsl_histogram_pdf_init ($p, $self->{H}));
267 gsl_histogram_accumulate($self->{H}, 50.5, -4);
268 ok_status(gsl_histogram_pdf_init ($p, $self->{H}), $GSL_EDOM);
271 sub GSL_HISTOGRAM_PDF_SAMPLE : Tests {
272 my $p = gsl_histogram_pdf_alloc(100);
273 ok_status(gsl_histogram_pdf_sample( $p, 0.5 ));
275 Test::Class->runtests;