xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/cmp.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate       chdir 't' if -d 't';
5*0Sstevel@tonic-gate       @INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate# 2s complement assumption. Won't break test, just makes the internals of
9*0Sstevel@tonic-gate# the SVs less interesting if were not on 2s complement system.
10*0Sstevel@tonic-gatemy $uv_max = ~0;
11*0Sstevel@tonic-gatemy $uv_maxm1 = ~0 ^ 1;
12*0Sstevel@tonic-gatemy $uv_big = $uv_max;
13*0Sstevel@tonic-gate$uv_big = ($uv_big - 20000) | 1;
14*0Sstevel@tonic-gatemy ($iv0, $iv1, $ivm1, $iv_min, $iv_max, $iv_big, $iv_small);
15*0Sstevel@tonic-gate$iv_max = $uv_max; # Do copy, *then* divide
16*0Sstevel@tonic-gate$iv_max /= 2;
17*0Sstevel@tonic-gate$iv_min = $iv_max;
18*0Sstevel@tonic-gate{
19*0Sstevel@tonic-gate  use integer;
20*0Sstevel@tonic-gate  $iv0 = 2 - 2;
21*0Sstevel@tonic-gate  $iv1 = 3 - 2;
22*0Sstevel@tonic-gate  $ivm1 = 2 - 3;
23*0Sstevel@tonic-gate  $iv_max -= 1;
24*0Sstevel@tonic-gate  $iv_min += 0;
25*0Sstevel@tonic-gate  $iv_big = $iv_max - 3;
26*0Sstevel@tonic-gate  $iv_small = $iv_min + 2;
27*0Sstevel@tonic-gate}
28*0Sstevel@tonic-gatemy $uv_bigi = $iv_big;
29*0Sstevel@tonic-gate$uv_bigi |= 0x0;
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gatemy @array = qw(perl rules);
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate# Seems one needs to perform the maths on 'Inf' to get the NV correctly primed.
34*0Sstevel@tonic-gate@FOO = ('s', 'N/A', 'a', 'NaN', -1, undef, 0, 1, 3.14, 1e37, 0.632120558, -.5,
35*0Sstevel@tonic-gate	'Inf'+1, '-Inf'-1, 0x0, 0x1, 0x5, 0xFFFFFFFF, $uv_max, $uv_maxm1,
36*0Sstevel@tonic-gate	$uv_big, $uv_bigi, $iv0, $iv1, $ivm1, $iv_min, $iv_max, $iv_big,
37*0Sstevel@tonic-gate	$iv_small, \$array[0], \$array[0], \$array[1], \$^X);
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate$expect = 7 * ($#FOO+2) * ($#FOO+1);
40*0Sstevel@tonic-gateprint "1..$expect\n";
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gatesub nok ($$$$$$$$) {
43*0Sstevel@tonic-gate  my ($test, $left, $threeway, $right, $result, $i, $j, $boolean) = @_;
44*0Sstevel@tonic-gate  $result = defined $result ? "'$result'" : 'undef';
45*0Sstevel@tonic-gate  print "not ok $test # ($left <=> $right) gives: $result \$i=$i \$j=$j, $boolean disagrees\n";
46*0Sstevel@tonic-gate}
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gatemy $ok = 0;
49*0Sstevel@tonic-gatefor my $i (0..$#FOO) {
50*0Sstevel@tonic-gate    for my $j ($i..$#FOO) {
51*0Sstevel@tonic-gate	$ok++;
52*0Sstevel@tonic-gate	# Comparison routines may convert these internally, which would change
53*0Sstevel@tonic-gate	# what is used to determine the comparison on later runs. Hence copy
54*0Sstevel@tonic-gate	my ($i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10,
55*0Sstevel@tonic-gate	    $i11, $i12, $i13, $i14, $i15, $i16, $i17) =
56*0Sstevel@tonic-gate	  ($FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
57*0Sstevel@tonic-gate	   $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i],
58*0Sstevel@tonic-gate	   $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i], $FOO[$i]);
59*0Sstevel@tonic-gate	my ($j1, $j2, $j3, $j4, $j5, $j6, $j7, $j8, $j9, $j10,
60*0Sstevel@tonic-gate	    $j11, $j12, $j13, $j14, $j15, $j16, $j17) =
61*0Sstevel@tonic-gate	  ($FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
62*0Sstevel@tonic-gate	   $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j],
63*0Sstevel@tonic-gate	   $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j], $FOO[$j]);
64*0Sstevel@tonic-gate	my $cmp = $i1 <=> $j1;
65*0Sstevel@tonic-gate	if (!defined($cmp) ? !($i2 < $j2)
66*0Sstevel@tonic-gate	    : ($cmp == -1 && $i2 < $j2 ||
67*0Sstevel@tonic-gate	       $cmp == 0  && !($i2 < $j2) ||
68*0Sstevel@tonic-gate	       $cmp == 1  && !($i2 < $j2)))
69*0Sstevel@tonic-gate	{
70*0Sstevel@tonic-gate	    print "ok $ok\n";
71*0Sstevel@tonic-gate	}
72*0Sstevel@tonic-gate	else {
73*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<');
74*0Sstevel@tonic-gate	}
75*0Sstevel@tonic-gate	$ok++;
76*0Sstevel@tonic-gate	if (!defined($cmp) ? !($i4 == $j4)
77*0Sstevel@tonic-gate	    : ($cmp == -1 && !($i4 == $j4) ||
78*0Sstevel@tonic-gate	       $cmp == 0  && $i4 == $j4 ||
79*0Sstevel@tonic-gate	       $cmp == 1  && !($i4 == $j4)))
80*0Sstevel@tonic-gate	{
81*0Sstevel@tonic-gate	    print "ok $ok\n";
82*0Sstevel@tonic-gate	}
83*0Sstevel@tonic-gate	else {
84*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '==');
85*0Sstevel@tonic-gate	}
86*0Sstevel@tonic-gate	$ok++;
87*0Sstevel@tonic-gate	if (!defined($cmp) ? !($i5 > $j5)
88*0Sstevel@tonic-gate	    : ($cmp == -1 && !($i5 > $j5) ||
89*0Sstevel@tonic-gate	       $cmp == 0  && !($i5 > $j5) ||
90*0Sstevel@tonic-gate	       $cmp == 1  && ($i5 > $j5)))
91*0Sstevel@tonic-gate	{
92*0Sstevel@tonic-gate	    print "ok $ok\n";
93*0Sstevel@tonic-gate	}
94*0Sstevel@tonic-gate	else {
95*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>');
96*0Sstevel@tonic-gate	}
97*0Sstevel@tonic-gate	$ok++;
98*0Sstevel@tonic-gate	if (!defined($cmp) ? !($i6 >= $j6)
99*0Sstevel@tonic-gate	    : ($cmp == -1 && !($i6 >= $j6) ||
100*0Sstevel@tonic-gate	       $cmp == 0  && $i6 >= $j6 ||
101*0Sstevel@tonic-gate	       $cmp == 1  && $i6 >= $j6))
102*0Sstevel@tonic-gate	{
103*0Sstevel@tonic-gate	    print "ok $ok\n";
104*0Sstevel@tonic-gate	}
105*0Sstevel@tonic-gate	else {
106*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '>=');
107*0Sstevel@tonic-gate	}
108*0Sstevel@tonic-gate	$ok++;
109*0Sstevel@tonic-gate	# OK, so the docs are wrong it seems. NaN != NaN
110*0Sstevel@tonic-gate	if (!defined($cmp) ? ($i7 != $j7)
111*0Sstevel@tonic-gate	    : ($cmp == -1 && $i7 != $j7 ||
112*0Sstevel@tonic-gate	       $cmp == 0  && !($i7 != $j7) ||
113*0Sstevel@tonic-gate	       $cmp == 1  && $i7 != $j7))
114*0Sstevel@tonic-gate	{
115*0Sstevel@tonic-gate	    print "ok $ok\n";
116*0Sstevel@tonic-gate	}
117*0Sstevel@tonic-gate	else {
118*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '!=');
119*0Sstevel@tonic-gate	}
120*0Sstevel@tonic-gate	$ok++;
121*0Sstevel@tonic-gate	if (!defined($cmp) ? !($i8 <= $j8)
122*0Sstevel@tonic-gate	    : ($cmp == -1 && $i8 <= $j8 ||
123*0Sstevel@tonic-gate	       $cmp == 0  && $i8 <= $j8 ||
124*0Sstevel@tonic-gate	       $cmp == 1  && !($i8 <= $j8)))
125*0Sstevel@tonic-gate	{
126*0Sstevel@tonic-gate	    print "ok $ok\n";
127*0Sstevel@tonic-gate	}
128*0Sstevel@tonic-gate	else {
129*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=');
130*0Sstevel@tonic-gate	}
131*0Sstevel@tonic-gate	$ok++;
132*0Sstevel@tonic-gate        my $pmc =  $j16 <=> $i16; # cmp it in reverse
133*0Sstevel@tonic-gate        # Should give -ve of other answer, or undef for NaNs
134*0Sstevel@tonic-gate        # a + -a should be zero. not zero is truth. which avoids using ==
135*0Sstevel@tonic-gate	if (defined($cmp) ? !($cmp + $pmc) : !defined $pmc)
136*0Sstevel@tonic-gate	{
137*0Sstevel@tonic-gate	    print "ok $ok\n";
138*0Sstevel@tonic-gate	}
139*0Sstevel@tonic-gate	else {
140*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, '<=> transposed');
141*0Sstevel@tonic-gate	}
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate	# String comparisons
145*0Sstevel@tonic-gate	$ok++;
146*0Sstevel@tonic-gate	$cmp = $i9 cmp $j9;
147*0Sstevel@tonic-gate	if ($cmp == -1 && $i10 lt $j10 ||
148*0Sstevel@tonic-gate	    $cmp == 0  && !($i10 lt $j10) ||
149*0Sstevel@tonic-gate	    $cmp == 1  && !($i10 lt $j10))
150*0Sstevel@tonic-gate	{
151*0Sstevel@tonic-gate	    print "ok $ok\n";
152*0Sstevel@tonic-gate	}
153*0Sstevel@tonic-gate	else {
154*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'lt');
155*0Sstevel@tonic-gate	}
156*0Sstevel@tonic-gate	$ok++;
157*0Sstevel@tonic-gate	if ($cmp == -1 && !($i11 eq $j11) ||
158*0Sstevel@tonic-gate	    $cmp == 0  && ($i11 eq $j11) ||
159*0Sstevel@tonic-gate	    $cmp == 1  && !($i11 eq $j11))
160*0Sstevel@tonic-gate	{
161*0Sstevel@tonic-gate	    print "ok $ok\n";
162*0Sstevel@tonic-gate	}
163*0Sstevel@tonic-gate	else {
164*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'eq');
165*0Sstevel@tonic-gate	}
166*0Sstevel@tonic-gate	$ok++;
167*0Sstevel@tonic-gate	if ($cmp == -1 && !($i12 gt $j12) ||
168*0Sstevel@tonic-gate	    $cmp == 0  && !($i12 gt $j12) ||
169*0Sstevel@tonic-gate	    $cmp == 1  && ($i12 gt $j12))
170*0Sstevel@tonic-gate	{
171*0Sstevel@tonic-gate	    print "ok $ok\n";
172*0Sstevel@tonic-gate	}
173*0Sstevel@tonic-gate	else {
174*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'gt');
175*0Sstevel@tonic-gate	}
176*0Sstevel@tonic-gate	$ok++;
177*0Sstevel@tonic-gate	if ($cmp == -1 && $i13 le $j13 ||
178*0Sstevel@tonic-gate	    $cmp == 0  && ($i13 le $j13) ||
179*0Sstevel@tonic-gate	    $cmp == 1  && !($i13 le $j13))
180*0Sstevel@tonic-gate	{
181*0Sstevel@tonic-gate	    print "ok $ok\n";
182*0Sstevel@tonic-gate	}
183*0Sstevel@tonic-gate	else {
184*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'le');
185*0Sstevel@tonic-gate	}
186*0Sstevel@tonic-gate	$ok++;
187*0Sstevel@tonic-gate	if ($cmp == -1 && ($i14 ne $j14) ||
188*0Sstevel@tonic-gate	    $cmp == 0  && !($i14 ne $j14) ||
189*0Sstevel@tonic-gate	    $cmp == 1  && ($i14 ne $j14))
190*0Sstevel@tonic-gate	{
191*0Sstevel@tonic-gate	    print "ok $ok\n";
192*0Sstevel@tonic-gate	}
193*0Sstevel@tonic-gate	else {
194*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ne');
195*0Sstevel@tonic-gate	}
196*0Sstevel@tonic-gate	$ok++;
197*0Sstevel@tonic-gate	if ($cmp == -1 && !($i15 ge $j15) ||
198*0Sstevel@tonic-gate	    $cmp == 0  && ($i15 ge $j15) ||
199*0Sstevel@tonic-gate	    $cmp == 1  && ($i15 ge $j15))
200*0Sstevel@tonic-gate	{
201*0Sstevel@tonic-gate	    print "ok $ok\n";
202*0Sstevel@tonic-gate	}
203*0Sstevel@tonic-gate	else {
204*0Sstevel@tonic-gate	    nok ($ok, $i3, 'cmp', $j3, $cmp, $i, $j, 'ge');
205*0Sstevel@tonic-gate	}
206*0Sstevel@tonic-gate	$ok++;
207*0Sstevel@tonic-gate        $pmc =  $j17 cmp $i17; # cmp it in reverse
208*0Sstevel@tonic-gate        # Should give -ve of other answer
209*0Sstevel@tonic-gate        # a + -a should be zero. not zero is truth. which avoids using ==
210*0Sstevel@tonic-gate	if (!($cmp + $pmc))
211*0Sstevel@tonic-gate	{
212*0Sstevel@tonic-gate	    print "ok $ok\n";
213*0Sstevel@tonic-gate	}
214*0Sstevel@tonic-gate	else {
215*0Sstevel@tonic-gate	    nok ($ok, $i3, '<=>', $j3, $cmp, $i, $j, 'cmp transposed');
216*0Sstevel@tonic-gate	}
217*0Sstevel@tonic-gate    }
218*0Sstevel@tonic-gate}
219