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 if ($operator eq 'print') {
180 } elsif ($operator eq 'syswrite') {
187 } elsif ($operator eq 'syswrite len') {
194 } elsif ($operator eq 'syswrite off'
195 || $operator eq 'syswrite len off') {
196 syswrite $fh, $u, 2, 1;
197 syswrite $fh, $u, 2, 1;
198 syswrite $fh, $u, 2, 1;
199 syswrite $fh, $l, 2, 1;
200 syswrite $fh, $l, 2, 1;
201 syswrite $fh, $l, 2, 1;
206 seek $fh, 0, 0 or die $!;
208 chomp ($line = <$fh>);
209 is ($line, $E_acute, "$operator $layer");
210 chomp ($line = <$fh>);
211 is ($line, $E_acute, "$operator $layer");
212 chomp ($line = <$fh>);
213 is ($line, $E_acute, "$operator $layer");
214 chomp ($line = <$fh>);
215 is ($line, $e_acute, "$operator $layer");
216 chomp ($line = <$fh>);
217 is ($line, $e_acute, "$operator $layer");
218 chomp ($line = <$fh>);
219 is ($line, $e_acute, "$operator $layer");
225 my $little = "\243\243";
226 my $big = " \243 $little ! $little ! $little \243 ";
227 my $right = rindex $big, $little;
228 my $right1 = rindex $big, $little, 11;
229 my $left = index $big, $little;
230 my $left1 = index $big, $little, 4;
232 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
233 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
235 foreach my $b ($big, UTF8Toggle->new($big)) {
236 foreach my $l ($little, UTF8Toggle->new($little),
237 UTF8Toggle->new($little, 1)) {
238 is (rindex ($b, $l), $right, "rindex");
239 is (rindex ($b, $l), $right, "rindex");
240 is (rindex ($b, $l), $right, "rindex");
242 is (rindex ($b, $l, 11), $right1, "rindex 11");
243 is (rindex ($b, $l, 11), $right1, "rindex 11");
244 is (rindex ($b, $l, 11), $right1, "rindex 11");
246 is (index ($b, $l), $left, "index");
247 is (index ($b, $l), $left, "index");
248 is (index ($b, $l), $left, "index");
250 is (index ($b, $l, 4), $left1, "index 4");
251 is (index ($b, $l, 4), $left1, "index 4");
252 is (index ($b, $l, 4), $left1, "index 4");
257 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
258 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
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");
262 like ($pieces ^ $bits, 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");
267 foreach my $value ("\243", UTF8Toggle->new("\243")) {
268 is (pack ("A/A", $value), pack ("A/A", "\243"),
269 "pack copes with overloading");
270 is (pack ("A/A", $value), pack ("A/A", "\243"));
271 is (pack ("A/A", $value), pack ("A/A", "\243"));
274 foreach my $value ("\243", UTF8Toggle->new("\243")) {
276 $v = substr $value, 0, 1;
278 $v = substr $value, 0, 1;
280 $v = substr $value, 0, 1;
286 use overload '""' => sub { $_[0]->{data} }
290 my $text = bless { data => "\x{3075}" }, 'RT69422';
291 my $p = substr $text, 0, 1;