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') {
172 } elsif ($operator eq 'syswrite') {
179 } elsif ($operator eq 'syswrite len') {
186 } elsif ($operator eq 'syswrite off'
187 || $operator eq 'syswrite len off') {
188 syswrite $fh, $u, 2, 1;
189 syswrite $fh, $u, 2, 1;
190 syswrite $fh, $u, 2, 1;
191 syswrite $fh, $l, 2, 1;
192 syswrite $fh, $l, 2, 1;
193 syswrite $fh, $l, 2, 1;
198 seek $fh, 0, 0 or die $!;
200 chomp ($line = <$fh>);
201 is ($line, "\311", "$operator $layer");
202 chomp ($line = <$fh>);
203 is ($line, "\311", "$operator $layer");
204 chomp ($line = <$fh>);
205 is ($line, "\311", "$operator $layer");
206 chomp ($line = <$fh>);
207 is ($line, "\351", "$operator $layer");
208 chomp ($line = <$fh>);
209 is ($line, "\351", "$operator $layer");
210 chomp ($line = <$fh>);
211 is ($line, "\351", "$operator $layer");
214 unlink $tmpfile or die $!;
218 my $little = "\243\243";
219 my $big = " \243 $little ! $little ! $little \243 ";
220 my $right = rindex $big, $little;
221 my $right1 = rindex $big, $little, 11;
222 my $left = index $big, $little;
223 my $left1 = index $big, $little, 4;
225 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
226 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
228 foreach my $b ($big, UTF8Toggle->new($big)) {
229 foreach my $l ($little, UTF8Toggle->new($little),
230 UTF8Toggle->new($little, 1)) {
231 is (rindex ($b, $l), $right, "rindex");
232 is (rindex ($b, $l), $right, "rindex");
233 is (rindex ($b, $l), $right, "rindex");
235 is (rindex ($b, $l, 11), $right1, "rindex 11");
236 is (rindex ($b, $l, 11), $right1, "rindex 11");
237 is (rindex ($b, $l, 11), $right1, "rindex 11");
239 is (index ($b, $l), $left, "index");
240 is (index ($b, $l), $left, "index");
241 is (index ($b, $l), $left, "index");
243 is (index ($b, $l, 4), $left1, "index 4");
244 is (index ($b, $l, 4), $left1, "index 4");
245 is (index ($b, $l, 4), $left1, "index 4");
250 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
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");
253 like ($bits ^ $pieces, 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");
257 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
260 foreach my $value ("\243", UTF8Toggle->new("\243")) {
261 is (pack ("A/A", $value), pack ("A/A", "\243"),
262 "pack copes with overloading");
263 is (pack ("A/A", $value), pack ("A/A", "\243"));
264 is (pack ("A/A", $value), pack ("A/A", "\243"));
268 1 while -f $tmpfile and unlink $tmpfile || die "unlink '$tmpfile': $!";