1use warnings;
2use strict;
3use InlineX::CPP2XS qw(cpp2xs);
4
5print "1..64\n";
6
7my $bd = 'demos/cpp2xs_utility';
8my ($emanif, $epm, $exs, $eauto, $etest, $emap, $emake);
9my @w = qw(FastSieve.pm FastSieve.xs MANIFEST CPP.map ilcpptest.cpp auto_include.in Makefile.PL);
10
11my %config_opts = (
12                  'DIST' => 1,
13                  'VERSION' => 0.07,
14                  'EXPORT_OK_ALL' => 1,
15                  'SRC_LOCATION' => "demos/cpp2xs_utility/src/FastSieve.cpp",
16                  );
17
18cpp2xs('Math::Prime::FastSieve', 'Math::Prime::FastSieve', $bd, \%config_opts);
19
20### Check that the MANIFEST exists
21if(-f "$bd/MANIFEST") {
22  $emanif = 1;
23  print "ok 1\n";
24}
25else {print "not ok 1\n"}
26
27### Check that the contents of the MANIFEST are as expected
28if($emanif) {
29  open RDM, '<', "$bd/MANIFEST" or die "Can't open $bd/MANIFEST for reading: $!";
30  my @h = <RDM>;
31  close RDM or die "Can't close ./$bd/MANIFEST after reading: $!";
32  chomp for @h;
33  my $i = 2;
34  ##############
35  for my $w(@w) {
36    my $ok = 0;
37    for my $h(@h) {
38      $ok = 1 if $h eq $w;
39    }
40    if($ok) {print "ok $i\n"}
41    else {
42      warn "Couldn't find $w in the MANIFEST file\n";
43      print "not ok $i\n";
44    }
45    $i++;
46  }
47  ##############
48  for my $h(@h) {
49    my $ok = 0;
50    for my $w(@w) {
51      $ok = 1 if $h eq $w;
52    }
53    if($ok) {print "ok $i\n"}
54    else {
55      warn "Couldn't find $h in the list of expected files\n";
56      print "not ok $i\n";
57    }
58    $i++;
59  }
60  ##############
61}
62else {
63warn "\nSkipping tests 2 to 15 - no MANIFEST was written\n";
64for(2 .. 15) {print "ok $_\n"}
65}
66
67### Remove the MANIFEST
68if(!unlink "$bd/MANIFEST") {warn "Failed to unlink $bd/MANIFEST : $!\n"}
69if(-f "$bd/MANIFEST") {
70  print "not ok 16\n";
71}
72else {print "ok 16\n"}
73
74### Check that FastSieve.pm was written
75if(-f "$bd/FastSieve.pm") {
76  $epm = 1;
77  print "ok 17\n";
78}
79else {print "not ok 17\n"}
80
81### Check that the syntax of FastSieve.pm is ok.
82if($epm) {
83  my $nok = system $^X, '-c', "$bd/FastSieve.pm";
84  if($nok) {
85    warn "Unable to successfully compile $bd/FastSieve.pm: $nok\n";
86    print "not ok 18\n";
87  }
88  else {print "ok 18\n"}
89}
90else {
91  warn "\nSkipping test 18 - FastSieve.pm was not written\n";
92  print "ok 18\n";
93}
94
95### Remove FastSieve.pm
96if(!unlink "$bd/FastSieve.pm") {warn "Failed to unlink $bd/FastSieve.pm : $!\n"}
97if(-f "$bd/FastSieve.pm") {
98  print "not ok 19n";
99}
100else {print "ok 19\n"}
101
102### Check that FastSieve.xs was written
103if(-f "$bd/FastSieve.xs") {
104  $exs = 1;
105  print "ok 20\n";
106}
107else {print "not ok 20\n"}
108
109### check that FastSieve.xs is not much smaller than it should be, and that it does not match /__INLINE_CPP_/
110if($exs) {
111  open RDXS, '<', "$bd/FastSieve.xs" or die "Couldn't open $bd/FastSieve.xs for reading: $!";
112  my @rdxs = <RDXS>;
113  close RDXS or die "Couldn't close $bd/FastSieve.xs after reading: $!";
114  if(@rdxs < 200) {
115    warn "$bd/FastSieve.xs is too small\n";
116    print "not ok 21\n";
117  }
118  else {print "ok 21\n"}
119  my $ok = 1;
120  for(@rdxs) {$ok = 0 if $_ =~ /__INLINE_CPP_/}
121  if($ok) {print "ok 22\n"}
122  else {print "not ok 22\n"}
123}
124else {
125  warn "\nSkipping tests 21 and 22 - $bd/FastSieve.xs was not written\n";
126  print "ok 21\n";
127  print "ok 22\n";
128}
129
130### Remove FastSieve.xs
131if(!unlink "$bd/FastSieve.xs") {warn "Failed to unlink $bd/FastSieve.xs : $!\n"}
132if(-f "$bd/FastSieve.pm") {
133  print "not ok 23\n";
134}
135else {print "ok 23\n"}
136
137### Check that auto_include.in was written
138if(-f "$bd/auto_include.in") {
139  $eauto = 1;
140  print "ok 24\n";
141}
142else {print "not ok 24\n"}
143
144### check that auto_include.in is not much smaller than it should be, and that it does not match /__INLINE_CPP_/
145if($eauto) {
146  open RDAUTO, '<', "$bd/auto_include.in" or die "Couldn't open $bd/auto_include.in for reading: $!";
147  my @rdauto = <RDAUTO>;
148  close RDAUTO or die "Couldn't close $bd/auto_include.in for reading: $!";
149  if(@rdauto < 12) {
150    warn "$bd/auto_include.in is too small\n";
151    print "not ok 25\n";
152  }
153  else {
154    print "ok 25\n";
155  }
156  my $ok = 1;
157  for(@rdauto) {
158    if($_ =~ /__INLINE_CPP_/) {$ok = 0}
159  }
160  if($ok) {print "ok 26\n"}
161  else {print "not ok 26\n"}
162}
163else {
164  warn "\nSkipping tests 25 and 26 - no auto_include.in was written\n";
165  print "ok 25\n";
166  print "ok 26\n";
167}
168
169### Remove auto_include.in
170if(!unlink "$bd/auto_include.in") {warn "Failed to unlink $bd/auto_include.in : $!\n"}
171if(-f "$bd/auto_include.in") {
172  print "not ok 27\n";
173}
174else {print "ok 27\n"}
175
176### Check that the Makefile.PL was written
177if(-f "$bd/Makefile.PL") {
178  $emake = 1;
179  print "ok 28\n";
180}
181else {print "not ok 28\n"}
182
183### check that the Makefile.PL is not much smaller than expected, and that its syntax is ok
184if($emake) {
185  open RDMAKE, '<', "$bd/Makefile.PL" or die "Can't open $bd/Makefile.PL for reading: $!";
186  my @rdmake = <RDMAKE>;
187  close RDMAKE or die "Can't close $bd/Makefile.PL after reading: $!";
188  if(@rdmake < 120) {
189    warn "$bd/Makefile.PL is too small\n";
190    print "not ok 29\n";
191  }
192  else {print "ok 29\n"}
193
194  my $nok = system $^X, '-c', "$bd/Makefile.PL";
195  if($nok) {
196    warn "Unable to successfully compile $bd/Makefile.PL: $nok\n";
197    print "not ok 30\n";
198  }
199  else {print "ok 30\n"}
200}
201else {
202  warn "\nSkipping tests 29 and 30 - no Makefile.PL written\n";
203  print "ok 29\n";
204  print "ok 30\n";
205}
206
207### Remove the Makefile.PL
208if(!unlink "$bd/Makefile.PL") {warn "Failed to unlink $bd/Makefile.PL : $!\n"}
209if(-f "$bd/Makefile.PL") {
210  print "not ok 31\n";
211}
212else {print "ok 31\n"}
213
214### Check that CPP.map was written.
215if(-f "$bd/CPP.map") {
216  $emap = 1;
217  print "ok 32\n";
218}
219else {print "not ok 32\n"}
220
221### Check that CPP.map is not much smaller than expected
222if($emap) {
223  open RDMAP, '<',"$bd/CPP.map" or die "Couldn't open $bd/CPP.map for reading: $!";
224  my @rdmap = <RDMAP>;
225  close RDMAP or die "Couldn't close $bd/CPP.map after reading: $!";
226  if(@rdmap < 6) {
227    warn "$bd/CPP.map is too small\n";
228    print "not ok 33\n";
229  }
230  else {print "ok 33\n"}
231}
232else {
233  warn "\nSkipping test 33 - no $bd/CPP.map was written\n";
234  print "ok 33\n";
235}
236
237### Remove CPP.map
238if(!unlink "$bd/CPP.map") {warn "Failed to unlink $bd/CPP.map : $!\n"}
239if(-f "$bd/CPP.map") {
240  print "not ok 34\n";
241}
242else {print "ok 34\n"}
243
244### Check that ilcpptest.cpp was written
245if(-f "$bd/ilcpptest.cpp") {
246  $etest = 1;
247  print "ok 35\n";
248}
249else {print "not ok 35\n"}
250
251### Check that ilcpptest.cpp is not much smaller than expected.
252if($etest) {
253  open RDTEST, '<',"$bd/ilcpptest.cpp" or die "Couldn't open $bd/ilcpptest.cpp for reading: $!";
254  my @rdtest = <RDTEST>;
255  close RDTEST or die "Couldn't close $bd/ilcpptest.cpp after reading: $!";
256  if(@rdtest < 3) {
257    warn "$bd/ilcpptest.cpp is too small\n";
258    print "not ok 36\n";
259  }
260  else {print "ok 36\n"}
261}
262else {
263  warn "\nSkipping test 36 - no $bd/ilcpptest.cpp was written\n";
264  print "ok 36\n";
265}
266
267### Remove ilcpptest.cpp.
268if(!unlink "$bd/ilcpptest.cpp") {warn "Failed to unlink $bd/ilcpptest.cpp: $!\n"}
269if(-f "$bd/ilcpptest.cpp") {
270  print "not ok 37\n";
271}
272else {print "ok 37\n"}
273
274#########  ###########     ################ ###
275#########  ###########     ################ ###
276#########  ###########     ################ ###
277#########  ###########     ################ ###
278
279$bd = 'demos/cpp2xs_utility/mybuild';
280($emanif, $epm, $exs, $emap, $emake) = (0, 0, 0, 0, 0);
281@w = qw(FastSieve.pm FastSieve.xs MANIFEST CPP.map Makefile.PL);
282
283%config_opts =    (
284                  'WRITE_MAKEFILE_PL' => 1,
285                  'WRITE_PM' => 1,
286                  'MANIF' => 1,
287                  'VERSION' => 0.07,
288                  'EXPORT_OK_ALL' => 1,
289                  'SRC_LOCATION' => "demos/cpp2xs_utility/src/FastSieve.cpp",
290                  );
291
292cpp2xs('Math::Prime::FastSieve', 'Math::Prime::FastSieve', $bd, \%config_opts);
293
294### check that the MANIFEST was written
295if(-f "$bd/MANIFEST") {
296  $emanif = 1;
297  print "ok 38\n";
298}
299else {print "not ok 38\n"}
300
301### Check that the contents of the MANIFEST is as expected
302if($emanif) {
303  open RDM, '<', "$bd/MANIFEST" or die "Can't open $bd/MANIFEST for reading: $!";
304  my @h = <RDM>;
305  close RDM or die "Can't close ./$bd/MANIFEST after reading: $!";
306  chomp for @h;
307  my $i = 39;
308  ##############
309  for my $w(@w) {
310    my $ok = 0;
311    for my $h(@h) {
312      $ok = 1 if $h eq $w;
313    }
314    if($ok) {print "ok $i\n"}
315    else {
316      warn "Couldn't find $w in the MANIFEST file\n";
317      print "not ok $i\n";
318    }
319    $i++;
320  }
321  ##############
322  for my $h(@h) {
323    my $ok = 0;
324    for my $w(@w) {
325      $ok = 1 if $h eq $w;
326    }
327    if($ok) {print "ok $i\n"}
328    else {
329      warn "Couldn't find $h in the list of expected files\n";
330      print "not ok $i\n";
331    }
332    $i++;
333  }
334  ##############
335}
336else {
337warn "\nSkipping tests 2 to 15 - no MANIFEST was written\n";
338for(39 .. 48) {print "ok $_\n"}
339}
340
341### Remove the MANIFEST
342if(!unlink "$bd/MANIFEST") {warn "Failed to unlink $bd/MANIFEST : $!\n"}
343if(-f "$bd/MANIFEST") {
344  print "not ok 49\n";
345}
346else {print "ok 49\n"}
347
348### Check that FastSieve.pm was written
349if(-f "$bd/FastSieve.pm") {
350  $epm = 1;
351  print "ok 50\n";
352}
353else {print "not ok 50\n"}
354
355### Check that the syntax of FastSieve.pm is ok
356if($epm) {
357  my $nok = system $^X, '-c', "$bd/FastSieve.pm";
358  if($nok) {
359    warn "Unable to successfully compile $bd/FastSieve.pm: $nok\n";
360    print "not ok 51\n";
361  }
362  else {print "ok 51\n"}
363}
364else {
365  warn "\nSkipping test 51 - FastSieve.pm was not written\n";
366  print "ok 51\n";
367}
368
369### Remove FastSieve.pm
370if(!unlink "$bd/FastSieve.pm") {warn "Failed to unlink $bd/FastSieve.pm : $!\n"}
371if(-f "$bd/FastSieve.pm") {
372  print "not ok 52n";
373}
374else {print "ok 52\n"}
375
376### Check that FastSieve.xs was written
377if(-f "$bd/FastSieve.xs") {
378  $exs = 1;
379  print "ok 53\n";
380}
381else {print "not ok 53\n"}
382
383### Check that FastSieve.xs is not much smaller than expected, and that it matches /__INLINE_CPP_/ iff
384### $Inline::CPP::Config::cpp_flavor_defs matches /_Inline_CPP_/.
385if($exs) {
386  open RDXS, '<', "$bd/FastSieve.xs" or die "Couldn't open $bd/FastSieve.xs for reading: $!";
387  my @rdxs = <RDXS>;
388  close RDXS or die "Couldn't close $bd/FastSieve.xs after reading: $!";
389  if(@rdxs < 200) {
390    warn "$bd/FastSieve.xs is too small\n";
391    print "not ok 54\n";
392  }
393  else {print "ok 54\n"}
394  my $ok = $Inline::CPP::Config::cpp_flavor_defs;
395  $ok = 0;
396  if($Inline::CPP::Config::cpp_flavor_defs =~ /__INLINE_CPP_/) {
397    for(@rdxs) {
398      $ok = 1 if $_ =~ /__INLINE_CPP_/;
399    }
400  }
401  else {
402    $ok = 1;
403    for(@rdxs) {
404      $ok = 0 if $_ =~ /__INLINE_CPP_/;
405    }
406  }
407
408  if($ok) {print "ok 55\n"}
409  else {print "not ok 55\n"}
410}
411else {
412  warn "\nSkipping tests 54 and 55 - $bd/FastSieve.xs was not written\n";
413  print "ok 54\n";
414  print "ok 55\n";
415}
416
417### Remove FastSieve.xs
418if(!unlink "$bd/FastSieve.xs") {warn "Failed to unlink $bd/FastSieve.xs : $!\n"}
419if(-f "$bd/FastSieve.pm") {
420  print "not ok 56\n;"
421}
422else {print "ok 56\n"}
423
424### Check that auto_include was *not* written (and remove it if it was).
425if(-f "$bd/auto_include.in") {
426  print "not ok 57\n";
427  warn "Couldn't unlink $bd/auto_include.in\n"
428    unless unlink "$bd/auto_include.in";
429}
430else {print "ok 57\n"}
431
432### Check that Makefile.PL was written
433if(-f "$bd/Makefile.PL") {
434  $emake = 1;
435  print "ok 58\n";
436}
437else {print "not ok 58\n"}
438
439### Check that the size of the Makefile.PL is within expectations, and that the syntax of the Makefile.PL is OK
440if($emake) {
441  open RDMAKE, '<', "$bd/Makefile.PL" or die "Can't open $bd/Makefile.PL for reading: $!";
442  my @rdmake = <RDMAKE>;
443  close RDMAKE or die "Can't close $bd/Makefile.PL after reading: $!";
444  if(@rdmake < 4 || @rdmake > 50) {
445    warn "$bd/Makefile.PL is not the right size\n";
446    print "not ok 59\n";
447  }
448  else {print "ok 59\n"}
449
450  my $nok = system $^X, '-c', "$bd/Makefile.PL";
451  if($nok) {
452    warn "Unable to successfully compile $bd/Makefile.PL: $nok\n";
453    print "not ok 60\n";
454  }
455  else {print "ok 60\n"}
456}
457else {
458  warn "\nSkipping tests 59 and 60 - no Makefile.PL written\n";
459  print "ok 59\n";
460  print "ok 60\n";
461}
462
463### Remove the Makefile.PL
464if(!unlink "$bd/Makefile.PL") {warn "Failed to unlink $bd/Makefile.PL : $!\n"}
465if(-f "$bd/Makefile.PL") {
466  print "not ok 61\n";
467}
468else {print "ok 61\n"}
469
470### Check that CPP.map was written
471if(-f "$bd/CPP.map") {
472  $emap = 1;
473  print "ok 62\n";
474}
475else {print "not ok 62\n"}
476
477### Check that CPP.map is not much smaller than expected
478if($emap) {
479  open RDMAP, '<',"$bd/CPP.map" or die "Couldn't open $bd/CPP.map for reading: $!";
480  my @rdmap = <RDMAP>;
481  close RDMAP or die "Couldn't close $bd/CPP.map after reading: $!";
482  if(@rdmap < 6) {
483    warn "$bd/CPP.map is too small\n";
484    print "not ok 63\n";
485  }
486  else {print "ok 63\n"}
487}
488else {
489  warn "\nSkipping test 63 - no $bd/CPP.map was written\n";
490  print "ok 63\n";
491}
492
493### Remove CPP.map
494if(!unlink "$bd/CPP.map") {warn "Failed to unlink $bd/CPP.map : $!\n"}
495if(-f "$bd/CPP.map") {
496  print "not ok 64\n";
497}
498else {print "ok 64\n"}
499
500