This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update CPANPLUS-Dist-Build to CPAN version 0.50
[perl5.git] / ext / B / t / b.t
CommitLineData
ccc418af
GS
1#!./perl
2
3BEGIN {
74517a3a 4 unshift @INC, 't';
9cd8f857
NC
5 require Config;
6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
8 exit 0;
9 }
ccc418af
GS
10}
11
12$| = 1;
13use warnings;
14use strict;
f9a20969 15use Test::More;
ccc418af 16
c5f0f3aa 17BEGIN { use_ok( 'B' ); }
ccc418af 18
08c6f5ec 19
87a42246
MS
20package Testing::Symtable;
21use vars qw($This @That %wibble $moo %moo);
22my $not_a_sym = 'moo';
ccc418af 23
87a42246
MS
24sub moo { 42 }
25sub car { 23 }
ccc418af 26
f70490b9 27
87a42246
MS
28package Testing::Symtable::Foo;
29sub yarrow { "Hock" }
f70490b9 30
87a42246
MS
31package Testing::Symtable::Bar;
32sub hock { "yarrow" }
9b86dfa2 33
87a42246
MS
34package main;
35use vars qw(%Subs);
36local %Subs = ();
37B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
38 'Testing::Symtable::');
ccc418af 39
87a42246
MS
40sub B::GV::find_syms {
41 my($symbol) = @_;
de3f1649 42
87a42246 43 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
cfe9256d 44}
ccc418af 45
87a42246
MS
46my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
47 BEGIN);
48push @syms, "Testing::Symtable::Foo::yarrow";
ccc418af 49
87a42246 50# Make sure we hit all the expected symbols.
c5f0f3aa 51ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
1e1dbab6 52
87a42246 53# Make sure we only hit them each once.
c5f0f3aa
RGS
54ok( (!grep $_ != 1, values %Subs), '...and found once' );
55
56# Tests for MAGIC / MOREMAGIC
57ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
58{
59 my $e = '';
60 local $SIG{__DIE__} = sub { $e = $_[0] };
61 # Used to dump core, bug #16828
62 eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
63 like( $e, qr/Can't call method "TYPE" on an undefined value/,
64 '$. has no more magic' );
65}
01b509b0 66
5c35adbb
NC
67my $r = qr/foo/;
68my $obj = B::svref_2object($r);
69my $regexp = ($] < 5.011) ? $obj->MAGIC : $obj;
70ok($regexp->precomp() eq 'foo', 'Get string from qr//');
71like($regexp->REGEX(), qr/\d+/, "REGEX() returns numeric value");
01b509b0
SP
72my $iv = 1;
73my $iv_ref = B::svref_2object(\$iv);
74is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
75is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
76# Flag tests are needed still
77#diag $iv_ref->FLAGS();
78my $iv_ret = $iv_ref->object_2svref();
79is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
80is($$iv_ret, $iv, "Test object_2svref()");
81is($iv_ref->int_value, $iv, "Test int_value()");
82is($iv_ref->IV, $iv, "Test IV()");
83is($iv_ref->IVX(), $iv, "Test IVX()");
84is($iv_ref->UVX(), $iv, "Test UVX()");
85
86my $pv = "Foo";
87my $pv_ref = B::svref_2object(\$pv);
88is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
89is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
90# Flag tests are needed still
91#diag $pv_ref->FLAGS();
92my $pv_ret = $pv_ref->object_2svref();
93is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
94is($$pv_ret, $pv, "Test object_2svref()");
95is($pv_ref->PV(), $pv, "Test PV()");
96eval { is($pv_ref->RV(), $pv, "Test RV()"); };
97ok($@, "Test RV()");
98is($pv_ref->PVX(), $pv, "Test PVX()");
99
100my $nv = 1.1;
101my $nv_ref = B::svref_2object(\$nv);
102is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
103is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
104# Flag tests are needed still
105#diag $nv_ref->FLAGS();
106my $nv_ret = $nv_ref->object_2svref();
107is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
108is($$nv_ret, $nv, "Test object_2svref()");
109is($nv_ref->NV, $nv, "Test NV()");
110is($nv_ref->NVX(), $nv, "Test NVX()");
111
112my $null = undef;
113my $null_ref = B::svref_2object(\$null);
114is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
115is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
116# Flag tests are needed still
117#diag $null_ref->FLAGS();
118my $null_ret = $nv_ref->object_2svref();
119is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
120is($$null_ret, $nv, "Test object_2svref()");
121
4df7f6af 122my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
01b509b0
SP
123my $cv = sub{ 1; };
124my $cv_ref = B::svref_2object(\$cv);
4df7f6af
NC
125is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
126is(ref $cv_ref, "$RV_class",
127 "Test $RV_class return from svref_2object - code");
01b509b0
SP
128my $cv_ret = $cv_ref->object_2svref();
129is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
130is($$cv_ret, $cv, "Test object_2svref()");
131
132my $av = [];
133my $av_ref = B::svref_2object(\$av);
4df7f6af
NC
134is(ref $av_ref, "$RV_class",
135 "Test $RV_class return from svref_2object - array");
01b509b0
SP
136
137my $hv = [];
138my $hv_ref = B::svref_2object(\$hv);
4df7f6af
NC
139is(ref $hv_ref, "$RV_class",
140 "Test $RV_class return from svref_2object - hash");
01b509b0
SP
141
142local *gv = *STDOUT;
143my $gv_ref = B::svref_2object(\*gv);
144is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
145ok(! $gv_ref->is_empty(), "Test is_empty()");
146is($gv_ref->NAME(), "gv", "Test NAME()");
147is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
148like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
2da668d2
SP
149
150# The following return B::SPECIALs.
151is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
152is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
153is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
154
155# More utility functions
156is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
157is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
158is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
b45732d6
NC
159{
160 my $hash = B::hash("wibble");
161 like($hash, qr/\A0x[0-9a-f]+\z/, "Testing B::hash(\"wibble\")");
162 unlike($hash, qr/\A0x0+\z/, "Testing B::hash(\"wibble\")");
163
164 like(B::hash("\0" x $_), qr/\A0x0+\z/, "Testing B::hash(\"0\" x $_)")
165 for 0..19;
8c5b7c71
NC
166
167 $hash = eval {B::hash(chr 256)};
168 is($hash, undef, "B::hash() refuses non-octets");
169 like($@, qr/^Wide character in subroutine entry/);
170
171 $hash = B::hash(chr 163);
172 my $str = chr(163) . chr 256;
173 chop $str;
174 is(B::hash($str), $hash, 'B::hash() with chr 128-256 is well-behaved');
b45732d6 175}
f9a20969
NC
176{
177 is(B::cstring(undef), '0', "Testing B::cstring(undef)");
178 is(B::perlstring(undef), '0', "Testing B::perlstring(undef)");
179
180 my @common = map {eval $_, $_}
574fde55
NC
181 '"wibble"', '"\""', '"\'"', '"\\\\"', '"\\n\\r\\t\\b\\a\\f"', '"\000"',
182 '"\000\000"', '"\000Bing\000"', ord 'N' == 78 ? '"\\177"' : ();
f9a20969
NC
183
184 my $oct = sprintf "\\%03o", ord '?';
185 my @tests = (@common, '$_', '"$_"', '@_', '"@_"', '??N', qq{"$oct?N"},
186 ord 'N' == 78 ? (chr 11, '"\v"'): ());
187 while (my ($test, $expect) = splice @tests, 0, 2) {
188 is(B::cstring($test), $expect, "B::cstring($expect)");
189 }
190
191 @tests = (@common, '$_', '"\$_"', '@_', '"\@_"', '??N', '"??N"',
192 chr 256, '"\x{100}"', chr 65536, '"\x{10000}"',
193 ord 'N' == 78 ? (chr 11, '"\013"'): ());
194 while (my ($test, $expect) = splice @tests, 0, 2) {
195 is(B::perlstring($test), $expect, "B::perlstring($expect)");
196 utf8::upgrade $test;
197 $expect =~ s/\\b/\\x\{8\}/g;
198 $expect =~ s/\\([0-7]{3})/sprintf "\\x\{%x\}", oct $1/eg;
199 is(B::perlstring($test), $expect, "B::perlstring($expect) (Unicode)");
200 }
201}
01c3a485 202{
574fde55
NC
203 my @tests = ((map {eval(qq{"$_"}), $_} '\\n', '\\r', '\\t',
204 '\\b', '\\a', '\\f', '\\000', '\\\'', '?'), '"', '"',
205 ord 'N' == 78 ? (chr 11, '\v', "\177", '\\177') : ());
01c3a485
NC
206
207 while (my ($test, $expect) = splice @tests, 0, 2) {
208 is(B::cchar($test), "'${expect}'", "B::cchar(qq{$expect})");
209 }
210}
211
2da668d2
SP
212is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
213is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
fdecdb95 214is(B::opnumber("chop"), 38, "Testing opnumber with opname (chop)");
5ce57cc0
JJ
215
216{
217 no warnings 'once';
218 my $sg = B::sub_generation();
e1a479c5 219 *UNIVERSAL::hand_waving = sub { };
5ce57cc0
JJ
220 ok( $sg < B::sub_generation, "sub_generation increments" );
221}
222
223{
224 my $ag = B::amagic_generation();
225 {
226
227 package Whatever;
228 require overload;
229 overload->import( '""' => sub {"What? You want more?!"} );
230 }
231 ok( $ag < B::amagic_generation, "amagic_generation increments" );
232}
f9a20969 233
fdbd1d64
NC
234is(B::svref_2object(sub {})->ROOT->ppaddr, 'PL_ppaddr[OP_LEAVESUB]',
235 'OP->ppaddr');
236
f9a20969 237done_testing();