This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
skip some tests under -Uusedl
[perl5.git] / t / op / cmp.t
CommitLineData
44a8e56a
PP
1#!./perl
2
28e5dec8
JH
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8# 2s complement assumption. Won't break test, just makes the internals of
9# the SVs less interesting if were not on 2s complement system.
10my $uv_max = ~0;
11my $uv_maxm1 = ~0 ^ 1;
12my $uv_big = $uv_max;
13$uv_big = ($uv_big - 20000) | 1;
14my ($iv0, $iv1, $ivm1, $iv_min, $iv_max, $iv_big, $iv_small);
15$iv_max = $uv_max; # Do copy, *then* divide
16$iv_max /= 2;
17$iv_min = $iv_max;
18{
19 use integer;
20 $iv0 = 2 - 2;
21 $iv1 = 3 - 2;
22 $ivm1 = 2 - 3;
23 $iv_max -= 1;
24 $iv_min += 0;
25 $iv_big = $iv_max - 3;
26 $iv_small = $iv_min + 2;
27}
28my $uv_bigi = $iv_big;
29$uv_bigi |= 0x0;
30
e61d22ef
NC
31my @array = qw(perl rules);
32
08a6f934
NC
33my @raw, @upgraded, @utf8;
34foreach ("\x{1F4A9}", chr(163), 'N') {
35 push @raw, $_;
36 my $temp = $_ . chr 256;
37 chop $temp;
38 push @upgraded, $temp;
39 my $utf8 = $_;
40 next if utf8::upgrade($utf8) == length $_;
41 utf8::encode($utf8);
42 push @utf8, $utf8;
43}
44
28e5dec8
JH
45# Seems one needs to perform the maths on 'Inf' to get the NV correctly primed.
46@FOO = ('s', 'N/A', 'a', 'NaN', -1, undef, 0, 1, 3.14, 1e37, 0.632120558, -.5,
47 'Inf'+1, '-Inf'-1, 0x0, 0x1, 0x5, 0xFFFFFFFF, $uv_max, $uv_maxm1,
48 $uv_big, $uv_bigi, $iv0, $iv1, $ivm1, $iv_min, $iv_max, $iv_big,
08a6f934
NC
49 $iv_small, \$array[0], \$array[0], \$array[1], \$^X, @raw, @upgraded,
50 @utf8);
44a8e56a 51
08a6f934 52$expect = 7 * ($#FOO+2) * ($#FOO+1) + 6 * @raw + 6 * @utf8;
44a8e56a
PP
53print "1..$expect\n";
54
a355d973 55sub nok ($$$$$$$$) {
2e3031cb
NC
56 my ($test, $left, $threeway, $right, $result, $i, $j, $boolean) = @_;
57 $result = defined $result ? "'$result'" : 'undef';
0663fb34 58 print "not ok $test # ($left $threeway $right) gives: $result \$i=$i \$j=$j, $boolean disagrees\n";
2e3031cb
NC
59}
60
44a8e56a
PP
61my $ok = 0;
62for my $i (0..$#FOO) {
63 for my $j ($i..$#FOO) {
64 $ok++;
28e5dec8
JH
65 # Comparison routines may convert these internally, which would change
66 # what is used to determine the comparison on later runs. Hence copy
67 my ($i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10,
e61d22ef
NC
68 $i11, $i12, $i13, $i14, $i15, $i16, $i17) =
69 ($FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
70 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
28e5dec8
JH
71 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i]);
72 my ($j1, $j2, $j3, $j4, $j5, $j6, $j7, $j8, $j9, $j10,
e61d22ef
NC
73 $j11, $j12, $j13, $j14, $j15, $j16, $j17) =
74 ($FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
75 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
28e5dec8
JH
76 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j]);
77 my $cmp = $i1 <=> $j1;
78 if (!defined($cmp) ? !($i2 < $j2)
79 : ($cmp == -1 && $i2 < $j2 ||
80 $cmp == 0 && !($i2 < $j2) ||
81 $cmp == 1 && !($i2 < $j2)))
82 {
83 print "ok $ok\n";
84 }
85 else {
2e3031cb 86 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<');
28e5dec8
JH
87 }
88 $ok++;
89 if (!defined($cmp) ? !($i4 == $j4)
90 : ($cmp == -1 && !($i4 == $j4) ||
91 $cmp == 0 && $i4 == $j4 ||
92 $cmp == 1 && !($i4 == $j4)))
93 {
94 print "ok $ok\n";
95 }
96 else {
a355d973 97 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '==');
28e5dec8
JH
98 }
99 $ok++;
100 if (!defined($cmp) ? !($i5 > $j5)
101 : ($cmp == -1 && !($i5 > $j5) ||
102 $cmp == 0 && !($i5 > $j5) ||
103 $cmp == 1 && ($i5 > $j5)))
104 {
105 print "ok $ok\n";
106 }
107 else {
a355d973 108 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>');
28e5dec8
JH
109 }
110 $ok++;
111 if (!defined($cmp) ? !($i6 >= $j6)
112 : ($cmp == -1 && !($i6 >= $j6) ||
113 $cmp == 0 && $i6 >= $j6 ||
114 $cmp == 1 && $i6 >= $j6))
115 {
116 print "ok $ok\n";
117 }
118 else {
a355d973 119 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>=');
28e5dec8
JH
120 }
121 $ok++;
122 # OK, so the docs are wrong it seems. NaN != NaN
123 if (!defined($cmp) ? ($i7 != $j7)
124 : ($cmp == -1 && $i7 != $j7 ||
125 $cmp == 0 && !($i7 != $j7) ||
126 $cmp == 1 && $i7 != $j7))
127 {
128 print "ok $ok\n";
129 }
130 else {
a355d973 131 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '!=');
28e5dec8
JH
132 }
133 $ok++;
134 if (!defined($cmp) ? !($i8 <= $j8)
135 : ($cmp == -1 && $i8 <= $j8 ||
136 $cmp == 0 && $i8 <= $j8 ||
137 $cmp == 1 && !($i8 <= $j8)))
138 {
139 print "ok $ok\n";
140 }
141 else {
a355d973 142 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=');
28e5dec8
JH
143 }
144 $ok++;
e61d22ef
NC
145 my $pmc = $j16 <=> $i16; # cmp it in reverse
146 # Should give -ve of other answer, or undef for NaNs
147 # a + -a should be zero. not zero is truth. which avoids using ==
148 if (defined($cmp) ? !($cmp + $pmc) : !defined $pmc)
149 {
150 print "ok $ok\n";
151 }
152 else {
153 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=> transposed');
154 }
155
156
157 # String comparisons
158 $ok++;
28e5dec8
JH
159 $cmp = $i9 cmp $j9;
160 if ($cmp == -1 && $i10 lt $j10 ||
161 $cmp == 0 && !($i10 lt $j10) ||
162 $cmp == 1 && !($i10 lt $j10))
163 {
164 print "ok $ok\n";
165 }
166 else {
2e3031cb 167 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'lt');
28e5dec8
JH
168 }
169 $ok++;
170 if ($cmp == -1 && !($i11 eq $j11) ||
171 $cmp == 0 && ($i11 eq $j11) ||
172 $cmp == 1 && !($i11 eq $j11))
173 {
174 print "ok $ok\n";
175 }
176 else {
2e3031cb 177 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'eq');
28e5dec8
JH
178 }
179 $ok++;
180 if ($cmp == -1 && !($i12 gt $j12) ||
181 $cmp == 0 && !($i12 gt $j12) ||
182 $cmp == 1 && ($i12 gt $j12))
183 {
184 print "ok $ok\n";
185 }
186 else {
2e3031cb 187 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'gt');
28e5dec8
JH
188 }
189 $ok++;
190 if ($cmp == -1 && $i13 le $j13 ||
191 $cmp == 0 && ($i13 le $j13) ||
192 $cmp == 1 && !($i13 le $j13))
193 {
194 print "ok $ok\n";
195 }
196 else {
2e3031cb 197 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'le');
28e5dec8
JH
198 }
199 $ok++;
200 if ($cmp == -1 && ($i14 ne $j14) ||
201 $cmp == 0 && !($i14 ne $j14) ||
202 $cmp == 1 && ($i14 ne $j14))
44a8e56a
PP
203 {
204 print "ok $ok\n";
205 }
206 else {
2e3031cb 207 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ne');
44a8e56a
PP
208 }
209 $ok++;
28e5dec8
JH
210 if ($cmp == -1 && !($i15 ge $j15) ||
211 $cmp == 0 && ($i15 ge $j15) ||
212 $cmp == 1 && ($i15 ge $j15))
44a8e56a
PP
213 {
214 print "ok $ok\n";
215 }
216 else {
2e3031cb 217 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ge');
44a8e56a 218 }
e61d22ef
NC
219 $ok++;
220 $pmc = $j17 cmp $i17; # cmp it in reverse
221 # Should give -ve of other answer
222 # a + -a should be zero. not zero is truth. which avoids using ==
223 if (!($cmp + $pmc))
224 {
225 print "ok $ok\n";
226 }
227 else {
0663fb34 228 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'cmp transposed');
e61d22ef 229 }
44a8e56a
PP
230 }
231}
08a6f934
NC
232
233# We know the answers for these. We can rely on the consistency checks above
234# to test the other string comparisons.
235
236while (my ($i, $v) = each @raw) {
237 # Copy, to avoid any inadvertent conversion
238 my ($raw, $cooked, $not);
239 $raw = $v;
240 $cooked = $upgraded[$i];
241 $not = $raw eq $cooked ? '' : 'not ';
242 printf "%sok %d # eq, chr %d\n", $not, ++$ok, ord $raw;
243
244 $raw = $v;
245 $cooked = $upgraded[$i];
246 $not = $raw ne $cooked ? 'not ' : '';
247 printf "%sok %d # ne, chr %d\n", $not, ++$ok, ord $raw;
248
249 $raw = $v;
250 $cooked = $upgraded[$i];
251 $not = (($raw cmp $cooked) == 0) ? '' : 'not ';
252 printf "%sok %d # cmp, chr %d\n", $not, ++$ok, ord $raw;
253
254 # And now, transposed.
255 $raw = $v;
256 $cooked = $upgraded[$i];
257 $not = $cooked eq $raw ? '' : 'not ';
258 printf "%sok %d # eq, chr %d\n", $not, ++$ok, ord $raw;
259
260 $raw = $v;
261 $cooked = $upgraded[$i];
262 $not = $cooked ne $raw ? 'not ' : '';
263 printf "%sok %d # ne, chr %d\n", $not, ++$ok, ord $raw;
264
265 $raw = $v;
266 $cooked = $upgraded[$i];
267 $not = (($cooked cmp $raw) == 0) ? '' : 'not ';
268 printf "%sok %d # cmp, chr %d\n", $not, ++$ok, ord $raw;
269}
270
271while (my ($i, $v) = each @utf8) {
272 # Copy, to avoid any inadvertent conversion
273 my ($raw, $cooked, $not);
274 $raw = $raw[$i];
275 $cooked = $v;
276 $not = $raw eq $cooked ? 'not ' : '';
277 printf "%sok %d # eq vs octets, chr %d\n", $not, ++$ok, ord $raw;
278
279 $raw = $raw[$i];
280 $cooked = $v;
281 $not = $raw ne $cooked ? '' : 'not ';
282 printf "%sok %d # ne vs octets, chr %d\n", $not, ++$ok, ord $raw;
283
284 $raw = $raw[$i];
285 $cooked = $v;
286 $not = (($raw cmp $cooked) == 0) ? 'not ' : '';
287 printf "%sok %d # cmp vs octects, chr %d\n", $not, ++$ok, ord $raw;
288
289 # And now, transposed.
290 $raw = $raw[$i];
291 $cooked = $v;
292 $not = $cooked eq $raw ? 'not ' : '';
293 printf "%sok %d # eq vs octets, chr %d\n", $not, ++$ok, ord $raw;
294
295 $raw = $raw[$i];
296 $cooked = $v;
297 $not = $cooked ne $raw? '' : 'not ';
298 printf "%sok %d # ne vs octets, chr %d\n", $not, ++$ok, ord $raw;
299
300 $raw = $raw[$i];
301 $cooked = $v;
302 $not = (($cooked cmp $raw) == 0) ? 'not ' : '';
303 printf "%sok %d # cmp vs octects, chr %d\n", $not, ++$ok, ord $raw;
304}