Debian Codegraph @

t/450-api/130-cpp.t

raw
1 #!perl -w
2 #
3 # this tests the API headers are C++ compatible
4 use strict;
5 use Test::More;
6 eval "require Inline::CPP;";
7 plan skip_all => "Inline::CPP required for testing C++ compatibility" if $@;
8
9 use Cwd 'getcwd';
10 plan skip_all => "Inline won't work in directories with spaces"
11 if getcwd() =~ / /;
12
13 plan skip_all => "perl 5.005_04, 5.005_05 too buggy"
14 if $] =~ /^5\.005_0[45]$/;
15
16 -d "testout" or mkdir "testout";
17
18 print STDERR "Inline::CPP version $Inline::CPP::VERSION\n";
19
20 require Inline;
21 Inline->import(with => 'Imager');
22 Inline->import("FORCE"); # force rebuild
23 #Inline->import(C => Config => OPTIMIZE => "-g");
24
25 Inline->bind(CPP => <<'EOS');
26 #include <math.h>
27
28 int pixel_count(Imager::ImgRaw im) {
29 return im->xsize * im->ysize;
30 }
31
32 int count_color(Imager::ImgRaw im, Imager::Color c) {
33 int count = 0, x, y, chan;
34 i_color read_c;
35
36 for (x = 0; x < im->xsize; ++x) {
37 for (y = 0; y < im->ysize; ++y) {
38 int match = 1;
39 i_gpix(im, x, y, &read_c);
40 for (chan = 0; chan < im->channels; ++chan) {
41 if (read_c.channel[chan] != c->channel[chan]) {
42 match = 0;
43 break;
44 }
45 }
46 if (match)
47 ++count;
48 }
49 }
50
51 return count;
52 }
53
54 Imager make_10x10() {
55 i_img *im = i_img_8_new(10, 10, 3);
56 i_color c;
57 c.channel[0] = c.channel[1] = c.channel[2] = 255;
58 i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &c);
59
60 return im;
61 }
62
63 EOS
64
65 my $im = Imager->new(xsize=>50, ysize=>50);
66 is(pixel_count($im), 2500, "pixel_count");
67
68 my $black = Imager::Color->new(0,0,0);
69 is(count_color($im, $black), 2500, "count_color black on black image");
70
71 my $im2 = make_10x10();
72 my $white = Imager::Color->new(255, 255, 255);
73 is(count_color($im2, $white), 100, "check new image white count");
74 ok($im2->box(filled=>1, xmin=>1, ymin=>1, xmax => 8, ymax=>8, color=>$black),
75 "try new image");
76 is(count_color($im2, $black), 64, "check modified black count");
77 is(count_color($im2, $white), 36, "check modified white count");
78
79
80 done_testing();