This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
some WinCE compilers require a little correction
[perl5.git] / t / op / each.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 plan tests => 39;
10
11 $h{'abc'} = 'ABC';
12 $h{'def'} = 'DEF';
13 $h{'jkl','mno'} = "JKL\034MNO";
14 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
15 $h{'a'} = 'A';
16 $h{'b'} = 'B';
17 $h{'c'} = 'C';
18 $h{'d'} = 'D';
19 $h{'e'} = 'E';
20 $h{'f'} = 'F';
21 $h{'g'} = 'G';
22 $h{'h'} = 'H';
23 $h{'i'} = 'I';
24 $h{'j'} = 'J';
25 $h{'k'} = 'K';
26 $h{'l'} = 'L';
27 $h{'m'} = 'M';
28 $h{'n'} = 'N';
29 $h{'o'} = 'O';
30 $h{'p'} = 'P';
31 $h{'q'} = 'Q';
32 $h{'r'} = 'R';
33 $h{'s'} = 'S';
34 $h{'t'} = 'T';
35 $h{'u'} = 'U';
36 $h{'v'} = 'V';
37 $h{'w'} = 'W';
38 $h{'x'} = 'X';
39 $h{'y'} = 'Y';
40 $h{'z'} = 'Z';
41
42 @keys = keys %h;
43 @values = values %h;
44
45 ok ($#keys == 29, "keys");
46 ok ($#values == 29, "values");
47
48 $i = 0;         # stop -w complaints
49
50 while (($key,$value) = each(%h)) {
51     if ($key eq $keys[$i] && $value eq $values[$i]
52         && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
53         $key =~ y/a-z/A-Z/;
54         $i++ if $key eq $value;
55     }
56 }
57
58 ok ($i == 30, "each count");
59
60 @keys = ('blurfl', keys(%h), 'dyick');
61 ok ($#keys == 31, "added a key");
62
63 $size = ((split('/',scalar %h))[1]);
64 keys %h = $size * 5;
65 $newsize = ((split('/',scalar %h))[1]);
66 ok ($newsize == $size * 8, "resize");
67 keys %h = 1;
68 $size = ((split('/',scalar %h))[1]);
69 ok ($size == $newsize, "same size");
70 %h = (1,1);
71 $size = ((split('/',scalar %h))[1]);
72 ok ($size == $newsize, "still same size");
73 undef %h;
74 %h = (1,1);
75 $size = ((split('/',scalar %h))[1]);
76 ok ($size == 8, "size 8");
77
78 # test scalar each
79 %hash = 1..20;
80 $total = 0;
81 $total += $key while $key = each %hash;
82 ok ($total == 100, "test scalar each");
83
84 for (1..3) { @foo = each %hash }
85 keys %hash;
86 $total = 0;
87 $total += $key while $key = each %hash;
88 ok ($total == 100, "test scalar keys resets iterator");
89
90 for (1..3) { @foo = each %hash }
91 $total = 0;
92 $total += $key while $key = each %hash;
93 ok ($total != 100, "test iterator of each is being maintained");
94
95 for (1..3) { @foo = each %hash }
96 values %hash;
97 $total = 0;
98 $total += $key while $key = each %hash;
99 ok ($total == 100, "test values keys resets iterator");
100
101 $size = (split('/', scalar %hash))[1];
102 keys(%hash) = $size / 2;
103 ok ($size == (split('/', scalar %hash))[1]);
104 keys(%hash) = $size + 100;
105 ok ($size != (split('/', scalar %hash))[1]);
106
107 ok (keys(%hash) == 10, "keys (%hash)");
108
109 ok (keys(hash) == 10, "keys (hash)");
110
111 $i = 0;
112 %h = (a => A, b => B, c=> C, d => D, abc => ABC);
113 @keys = keys(h);
114 @values = values(h);
115 while (($key, $value) = each(h)) {
116         if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
117                 $i++;
118         }
119 }
120 ok ($i == 5);
121
122 @tests = (&next_test, &next_test, &next_test);
123 {
124     package Obj;
125     sub DESTROY { print "ok $::tests[1] # DESTROY called\n"; }
126     {
127         my $h = { A => bless [], __PACKAGE__ };
128         while (my($k,$v) = each %$h) {
129             print "ok $::tests[0]\n" if $k eq 'A' and ref($v) eq 'Obj';
130         }
131     }
132     print "ok $::tests[2]\n";
133 }
134
135 # Check for Unicode hash keys.
136 %u = ("\x{12}", "f", "\x{123}", "fo", "\x{1234}",  "foo");
137 $u{"\x{12345}"}  = "bar";
138 @u{"\x{10FFFD}"} = "zap";
139
140 my %u2;
141 foreach (keys %u) {
142     ok (length() == 1, "Check length of " . _qq $_);
143     $u2{$_} = $u{$_};
144 }
145 ok (eq_hash(\%u, \%u2), "copied unicode hash keys correctly?");
146
147 $a = "\xe3\x81\x82"; $A = "\x{3042}";
148 %b = ( $a => "non-utf8");
149 %u = ( $A => "utf8");
150
151 ok (!exists $b{$A}, "utf8 key in bytes hash");
152 ok (!exists $u{$a}, "bytes key in utf8 hash");
153 print "# $b{$_}\n" for keys %b; # Used to core dump before change #8056.
154 pass ("if we got here change 8056 worked");
155 print "# $u{$_}\n" for keys %u; # Used to core dump before change #8056.
156 pass ("change 8056 is thanks to Inaba Hiroto");
157
158 # on EBCDIC chars are mapped differently so pick something that needs encoding
159 # there too.
160 $d = pack("U*", 0xe3, 0x81, 0xAF);
161 { use bytes; $ol = bytes::length($d) }
162 ok ($ol > 3, "check encoding on EBCDIC");
163 %u = ($d => "downgrade");
164 for (keys %u) {
165     ok (length == 3, "check length"); 
166     is ($_, pack("U*", 0xe3, 0x81, 0xAF), "check value");
167 }
168 {
169     { use bytes; ok (bytes::length($d) == $ol) }
170 }
171
172 {
173     my %u;
174     my $u0 = pack("U0U", 0x00FF);
175     my $b0 = "\xC3\xBF";          # 0xCB 0xBF is U+00FF in UTF-8
176     my $u1 = pack("U0U", 0x0100);
177     my $b1 = "\xC4\x80";          # 0xC4 0x80 is U+0100 in UTF-8
178
179     $u{$u0} = 1;
180     $u{$b0} = 2; 
181     $u{$u1} = 3;
182     $u{$b1} = 4;
183
184     is(scalar keys %u, 4, "four different Unicode keys"); 
185     is($u{$u0}, 1, "U+00FF        -> 1");
186     is($u{$b0}, 2, "U+00C3 U+00BF -> 2");
187     is($u{$u1}, 3, "U+0100        -> 3 ");
188     is($u{$b1}, 4, "U+00C4 U+0080 -> 4");
189 }