This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Really increase $File::DosGlob::VERSION to 1.07
[perl5.git] / ext / XS-APItest / t / utf8.t
CommitLineData
fed3ba5d
NC
1#!perl -w
2
3use strict;
4use Test::More;
5
6use XS::APItest;
7
8foreach ([0, '', '', 'empty'],
9 [0, 'N', 'N', '1 char'],
10 [1, 'NN', 'N', '1 char substring'],
11 [-2, 'Perl', 'Rules', 'different'],
12 [0, chr 163, chr 163, 'pound sign'],
13 [1, chr (163) . 10, chr (163) . 1, '10 pounds is more than 1 pound'],
14 [1, chr(163) . chr(163), chr 163, '2 pound signs are more than 1'],
15 [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
16 [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
17 ) {
18 my ($expect, $left, $right, $desc) = @$_;
19 my $copy = $right;
20 utf8::encode($copy);
21 is(bytes_cmp_utf8($left, $copy), $expect, $desc);
22 next if $right =~ tr/\0-\377//c;
23 utf8::encode($left);
24 is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
25}
26
eb83ed87
KW
27# Test uft8n_to_uvuni(). These provide essentially complete code coverage.
28
29# Copied from utf8.h
30my $UTF8_ALLOW_EMPTY = 0x0001;
31my $UTF8_ALLOW_CONTINUATION = 0x0002;
32my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
33my $UTF8_ALLOW_SHORT = 0x0008;
34my $UTF8_ALLOW_LONG = 0x0010;
35my $UTF8_DISALLOW_SURROGATE = 0x0020;
36my $UTF8_WARN_SURROGATE = 0x0040;
37my $UTF8_DISALLOW_NONCHAR = 0x0080;
38my $UTF8_WARN_NONCHAR = 0x0100;
39my $UTF8_DISALLOW_SUPER = 0x0200;
40my $UTF8_WARN_SUPER = 0x0400;
41my $UTF8_DISALLOW_FE_FF = 0x0800;
42my $UTF8_WARN_FE_FF = 0x1000;
43my $UTF8_CHECK_ONLY = 0x2000;
44
45my $REPLACEMENT = 0xFFFD;
46
47my @warnings;
48
49use warnings 'utf8';
50local $SIG{__WARN__} = sub { push @warnings, @_ };
51
52# First test the malformations. All these raise category utf8 warnings.
53foreach my $test (
54 [ "zero length string malformation", "", 0,
55 $UTF8_ALLOW_EMPTY, 0, 0,
56 qr/empty string/
57 ],
58 [ "orphan continuation byte malformation", "\x80a", 2,
59 $UTF8_ALLOW_CONTINUATION, $REPLACEMENT, 1,
60 qr/unexpected continuation byte/
61 ],
62 [ "premature next character malformation (immediate)", "\xc2a", 2,
63 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 1,
64 qr/unexpected non-continuation byte.*immediately after start byte/
65 ],
66 [ "premature next character malformation (non-immediate)", "\xf0\x80a", 3,
67 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 2,
68 qr/unexpected non-continuation byte .* 2 bytes after start byte/
69 ],
70 [ "too short malformation", "\xf0\x80a", 2,
71 # Having the 'a' after this, but saying there are only 2 bytes also
72 # tests that we pay attention to the passed in length
73 $UTF8_ALLOW_SHORT, $REPLACEMENT, 2,
74 qr/2 bytes, need 4/
75 ],
76 [ "overlong malformation", "\xc1\xaf", 2,
77 $UTF8_ALLOW_LONG, ord('o'), 2,
78 qr/2 bytes, need 1/
79 ],
80 [ "overflow malformation", "\xff\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf", 13,
81 0, # There is no way to allow this malformation
82 $REPLACEMENT, 13,
83 qr/overflow/
84 ],
85) {
86 my ($testname, $bytes, $length, $allow_flags, $allowed_uv, $expected_len, $message ) = @$test;
87
88 next if ! ok(length($bytes) >= $length, "$testname: Make sure won't read beyond buffer: " . length($bytes) . " >= $length");
89
90 # Test what happens when this malformation is not allowed
91 undef @warnings;
92 my $ret_ref = test_utf8n_to_uvuni($bytes, $length, 0);
93 is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
94 is($ret_ref->[1], $expected_len, "$testname: disallowed: Returns expected length");
95 if (is(scalar @warnings, 1, "$testname: disallowed: Got a single warning ")) {
96 like($warnings[0], $message, "$testname: disallowed: Got expected warning");
97 }
98 else {
99 if (scalar @warnings) {
100 note "The warnings were: " . join(", ", @warnings);
101 }
102 }
103
104 { # Next test when disallowed, and warnings are off.
105 undef @warnings;
106 no warnings 'utf8';
107 my $ret_ref = test_utf8n_to_uvuni($bytes, $length, 0);
108 is($ret_ref->[0], 0, "$testname: disallowed: no warnings 'utf8': Returns 0");
109 is($ret_ref->[1], $expected_len, "$testname: disallowed: no warnings 'utf8': Returns expected length");
110 if (!is(scalar @warnings, 0, "$testname: disallowed: no warnings 'utf8': no warnings generated")) {
111 note "The warnings were: " . join(", ", @warnings);
112 }
113 }
114
115 # Test with CHECK_ONLY
116 undef @warnings;
117 $ret_ref = test_utf8n_to_uvuni($bytes, $length, $UTF8_CHECK_ONLY);
118 is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
119 is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns expected length");
120 if (! is(scalar @warnings, 0, "$testname: CHECK_ONLY: no warnings generated")) {
121 note "The warnings were: " . join(", ", @warnings);
122 }
123
124 next if $allow_flags == 0; # Skip if can't allow this malformation
125
126 # Test when the malformation is allowed
127 undef @warnings;
128 $ret_ref = test_utf8n_to_uvuni($bytes, $length, $allow_flags);
129 is($ret_ref->[0], $allowed_uv, "$testname: allowed: Returns expected uv");
130 is($ret_ref->[1], $expected_len, "$testname: allowed: Returns expected length");
131 if (!is(scalar @warnings, 0, "$testname: allowed: no warnings generated"))
132 {
133 note "The warnings were: " . join(", ", @warnings);
134 }
135}
136
137my $FF_ret;
138
139use Unicode::UCD;
140my $has_quad = ($Unicode::UCD::MAX_CP > 0xFFFF_FFFF);
141if ($has_quad) {
142 no warnings qw{portable overflow};
143 $FF_ret = 0x1000000000;
144}
145else { # The above overflows unless a quad platform
146 $FF_ret = 0;
147}
148
149# Now test the cases where a legal code point is generated, but may or may not
150# be allowed/warned on.
151foreach my $test (
152 [ "surrogate", "\xed\xa4\x8d",
153 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, 'surrogate', 0xD90D, 3,
154 qr/surrogate/
155 ],
156 [ "non_unicode", "\xf4\x90\x80\x80",
157 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, 'non_unicode', 0x110000, 4,
158 qr/not Unicode/
159 ],
160 [ "non-character code point", "\xEF\xB7\x90",
161 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, 'nonchar', 0xFDD0, 3,
162 qr/Unicode non-character.*is illegal for open interchange/
163 ],
164 [ "begins with FE", "\xfe\x82\x80\x80\x80\x80\x80",
165
166 # This code point is chosen so that it is representable in a UV on
167 # 32-bit machines, otherwise we would have to handle it like the FF
168 # ones
169 $UTF8_WARN_FE_FF, $UTF8_DISALLOW_FE_FF, 'utf8', 0x80000000, 7,
170 qr/Code point beginning with byte .* is not Unicode, and not portable/
171 ],
172 [ "begins with FF", "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
173 $UTF8_WARN_FE_FF, $UTF8_DISALLOW_FE_FF, 'utf8', $FF_ret, 13,
174 qr/Code point beginning with byte .* is not Unicode, and not portable/
175 ],
176) {
177 my ($testname, $bytes, $warn_flags, $disallow_flags, $category, $allowed_uv, $expected_len, $message ) = @$test;
178
179 my $length = length $bytes;
180
181 # This is more complicated than the malformations tested earlier, as there
182 # are several orthogonal variables involved. We test all the subclasses
183 # of utf8 warnings to verify they work with and without the utf8 class,
184 # and don't have effects on other sublass warnings
185 foreach my $warning (0, 'utf8', 'surrogate', 'nonchar', 'non_unicode') {
186 foreach my $warn_flag (0, $warn_flags) {
187 foreach my $disallow_flag (0, $disallow_flags) {
188
189 # On 32-bit machines, anything beginning with \xff is not
190 # representable, and would overflow even if we were to allow
191 # them in this test.
192 next if ! $has_quad
193 && ! $disallow_flag
194 && substr($bytes, 0, 1) eq "\xff";
195
196 no warnings 'utf8';
197 my $eval_warn = $warning eq 0 ? "no warnings" : "use warnings '$warning'";
198 my $this_name = "$testname: " . (($disallow_flag) ? 'disallowed' : 'allowed');
199 $this_name .= ", $eval_warn";
200 $this_name .= ", " . (($warn_flag) ? 'with warning flag' : 'no warning flag');
201
202 undef @warnings;
203 my $ret_ref;
204 #note __LINE__ . ": $eval_warn; \$ret_ref = test_utf8n_to_uvuni('$bytes', $length, $warn_flag|$disallow_flag)";
205 my $eval_text = "$eval_warn; \$ret_ref = test_utf8n_to_uvuni('$bytes', $length, $warn_flag|$disallow_flag)";
206 eval "$eval_text";
207 if (! ok ("$@ eq ''", "$this_name: eval succeeded")) {
208 note "\$!='$!'; eval'd=\"$eval_text\"";
209 next;
210 }
211 if ($disallow_flag) {
212 is($ret_ref->[0], 0, "$this_name: Returns 0");
213 }
214 else {
215 is($ret_ref->[0], $allowed_uv, "$this_name: Returns expected uv");
216 }
217 is($ret_ref->[1], $expected_len, "$this_name: Returns expected length");
218
219 if ($warn_flag && ($warning eq 'utf8' || $warning eq $category)) {
220 if (is(scalar @warnings, 1, "$this_name: Got a single warning ")) {
221 like($warnings[0], $message, "$this_name: Got expected warning");
222 }
223 else {
224 if (scalar @warnings) {
225 note "The warnings were: " . join(", ", @warnings);
226 }
227 }
228 }
229 else {
230 if (!is(scalar @warnings, 0, "$this_name: No warnings generated"))
231 {
232 note "The warnings were: " . join(", ", @warnings);
233 }
234 }
235
236 if ($disallow_flag) {
237 undef @warnings;
238 $ret_ref = test_utf8n_to_uvuni($bytes, $length, $disallow_flag|$UTF8_CHECK_ONLY);
239 is($ret_ref->[0], 0, "$this_name, CHECK_ONLY: Returns 0");
240 is($ret_ref->[1], -1, "$this_name: CHECK_ONLY: returns expected length");
241 if (! is(scalar @warnings, 0, "$this_name, CHECK_ONLY: no warnings generated")) {
242 note "The warnings were: " . join(", ", @warnings);
243 }
244 }
245 }
246 }
247 }
248}
249
fed3ba5d 250done_testing;