This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert t/op/oct.t to using test.pl for comparisons and diagnostics.
[perl5.git] / t / op / oct.t
CommitLineData
8d063cd8
LW
1#!./perl
2
53305cf1
NC
3# tests 51 onwards aren't all warnings clean. (intentionally)
4
c91473c9 5require './test.pl';
53305cf1 6
c91473c9 7plan(tests => 77);
53305cf1
NC
8
9sub test ($$$) {
10 my ($act, $string, $value) = @_;
11 my $result;
12 if ($act eq 'oct') {
13 $result = oct $string;
14 } elsif ($act eq 'hex') {
15 $result = hex $string;
16 } else {
17 die "Unknown action 'act'";
18 }
c91473c9
NC
19 my $desc = ($^O ne 'VMS' || length $string <= 256) && "$act \"$string\"";
20
21 unless (cmp_ok($value, '==', $result, $desc)) {
53305cf1 22 my ($valstr, $resstr);
333f87f2 23 if ($act eq 'hex' or $string =~ /x/i) {
53305cf1
NC
24 $valstr = sprintf "0x%X", $value;
25 $resstr = sprintf "0x%X", $result;
333f87f2 26 } elsif ($string =~ /b/i) {
53305cf1
NC
27 $valstr = sprintf "0b%b", $value;
28 $resstr = sprintf "0b%b", $result;
29 } else {
30 $valstr = sprintf "0%o", $value;
31 $resstr = sprintf "0%o", $result;
32 }
c91473c9 33 diag("$act \"$string\" gives \"$result\" ($resstr), not $value ($valstr)\n");
53305cf1 34 }
53305cf1
NC
35}
36
37test ('oct', '0b1_0101', 0b101_01);
38test ('oct', '0b10_101', 0_2_5);
39test ('oct', '0b101_01', 2_1);
40test ('oct', '0b1010_1', 0x1_5);
41
42test ('oct', 'b1_0101', 0b10101);
43test ('oct', 'b10_101', 025);
44test ('oct', 'b101_01', 21);
45test ('oct', 'b1010_1', 0x15);
46
47test ('oct', '01_234', 0b10_1001_1100);
48test ('oct', '012_34', 01234);
49test ('oct', '0123_4', 668);
50test ('oct', '01234', 0x29c);
51
52test ('oct', '0x1_234', 0b10010_00110100);
53test ('oct', '0x12_34', 01_1064);
54test ('oct', '0x123_4', 4660);
55test ('oct', '0x1234', 0x12_34);
56
57test ('oct', 'x1_234', 0b100100011010_0);
58test ('oct', 'x12_34', 0_11064);
59test ('oct', 'x123_4', 4660);
60test ('oct', 'x1234', 0x_1234);
61
62test ('hex', '01_234', 0b_1001000110100);
63test ('hex', '012_34', 011064);
64test ('hex', '0123_4', 4660);
65test ('hex', '01234_', 0x1234);
66
67test ('hex', '0x_1234', 0b1001000110100);
68test ('hex', '0x1_234', 011064);
69test ('hex', '0x12_34', 4660);
70test ('hex', '0x1234_', 0x1234);
71
72test ('hex', 'x_1234', 0b1001000110100);
73test ('hex', 'x12_34', 011064);
74test ('hex', 'x123_4', 4660);
75test ('hex', 'x1234_', 0x1234);
76
77test ('oct', '0b1111_1111_1111_1111_1111_1111_1111_1111', 4294967295);
78test ('oct', '037_777_777_777', 4294967295);
79test ('oct', '0xffff_ffff', 4294967295);
80test ('hex', '0xff_ff_ff_ff', 4294967295);
b21ed0a9
GS
81
82$_ = "\0_7_7";
c91473c9
NC
83is(length, 5);
84is($_, "\0"."_"."7"."_"."7");
b21ed0a9 85chop, chop, chop, chop;
c91473c9 86is($_, "\0");
2f2d036a
PP
87if (ord("\t") != 9) {
88 # question mark is 111 in 1047, 037, && POSIX-BC
c91473c9 89 is("\157_", "?_");
2f2d036a
PP
90}
91else {
c91473c9 92 is("\077_", "?_");
2f2d036a 93}
b21ed0a9
GS
94
95$_ = "\x_7_7";
c91473c9
NC
96is(length, 5);
97is($_, "\0"."_"."7"."_"."7");
b21ed0a9 98chop, chop, chop, chop;
c91473c9 99is($_, "\0");
2f2d036a
PP
100if (ord("\t") != 9) {
101 # / is 97 in 1047, 037, && POSIX-BC
c91473c9 102 is("\x61_", "/_");
2f2d036a
PP
103}
104else {
c91473c9 105 is("\x2F_", "/_");
2f2d036a 106}
93312bbf 107
53305cf1
NC
108test ('oct', '0b'.( '0'x10).'1_0101', 0b101_01);
109test ('oct', '0b'.( '0'x100).'1_0101', 0b101_01);
110test ('oct', '0b'.('0'x1000).'1_0101', 0b101_01);
111
112test ('hex', ( '0'x10).'01234', 0x1234);
113test ('hex', ( '0'x100).'01234', 0x1234);
114test ('hex', ('0'x1000).'01234', 0x1234);
115
116# Things that perl 5.6.1 and 5.7.2 did wrong (plus some they got right)
117test ('oct', "b00b0101", 0);
118test ('oct', "bb0101", 0);
119test ('oct', "0bb0101", 0);
120
121test ('oct', "0x0x3A", 0);
122test ('oct', "0xx3A", 0);
123test ('oct', "x0x3A", 0);
124test ('oct', "xx3A", 0);
125test ('oct', "0x3A", 0x3A);
126test ('oct', "x3A", 0x3A);
127
128test ('oct', "0x0x4", 0);
129test ('oct', "0xx4", 0);
130test ('oct', "x0x4", 0);
131test ('oct', "xx4", 0);
132test ('oct', "0x4", 4);
133test ('oct', "x4", 4);
134
135test ('hex', "0x3A", 0x3A);
136test ('hex', "x3A", 0x3A);
137
138test ('hex', "0x4", 4);
139test ('hex', "x4", 4);
93312bbf 140
1ac95999 141eval '$a = oct "10\x{100}"';
c91473c9 142like($@, qr/Wide character/);
1ac95999
JH
143
144eval '$a = hex "ab\x{100}"';
c91473c9 145like($@, qr/Wide character/);
333f87f2
BL
146
147# Allow uppercase base markers (#76296)
148
149test ('hex', "0XCAFE", 0xCAFE);
150test ('hex', "XCAFE", 0xCAFE);
151test ('oct', "0XCAFE", 0xCAFE);
152test ('oct', "XCAFE", 0xCAFE);
153test ('oct', "0B101001", 0b101001);
154test ('oct', "B101001", 0b101001);