This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Eliminate SVt_RV, and use SVt_IV to store plain references.
[perl5.git] / ext / B / t / b.t
CommitLineData
ccc418af
GS
1#!./perl
2
3BEGIN {
5638aaac
SM
4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 if ($^O eq 'MacOS') {
7 @INC = qw(: ::lib ::macos:lib);
8 } else {
9 @INC = '.';
10 push @INC, '../lib';
11 }
1b026014 12 } else {
5638aaac 13 unshift @INC, 't';
db5fd395 14 }
9cd8f857
NC
15 require Config;
16 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
17 print "1..0 # Skip -- Perl configured without B module\n";
18 exit 0;
19 }
ccc418af
GS
20}
21
22$| = 1;
23use warnings;
24use strict;
5ce57cc0 25use Test::More tests => 57;
ccc418af 26
c5f0f3aa 27BEGIN { use_ok( 'B' ); }
ccc418af 28
08c6f5ec 29
87a42246
MS
30package Testing::Symtable;
31use vars qw($This @That %wibble $moo %moo);
32my $not_a_sym = 'moo';
ccc418af 33
87a42246
MS
34sub moo { 42 }
35sub car { 23 }
ccc418af 36
f70490b9 37
87a42246
MS
38package Testing::Symtable::Foo;
39sub yarrow { "Hock" }
f70490b9 40
87a42246
MS
41package Testing::Symtable::Bar;
42sub hock { "yarrow" }
9b86dfa2 43
87a42246
MS
44package main;
45use vars qw(%Subs);
46local %Subs = ();
47B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
48 'Testing::Symtable::');
ccc418af 49
87a42246
MS
50sub B::GV::find_syms {
51 my($symbol) = @_;
de3f1649 52
87a42246 53 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
cfe9256d 54}
ccc418af 55
87a42246
MS
56my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
57 BEGIN);
58push @syms, "Testing::Symtable::Foo::yarrow";
ccc418af 59
87a42246 60# Make sure we hit all the expected symbols.
c5f0f3aa 61ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
1e1dbab6 62
87a42246 63# Make sure we only hit them each once.
c5f0f3aa
RGS
64ok( (!grep $_ != 1, values %Subs), '...and found once' );
65
66# Tests for MAGIC / MOREMAGIC
67ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
68{
69 my $e = '';
70 local $SIG{__DIE__} = sub { $e = $_[0] };
71 # Used to dump core, bug #16828
72 eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
73 like( $e, qr/Can't call method "TYPE" on an undefined value/,
74 '$. has no more magic' );
75}
01b509b0 76
f550f1f8
SP
77ok(B::svref_2object(qr/foo/)->MAGIC->precomp() eq 'foo', 'Get string from qr//');
78like(B::svref_2object(qr/foo/)->MAGIC->REGEX(), qr/\d+/, "REGEX() returns numeric value");
01b509b0
SP
79my $iv = 1;
80my $iv_ref = B::svref_2object(\$iv);
81is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
82is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
83# Flag tests are needed still
84#diag $iv_ref->FLAGS();
85my $iv_ret = $iv_ref->object_2svref();
86is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
87is($$iv_ret, $iv, "Test object_2svref()");
88is($iv_ref->int_value, $iv, "Test int_value()");
89is($iv_ref->IV, $iv, "Test IV()");
90is($iv_ref->IVX(), $iv, "Test IVX()");
91is($iv_ref->UVX(), $iv, "Test UVX()");
92
93my $pv = "Foo";
94my $pv_ref = B::svref_2object(\$pv);
95is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
96is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
97# Flag tests are needed still
98#diag $pv_ref->FLAGS();
99my $pv_ret = $pv_ref->object_2svref();
100is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
101is($$pv_ret, $pv, "Test object_2svref()");
102is($pv_ref->PV(), $pv, "Test PV()");
103eval { is($pv_ref->RV(), $pv, "Test RV()"); };
104ok($@, "Test RV()");
105is($pv_ref->PVX(), $pv, "Test PVX()");
106
107my $nv = 1.1;
108my $nv_ref = B::svref_2object(\$nv);
109is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
110is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
111# Flag tests are needed still
112#diag $nv_ref->FLAGS();
113my $nv_ret = $nv_ref->object_2svref();
114is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
115is($$nv_ret, $nv, "Test object_2svref()");
116is($nv_ref->NV, $nv, "Test NV()");
117is($nv_ref->NVX(), $nv, "Test NVX()");
118
119my $null = undef;
120my $null_ref = B::svref_2object(\$null);
121is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
122is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
123# Flag tests are needed still
124#diag $null_ref->FLAGS();
125my $null_ret = $nv_ref->object_2svref();
126is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
127is($$null_ret, $nv, "Test object_2svref()");
128
4df7f6af 129my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
01b509b0
SP
130my $cv = sub{ 1; };
131my $cv_ref = B::svref_2object(\$cv);
4df7f6af
NC
132is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
133is(ref $cv_ref, "$RV_class",
134 "Test $RV_class return from svref_2object - code");
01b509b0
SP
135my $cv_ret = $cv_ref->object_2svref();
136is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
137is($$cv_ret, $cv, "Test object_2svref()");
138
139my $av = [];
140my $av_ref = B::svref_2object(\$av);
4df7f6af
NC
141is(ref $av_ref, "$RV_class",
142 "Test $RV_class return from svref_2object - array");
01b509b0
SP
143
144my $hv = [];
145my $hv_ref = B::svref_2object(\$hv);
4df7f6af
NC
146is(ref $hv_ref, "$RV_class",
147 "Test $RV_class return from svref_2object - hash");
01b509b0
SP
148
149local *gv = *STDOUT;
150my $gv_ref = B::svref_2object(\*gv);
151is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
152ok(! $gv_ref->is_empty(), "Test is_empty()");
153is($gv_ref->NAME(), "gv", "Test NAME()");
154is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
155like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
2da668d2
SP
156
157# The following return B::SPECIALs.
158is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
159is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
160is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
161
162# More utility functions
163is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
164is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
165is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
166like(B::hash("wibble"), qr/0x[0-9a-f]*/, "Testing B::hash()");
167is(B::cstring("wibble"), '"wibble"', "Testing B::cstring()");
168is(B::perlstring("wibble"), '"wibble"', "Testing B::perlstring()");
169is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
170is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
fdecdb95 171is(B::opnumber("chop"), 38, "Testing opnumber with opname (chop)");
5ce57cc0
JJ
172
173{
174 no warnings 'once';
175 my $sg = B::sub_generation();
e1a479c5 176 *UNIVERSAL::hand_waving = sub { };
5ce57cc0
JJ
177 ok( $sg < B::sub_generation, "sub_generation increments" );
178}
179
180{
181 my $ag = B::amagic_generation();
182 {
183
184 package Whatever;
185 require overload;
186 overload->import( '""' => sub {"What? You want more?!"} );
187 }
188 ok( $ag < B::amagic_generation, "amagic_generation increments" );
189}