This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
test in change#4428 needs strict interpretation of C modulus
[perl5.git] / t / op / each.t
1 #!./perl
2
3 print "1..19\n";
4
5 $h{'abc'} = 'ABC';
6 $h{'def'} = 'DEF';
7 $h{'jkl','mno'} = "JKL\034MNO";
8 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
9 $h{'a'} = 'A';
10 $h{'b'} = 'B';
11 $h{'c'} = 'C';
12 $h{'d'} = 'D';
13 $h{'e'} = 'E';
14 $h{'f'} = 'F';
15 $h{'g'} = 'G';
16 $h{'h'} = 'H';
17 $h{'i'} = 'I';
18 $h{'j'} = 'J';
19 $h{'k'} = 'K';
20 $h{'l'} = 'L';
21 $h{'m'} = 'M';
22 $h{'n'} = 'N';
23 $h{'o'} = 'O';
24 $h{'p'} = 'P';
25 $h{'q'} = 'Q';
26 $h{'r'} = 'R';
27 $h{'s'} = 'S';
28 $h{'t'} = 'T';
29 $h{'u'} = 'U';
30 $h{'v'} = 'V';
31 $h{'w'} = 'W';
32 $h{'x'} = 'X';
33 $h{'y'} = 'Y';
34 $h{'z'} = 'Z';
35
36 @keys = keys %h;
37 @values = values %h;
38
39 if ($#keys == 29 && $#values == 29) {print "ok 1\n";} else {print "not ok 1\n";}
40
41 $i = 0;         # stop -w complaints
42
43 while (($key,$value) = each(%h)) {
44     if ($key eq $keys[$i] && $value eq $values[$i]
45         && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
46         $key =~ y/a-z/A-Z/;
47         $i++ if $key eq $value;
48     }
49 }
50
51 if ($i == 30) {print "ok 2\n";} else {print "not ok 2\n";}
52
53 @keys = ('blurfl', keys(%h), 'dyick');
54 if ($#keys == 31) {print "ok 3\n";} else {print "not ok 3\n";}
55
56 $size = ((split('/',scalar %h))[1]);
57 keys %h = $size * 5;
58 $newsize = ((split('/',scalar %h))[1]);
59 if ($newsize == $size * 8) {print "ok 4\n";} else {print "not ok 4\n";}
60 keys %h = 1;
61 $size = ((split('/',scalar %h))[1]);
62 if ($size == $newsize) {print "ok 5\n";} else {print "not ok 5\n";}
63 %h = (1,1);
64 $size = ((split('/',scalar %h))[1]);
65 if ($size == $newsize) {print "ok 6\n";} else {print "not ok 6\n";}
66 undef %h;
67 %h = (1,1);
68 $size = ((split('/',scalar %h))[1]);
69 if ($size == 8) {print "ok 7\n";} else {print "not ok 7\n";}
70
71 # test scalar each
72 %hash = 1..20;
73 $total = 0;
74 $total += $key while $key = each %hash;
75 print "# Scalar each is bad.\nnot " unless $total == 100;
76 print "ok 8\n";
77
78 for (1..3) { @foo = each %hash }
79 keys %hash;
80 $total = 0;
81 $total += $key while $key = each %hash;
82 print "# Scalar keys isn't resetting the iterator.\nnot " if $total != 100;
83 print "ok 9\n";
84
85 for (1..3) { @foo = each %hash }
86 $total = 0;
87 $total += $key while $key = each %hash;
88 print "# Iterator of each isn't being maintained.\nnot " if $total == 100;
89 print "ok 10\n";
90
91 for (1..3) { @foo = each %hash }
92 values %hash;
93 $total = 0;
94 $total += $key while $key = each %hash;
95 print "# Scalar values isn't resetting the iterator.\nnot " if $total != 100;
96 print "ok 11\n";
97
98 $size = (split('/', scalar %hash))[1];
99 keys(%hash) = $size / 2;
100 print "not " if $size != (split('/', scalar %hash))[1];
101 print "ok 12\n";
102 keys(%hash) = $size + 100;
103 print "not " if $size == (split('/', scalar %hash))[1];
104 print "ok 13\n";
105
106 print "not " if keys(%hash) != 10;
107 print "ok 14\n";
108
109 print keys(hash) != 10 ? "not ok 15\n" : "ok 15\n";
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 if ($i == 5) { print "ok 16\n" } else { print "not ok\n" }
121
122 {
123     package Obj;
124     sub DESTROY { print "ok 18\n"; }
125     {
126         my $h = { A => bless [], __PACKAGE__ };
127         while (my($k,$v) = each %$h) {
128             print "ok 17\n" if $k eq 'A' and ref($v) eq 'Obj';
129         }
130     }
131     print "ok 19\n";
132 }
133