4 if ($ENV{'PERL_CORE'}){
10 use Test::More tests => 208;
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]);
38 foreach my $t ("ASCII", "B\366se") {
39 my $length = length $t;
41 my $u = UTF8Toggle->new($t);
42 is (length $u, $length, "length of '$t'");
43 is (length $u, $length, "length of '$t'");
44 is (length $u, $length, "length of '$t'");
45 is (length $u, $length, "length of '$t'");
48 my $u = UTF8Toggle->new("\311");
51 is ($lc, "\311", "E acute -> e acute");
54 is ($lc, "\351", "E acute -> e acute");
57 is ($lc, "\311", "E acute -> e acute");
59 $u = UTF8Toggle->new("\351");
62 is ($uc, "\351", "e acute -> E acute");
65 is ($uc, "\311", "e acute -> E acute");
68 is ($uc, "\351", "e acute -> E acute");
70 $u = UTF8Toggle->new("\311");
73 is ($lc, "\311", "E acute -> e acute");
76 is ($lc, "\351", "E acute -> e acute");
79 is ($lc, "\311", "E acute -> e acute");
81 $u = UTF8Toggle->new("\351");
84 is ($uc, "\351", "e acute -> E acute");
87 is ($uc, "\311", "e acute -> E acute");
90 is ($uc, "\351", "e acute -> E acute");
92 my $have_setlocale = 0;
95 import POSIX ':locale_h';
100 if (!$have_setlocale) {
101 skip "No setlocale", 24;
102 } elsif (!setlocale(&POSIX::LC_ALL, "en_GB.ISO8859-1")) {
103 skip "Could not setlocale to en_GB.ISO8859-1", 24;
104 } elsif ($^O eq 'dec_osf' || $^O eq 'VMS') {
105 skip "$^O has broken en_GB.ISO8859-1 locale", 24;
108 my $u = UTF8Toggle->new("\311");
111 is ($lc, "\351", "E acute -> e acute");
114 is ($lc, "\351", "E acute -> e acute");
117 is ($lc, "\351", "E acute -> e acute");
119 $u = UTF8Toggle->new("\351");
122 is ($uc, "\311", "e acute -> E acute");
125 is ($uc, "\311", "e acute -> E acute");
128 is ($uc, "\311", "e acute -> E acute");
130 $u = UTF8Toggle->new("\311");
133 is ($lc, "\351", "E acute -> e acute");
136 is ($lc, "\351", "E acute -> e acute");
139 is ($lc, "\351", "E acute -> e acute");
141 $u = UTF8Toggle->new("\351");
144 is ($uc, "\311", "e acute -> E acute");
147 is ($uc, "\311", "e acute -> E acute");
150 is ($uc, "\311", "e acute -> E acute");
154 my $tmpfile = 'overload.tmp';
156 foreach my $operator ('print', 'syswrite', 'syswrite len', 'syswrite off',
157 'syswrite len off') {
158 foreach my $layer ('', ':utf8') {
159 open my $fh, "+>$layer", $tmpfile or die $!;
160 my $pad = $operator =~ /\boff\b/ ? "\243" : "";
161 my $trail = $operator =~ /\blen\b/ ? "!" : "";
162 my $u = UTF8Toggle->new("$pad\311\n$trail");
163 my $l = UTF8Toggle->new("$pad\351\n$trail", 1);
164 if ($operator eq 'print') {
171 } elsif ($operator eq 'syswrite') {
178 } elsif ($operator eq 'syswrite len') {
185 } elsif ($operator eq 'syswrite off'
186 || $operator eq 'syswrite len off') {
187 syswrite $fh, $u, 2, 1;
188 syswrite $fh, $u, 2, 1;
189 syswrite $fh, $u, 2, 1;
190 syswrite $fh, $l, 2, 1;
191 syswrite $fh, $l, 2, 1;
192 syswrite $fh, $l, 2, 1;
197 seek $fh, 0, 0 or die $!;
199 chomp ($line = <$fh>);
200 is ($line, "\311", "$operator $layer");
201 chomp ($line = <$fh>);
202 is ($line, "\311", "$operator $layer");
203 chomp ($line = <$fh>);
204 is ($line, "\311", "$operator $layer");
205 chomp ($line = <$fh>);
206 is ($line, "\351", "$operator $layer");
207 chomp ($line = <$fh>);
208 is ($line, "\351", "$operator $layer");
209 chomp ($line = <$fh>);
210 is ($line, "\351", "$operator $layer");
213 unlink $tmpfile or die $!;
217 my $little = "\243\243";
218 my $big = " \243 $little ! $little ! $little \243 ";
219 my $right = rindex $big, $little;
220 my $right1 = rindex $big, $little, 11;
221 my $left = index $big, $little;
222 my $left1 = index $big, $little, 4;
224 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
225 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
227 foreach my $b ($big, UTF8Toggle->new($big)) {
228 foreach my $l ($little, UTF8Toggle->new($little),
229 UTF8Toggle->new($little, 1)) {
230 is (rindex ($b, $l), $right, "rindex");
231 is (rindex ($b, $l), $right, "rindex");
232 is (rindex ($b, $l), $right, "rindex");
234 is (rindex ($b, $l, 11), $right1, "rindex 11");
235 is (rindex ($b, $l, 11), $right1, "rindex 11");
236 is (rindex ($b, $l, 11), $right1, "rindex 11");
238 is (index ($b, $l), $left, "index");
239 is (index ($b, $l), $left, "index");
240 is (index ($b, $l), $left, "index");
242 is (index ($b, $l, 4), $left1, "index 4");
243 is (index ($b, $l, 4), $left1, "index 4");
244 is (index ($b, $l, 4), $left1, "index 4");
249 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
250 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
251 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
252 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
254 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
255 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
256 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
259 foreach my $value ("\243", UTF8Toggle->new("\243")) {
260 is (pack ("A/A", $value), pack ("A/A", "\243"),
261 "pack copes with overloading");
262 is (pack ("A/A", $value), pack ("A/A", "\243"));
263 is (pack ("A/A", $value), pack ("A/A", "\243"));
267 1 while -f $tmpfile and unlink $tmpfile || die "unlink '$tmpfile': $!";