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