1#!./perl
2
3#
4# test the conversion operators
5#
6# Notations:
7#
8# "N p i N vs N N":  Apply op-N, then op-p, then op-i, then reporter-N
9# Compare with application of op-N, then reporter-N
10# Right below are descriptions of different ops and reporters.
11
12# We do not use these subroutines any more, sub overhead makes a "switch"
13# solution better:
14
15# obviously, 0, 1 and 2, 3 are destructive.  (XXXX 64-bit? 4 destructive too)
16
17# *0 = sub {--$_[0]};		# -
18# *1 = sub {++$_[0]};		# +
19
20# # Converters
21# *2 = sub { $_[0] = $max_uv & $_[0]}; # U
22# *3 = sub { use integer; $_[0] += $zero}; # I
23# *4 = sub { $_[0] += $zero};	# N
24# *5 = sub { $_[0] = "$_[0]" };	# P
25
26# # Side effects
27# *6 = sub { $max_uv & $_[0]};	# u
28# *7 = sub { use integer; $_[0] + $zero};	# i
29# *8 = sub { $_[0] + $zero};	# n
30# *9 = sub { $_[0] . "" };	# p
31
32# # Reporters
33# sub a2 { sprintf "%u", $_[0] }	# U
34# sub a3 { sprintf "%d", $_[0] }	# I
35# sub a4 { sprintf "%g", $_[0] }	# N
36# sub a5 { "$_[0]" }		# P
37
38BEGIN {
39    chdir 't' if -d 't';
40    require './test.pl';
41    if (pack("d", 1) =~ /^[\x80\10]\x40/) {
42        skip_all("VAX float cannot do infinity");
43    }
44    set_up_inc('../lib');
45}
46
47use strict;
48
49my $max_chain = $ENV{PERL_TEST_NUMCONVERTS} || 2;
50
51# Bulk out if unsigned type is hopelessly wrong:
52my $max_uv1 = ~0;
53my $max_uv2 = sprintf "%u", $max_uv1 ** 6; # 6 is an arbitrary number here
54my $big_iv = do {use integer; $max_uv1 * 16}; # 16 is an arbitrary number here
55my $max_uv_less3 = $max_uv1 - 3;
56
57print "# max_uv1 = $max_uv1, max_uv2 = $max_uv2, big_iv = $big_iv\n";
58print "# max_uv_less3 = $max_uv_less3\n";
59if ($max_uv1 ne $max_uv2 or $big_iv > $max_uv1 or $max_uv1 == $max_uv_less3) {
60  eval { require Config; };
61  my $message = 'unsigned perl arithmetic is not sane';
62  $message .= " (common in 64-bit platforms)" if $Config::Config{d_quad};
63  skip_all($message);
64}
65if ($max_uv_less3 =~ tr/0-9//c) {
66  skip_all('this perl stringifies large unsigned integers using E notation');
67}
68
69my $st_t = 4*4;			# We try 4 initializers and 4 reporters
70
71my $num = 0;
72$num += 10**$_ - 4**$_ for 1.. $max_chain;
73$num *= $st_t;
74$num += $::additional_tests;
75plan(tests => $num);		# In fact 15 times more subsubtests...
76
77my $max_uv = ~0;
78my $max_iv = int($max_uv/2);
79my $zero = 0;
80
81my $l_uv = length $max_uv;
82my $l_iv = length $max_iv;
83
84# Hope: the first digits are good
85my $larger_than_uv = substr 97 x 100, 0, $l_uv;
86my $smaller_than_iv = substr 12 x 100, 0, $l_iv;
87my $yet_smaller_than_iv = substr 97 x 100, 0, ($l_iv - 1);
88
89my @list = (1, $yet_smaller_than_iv, $smaller_than_iv, $max_iv, $max_iv + 1,
90	    $max_uv, $max_uv + 1);
91unshift @list, (reverse map -$_, @list), 0; # 15 elts
92@list = map "$_", @list; # Normalize
93
94note("@list");
95
96# need to special case ++ for max_uv, as ++ "magic" on a string gives
97# another string, whereas ++ magic on a string used as a number gives
98# a number. Not a problem when NV preserves UV, but if it doesn't then
99# stringification of the latter gives something in e notation.
100
101my $max_uv_pp = "$max_uv"; $max_uv_pp++;
102my $max_uv_p1 = "$max_uv"; $max_uv_p1+=0; $max_uv_p1++;
103
104# Also need to cope with %g notation for max_uv_p1 that actually gives an
105# integer less than max_uv because of correct rounding for the limited
106# precision. This bites for 12 byte long doubles and 8 byte UVs
107
108my $temp = $max_uv_p1;
109my $max_uv_p1_as_iv;
110{use integer; $max_uv_p1_as_iv = 0 + sprintf "%s", $temp}
111my $max_uv_p1_as_uv = 0 | sprintf "%s", $temp;
112
113my @opnames = split //, "-+UINPuinp";
114
115# @list = map { 2->($_), 3->($_), 4->($_), 5->($_),  } @list; # Prepare input
116
117my $test = 1;
118my $nok;
119for my $num_chain (1..$max_chain) {
120  my @ops = map [split //], grep /[4-9]/,
121    map { sprintf "%0${num_chain}d", $_ }  0 .. 10**$num_chain - 1;
122
123  #@ops = ([]) unless $num_chain;
124  #@ops = ([6, 4]);
125
126  for my $op (@ops) {
127    for my $first (2..5) {
128      for my $last (2..5) {
129	$nok = 0;
130	my @otherops = grep $_ <= 3, @$op;
131	my @curops = ($op,\@otherops);
132
133	for my $num (@list) {
134	  my $inpt;
135	  my @ans;
136
137	  for my $short (0, 1) {
138	    # undef $inpt;	# Forget all we had - some bugs were masked
139
140	    $inpt = $num;	# Try to not contaminate $num...
141	    $inpt = "$inpt";
142	    if ($first == 2) {
143	      $inpt = $max_uv & $inpt; # U 2
144	    } elsif ($first == 3) {
145	      use integer; $inpt += $zero; # I 3
146	    } elsif ($first == 4) {
147	      $inpt += $zero;	# N 4
148	    } else {
149	      $inpt = "$inpt";	# P 5
150	    }
151
152	    # Saves 20% of time - not with this logic:
153	    #my $tmp = $inpt;
154	    #my $tmp1 = $num;
155	    #next if $num_chain > 1
156	    #  and "$tmp" ne "$tmp1"; # Already the coercion gives problems...
157
158	    for my $curop (@{$curops[$short]}) {
159	      if ($curop < 5) {
160		if ($curop < 3) {
161		  if ($curop == 0) {
162		    --$inpt;	# - 0
163		  } elsif ($curop == 1) {
164		    ++$inpt;	# + 1
165		  } else {
166		    $inpt = $max_uv & $inpt; # U 2
167		  }
168		} elsif ($curop == 3) {
169		  use integer; $inpt += $zero;
170		} else {
171		  $inpt += $zero; # N 4
172		}
173	      } elsif ($curop < 8) {
174		if ($curop == 5) {
175		  $inpt = "$inpt"; # P 5
176		} elsif ($curop == 6) {
177		  my $dummy = $max_uv & $inpt; # u 6
178		} else {
179		  use integer; my $dummy = $inpt + $zero;
180		}
181	      } elsif ($curop == 8) {
182		my $dummy = $inpt + $zero;	# n 8
183	      } else {
184		my $dummy = $inpt . "";	# p 9
185	      }
186	    }
187
188	    if ($last == 2) {
189	      $inpt = sprintf "%u", $inpt; # U 2
190	    } elsif ($last == 3) {
191	      $inpt = sprintf "%d", $inpt; # I 3
192	    } elsif ($last == 4) {
193	      $inpt = sprintf "%g", $inpt; # N 4
194	    } else {
195	      $inpt = "$inpt";	# P 5
196	    }
197	    push @ans, $inpt;
198	  }
199	  if ($ans[0] ne $ans[1]) {
200	    my $diag = "'$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]";
201	    my $excuse;
202	    # XXX ought to check that "+" was in the list of opnames
203	    if ((($ans[0] eq $max_uv_pp) and ($ans[1] eq $max_uv_p1))
204		or (($ans[1] eq $max_uv_pp) and ($ans[0] eq $max_uv_p1))) {
205	      # string ++ versus numeric ++. Tolerate this little
206	      # bit of insanity
207	      $excuse = "ok, as string ++ of max_uv is \"$max_uv_pp\", numeric is $max_uv_p1";
208	    } elsif ($opnames[$last] eq 'I' and $ans[1] eq "-1"
209		     and $ans[0] eq $max_uv_p1_as_iv) {
210              # Max UV plus 1 is NV. This NV may stringify in E notation.
211              # And the number of decimal digits shown in E notation will depend
212              # on the binary digits in the mantissa. And it may be that
213              # (say)  18446744073709551616 in E notation is truncated to
214              # (say) 1.8446744073709551e+19 (say) which gets converted back
215              # as    1.8446744073709551000e+19
216              # ie    18446744073709551000
217              # which isn't the integer we first had.
218              # But each step of conversion is correct. So it's not an error.
219              # (Only shows up for 64 bit UVs and NVs with 64 bit mantissas,
220              #  and on Crays (64 bit integers, 48 bit mantissas) IIRC)
221	      $excuse = "ok, \"$max_uv_p1\" correctly converts to IV \"$max_uv_p1_as_iv\"";
222	    } elsif ($opnames[$last] eq 'U' and $ans[1] eq ~0
223		     and $ans[0] eq $max_uv_p1_as_uv) {
224              # as aboce
225	      $excuse = "ok, \"$max_uv_p1\" correctly converts to UV \"$max_uv_p1_as_uv\"";
226	    } elsif (grep {defined $_ && /^N$/} @opnames[@{$curops[0]}]
227		     and $ans[0] == $ans[1] and $ans[0] <= ~0
228                     # First must be in E notation (ie not just digits) and
229                     # second must still be an integer.
230		     # eg 1.84467440737095516e+19
231		     # 1.84467440737095516e+19 for 64 bit mantissa is in the
232		     # integer range, so 1.84467440737095516e+19 + 0 is treated
233		     # as integer addition. [should it be?]
234		     # and 18446744073709551600 + 0 is 18446744073709551600
235		     # Which isn't the string you first thought of.
236                     # I can't remember why there isn't symmetry in this
237                     # exception, ie why only the first ops are tested for 'N'
238                     and $ans[0] != /^-?\d+$/ and $ans[1] !~ /^-?\d+$/) {
239	      $excuse = "ok, numerically equal - notation changed due to adding zero";
240	    } else {
241	      $nok++,
242	      diag($diag);
243	    }
244	    if ($excuse) {
245	      note($diag);
246	      note($excuse);
247	    }
248	  }
249	}
250	ok($nok == 0);
251      }
252    }
253  }
254}
255
256# Tests that use test.pl start here.
257BEGIN { $::additional_tests = 4 }
258
259ok(-0.0 eq "0", 'negative zero stringifies as 0');
260ok(!-0.0, "neg zero is boolean false");
261my $nz = -0.0;
262{ my $dummy = "$nz"; }
263ok(!$nz, 'previously stringified -0.0 is boolean false');
264$nz = -0.0;
265is sprintf("%+.f", - -$nz), sprintf("%+.f", - -$nz),
266  "negation does not coerce negative zeroes";
267