1# -*- cperl -*-
2
3use strict;
4use warnings;
5
6use Test::More;
7
8use Config::AutoConf;
9
10END
11{
12    foreach my $f (<config*.*>)
13    {
14        -e $f and unlink $f;
15    }
16}
17
18## OK, we really hope people have sdtio.h around
19Config::AutoConf->check_header("stdio.h") or plan skip_all => "No working compile environment";
20ok(!Config::AutoConf->check_header("astupidheaderfile.h"));
21is(Config::AutoConf->check_headers("astupidheaderfile.h", "stdio.h"), "stdio.h");
22
23# check several headers at once
24my $ac = Config::AutoConf->new(logfile => "config2.log");
25eval { $ac->check_default_headers(); };
26ok(!$@, "check_default_headers") or diag($@);
27## we should find at least a stdio.h ...
28note("Checking for cache value " . $ac->_cache_name("stdio.h"));
29ok($ac->cache_val($ac->_cache_name("stdio.h")), "found stdio.h");
30
31# some complex header tests for wide OS support
32eval { $ac->check_dirent_header(); };
33ok(!$@, "check_dirent_header") or diag($@);
34
35# check predeclared symbol
36# as we test a perl module, we expect perl.h available and suitable
37my $include_perl = "#include <EXTERN.h>\n#include <perl.h>";
38
39SKIP:
40{
41    skip "Constants not defined on this Perl version", 2 if $] <= 5.01000;
42
43    ok $ac->check_decl("PERL_VERSION_STRING", {prologue => $include_perl}), "PERL_VERSION_STRING declared";
44
45    ok $ac->check_decls(
46        [qw(PERL_API_REVISION PERL_API_VERSION PERL_API_SUBVERSION)],
47        {prologue => $ac->_default_includes_with_perl}
48      ),
49      "PERL_API_* declared";
50}
51
52ok $ac->check_decl("perl_parse(PerlInterpreter *, XSINIT_t , int , char** , char** )", {prologue => $include_perl}),
53  "perl_parse() declared";
54
55SCOPE:
56{
57    # test outside cache control
58    local $ENV{ac_cv_type_complete_useless_datatype} = 1;
59    ok $ac->check_type("complete_useless_datatype"), "External overwritten type test";
60}
61
62# check declared types
63ok $ac->check_type("I32", {prologue => $include_perl}), "I32 is valid type";
64
65ok $ac->check_types(["SV *", "AV *", "HV *"], {prologue => $include_perl}), "[SAH]V * are valid types";
66
67# check size of perl types
68my $typesize = $ac->check_sizeof_type("I32", {prologue => $include_perl});
69ok $typesize, "I32 has size of " . ($typesize ? $typesize : "n/a") . " bytes";
70
71ok $ac->check_sizeof_types(["I32", "SV *", "AV", "HV *", "SV.sv_refcnt"], {prologue => $include_perl}),
72  "Could determined sizes for I32, SV *, AV, HV *, SV.sv_refcnt";
73
74my $compute = $ac->compute_int("-sizeof(I32)", {prologue => $include_perl});
75cmp_ok($compute, "==", 0 - $typesize, "Compute (-sizeof(I32))");
76
77# check perl data structure members
78ok $ac->check_member("struct av.sv_any", {prologue => $include_perl}), "have struct av.sv_any member";
79
80ok $ac->check_members(["struct hv.sv_any", "struct STRUCT_SV.sv_any"], {prologue => $include_perl}),
81  "have struct hv.sv_any and struct STRUCT_SV.sv_any members";
82
83my $struct_in_struct_prlg = <<EOP;
84struct S1 {
85    int i;
86    int j;
87};
88
89struct S2 {
90    struct S2 *next;
91    struct S1 s1;
92};
93EOP
94
95ok $ac->check_member("struct S2.s1", {prologue => $struct_in_struct_prlg}), "have struct S2.s1 member";
96
97# check aligning
98ok $ac->check_alignof_type("I32",          {prologue => $include_perl}), "Align of I32";
99ok $ac->check_alignof_type("SV.sv_refcnt", {prologue => $include_perl}), "Align of SV.sv_refcnt";
100ok $ac->check_alignof_types(["I32", "U32", "AV", "HV *", "SV.sv_refcnt"], {prologue => $include_perl}),
101  "Could determine the sizes of I32, U32, AV, HV *, SV.sv_refcnt";
102
103#
104# Let's take REGEXP structure members as of perlreapi to test check_members
105#
106my @members =
107  qw/jdd jdd2 engine mother_re extflags minlen minlenret gofs substrs nparens intflags pprivate lastparen lastcloseparen swap offs subbeg saved_copy sublen suboffset subcoffset prelen precomp wrapped wraplen seen_evals paren_names refcnt/;
108
109ok(
110    $ac->check_members(
111        [map { "struct regexp.$_" } @members],
112        {
113            prologue => "#include \"EXTERN.h\"
114#include \"perl.h\"
115#include \"XSUB.h\""
116        }
117    ),
118    "Check struct regexp"
119);
120
121Config::AutoConf->write_config_h();
122ok(-f "config.h", "default config.h created");
123my $fsize;
124ok($fsize = (stat("config.h"))[7], "config.h contains content");
125$ac->write_config_h();
126ok(-f "config.h", "default config.h created");
127cmp_ok((stat("config.h"))[7], ">", $fsize, "2nd config.h is bigger than first (more checks made)");
128
129my ($fh, $fbuf, $dbuf);
130open($fh, "<", "config.h");
131{ local $/; $fbuf = <$fh>; }
132close($fh);
133
134if ($] < 5.008)
135{
136    require IO::String;
137    $fh = IO::String->new($dbuf);
138}
139else
140{
141    open($fh, "+>", \$dbuf);
142}
143$ac->write_config_h($fh);
144close($fh);
145$fh = undef;
146
147cmp_ok($dbuf, "eq", $fbuf, "file and direct write computes equal");
148
149like($dbuf, qr/COMPLETE_USELESS_DATATYPE/, "complete_useless_datatype in config.h even if injected");
150
151TODO:
152{
153    -f "META.yml" or $ENV{AUTOMATED_TESTING} = 1;
154    local $TODO = "Quick fix: TODO - analyse diag later" unless $ENV{AUTOMATED_TESTING};
155    my @old_logfh;
156    $dbuf = "";
157
158    if ($] < 5.008)
159    {
160        $fh = IO::String->new($dbuf);
161    }
162    else
163    {
164        open($fh, "+>", \$dbuf);
165    }
166    @old_logfh = @{$ac->{logfh}};
167    $ac->add_log_fh($fh);
168    cmp_ok(scalar @{$ac->{logfh}}, "==", 2, "Successfully added 2nd loghandle");
169
170    ok($ac->check_compile_perlapi(), "Could compile perl extensions") or diag($dbuf);
171    scalar @old_logfh and $ac->delete_log_fh($fh);
172    scalar @old_logfh and is_deeply(\@old_logfh, $ac->{logfh}, "add_log_fh/delete_log_fh");
173    defined $fh       and close($fh);
174    $fh = undef;
175}
176
177SCOPE:
178{
179    local $ENV{ac_cv_insane_h} = "/usr/include/insane.h";
180    my $insane_h = $ac->check_header("insane.h");
181    is($insane_h, $ENV{ac_cv_insane_h}, "Cache override for header files work");
182}
183
184done_testing;
185