This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Create subdirectory t/opbasic. Move 5 test files there.
[perl5.git] / t / opbasic / 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
e76f5e87
DM
55my $bad_NaN = 0;
56
57{
58 # gcc's -ffast-math option may stop NaNs working correctly
59 use Config;
60 my $ccflags = $Config{ccflags} // '';
61 $bad_NaN = 1 if $ccflags =~ /-ffast-math\b/;
62}
63
a355d973 64sub nok ($$$$$$$$) {
2e3031cb
NC
65 my ($test, $left, $threeway, $right, $result, $i, $j, $boolean) = @_;
66 $result = defined $result ? "'$result'" : 'undef';
e76f5e87
DM
67 if ($bad_NaN && ($left eq 'NaN' || $right eq 'NaN')) {
68 print "ok $test # skipping failed NaN test under -ffast-math\n";
69 }
70 else {
71 print "not ok $test # ($left $threeway $right) gives: $result \$i=$i \$j=$j, $boolean disagrees\n";
72 }
2e3031cb
NC
73}
74
44a8e56a
PP
75my $ok = 0;
76for my $i (0..$#FOO) {
77 for my $j ($i..$#FOO) {
78 $ok++;
28e5dec8
JH
79 # Comparison routines may convert these internally, which would change
80 # what is used to determine the comparison on later runs. Hence copy
81 my ($i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10,
e61d22ef
NC
82 $i11, $i12, $i13, $i14, $i15, $i16, $i17) =
83 ($FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
84 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
28e5dec8
JH
85 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i]);
86 my ($j1, $j2, $j3, $j4, $j5, $j6, $j7, $j8, $j9, $j10,
e61d22ef
NC
87 $j11, $j12, $j13, $j14, $j15, $j16, $j17) =
88 ($FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
89 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
28e5dec8
JH
90 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j]);
91 my $cmp = $i1 <=> $j1;
92 if (!defined($cmp) ? !($i2 < $j2)
93 : ($cmp == -1 && $i2 < $j2 ||
94 $cmp == 0 && !($i2 < $j2) ||
95 $cmp == 1 && !($i2 < $j2)))
96 {
97 print "ok $ok\n";
98 }
99 else {
2e3031cb 100 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<');
28e5dec8
JH
101 }
102 $ok++;
103 if (!defined($cmp) ? !($i4 == $j4)
104 : ($cmp == -1 && !($i4 == $j4) ||
105 $cmp == 0 && $i4 == $j4 ||
106 $cmp == 1 && !($i4 == $j4)))
107 {
108 print "ok $ok\n";
109 }
110 else {
a355d973 111 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '==');
28e5dec8
JH
112 }
113 $ok++;
114 if (!defined($cmp) ? !($i5 > $j5)
115 : ($cmp == -1 && !($i5 > $j5) ||
116 $cmp == 0 && !($i5 > $j5) ||
117 $cmp == 1 && ($i5 > $j5)))
118 {
119 print "ok $ok\n";
120 }
121 else {
a355d973 122 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>');
28e5dec8
JH
123 }
124 $ok++;
125 if (!defined($cmp) ? !($i6 >= $j6)
126 : ($cmp == -1 && !($i6 >= $j6) ||
127 $cmp == 0 && $i6 >= $j6 ||
128 $cmp == 1 && $i6 >= $j6))
129 {
130 print "ok $ok\n";
131 }
132 else {
a355d973 133 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>=');
28e5dec8
JH
134 }
135 $ok++;
136 # OK, so the docs are wrong it seems. NaN != NaN
137 if (!defined($cmp) ? ($i7 != $j7)
138 : ($cmp == -1 && $i7 != $j7 ||
139 $cmp == 0 && !($i7 != $j7) ||
140 $cmp == 1 && $i7 != $j7))
141 {
142 print "ok $ok\n";
143 }
144 else {
a355d973 145 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '!=');
28e5dec8
JH
146 }
147 $ok++;
148 if (!defined($cmp) ? !($i8 <= $j8)
149 : ($cmp == -1 && $i8 <= $j8 ||
150 $cmp == 0 && $i8 <= $j8 ||
151 $cmp == 1 && !($i8 <= $j8)))
152 {
153 print "ok $ok\n";
154 }
155 else {
a355d973 156 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=');
28e5dec8
JH
157 }
158 $ok++;
e61d22ef
NC
159 my $pmc = $j16 <=> $i16; # cmp it in reverse
160 # Should give -ve of other answer, or undef for NaNs
161 # a + -a should be zero. not zero is truth. which avoids using ==
162 if (defined($cmp) ? !($cmp + $pmc) : !defined $pmc)
163 {
164 print "ok $ok\n";
165 }
166 else {
167 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=> transposed');
168 }
169
170
171 # String comparisons
172 $ok++;
28e5dec8
JH
173 $cmp = $i9 cmp $j9;
174 if ($cmp == -1 && $i10 lt $j10 ||
175 $cmp == 0 && !($i10 lt $j10) ||
176 $cmp == 1 && !($i10 lt $j10))
177 {
178 print "ok $ok\n";
179 }
180 else {
2e3031cb 181 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'lt');
28e5dec8
JH
182 }
183 $ok++;
184 if ($cmp == -1 && !($i11 eq $j11) ||
185 $cmp == 0 && ($i11 eq $j11) ||
186 $cmp == 1 && !($i11 eq $j11))
187 {
188 print "ok $ok\n";
189 }
190 else {
2e3031cb 191 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'eq');
28e5dec8
JH
192 }
193 $ok++;
194 if ($cmp == -1 && !($i12 gt $j12) ||
195 $cmp == 0 && !($i12 gt $j12) ||
196 $cmp == 1 && ($i12 gt $j12))
197 {
198 print "ok $ok\n";
199 }
200 else {
2e3031cb 201 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'gt');
28e5dec8
JH
202 }
203 $ok++;
204 if ($cmp == -1 && $i13 le $j13 ||
205 $cmp == 0 && ($i13 le $j13) ||
206 $cmp == 1 && !($i13 le $j13))
207 {
208 print "ok $ok\n";
209 }
210 else {
2e3031cb 211 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'le');
28e5dec8
JH
212 }
213 $ok++;
214 if ($cmp == -1 && ($i14 ne $j14) ||
215 $cmp == 0 && !($i14 ne $j14) ||
216 $cmp == 1 && ($i14 ne $j14))
44a8e56a
PP
217 {
218 print "ok $ok\n";
219 }
220 else {
2e3031cb 221 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ne');
44a8e56a
PP
222 }
223 $ok++;
28e5dec8
JH
224 if ($cmp == -1 && !($i15 ge $j15) ||
225 $cmp == 0 && ($i15 ge $j15) ||
226 $cmp == 1 && ($i15 ge $j15))
44a8e56a
PP
227 {
228 print "ok $ok\n";
229 }
230 else {
2e3031cb 231 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ge');
44a8e56a 232 }
e61d22ef
NC
233 $ok++;
234 $pmc = $j17 cmp $i17; # cmp it in reverse
235 # Should give -ve of other answer
236 # a + -a should be zero. not zero is truth. which avoids using ==
237 if (!($cmp + $pmc))
238 {
239 print "ok $ok\n";
240 }
241 else {
0663fb34 242 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'cmp transposed');
e61d22ef 243 }
44a8e56a
PP
244 }
245}
08a6f934
NC
246
247# We know the answers for these. We can rely on the consistency checks above
248# to test the other string comparisons.
249
250while (my ($i, $v) = each @raw) {
251 # Copy, to avoid any inadvertent conversion
252 my ($raw, $cooked, $not);
253 $raw = $v;
254 $cooked = $upgraded[$i];
255 $not = $raw eq $cooked ? '' : 'not ';
256 printf "%sok %d # eq, chr %d\n", $not, ++$ok, ord $raw;
257
258 $raw = $v;
259 $cooked = $upgraded[$i];
260 $not = $raw ne $cooked ? 'not ' : '';
261 printf "%sok %d # ne, chr %d\n", $not, ++$ok, ord $raw;
262
263 $raw = $v;
264 $cooked = $upgraded[$i];
265 $not = (($raw cmp $cooked) == 0) ? '' : 'not ';
266 printf "%sok %d # cmp, chr %d\n", $not, ++$ok, ord $raw;
267
268 # And now, transposed.
269 $raw = $v;
270 $cooked = $upgraded[$i];
271 $not = $cooked eq $raw ? '' : 'not ';
272 printf "%sok %d # eq, chr %d\n", $not, ++$ok, ord $raw;
273
274 $raw = $v;
275 $cooked = $upgraded[$i];
276 $not = $cooked ne $raw ? 'not ' : '';
277 printf "%sok %d # ne, chr %d\n", $not, ++$ok, ord $raw;
278
279 $raw = $v;
280 $cooked = $upgraded[$i];
281 $not = (($cooked cmp $raw) == 0) ? '' : 'not ';
282 printf "%sok %d # cmp, chr %d\n", $not, ++$ok, ord $raw;
283}
284
285while (my ($i, $v) = each @utf8) {
286 # Copy, to avoid any inadvertent conversion
287 my ($raw, $cooked, $not);
288 $raw = $raw[$i];
289 $cooked = $v;
290 $not = $raw eq $cooked ? 'not ' : '';
291 printf "%sok %d # eq vs octets, chr %d\n", $not, ++$ok, ord $raw;
292
293 $raw = $raw[$i];
294 $cooked = $v;
295 $not = $raw ne $cooked ? '' : 'not ';
296 printf "%sok %d # ne vs octets, chr %d\n", $not, ++$ok, ord $raw;
297
298 $raw = $raw[$i];
299 $cooked = $v;
300 $not = (($raw cmp $cooked) == 0) ? 'not ' : '';
301 printf "%sok %d # cmp vs octects, chr %d\n", $not, ++$ok, ord $raw;
302
303 # And now, transposed.
304 $raw = $raw[$i];
305 $cooked = $v;
306 $not = $cooked eq $raw ? 'not ' : '';
307 printf "%sok %d # eq vs octets, chr %d\n", $not, ++$ok, ord $raw;
308
309 $raw = $raw[$i];
310 $cooked = $v;
311 $not = $cooked ne $raw? '' : 'not ';
312 printf "%sok %d # ne vs octets, chr %d\n", $not, ++$ok, ord $raw;
313
314 $raw = $raw[$i];
315 $cooked = $v;
316 $not = (($cooked cmp $raw) == 0) ? 'not ' : '';
317 printf "%sok %d # cmp vs octects, chr %d\n", $not, ++$ok, ord $raw;
318}