3 # $Id: utf8.t,v 1.0.1.2 2000/09/28 21:44:17 ram Exp $
8 print "1..0 # Skip: no utf8 hash key support\n";
13 @INC = ('.', '../lib');
17 require Config; import Config;
19 if($Config{'extensions'} !~ /\bStorable\b/) {
20 print "1..0 # Skip: Storable was not built\n";
27 our $DEBUGME = shift || 0;
28 use Storable qw(store nstore retrieve thaw freeze);
31 $Storable::DEBUGME = ($DEBUGME > 1);
33 # Better than no plan, because I was getting out of memory errors, at which
34 # point Test::More tidily prints up 1..79 as if I meant to finish there.
35 use Test::More tests=>148;
37 use Encode qw(is_utf8);
40 $Storable::canonical = $Storable::canonical; # Shut up a used only once warning.
42 for $Storable::canonical (0, 1) {
44 # first we generate a nasty hash which keys include both utf8
45 # on and off with identical PVs
47 no utf8; # we have a naked 8-bit byte below (in Latin 1, anyway)
49 # In Latin 1 -ese the below ord() should end up 0xc0 (192),
50 # in EBCDIC 0x64 (100). Both should end up being UTF-8/UTF-EBCDIC.
52 ord("Á"), # LATIN CAPITAL LETTER A WITH GRAVE
53 0x3000, #IDEOGRAPHIC SPACE
56 foreach my $i (@ords){
57 my $u = chr($i); utf8::upgrade($u);
58 # warn sprintf "%d,%d", bytes::length($u), is_utf8($u);
59 my $b = pack("C*", unpack("C*", $u));
60 # warn sprintf "%d,%d" ,bytes::length($b), is_utf8($b);
63 "equivalence - with utf8flag");
64 is (pack("C*", unpack("C*", $u)), pack("C*", unpack("C*", $b)),
65 "equivalence - without utf8flag");
67 $utf8hash{$u} = $utf8hash{$b} = $i;
72 return scalar keys %$href;
76 is($nk = nkeys(\%utf8hash), scalar(@ords)*2,
77 "nasty hash generated (nkeys=$nk)");
79 # now let the show begin!
81 my $thawed = thaw(freeze(\%utf8hash));
83 is($nk = nkeys($thawed),
85 "scalar keys \%{\$thawed} (nkeys=$nk)");
86 for my $k (sort keys %$thawed){
87 is($utf8hash{$k}, $thawed->{$k}, "frozen item chr($utf8hash{$k})");
90 my $storage = "utfhash.po"; # po = perl object!
93 ok((nstore \%utf8hash, $storage), "nstore to $storage");
94 ok(($retrieved = retrieve($storage)), "retrieve from $storage");
96 is($nk = nkeys($retrieved),
98 "scalar keys \%{\$retrieved} (nkeys=$nk)");
99 for my $k (sort keys %$retrieved){
100 is($utf8hash{$k}, $retrieved->{$k}, "nstored item chr($utf8hash{$k})");
105 ok((store \%utf8hash, $storage), "store to $storage");
106 ok(($retrieved = retrieve($storage)), "retrieve from $storage");
107 is($nk = nkeys($retrieved),
109 "scalar keys \%{\$retrieved} (nkeys=$nk)");
110 for my $k (sort keys %$retrieved){
111 is($utf8hash{$k}, $retrieved->{$k}, "stored item chr($utf8hash{$k})");
113 $DEBUGME or unlink $storage;
115 # On the premis that more tests are good, here are NWC's tests:
120 return (undef, $_[0]);
125 my $utf8 = "Schlo\xdf" . chr 256;
128 # Set this to 1 to test the test by bypassing Storable.
132 my ($object, $package) = @_;
134 is ref $object, 'HASH', "$object is unblessed";
137 isa_ok ($object, $package);
138 my ($garbage, $copy) = eval {$object->me_second};
139 is $@, "", "check it has correct method";
140 cmp_ok $copy, '==', $object, "and that it returns the same object";
143 # Thanks to Dan Kogai for the Kanji for "castle" (which he informs me also
144 # means 'a city' in Mandarin).
145 my %hash = (map {$_, $_} 'castle', "ch\xe5teau", $utf8, "\x{57CE}");
147 for my $package ('', 'Hash_Test') {
148 # Run through and sanity check these.
150 bless \%hash, $package;
154 my $r = 0 + $hash{$_} =~ /^\w+$/;
155 cmp_ok ($l, '==', $r);
158 # Grr. This cperl mode thinks that ${ is a punctuation variable.
159 # I presume it's punishment for using xemacs rather than emacs. Or OS/2 :-)
160 my $copy = $bypass ? \%hash : ${thaw freeze \\%hash};
161 class_test ($copy, $package);
165 my $r = 0 + $copy->{$_} =~ /^\w+$/;
166 cmp_ok ($l, '==', $r, sprintf "key length %d", length $_);
170 my $bytes = my $char = chr 27182;
171 utf8::encode ($bytes);
173 my $orig = {$char => 1};
175 bless $orig, $package;
177 my $just_utf8 = $bypass ? $orig : ${thaw freeze \$orig};
178 class_test ($just_utf8, $package);
179 cmp_ok (scalar keys %$just_utf8, '==', 1, "1 key in utf8?");
180 cmp_ok ($just_utf8->{$char}, '==', 1, "utf8 key present?");
181 ok (!exists $just_utf8->{$bytes}, "bytes key absent?");
183 $orig = {$bytes => 1};
185 bless $orig, $package;
187 my $just_bytes = $bypass ? $orig : ${thaw freeze \$orig};
188 class_test ($just_bytes, $package);
190 cmp_ok (scalar keys %$just_bytes, '==', 1, "1 key in bytes?");
191 cmp_ok ($just_bytes->{$bytes}, '==', 1, "bytes key present?");
192 ok (!exists $just_bytes->{$char}, "utf8 key absent?");
194 die sprintf "Both have length %d, which is crazy", length $char
195 if length $char == length $bytes;
197 $orig = {$bytes => length $bytes, $char => length $char};
199 bless $orig, $package;
201 my $both = $bypass ? $orig : ${thaw freeze \$orig};
202 class_test ($both, $package);
204 cmp_ok (scalar keys %$both, '==', 2, "2 keys?");
205 cmp_ok ($both->{$bytes}, '==', length $bytes, "bytes key present?");
206 cmp_ok ($both->{$char}, '==', length $char, "utf8 key present?");