Skip to content
Newer
Older
100644 316 lines (267 sloc) 10.3 KB
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
1 package Math::GSL::Histogram2D::Test;
2 use base q{Test::Class};
a316328 @leto Write a todo test for gsl_histogram2d_xsigma
authored Sep 6, 2011
3 use Test::More tests => 89;
0cac639 @leto Continuing to add # of tests and refactoring tests
authored Nov 27, 2008
4 use Math::GSL qw/:all/;
5 use Math::GSL::Test qw/:all/;
6 use Math::GSL::Errno qw/:all/;
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
7 use Math::GSL::Histogram2D qw/:all/;
8 use Data::Dumper;
9 use strict;
10
11 BEGIN{ gsl_set_error_handler_off(); }
12
13 sub make_fixture : Test(setup) {
14 my $self = shift;
15 $self->{H} = gsl_histogram2d_alloc( 100, 100 );
b848634 @leto Call gsl_histogram2d_set_ranges_uniform before calling gsl_histogram2…
authored Sep 6, 2011
16 gsl_histogram2d_set_ranges_uniform($self->{H}, 1, 100, 1, 100);
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
17 }
18
19 sub teardown : Test(teardown) {
20 unlink 'histogram2d' if -f 'histogram2d';
21 }
22
23 sub GSL_HISTOGRAM2D_ALLOC : Tests {
24 my $h = gsl_histogram2d_alloc(100,100);
25 isa_ok($h, 'Math::GSL::Histogram2D');
26 gsl_histogram2d_free($h);
27 ok(!$@, 'gsl_histogram_free');
28 }
29
30 sub GSL_HISTOGRAM2D_SET_RANGES : Tests {
31 my $self = shift;
32 my $ranges = [ 0 .. 100];
33 ok_status(gsl_histogram2d_set_ranges($self->{H}, $ranges, 100 + 1, $ranges, 100+1));
34 }
35
36 sub GSL_HISTOGRAM2D_SET_RANGES_UNIFORM : Tests {
37 my $self = shift;
38 ok_status(gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100));
39 }
40
41 sub MEMCPY : Tests {
42 my $self = shift;
43 my $copy = gsl_histogram2d_alloc(100,100);
44
45 ok_status(gsl_histogram2d_memcpy($copy, $self->{H}));
46
47 my $bob = gsl_histogram2d_alloc(50,50);
48 ok_status(gsl_histogram2d_memcpy($bob, $self->{H}), $GSL_EINVAL);
49 }
50
b848634 @leto Call gsl_histogram2d_set_ranges_uniform before calling gsl_histogram2…
authored Sep 6, 2011
51 sub CLONE : Tests {
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
52 my $self = shift;
53 my $copy = gsl_histogram2d_clone($self->{H});
b848634 @leto Call gsl_histogram2d_set_ranges_uniform before calling gsl_histogram2…
authored Sep 6, 2011
54 isa_ok( $copy, 'Math::GSL::Histogram2D::gsl_histogram2d');
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
55 }
56
57 sub INCREMENT : Tests {
58 my $self = shift;
59 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
60
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);
64 }
65
66 sub GET : Tests {
67 my $self = shift;
68 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
69
70 ok_status(gsl_histogram2d_increment($self->{H}, 50.5, 50.5 ));
71 cmp_ok(1,'==', gsl_histogram2d_get($self->{H}, 50, 50 ) );
72 }
73
74 sub XMIN_XMAX_YMIN_YMAX : Tests {
75 my $self = shift;
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}));
81 }
82
83 sub MIN_VAL_MAX_VAL : Tests {
84 my $self = shift;
85 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
86 ok_status(gsl_histogram2d_increment($self->{H}, 50.5, 50.5 ));
87
88 cmp_ok(1,'==', gsl_histogram2d_max_val($self->{H}));
89 cmp_ok(0,'==', gsl_histogram2d_min_val($self->{H}));
90 }
91
92 sub MEAN : Tests {
93 my $self = shift;
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 ));
97
98 ok_similar(31, gsl_histogram2d_xmean($self->{H}));
99 ok_similar(31, gsl_histogram2d_ymean($self->{H}));
100 }
101
102 sub SUM : Tests {
103 my $self = shift;
104 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
105
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}));
109 }
110
111 sub SHIFT : Tests {
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));
115 for my $i (0..4) {
116 is_deeply( [ map { gsl_histogram2d_get($h, $_, $i) } (0..4) ],
117 [ (2) x 5 ]
118 );}
119 }
120
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 ));
126
127 ok_status(gsl_histogram2d_fwrite($stream, $H));
128 ok_status(gsl_fclose($stream));
129
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) ],
134 [ 0, 1, (0) x 3 ]
135 );
136 for my $i (1..4) {
137 is_deeply( [ map { gsl_histogram2d_get($h, $i, $_) } (0..4) ],
138 [ (0) x 5 ]
139 );}
140 ok_status(gsl_fclose($stream));
141 }
142
143 sub GET_XRANGE_YRANGE : Tests {
144 my $self = shift;
145 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
146 my @got = gsl_histogram2d_get_xrange($self->{H}, 50);
147 ok_status($got[0]);
148 @got = gsl_histogram2d_get_yrange($self->{H}, 50);
149 ok_status($got[0]);
150 is_deeply( [ $got[1], $got[2]], [50, 51]);
151 is_deeply( [ $got[1], $got[2]], [50, 51]);
152 }
153
154 sub FIND : Tests {
155 my $self = shift;
156 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
157 my @got = gsl_histogram2d_find($self->{H}, 1, 1);
158 ok_status($got[0]);
159 cmp_ok($got[1], '==', 1);
160 cmp_ok($got[2], '==', 1);
161 }
162
163 sub ACCUMULATE : Tests {
164 my $self = shift;
165 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
166
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);
170 }
171
172 sub NX_NY : Tests {
173 my $self = shift;
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);
177 }
178
179 sub RESET : Tests {
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);
184 for my $i (0..4) {
185 is_deeply( [ map { gsl_histogram2d_get($h, $i, $_) } (0..4) ],
186 [ (0) x 5 ]
187 );}
188 }
189
190 sub MIN_BIN_MAX_BIN : Tests {
191 my $self = shift;
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);
196 }
197
a316328 @leto Write a todo test for gsl_histogram2d_xsigma
authored Sep 6, 2011
198 sub GSL_HISTOGRAM2D_XSIGMA : Tests(1) {
199 my $self = shift;
200
201 local $TODO = "gsl_histogram2d_xsigma";
202
203 gsl_histogram2d_increment($self->{H}, 0, 42);
204 gsl_histogram2d_increment($self->{H}, 42, 1);
205
206 my $xsigma = gsl_histogram2d_xsigma($self->{H});
207 # what is the correct answer?
208 is_similar(42, $xsigma, 1e-8, 'gsl_histogram2d_xsigma');
83513e9 @leto Move tests out of lib/Math/GSL/*/Test.pm into t/*.t directly
authored Aug 31, 2008
209 }
210
211 sub EQUAL_BINS_P : Tests {
212 my $self = shift;
213 my $h2 = gsl_histogram2d_alloc(100,100);
214 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
215 gsl_histogram2d_set_ranges_uniform($h2, 0, 100, 0, 100);
216 cmp_ok(gsl_histogram2d_equal_bins_p($self->{H}, $h2), '==', 1);
217 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 50, 0, 50);
218 cmp_ok(gsl_histogram2d_equal_bins_p($self->{H}, $h2), '==', 0);
219 }
220
221 sub ADD : Tests {
222 my $self = shift;
223 my $h2 = gsl_histogram2d_alloc(100, 100);
224 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
225 gsl_histogram2d_set_ranges_uniform($h2, 0, 100, 0, 100);
226 gsl_histogram2d_increment($h2, 50.5, 50.5 );
227 ok_status(gsl_histogram2d_add($self->{H}, $h2));
228 cmp_ok(gsl_histogram2d_get($self->{H}, 50, 50), '==', 1);
229 }
230
231 sub SUB : Tests {
232 my $self = shift;
233 my $h2 = gsl_histogram2d_alloc(100, 100);
234 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
235 gsl_histogram2d_set_ranges_uniform($h2, 0, 100, 0, 100);
236 gsl_histogram2d_increment($h2, 50.5, 50.5 );
237 gsl_histogram2d_increment($self->{H}, 50.5, 50.5 );
238 ok_status(gsl_histogram2d_sub($self->{H}, $h2));
239 cmp_ok(gsl_histogram2d_get($self->{H}, 50, 50), '==', 0);
240 }
241
242 sub MUL : Tests {
243 my $self = shift;
244 my $h2 = gsl_histogram2d_alloc(100, 100);
245 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
246 gsl_histogram2d_set_ranges_uniform($h2, 0, 100, 0, 100);
247 gsl_histogram2d_accumulate($h2, 50.5, 50.5, 2);
248 gsl_histogram2d_accumulate($self->{H}, 50.5, 50.5, 3);
249 ok_status(gsl_histogram2d_mul($self->{H}, $h2));
250 cmp_ok(gsl_histogram2d_get($self->{H}, 50, 50), '==', 6);
251 }
252
253 sub DIV : Tests {
254 my $self = shift;
255 my $h2 = gsl_histogram2d_alloc(100, 100);
256 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
257 gsl_histogram2d_set_ranges_uniform($h2, 0, 100, 0, 100);
258 gsl_histogram2d_accumulate($h2, 50.5, 50.5, 2);
259 gsl_histogram2d_accumulate($self->{H}, 50.5, 50.5, 4);
260 ok_status(gsl_histogram2d_div($self->{H}, $h2));
261 cmp_ok(gsl_histogram2d_get($self->{H}, 50, 50), '==', 2);
262 }
263
264 sub SCALE : Tests {
265 my $self = shift;
266 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
267 gsl_histogram2d_accumulate($self->{H}, 50.5, 50.5, 4);
268 gsl_histogram2d_increment($self->{H}, 33.5, 33.5 );
269 ok_status(gsl_histogram2d_scale($self->{H}, 2));
270 cmp_ok(gsl_histogram2d_get($self->{H}, 50, 50), '==', 8);
271 cmp_ok(gsl_histogram2d_get($self->{H}, 33, 33), '==', 2);
272 }
273
274 sub FPRINTF_FSCANF : Tests {
275 my $H = gsl_histogram2d_alloc(5,5);
276 my $stream = gsl_fopen("histogram2d", 'w');
277 gsl_histogram2d_set_ranges_uniform($H, 0, 5, 0, 5);
278 ok_status(gsl_histogram2d_increment($H, 0.5, 0.5 ));
279
280 ok_status(gsl_histogram2d_fprintf($stream, $H, "%e", "%e"));
281 ok_status(gsl_fclose($stream));
282
283 $stream = gsl_fopen("histogram2d", 'r');
284 my $h = gsl_histogram2d_alloc(5,5);
285 ok_status(gsl_histogram2d_fscanf($stream, $h));
286 is_deeply( [ map { gsl_histogram2d_get($h, 0, $_) } (0..4) ],
287 [ 1, (0) x 4 ]
288 );
289 for my $i (1..4) {
290 is_deeply( [ map { gsl_histogram2d_get($h, $i, $_) } (0..4) ],
291 [ (0) x 5 ]
292 );}
293 ok_status(gsl_fclose($stream));
294 }
295
296 sub PDF_ALLOC : Tests {
297 my $pdf = gsl_histogram2d_pdf_alloc(100,100);
298 isa_ok($pdf, 'Math::GSL::Histogram2D' );
299 gsl_histogram2d_pdf_free($pdf);
300 ok(!$@, 'gsl_histogram2d_free');
301 }
302
303 sub PDF_INIT : Tests {
304 my $self = shift;
305 my $p = gsl_histogram2d_pdf_alloc(100, 100);
306 gsl_histogram2d_set_ranges_uniform($self->{H}, 0, 100, 0, 100);
307 ok_status(gsl_histogram2d_pdf_init ($p, $self->{H}));
308 gsl_histogram2d_accumulate($self->{H}, 50.5, 50.5, -4);
309 ok_status(gsl_histogram2d_pdf_init ($p, $self->{H}), $GSL_EDOM);
310 }
311
312 sub GSL_HISTOGRAM_PDF_SAMPLE : Tests {
313 local $TODO = "Don't know how to test this function";
314 }
175afb7 @Moisan Adding tests to Histogram2D, still need to check if every functions a…
Moisan authored Jul 28, 2008
315 Test::Class->runtests;
Something went wrong with that request. Please try again.