6 require Config; import Config;
7 require './test.pl'; require './charset_tools.pl';
15 use overload '""' => 'stringify', fallback => 1;
21 return bless [$value, $state], $class;
26 $self->[1] = ! $self->[1];
28 utf8::downgrade($self->[0]);
30 utf8::upgrade($self->[0]);
37 # These tests are based on characters 128-255 not having latin1, and hence
39 # no feature "unicode_strings";
42 foreach my $t ("ASCII", "B" . uni_to_native("\366") . "se") {
43 my $length = length $t;
45 my $u = UTF8Toggle->new($t);
46 is (length $u, $length, "length of '$t'");
47 is (length $u, $length, "length of '$t'");
48 is (length $u, $length, "length of '$t'");
49 is (length $u, $length, "length of '$t'");
52 my $E_acute = uni_to_native("\311");
53 my $e_acute = uni_to_native("\351");
54 my $u = UTF8Toggle->new($E_acute);
57 is ($lc, $E_acute, "E acute -> e acute");
60 is ($lc, $e_acute, "E acute -> e acute");
63 is ($lc, $E_acute, "E acute -> e acute");
65 $u = UTF8Toggle->new($e_acute);
68 is ($uc, $e_acute, "e acute -> E acute");
71 is ($uc, $E_acute, "e acute -> E acute");
74 is ($uc, $e_acute, "e acute -> E acute");
76 $u = UTF8Toggle->new($E_acute);
79 is ($lc, $E_acute, "E acute -> e acute");
82 is ($lc, $e_acute, "E acute -> e acute");
85 is ($lc, $E_acute, "E acute -> e acute");
87 $u = UTF8Toggle->new($e_acute);
90 is ($uc, $e_acute, "e acute -> E acute");
93 is ($uc, $E_acute, "e acute -> E acute");
96 is ($uc, $e_acute, "e acute -> E acute");
98 my $have_setlocale = 0;
101 if($Config{d_setlocale}) {
102 import POSIX ':locale_h';
108 if (!$have_setlocale) {
109 skip "No setlocale", 24;
110 } elsif (!setlocale(&POSIX::LC_ALL, "en_GB.ISO8859-1")) {
111 skip "Could not setlocale to en_GB.ISO8859-1", 24;
112 } elsif ($^O eq 'dec_osf' || $^O eq 'VMS') {
113 skip "$^O has broken en_GB.ISO8859-1 locale", 24;
116 my $u = UTF8Toggle->new($E_acute);
119 is ($lc, $e_acute, "E acute -> e acute");
122 is ($lc, $e_acute, "E acute -> e acute");
125 is ($lc, $e_acute, "E acute -> e acute");
127 $u = UTF8Toggle->new($e_acute);
130 is ($uc, $E_acute, "e acute -> E acute");
133 is ($uc, $E_acute, "e acute -> E acute");
136 is ($uc, $E_acute, "e acute -> E acute");
138 $u = UTF8Toggle->new($E_acute);
141 is ($lc, $e_acute, "E acute -> e acute");
144 is ($lc, $e_acute, "E acute -> e acute");
147 is ($lc, $e_acute, "E acute -> e acute");
149 $u = UTF8Toggle->new($e_acute);
152 is ($uc, $E_acute, "e acute -> E acute");
155 is ($uc, $E_acute, "e acute -> E acute");
158 is ($uc, $E_acute, "e acute -> E acute");
162 my $tmpfile = tempfile();
164 foreach my $operator ('print', 'syswrite', 'syswrite len', 'syswrite off',
165 'syswrite len off') {
166 foreach my $layer ('', ':utf8') {
167 open my $fh, "+>$layer", $tmpfile or die $!;
168 my $pad = $operator =~ /\boff\b/ ? "\243" : "";
169 my $trail = $operator =~ /\blen\b/ ? "!" : "";
170 my $u = UTF8Toggle->new("$pad$E_acute\n$trail");
171 my $l = UTF8Toggle->new("$pad$e_acute\n$trail", 1);
172 no warnings 'deprecated';
173 if ($operator eq 'print') {
181 } elsif ($operator eq 'syswrite') {
188 } elsif ($operator eq 'syswrite len') {
195 } elsif ($operator eq 'syswrite off'
196 || $operator eq 'syswrite len off') {
197 syswrite $fh, $u, 2, 1;
198 syswrite $fh, $u, 2, 1;
199 syswrite $fh, $u, 2, 1;
200 syswrite $fh, $l, 2, 1;
201 syswrite $fh, $l, 2, 1;
202 syswrite $fh, $l, 2, 1;
207 seek $fh, 0, 0 or die $!;
209 chomp ($line = <$fh>);
210 is ($line, $E_acute, "$operator $layer");
211 chomp ($line = <$fh>);
212 is ($line, $E_acute, "$operator $layer");
213 chomp ($line = <$fh>);
214 is ($line, $E_acute, "$operator $layer");
215 chomp ($line = <$fh>);
216 is ($line, $e_acute, "$operator $layer");
217 chomp ($line = <$fh>);
218 is ($line, $e_acute, "$operator $layer");
219 chomp ($line = <$fh>);
220 is ($line, $e_acute, "$operator $layer");
226 my $little = "\243\243";
227 my $big = " \243 $little ! $little ! $little \243 ";
228 my $right = rindex $big, $little;
229 my $right1 = rindex $big, $little, 11;
230 my $left = index $big, $little;
231 my $left1 = index $big, $little, 4;
233 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
234 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
236 foreach my $b ($big, UTF8Toggle->new($big)) {
237 foreach my $l ($little, UTF8Toggle->new($little),
238 UTF8Toggle->new($little, 1)) {
239 is (rindex ($b, $l), $right, "rindex");
240 is (rindex ($b, $l), $right, "rindex");
241 is (rindex ($b, $l), $right, "rindex");
243 is (rindex ($b, $l, 11), $right1, "rindex 11");
244 is (rindex ($b, $l, 11), $right1, "rindex 11");
245 is (rindex ($b, $l, 11), $right1, "rindex 11");
247 is (index ($b, $l), $left, "index");
248 is (index ($b, $l), $left, "index");
249 is (index ($b, $l), $left, "index");
251 is (index ($b, $l, 4), $left1, "index 4");
252 is (index ($b, $l, 4), $left1, "index 4");
253 is (index ($b, $l, 4), $left1, "index 4");
258 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
259 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
260 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
261 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
263 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
264 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
265 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
268 foreach my $value ("\243", UTF8Toggle->new("\243")) {
269 is (pack ("A/A", $value), pack ("A/A", "\243"),
270 "pack copes with overloading");
271 is (pack ("A/A", $value), pack ("A/A", "\243"));
272 is (pack ("A/A", $value), pack ("A/A", "\243"));
275 foreach my $value ("\243", UTF8Toggle->new("\243")) {
277 $v = substr $value, 0, 1;
279 $v = substr $value, 0, 1;
281 $v = substr $value, 0, 1;
287 use overload '""' => sub { $_[0]->{data} }
291 my $text = bless { data => "\x{3075}" }, 'RT69422';
292 my $p = substr $text, 0, 1;