Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
53305cf1 NC |
3 | # tests 51 onwards aren't all warnings clean. (intentionally) |
4 | ||
c91473c9 | 5 | require './test.pl'; |
53305cf1 | 6 | |
c91473c9 | 7 | plan(tests => 77); |
53305cf1 NC |
8 | |
9 | sub 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 | ||
37 | test ('oct', '0b1_0101', 0b101_01); | |
38 | test ('oct', '0b10_101', 0_2_5); | |
39 | test ('oct', '0b101_01', 2_1); | |
40 | test ('oct', '0b1010_1', 0x1_5); | |
41 | ||
42 | test ('oct', 'b1_0101', 0b10101); | |
43 | test ('oct', 'b10_101', 025); | |
44 | test ('oct', 'b101_01', 21); | |
45 | test ('oct', 'b1010_1', 0x15); | |
46 | ||
47 | test ('oct', '01_234', 0b10_1001_1100); | |
48 | test ('oct', '012_34', 01234); | |
49 | test ('oct', '0123_4', 668); | |
50 | test ('oct', '01234', 0x29c); | |
51 | ||
52 | test ('oct', '0x1_234', 0b10010_00110100); | |
53 | test ('oct', '0x12_34', 01_1064); | |
54 | test ('oct', '0x123_4', 4660); | |
55 | test ('oct', '0x1234', 0x12_34); | |
56 | ||
57 | test ('oct', 'x1_234', 0b100100011010_0); | |
58 | test ('oct', 'x12_34', 0_11064); | |
59 | test ('oct', 'x123_4', 4660); | |
60 | test ('oct', 'x1234', 0x_1234); | |
61 | ||
62 | test ('hex', '01_234', 0b_1001000110100); | |
63 | test ('hex', '012_34', 011064); | |
64 | test ('hex', '0123_4', 4660); | |
65 | test ('hex', '01234_', 0x1234); | |
66 | ||
67 | test ('hex', '0x_1234', 0b1001000110100); | |
68 | test ('hex', '0x1_234', 011064); | |
69 | test ('hex', '0x12_34', 4660); | |
70 | test ('hex', '0x1234_', 0x1234); | |
71 | ||
72 | test ('hex', 'x_1234', 0b1001000110100); | |
73 | test ('hex', 'x12_34', 011064); | |
74 | test ('hex', 'x123_4', 4660); | |
75 | test ('hex', 'x1234_', 0x1234); | |
76 | ||
77 | test ('oct', '0b1111_1111_1111_1111_1111_1111_1111_1111', 4294967295); | |
78 | test ('oct', '037_777_777_777', 4294967295); | |
79 | test ('oct', '0xffff_ffff', 4294967295); | |
80 | test ('hex', '0xff_ff_ff_ff', 4294967295); | |
b21ed0a9 GS |
81 | |
82 | $_ = "\0_7_7"; | |
c91473c9 NC |
83 | is(length, 5); |
84 | is($_, "\0"."_"."7"."_"."7"); | |
b21ed0a9 | 85 | chop, chop, chop, chop; |
c91473c9 | 86 | is($_, "\0"); |
2f2d036a PP |
87 | if (ord("\t") != 9) { |
88 | # question mark is 111 in 1047, 037, && POSIX-BC | |
c91473c9 | 89 | is("\157_", "?_"); |
2f2d036a PP |
90 | } |
91 | else { | |
c91473c9 | 92 | is("\077_", "?_"); |
2f2d036a | 93 | } |
b21ed0a9 GS |
94 | |
95 | $_ = "\x_7_7"; | |
c91473c9 NC |
96 | is(length, 5); |
97 | is($_, "\0"."_"."7"."_"."7"); | |
b21ed0a9 | 98 | chop, chop, chop, chop; |
c91473c9 | 99 | is($_, "\0"); |
2f2d036a PP |
100 | if (ord("\t") != 9) { |
101 | # / is 97 in 1047, 037, && POSIX-BC | |
c91473c9 | 102 | is("\x61_", "/_"); |
2f2d036a PP |
103 | } |
104 | else { | |
c91473c9 | 105 | is("\x2F_", "/_"); |
2f2d036a | 106 | } |
93312bbf | 107 | |
53305cf1 NC |
108 | test ('oct', '0b'.( '0'x10).'1_0101', 0b101_01); |
109 | test ('oct', '0b'.( '0'x100).'1_0101', 0b101_01); | |
110 | test ('oct', '0b'.('0'x1000).'1_0101', 0b101_01); | |
111 | ||
112 | test ('hex', ( '0'x10).'01234', 0x1234); | |
113 | test ('hex', ( '0'x100).'01234', 0x1234); | |
114 | test ('hex', ('0'x1000).'01234', 0x1234); | |
115 | ||
116 | # Things that perl 5.6.1 and 5.7.2 did wrong (plus some they got right) | |
117 | test ('oct', "b00b0101", 0); | |
118 | test ('oct', "bb0101", 0); | |
119 | test ('oct', "0bb0101", 0); | |
120 | ||
121 | test ('oct', "0x0x3A", 0); | |
122 | test ('oct', "0xx3A", 0); | |
123 | test ('oct', "x0x3A", 0); | |
124 | test ('oct', "xx3A", 0); | |
125 | test ('oct', "0x3A", 0x3A); | |
126 | test ('oct', "x3A", 0x3A); | |
127 | ||
128 | test ('oct', "0x0x4", 0); | |
129 | test ('oct', "0xx4", 0); | |
130 | test ('oct', "x0x4", 0); | |
131 | test ('oct', "xx4", 0); | |
132 | test ('oct', "0x4", 4); | |
133 | test ('oct', "x4", 4); | |
134 | ||
135 | test ('hex', "0x3A", 0x3A); | |
136 | test ('hex', "x3A", 0x3A); | |
137 | ||
138 | test ('hex', "0x4", 4); | |
139 | test ('hex', "x4", 4); | |
93312bbf | 140 | |
1ac95999 | 141 | eval '$a = oct "10\x{100}"'; |
c91473c9 | 142 | like($@, qr/Wide character/); |
1ac95999 JH |
143 | |
144 | eval '$a = hex "ab\x{100}"'; | |
c91473c9 | 145 | like($@, qr/Wide character/); |
333f87f2 BL |
146 | |
147 | # Allow uppercase base markers (#76296) | |
148 | ||
149 | test ('hex', "0XCAFE", 0xCAFE); | |
150 | test ('hex', "XCAFE", 0xCAFE); | |
151 | test ('oct', "0XCAFE", 0xCAFE); | |
152 | test ('oct', "XCAFE", 0xCAFE); | |
153 | test ('oct', "0B101001", 0b101001); | |
154 | test ('oct', "B101001", 0b101001); |