This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Check for sanity of UTF-8 keys in hashes
authorNick Ing-Simmons <nik@tiuk.ti.com>
Fri, 22 Mar 2002 10:50:53 +0000 (10:50 +0000)
committerNick Ing-Simmons <nik@tiuk.ti.com>
Fri, 22 Mar 2002 10:50:53 +0000 (10:50 +0000)
(feel free to add more tests...)

p4raw-id: //depot/perlio@15412

MANIFEST
t/op/utfhash.t [new file with mode: 0644]

index 0459088..1403dfd 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -2421,6 +2421,7 @@ t/op/undef.t                      See if undef works
 t/op/universal.t               See if UNIVERSAL class works
 t/op/unshift.t                 See if unshift works
 t/op/utf8decode.t              See if UTF-8 decoding works
+t/op/utfhash.t                 See if utf8 keys in hashes behave
 t/op/vec.t                     See if vectors work
 t/op/ver.t                     See if v-strings and the %v format flag work
 t/op/wantarray.t               See if wantarray works
diff --git a/t/op/utfhash.t b/t/op/utfhash.t
new file mode 100644 (file)
index 0000000..a955f28
--- /dev/null
@@ -0,0 +1,79 @@
+BEGIN {
+    chdir 't' if -d 't';
+    @INC = '../lib';
+    require './test.pl';
+
+    plan(tests => 37);
+}
+
+# Two hashes one will all keys 8-bit possible (initially), other
+# with a utf8 requiring key from the outset.
+
+my %hash8 = ( "\xff" => 0xff,
+              "\x7f" => 0x7f,
+            );
+my %hashu = ( "\xff" => 0xff,
+              "\x7f" => 0x7f,
+              "\x{1ff}" => 0x1ff,
+            );
+
+# Check that we can find the 8-bit things by various litterals
+is($hash8{"\x{00ff}"},0xFF);
+is($hash8{"\x{007f}"},0x7F);
+is($hash8{"\xff"},0xFF);
+is($hash8{"\x7f"},0x7F);
+is($hashu{"\x{00ff}"},0xFF);
+is($hashu{"\x{007f}"},0x7F);
+is($hashu{"\xff"},0xFF);
+is($hashu{"\x7f"},0x7F);
+
+# Now try same thing with variables forced into various forms.
+foreach my $a ("\x7f","\xff")
+ {
+  utf8::upgrade($a);
+  is($hash8{$a},ord($a));
+  is($hashu{$a},ord($a));
+  utf8::downgrade($a);
+  is($hash8{$a},ord($a));
+  is($hashu{$a},ord($a));
+  my $b = $a.chr(100);
+  chop($b);
+  is($hash8{$b},ord($b));
+  is($hashu{$b},ord($b));
+ }
+
+# Check we have not got an spurious extra keys
+is(join('',sort keys %hash8),"\x7f\xff");
+is(join('',sort keys %hashu),"\x7f\xff\x{1ff}");
+
+# Now add a utf8 key to the 8-bit hash
+$hash8{chr(0x1ff)} = 0x1ff;
+
+# Check we have not got an spurious extra keys
+is(join('',sort keys %hash8),"\x7f\xff\x{1ff}");
+
+foreach my $a ("\x7f","\xff","\x{1ff}")
+ {
+  utf8::upgrade($a);
+  is($hash8{$a},ord($a));
+  my $b = $a.chr(100);
+  chop($b);
+  is($hash8{$b},ord($b));
+ }
+
+# and remove utf8 from the other hash
+is(delete $hashu{chr(0x1ff)},0x1ff);
+is(join('',sort keys %hashu),"\x7f\xff");
+
+foreach my $a ("\x7f","\xff")
+ {
+  utf8::upgrade($a);
+  is($hashu{$a},ord($a));
+  utf8::downgrade($a);
+  is($hashu{$a},ord($a));
+  my $b = $a.chr(100);
+  chop($b);
+  is($hashu{$b},ord($b));
+ }
+
+