This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix errors in the diagnostic output of t/op/cmp.t
[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
28e5dec8
JH
33# Seems one needs to perform the maths on 'Inf' to get the NV correctly primed.
34@FOO = ('s', 'N/A', 'a', 'NaN', -1, undef, 0, 1, 3.14, 1e37, 0.632120558, -.5,
35 'Inf'+1, '-Inf'-1, 0x0, 0x1, 0x5, 0xFFFFFFFF, $uv_max, $uv_maxm1,
36 $uv_big, $uv_bigi, $iv0, $iv1, $ivm1, $iv_min, $iv_max, $iv_big,
e61d22ef 37 $iv_small, \$array[0], \$array[0], \$array[1], \$^X);
44a8e56a 38
e61d22ef 39$expect = 7 * ($#FOO+2) * ($#FOO+1);
44a8e56a
PP
40print "1..$expect\n";
41
a355d973 42sub nok ($$$$$$$$) {
2e3031cb
NC
43 my ($test, $left, $threeway, $right, $result, $i, $j, $boolean) = @_;
44 $result = defined $result ? "'$result'" : 'undef';
0663fb34 45 print "not ok $test # ($left $threeway $right) gives: $result \$i=$i \$j=$j, $boolean disagrees\n";
2e3031cb
NC
46}
47
44a8e56a
PP
48my $ok = 0;
49for my $i (0..$#FOO) {
50 for my $j ($i..$#FOO) {
51 $ok++;
28e5dec8
JH
52 # Comparison routines may convert these internally, which would change
53 # what is used to determine the comparison on later runs. Hence copy
54 my ($i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10,
e61d22ef
NC
55 $i11, $i12, $i13, $i14, $i15, $i16, $i17) =
56 ($FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
57 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
28e5dec8
JH
58 $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i]);
59 my ($j1, $j2, $j3, $j4, $j5, $j6, $j7, $j8, $j9, $j10,
e61d22ef
NC
60 $j11, $j12, $j13, $j14, $j15, $j16, $j17) =
61 ($FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
62 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
28e5dec8
JH
63 $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j]);
64 my $cmp = $i1 <=> $j1;
65 if (!defined($cmp) ? !($i2 < $j2)
66 : ($cmp == -1 && $i2 < $j2 ||
67 $cmp == 0 && !($i2 < $j2) ||
68 $cmp == 1 && !($i2 < $j2)))
69 {
70 print "ok $ok\n";
71 }
72 else {
2e3031cb 73 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<');
28e5dec8
JH
74 }
75 $ok++;
76 if (!defined($cmp) ? !($i4 == $j4)
77 : ($cmp == -1 && !($i4 == $j4) ||
78 $cmp == 0 && $i4 == $j4 ||
79 $cmp == 1 && !($i4 == $j4)))
80 {
81 print "ok $ok\n";
82 }
83 else {
a355d973 84 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '==');
28e5dec8
JH
85 }
86 $ok++;
87 if (!defined($cmp) ? !($i5 > $j5)
88 : ($cmp == -1 && !($i5 > $j5) ||
89 $cmp == 0 && !($i5 > $j5) ||
90 $cmp == 1 && ($i5 > $j5)))
91 {
92 print "ok $ok\n";
93 }
94 else {
a355d973 95 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>');
28e5dec8
JH
96 }
97 $ok++;
98 if (!defined($cmp) ? !($i6 >= $j6)
99 : ($cmp == -1 && !($i6 >= $j6) ||
100 $cmp == 0 && $i6 >= $j6 ||
101 $cmp == 1 && $i6 >= $j6))
102 {
103 print "ok $ok\n";
104 }
105 else {
a355d973 106 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>=');
28e5dec8
JH
107 }
108 $ok++;
109 # OK, so the docs are wrong it seems. NaN != NaN
110 if (!defined($cmp) ? ($i7 != $j7)
111 : ($cmp == -1 && $i7 != $j7 ||
112 $cmp == 0 && !($i7 != $j7) ||
113 $cmp == 1 && $i7 != $j7))
114 {
115 print "ok $ok\n";
116 }
117 else {
a355d973 118 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '!=');
28e5dec8
JH
119 }
120 $ok++;
121 if (!defined($cmp) ? !($i8 <= $j8)
122 : ($cmp == -1 && $i8 <= $j8 ||
123 $cmp == 0 && $i8 <= $j8 ||
124 $cmp == 1 && !($i8 <= $j8)))
125 {
126 print "ok $ok\n";
127 }
128 else {
a355d973 129 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=');
28e5dec8
JH
130 }
131 $ok++;
e61d22ef
NC
132 my $pmc = $j16 <=> $i16; # cmp it in reverse
133 # Should give -ve of other answer, or undef for NaNs
134 # a + -a should be zero. not zero is truth. which avoids using ==
135 if (defined($cmp) ? !($cmp + $pmc) : !defined $pmc)
136 {
137 print "ok $ok\n";
138 }
139 else {
140 nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=> transposed');
141 }
142
143
144 # String comparisons
145 $ok++;
28e5dec8
JH
146 $cmp = $i9 cmp $j9;
147 if ($cmp == -1 && $i10 lt $j10 ||
148 $cmp == 0 && !($i10 lt $j10) ||
149 $cmp == 1 && !($i10 lt $j10))
150 {
151 print "ok $ok\n";
152 }
153 else {
2e3031cb 154 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'lt');
28e5dec8
JH
155 }
156 $ok++;
157 if ($cmp == -1 && !($i11 eq $j11) ||
158 $cmp == 0 && ($i11 eq $j11) ||
159 $cmp == 1 && !($i11 eq $j11))
160 {
161 print "ok $ok\n";
162 }
163 else {
2e3031cb 164 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'eq');
28e5dec8
JH
165 }
166 $ok++;
167 if ($cmp == -1 && !($i12 gt $j12) ||
168 $cmp == 0 && !($i12 gt $j12) ||
169 $cmp == 1 && ($i12 gt $j12))
170 {
171 print "ok $ok\n";
172 }
173 else {
2e3031cb 174 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'gt');
28e5dec8
JH
175 }
176 $ok++;
177 if ($cmp == -1 && $i13 le $j13 ||
178 $cmp == 0 && ($i13 le $j13) ||
179 $cmp == 1 && !($i13 le $j13))
180 {
181 print "ok $ok\n";
182 }
183 else {
2e3031cb 184 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'le');
28e5dec8
JH
185 }
186 $ok++;
187 if ($cmp == -1 && ($i14 ne $j14) ||
188 $cmp == 0 && !($i14 ne $j14) ||
189 $cmp == 1 && ($i14 ne $j14))
44a8e56a
PP
190 {
191 print "ok $ok\n";
192 }
193 else {
2e3031cb 194 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ne');
44a8e56a
PP
195 }
196 $ok++;
28e5dec8
JH
197 if ($cmp == -1 && !($i15 ge $j15) ||
198 $cmp == 0 && ($i15 ge $j15) ||
199 $cmp == 1 && ($i15 ge $j15))
44a8e56a
PP
200 {
201 print "ok $ok\n";
202 }
203 else {
2e3031cb 204 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ge');
44a8e56a 205 }
e61d22ef
NC
206 $ok++;
207 $pmc = $j17 cmp $i17; # cmp it in reverse
208 # Should give -ve of other answer
209 # a + -a should be zero. not zero is truth. which avoids using ==
210 if (!($cmp + $pmc))
211 {
212 print "ok $ok\n";
213 }
214 else {
0663fb34 215 nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'cmp transposed');
e61d22ef 216 }
44a8e56a
PP
217 }
218}