fix data, labels, style draw() option handling to not set state in the
[imager-graph.git] / t / t10pie.t
CommitLineData
35574351
TC
1#!perl -w
2use strict;
3use Imager::Graph::Pie;
86cab8ec
TC
4use lib 't/lib';
5use Imager::Font::Test;
bfcf9414 6use Test::More;
35574351
TC
7
8-d 'testout'
9 or mkdir "testout", 0700
10 or die "Could not create output directory: $!";
11
12++$|;
35574351 13
35574351
TC
14use Imager qw(:handy);
15
86cab8ec
TC
16#my $fontfile = 'ImUgly.ttf';
17#my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
18# or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
19my $font = Imager::Font::Test->new();
35574351
TC
20
21my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
22my @labels = qw(alpha beta gamma delta epsilon phi gi);
23
a17e870a 24plan tests => 35;
bfcf9414 25
35574351
TC
26my $pie = Imager::Graph::Pie->new;
27ok($pie, "creating pie chart object");
28
29# this may change output quality too
35574351
TC
30
31print "# Imager version: $Imager::VERSION\n";
32print "# Font type: ",ref $font,"\n";
33
34my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font,
35 title=>{ text=>'Imager::Graph::Pie', size=>32 },
36 features=>{ outline=>1, labels=>1, pieblur=>0, },
37 outline=>{ line => '404040' },
bfcf9414 38 );
35574351 39
bfcf9414
TC
40ok($img1, "drawing first pie chart")
41 or print "# ",$pie->error,"\n";
35574351 42cmpimg($img1, "testimg/t10_pie1.png", 196880977);
bfcf9414 43$img1->write(file=>'testout/t10_pie1.ppm')
35574351
TC
44 or die "Cannot save pie1: ",$img1->errstr,"\n";
45
46my $img2 = $pie->draw(data=>\@data,
47 labels=>\@labels,
48 font=>$font,
49 title=>{ text=>'Imager::Graph::Pie', size=>36 },
50 features=>{ labelspconly=>1, _debugblur=>1,
51 legend=>1 },
7b94e723 52 legend=>{ border=>'000000', fill=>'C0C0C0', },
35574351 53 fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
bfcf9414 54 );
35574351 55
bfcf9414
TC
56ok($img2, "drawing second pie chart")
57 or print "# ",$pie->error,"\n";
35574351 58cmpimg($img2, "testimg/t10_pie2.png", 255956289);
bfcf9414 59$img2->write(file=>'testout/t10_pie2.ppm')
35574351
TC
60 or die "Cannot save pie2: ",$img2->errstr,"\n";
61
26c93f46
TC
62my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
63 font=>$font, style=>'fount_lin',
64 features=>[ 'legend', 'labelspconly', ],
65 legend=>{ valign=>'center' });
66ok($img3, "third chart")
67 or print "# ",$pie->error,"\n";
68$img3->write(file=>'testout/t10_lin_fount.ppm')
69 or die "Cannot save pie3: ",$img3->errstr,"\n";
70cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
71
72my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
73 font=>$font, style=>'fount_rad',
74 features=>[ 'legend', 'labelspc', ],
75 legend=>{ valign=>'bottom',
76 halign=>'left',
77 border=>'000080' });
78ok($img4, "fourth chart")
79 or print "# ",$pie->error,"\n";
80$img4->write(file=>'testout/t10_rad_fount.ppm')
81 or die "Cannot save pie3: ",$img4->errstr,"\n";
82cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
83
84my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
85 font=>$font, style=>'mono',
86 features=>[ 'allcallouts', 'labelspc' ],
87 legend=>{ valign=>'bottom',
88 halign=>'right' });
89ok($img5, "fifth chart")
90 or print "# ",$pie->error,"\n";
91$img5->write(file=>'testout/t10_mono.ppm')
92 or die "Cannot save pie3: ",$img5->errstr,"\n";
93cmpimg($img5, "testimg/t10_mono.png", 550_000);
94
95my $img6 = $pie->draw(data=>\@data, labels=>\@labels,
96 font=>$font, style=>'fount_lin',
97 features=>[ 'allcallouts', 'labelspc', 'legend' ],
98 legend=>
99 {
100 valign=>'top',
101 halign=>'center',
102 orientation => 'horizontal',
103 fill => { solid => Imager::Color->new(0, 0, 0, 32) },
104 patchborder => undef,
105 #size => 30,
106 });
107ok($img6, "sixth chart")
108 or print "# ",$pie->error,"\n";
109$img6->write(file=>'testout/t10_hlegend.ppm')
110 or die "Cannot save pie6: ",$img5->errstr,"\n";
111cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
112
bfcf9414 113{
26c93f46
TC
114 # RT #34813
115 # zero sized segments were drawn to cover the whole circle
116 my @data = ( 10, 8, 5, 0.000 );
117 my @labels = qw(alpha beta gamma);
a17e870a
TC
118 my @warned;
119 local $SIG{__WARN__} =
120 sub {
121 print STDERR $_[0];
122 push @warned, $_[0]
123 };
26c93f46
TC
124
125 my $img = $pie->draw
126 (
127 data => \@data,
128 labels => \@labels,
129 font => $font,
130 features => [ 'legend', 'labelspc', 'outline' ],
131 );
132 ok($img, "create graph with no 'others'");
133 ok($img->write(file => 'testout/t10_noother.ppm'),
134 "save it");
135 cmpimg($img, 'testimg/t10_noother.png', 500_000);
a17e870a
TC
136 unless (is(@warned, 0, "should be no warnings")) {
137 diag($_) for @warned;
138 }
35574351 139}
35574351 140
5d622bb8
TC
141{ # RT #535
142 # no font parameter would crash
143 my $im = $pie->draw
144 (
145 data => \@data,
146 title => 'test',
147 );
148 ok(!$im, "should fail to produce titled graph with no font");
149 like($pie->error, qr/title\.font/, "message should mention which font");
150
151 $im = $pie->draw
152 (
153 labels => \@labels,
154 data => \@data,
155 features => [ 'legend' ],
156 );
157 ok(!$im, "should fail to produce legended graph with no font");
158 like($pie->error, qr/legend\.font/, "message should mention which font");
159
160 $im = $pie->draw
161 (
162 data => \@data,
163 labels => \@labels,
164 features => [ 'legend' ],
165 legend => { orientation => "horizontal" },
166 );
167 ok(!$im, "should fail to produce horizontal legended graph with no font");
168 like($pie->error, qr/legend\.font/, "message should mention which font");
169
170 $im = $pie->draw
171 (
172 data => \@data,
173 labels => \@labels,
174 );
175 ok(!$im, "should fail to produce labelled graph with no font");
176 like($pie->error, qr/label\.font/, "message should mention which font");
177
d7fd5863
TC
178 $im = $pie->draw
179 (
180 data => \@data,
181 labels => \@labels,
182 features => [ 'allcallouts' ],
183 label => { font => $font },
184 );
185 ok(!$im, "should fail to produce callout labelled graph with no font");
186 like($pie->error, qr/callout\.font/, "message should mention which font");
187
188 # shouldn't need to set label font if doing all callouts
189 $im = $pie->draw
190 (
191 data => \@data,
192 labels => \@labels,
193 features => [ 'allcallouts' ],
194 callout => { font => $font },
195 );
196 ok($im, "should produce callout labelled graph with only callout font")
197 or print "# ", $pie->error, "\n";
198
199 # shouldn't need to set callout font if doing all labels
200 $im = $pie->draw
201 (
202 data => [ 1, 1, 1 ],
203 labels => [ qw/a b c/ ],
204 label => { font => $font }
205 );
206 ok($im, "should produce label only graph with only label font");
207}
208
209{
210 # draw with an empty data array is bad
211 # problem reported and fixed by Patrick Michaud
212 my $im = $pie->draw(data => []);
213 ok(!$im, "fail to draw with empty data");
214 like($pie->error, qr/No values/, "message appropriate");
215}
216
217{ # pie charts can't represent negative values
218 # problem reported and fixed by Patrick Michaud
219 my $im = $pie->draw(data => [ 10, -1, 10 ]);
220 ok(!$im, "fail to draw with negative value");
221 is($pie->error, "Data index 1 is less than zero", "check message");
222}
223
224{ # pie can't represent all zeros
225 # problem reported and fixed by Patrick Michaud
226 my $im = $pie->draw(data => [ 0, 0, 0 ]);
227 ok(!$im, "fail to draw with all zero values");
228 is($pie->error, "Sum of all data values is zero", "check message");
5d622bb8
TC
229}
230
35574351
TC
231sub cmpimg {
232 my ($img, $file, $limit) = @_;
233
234 $limit ||= 10000;
235
bfcf9414
TC
236 SKIP:
237 {
238 $Imager::formats{png}
239 or skip("No PNG support", 1);
240
35574351
TC
241 my $cmpimg = Imager->new;
242 $cmpimg->read(file=>$file)
243 or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
244 my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
bfcf9414 245 cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
35574351
TC
246 }
247}