xref: /openbsd-src/gnu/usr.bin/perl/cpan/bignum/t/overrides.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6# Test behaviour of hex and oct overrides in detail, and also how the three
7# modules interact.
8
9use Test::More tests => 31;
10
11my $hex_called;
12my $oct_called;
13
14# For testing that existing CORE::GLOBAL overrides are not clobbered
15BEGIN {
16    if ($] > 5.009004) {
17        no warnings 'syntax';
18        *CORE::GLOBAL::hex = sub(_) { ++$hex_called; CORE::hex(@_?$_[0]:$_) };
19        *CORE::GLOBAL::oct = sub(_) { ++$oct_called; CORE::oct(@_?$_[0]:$_) };
20    } else {
21        *CORE::GLOBAL::hex = sub(;$) { ++$hex_called; CORE::hex(@_?$_[0]:$_) };
22        *CORE::GLOBAL::oct = sub(;$) { ++$oct_called; CORE::oct(@_?$_[0]:$_) };
23    }
24}
25
26{
27    use bigint;
28    $_ = "20";
29    is hex, "32", 'bigint hex override without arguments infers $_';
30    is oct, "16", 'bigint oct override without arguments infers $_';
31    @_ = 1..20;
32    is hex(@_), "32", 'bigint hex override provides scalar context';
33    is oct(@_), "16", 'bigint oct override provides scalar context';
34  SKIP:
35    {
36        skip "no lexical hex/oct", 2 unless $] > "5.009004";
37        is ref hex(1), 'Math::BigInt',
38          'bigint hex() works when bigfloat and bigrat are loaded';
39        is ref oct(1), 'Math::BigInt',
40          'bigint oct() works when bigfloat and bigrat are loaded';
41    }
42}
43
44{
45    use bigfloat;
46    $_ = "20";
47    is hex, "32", 'bigfloat hex override without arguments infers $_';
48    is oct, "16", 'bigfloat oct override without arguments infers $_';
49    @_ = 1..20;
50    is hex(@_), "32", 'bigfloat hex override provides scalar context';
51    is oct(@_), "16", 'bigfloat oct override provides scalar context';
52  SKIP:
53    {
54        skip "no lexical hex/oct", 2 unless $] > "5.009004";
55        is ref hex(1), 'Math::BigFloat',
56          'bigfloat hex() works when bigint and bigrat are loaded';
57        is ref oct(1), 'Math::BigFloat',
58          'bigfloat oct() works when bigint and bigrat are loaded';
59    }
60}
61
62{
63    use bigrat;
64    $_ = "20";
65    is hex, "32", 'bigrat hex override without arguments infers $_';
66    is oct, "16", 'bigrat oct override without arguments infers $_';
67    @_ = 1..20;
68    is hex(@_), "32", 'bigrat hex override provides scalar context';
69    is oct(@_), "16", 'bigrat oct override provides scalar context';
70  SKIP:
71    {
72        skip "no lexical hex/oct", 2 unless $] > "5.009004";
73        is ref hex(1), 'Math::BigRat',
74          'bigrat hex() works when bigfloat and bigint are loaded';
75        is ref oct(1), 'Math::BigRat',
76          'bigrat oct() works when bigfloat and bigint are loaded';
77    }
78}
79
80$hex_called = 0;
81() = hex 0;
82is $hex_called, 1, 'existing hex overrides are called';
83$oct_called = 0;
84() = oct 0;
85is $oct_called, 1, 'existing oct overrides are called';
86
87{
88    package _importer;
89    {
90        use bigint 'hex', 'oct';
91        ::is \&hex, \&bigint::hex, 'exported hex function';
92        ::is \&oct, \&bigint::oct, 'exported oct function';
93    }
94    ::ok ref hex(), 'exported hex function returns ref outside pragma scope';
95    ::ok ref oct(), 'exported oct function returns ref outside pragma scope';
96    ::is oct("20"), "16", 'exported oct function works with "decimal"';
97    # (used to return 20 because it thought it was decimal)
98}
99
100{
101    package _importer2;
102    use bigfloat 'hex', 'oct';
103    ::is \&hex, \&bigfloat::hex, 'bigfloat exports hex';
104    ::is \&oct, \&bigfloat::oct, 'bigfloat exports oct';
105#    ::is \&hex, \&bigint::hex, 'bigfloat exports same hex as bigint';
106#    ::is \&oct, \&bigint::oct, 'bigfloat exports same oct as bigint';
107}
108
109{
110    package _importer3;
111    use bigrat 'hex', 'oct';
112    ::is \&hex, \&bigrat::hex, 'bigrat exports hex';
113    ::is \&oct, \&bigrat::oct, 'bigrat exports oct';
114#    ::is \&hex, \&bigint::hex, 'bigrat exports same hex as bigint';
115#    ::is \&oct, \&bigint::oct, 'bigrat exports same oct as bigint';
116}
117is ref(hex 0), "", 'hex export is not global';
118is ref(oct 0), "", 'oct export is not global';
119