1 package Math
::GSL
::Histogram2D
::Test
;
2 use Math
::GSL
::Test qw
/:all/;
3 use base
q{Test::Class};
5 use Math
::GSL qw
/:all/;
6 use Math
::GSL
::Histogram2D 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_histogram2d_alloc
( 100, 100 );
18 sub teardown
: Test
(teardown
) {
19 unlink 'histogram2d' if -f
'histogram2d';
22 sub GSL_HISTOGRAM2D_ALLOC
: Tests
{
23 my $h = gsl_histogram2d_alloc
(100,100);
24 isa_ok
($h, 'Math::GSL::Histogram2D');
25 gsl_histogram2d_free
($h);
26 ok
(!$@
, 'gsl_histogram_free');
29 sub GSL_HISTOGRAM2D_SET_RANGES
: Tests
{
31 my $ranges = [ 0 .. 100];
32 ok_status
(gsl_histogram2d_set_ranges
($self->{H
}, $ranges, 100 + 1, $ranges, 100+1));
35 sub GSL_HISTOGRAM2D_SET_RANGES_UNIFORM
: Tests
{
37 ok_status
(gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100));
42 my $copy = gsl_histogram2d_alloc
(100,100);
44 ok_status
(gsl_histogram2d_memcpy
($copy, $self->{H
}));
46 my $bob = gsl_histogram2d_alloc
(50,50);
47 ok_status
(gsl_histogram2d_memcpy
($bob, $self->{H
}), $GSL_EINVAL);
52 local $TODO = "gsl_histogram2d_clone does not return a gsl_histogram_t";
53 my $copy = gsl_histogram2d_clone
($self->{H
});
54 isa_ok
( $copy, 'Math::GSL::Histogram');
57 sub INCREMENT
: Tests
{
59 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
61 ok_status
(gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 ));
62 ok_status
(gsl_histogram2d_increment
($self->{H
}, -150.5, -150.5 ), $GSL_EDOM);
63 ok_status
(gsl_histogram2d_increment
($self->{H
}, 150.5, 150.5 ), $GSL_EDOM);
68 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
70 ok_status
(gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 ));
71 cmp_ok
(1,'==', gsl_histogram2d_get
($self->{H
}, 50, 50 ) );
74 sub XMIN_XMAX_YMIN_YMAX
: Tests
{
76 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
77 cmp_ok
(100,'==', gsl_histogram2d_xmax
($self->{H
}));
78 cmp_ok
(0,'==', gsl_histogram2d_xmin
($self->{H
}));
79 cmp_ok
(100,'==', gsl_histogram2d_ymax
($self->{H
}));
80 cmp_ok
(0,'==', gsl_histogram2d_ymin
($self->{H
}));
83 sub MIN_VAL_MAX_VAL
: Tests
{
85 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
86 ok_status
(gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 ));
88 cmp_ok
(1,'==', gsl_histogram2d_max_val
($self->{H
}));
89 cmp_ok
(0,'==', gsl_histogram2d_min_val
($self->{H
}));
94 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
95 ok_status
(gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 ));
96 ok_status
(gsl_histogram2d_increment
($self->{H
}, 11.5, 11.5 ));
98 ok_similar
(31, gsl_histogram2d_xmean
($self->{H
}));
99 ok_similar
(31, gsl_histogram2d_ymean
($self->{H
}));
104 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
106 ok_status
(gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 ));
107 ok_status
(gsl_histogram2d_increment
($self->{H
}, 11.5, 11.5 ));
108 ok_similar
(2, gsl_histogram2d_sum
($self->{H
}));
112 my $h = gsl_histogram2d_alloc
(5,5);
113 gsl_histogram2d_set_ranges_uniform
($h, 0, 5, 0, 5);
114 ok_status
(gsl_histogram2d_shift
($h, 2));
116 is_deeply
( [ map { gsl_histogram2d_get
($h, $_, $i) } (0..4) ],
121 sub FWRITE_FREAD
: Tests
{
122 my $H = gsl_histogram2d_alloc
(5,5);
123 my $stream = gsl_fopen
("histogram2d", 'w');
124 gsl_histogram2d_set_ranges_uniform
($H, 0, 5, 0, 5);
125 ok_status
(gsl_histogram2d_increment
($H, 0.5, 1.5 ));
127 ok_status
(gsl_histogram2d_fwrite
($stream, $H));
128 ok_status
(gsl_fclose
($stream));
130 $stream = gsl_fopen
("histogram2d", 'r');
131 my $h = gsl_histogram2d_alloc
(5, 5);
132 ok_status
(gsl_histogram2d_fread
($stream, $h));
133 is_deeply
( [ map { gsl_histogram2d_get
($h, 0, $_) } (0..4) ],
137 is_deeply
( [ map { gsl_histogram2d_get
($h, $i, $_) } (0..4) ],
140 ok_status
(gsl_fclose
($stream));
143 sub GET_XRANGE_YRANGE
: Tests
{
145 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
146 my @got = gsl_histogram2d_get_xrange
($self->{H
}, 50);
148 @got = gsl_histogram2d_get_yrange
($self->{H
}, 50);
150 is_deeply
( [ $got[1], $got[2]], [50, 51]);
151 is_deeply
( [ $got[1], $got[2]], [50, 51]);
156 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
157 my @got = gsl_histogram2d_find
($self->{H
}, 1, 1);
159 cmp_ok
($got[1], '==', 1);
160 cmp_ok
($got[2], '==', 1);
163 sub ACCUMULATE
: Tests
{
165 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
167 ok_status
(gsl_histogram2d_accumulate
($self->{H
}, 50.5, 50.5, 3 ));
168 cmp_ok
(3,'==', gsl_histogram2d_get
($self->{H
}, 50, 50 ) );
169 ok_status
(gsl_histogram2d_accumulate
($self->{H
}, -150.5, -150.5, 3 ), $GSL_EDOM);
174 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
175 cmp_ok
(gsl_histogram2d_nx
($self->{H
}), '==', 100);
176 cmp_ok
(gsl_histogram2d_ny
($self->{H
}), '==', 100);
180 my $h = gsl_histogram2d_alloc
(5,5);
181 gsl_histogram2d_set_ranges_uniform
($h, 0, 5, 0, 5);
182 gsl_histogram2d_shift
($h, 2);
183 gsl_histogram2d_reset
($h);
185 is_deeply
( [ map { gsl_histogram2d_get
($h, $i, $_) } (0..4) ],
190 sub MIN_BIN_MAX_BIN
: Tests
{
192 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
193 gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5);
194 cmp_ok
(gsl_histogram2d_min_bin
($self->{H
}), '==', 0);
195 cmp_ok
(gsl_histogram2d_max_bin
($self->{H
}), '==', 50);
198 sub GSL_HISTOGRAM2D_XSIGMA_YSIGMA
: Tests
{
199 local $TODO = "Don't know how to test this function";
202 sub EQUAL_BINS_P
: Tests
{
204 my $h2 = gsl_histogram2d_alloc
(100,100);
205 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
206 gsl_histogram2d_set_ranges_uniform
($h2, 0, 100, 0, 100);
207 cmp_ok
(gsl_histogram2d_equal_bins_p
($self->{H
}, $h2), '==', 1);
208 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 50, 0, 50);
209 cmp_ok
(gsl_histogram2d_equal_bins_p
($self->{H
}, $h2), '==', 0);
214 my $h2 = gsl_histogram2d_alloc
(100, 100);
215 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
216 gsl_histogram2d_set_ranges_uniform
($h2, 0, 100, 0, 100);
217 gsl_histogram2d_increment
($h2, 50.5, 50.5 );
218 ok_status
(gsl_histogram2d_add
($self->{H
}, $h2));
219 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 50, 50), '==', 1);
224 my $h2 = gsl_histogram2d_alloc
(100, 100);
225 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
226 gsl_histogram2d_set_ranges_uniform
($h2, 0, 100, 0, 100);
227 gsl_histogram2d_increment
($h2, 50.5, 50.5 );
228 gsl_histogram2d_increment
($self->{H
}, 50.5, 50.5 );
229 ok_status
(gsl_histogram2d_sub
($self->{H
}, $h2));
230 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 50, 50), '==', 0);
235 my $h2 = gsl_histogram2d_alloc
(100, 100);
236 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
237 gsl_histogram2d_set_ranges_uniform
($h2, 0, 100, 0, 100);
238 gsl_histogram2d_accumulate
($h2, 50.5, 50.5, 2);
239 gsl_histogram2d_accumulate
($self->{H
}, 50.5, 50.5, 3);
240 ok_status
(gsl_histogram2d_mul
($self->{H
}, $h2));
241 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 50, 50), '==', 6);
246 my $h2 = gsl_histogram2d_alloc
(100, 100);
247 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
248 gsl_histogram2d_set_ranges_uniform
($h2, 0, 100, 0, 100);
249 gsl_histogram2d_accumulate
($h2, 50.5, 50.5, 2);
250 gsl_histogram2d_accumulate
($self->{H
}, 50.5, 50.5, 4);
251 ok_status
(gsl_histogram2d_div
($self->{H
}, $h2));
252 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 50, 50), '==', 2);
257 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
258 gsl_histogram2d_accumulate
($self->{H
}, 50.5, 50.5, 4);
259 gsl_histogram2d_increment
($self->{H
}, 33.5, 33.5 );
260 ok_status
(gsl_histogram2d_scale
($self->{H
}, 2));
261 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 50, 50), '==', 8);
262 cmp_ok
(gsl_histogram2d_get
($self->{H
}, 33, 33), '==', 2);
265 sub FPRINTF_FSCANF
: Tests
{
266 my $H = gsl_histogram2d_alloc
(5,5);
267 my $stream = gsl_fopen
("histogram2d", 'w');
268 gsl_histogram2d_set_ranges_uniform
($H, 0, 5, 0, 5);
269 ok_status
(gsl_histogram2d_increment
($H, 0.5, 0.5 ));
271 ok_status
(gsl_histogram2d_fprintf
($stream, $H, "%e", "%e"));
272 ok_status
(gsl_fclose
($stream));
274 $stream = gsl_fopen
("histogram2d", 'r');
275 my $h = gsl_histogram2d_alloc
(5,5);
276 ok_status
(gsl_histogram2d_fscanf
($stream, $h));
277 is_deeply
( [ map { gsl_histogram2d_get
($h, 0, $_) } (0..4) ],
281 is_deeply
( [ map { gsl_histogram2d_get
($h, $i, $_) } (0..4) ],
284 ok_status
(gsl_fclose
($stream));
287 sub PDF_ALLOC
: Tests
{
288 my $pdf = gsl_histogram2d_pdf_alloc
(100,100);
289 isa_ok
($pdf, 'Math::GSL::Histogram2D' );
290 gsl_histogram2d_pdf_free
($pdf);
291 ok
(!$@
, 'gsl_histogram2d_free');
294 sub PDF_INIT
: Tests
{
296 my $p = gsl_histogram2d_pdf_alloc
(100, 100);
297 gsl_histogram2d_set_ranges_uniform
($self->{H
}, 0, 100, 0, 100);
298 ok_status
(gsl_histogram2d_pdf_init
($p, $self->{H
}));
299 gsl_histogram2d_accumulate
($self->{H
}, 50.5, 50.5, -4);
300 ok_status
(gsl_histogram2d_pdf_init
($p, $self->{H
}), $GSL_EDOM);
303 sub GSL_HISTOGRAM_PDF_SAMPLE
: Tests
{
304 local $TODO = "Don't know how to test this function";