3 # $Id: utf8.t,v 1.0.1.2 2000/09/28 21:44:17 ram Exp $
9 print "1..0 # Skip: no utf8 hash key support\n";
14 @INC = ('.', '../lib');
18 require Config; import Config;
20 if($Config{'extensions'} !~ /\bStorable\b/) {
21 print "1..0 # Skip: Storable was not built\n";
28 our $DEBUGME = shift || 0;
29 use Storable qw(store nstore retrieve thaw freeze);
32 $Storable::DEBUGME = ($DEBUGME > 1);
34 # Better than no plan, because I was getting out of memory errors, at which
35 # point Test::More tidily prints up 1..79 as if I meant to finish there.
36 use Test::More tests=>148;
38 use Encode qw(is_utf8);
41 $Storable::canonical = $Storable::canonical; # Shut up a used only once warning.
43 for $Storable::canonical (0, 1) {
45 # first we generate a nasty hash which keys include both utf8
46 # on and off with identical PVs
48 no utf8; # we have a naked 8-bit byte below (in Latin 1, anyway)
50 # In Latin 1 -ese the below ord() should end up 0xc0 (192),
51 # in EBCDIC 0x64 (100). Both should end up being UTF-8/UTF-EBCDIC.
53 ord("Á"), # LATIN CAPITAL LETTER A WITH GRAVE
54 0x3000, #IDEOGRAPHIC SPACE
57 foreach my $i (@ords){
58 my $u = chr($i); utf8::upgrade($u);
59 # warn sprintf "%d,%d", bytes::length($u), is_utf8($u);
60 my $b = pack("C*", unpack("C*", $u));
61 # warn sprintf "%d,%d" ,bytes::length($b), is_utf8($b);
64 "equivalence - with utf8flag");
65 is (pack("C*", unpack("C*", $u)), pack("C*", unpack("C*", $b)),
66 "equivalence - without utf8flag");
68 $utf8hash{$u} = $utf8hash{$b} = $i;
73 return scalar keys %$href;
77 is($nk = nkeys(\%utf8hash), scalar(@ords)*2,
78 "nasty hash generated (nkeys=$nk)");
80 # now let the show begin!
82 my $thawed = thaw(freeze(\%utf8hash));
84 is($nk = nkeys($thawed),
86 "scalar keys \%{\$thawed} (nkeys=$nk)");
87 for my $k (sort keys %$thawed){
88 is($utf8hash{$k}, $thawed->{$k}, "frozen item chr($utf8hash{$k})");
91 my $storage = "utfhash.po"; # po = perl object!
94 ok((nstore \%utf8hash, $storage), "nstore to $storage");
95 ok(($retrieved = retrieve($storage)), "retrieve from $storage");
97 is($nk = nkeys($retrieved),
99 "scalar keys \%{\$retrieved} (nkeys=$nk)");
100 for my $k (sort keys %$retrieved){
101 is($utf8hash{$k}, $retrieved->{$k}, "nstored item chr($utf8hash{$k})");
106 ok((store \%utf8hash, $storage), "store to $storage");
107 ok(($retrieved = retrieve($storage)), "retrieve from $storage");
108 is($nk = nkeys($retrieved),
110 "scalar keys \%{\$retrieved} (nkeys=$nk)");
111 for my $k (sort keys %$retrieved){
112 is($utf8hash{$k}, $retrieved->{$k}, "stored item chr($utf8hash{$k})");
114 $DEBUGME or unlink $storage;
116 # On the premis that more tests are good, here are NWC's tests:
121 return (undef, $_[0]);
126 my $utf8 = "Schlo\xdf" . chr 256;
129 # Set this to 1 to test the test by bypassing Storable.
133 my ($object, $package) = @_;
135 is ref $object, 'HASH', "$object is unblessed";
138 isa_ok ($object, $package);
139 my ($garbage, $copy) = eval {$object->me_second};
140 is $@, "", "check it has correct method";
141 cmp_ok $copy, '==', $object, "and that it returns the same object";
144 # Thanks to Dan Kogai for the Kanji for "castle" (which he informs me also
145 # means 'a city' in Mandarin).
146 my %hash = (map {$_, $_} 'castle', "ch\xe5teau", $utf8, "\x{57CE}");
148 for my $package ('', 'Hash_Test') {
149 # Run through and sanity check these.
151 bless \%hash, $package;
155 my $r = 0 + $hash{$_} =~ /^\w+$/;
156 cmp_ok ($l, '==', $r);
159 # Grr. This cperl mode thinks that ${ is a punctuation variable.
160 # I presume it's punishment for using xemacs rather than emacs. Or OS/2 :-)
161 my $copy = $bypass ? \%hash : ${thaw freeze \\%hash};
162 class_test ($copy, $package);
166 my $r = 0 + $copy->{$_} =~ /^\w+$/;
167 cmp_ok ($l, '==', $r, sprintf "key length %d", length $_);
171 my $bytes = my $char = chr 27182;
172 utf8::encode ($bytes);
174 my $orig = {$char => 1};
176 bless $orig, $package;
178 my $just_utf8 = $bypass ? $orig : ${thaw freeze \$orig};
179 class_test ($just_utf8, $package);
180 cmp_ok (scalar keys %$just_utf8, '==', 1, "1 key in utf8?");
181 cmp_ok ($just_utf8->{$char}, '==', 1, "utf8 key present?");
182 ok (!exists $just_utf8->{$bytes}, "bytes key absent?");
184 $orig = {$bytes => 1};
186 bless $orig, $package;
188 my $just_bytes = $bypass ? $orig : ${thaw freeze \$orig};
189 class_test ($just_bytes, $package);
191 cmp_ok (scalar keys %$just_bytes, '==', 1, "1 key in bytes?");
192 cmp_ok ($just_bytes->{$bytes}, '==', 1, "bytes key present?");
193 ok (!exists $just_bytes->{$char}, "utf8 key absent?");
195 die sprintf "Both have length %d, which is crazy", length $char
196 if length $char == length $bytes;
198 $orig = {$bytes => length $bytes, $char => length $char};
200 bless $orig, $package;
202 my $both = $bypass ? $orig : ${thaw freeze \$orig};
203 class_test ($both, $package);
205 cmp_ok (scalar keys %$both, '==', 2, "2 keys?");
206 cmp_ok ($both->{$bytes}, '==', length $bytes, "bytes key present?");
207 cmp_ok ($both->{$char}, '==', length $char, "utf8 key present?");