4 # test the conversion operators
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.
12 # We do not use these subroutines any more, sub overhead makes a "switch"
15 # obviously, 0, 1 and 2, 3 are destructive. (XXXX 64-bit? 4 destructive too)
17 # *0 = sub {--$_[0]}; # -
18 # *1 = sub {++$_[0]}; # +
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
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
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
45 my $max_chain = $ENV{PERL_TEST_NUMCONVERTS} || 2;
47 # Bulk out if unsigned type is hopelessly wrong:
49 my $max_uv2 = sprintf "%u", $max_uv1 ** 6; # 6 is an arbitrary number here
50 my $big_iv = do {use integer; $max_uv1 * 16}; # 16 is an arbitrary number here
51 my $max_uv_less3 = $max_uv1 - 3;
53 print "# max_uv1 = $max_uv1, max_uv2 = $max_uv2, big_iv = $big_iv\n";
54 print "# max_uv_less3 = $max_uv_less3\n";
55 if ($max_uv1 ne $max_uv2 or $big_iv > $max_uv1 or $max_uv1 == $max_uv_less3) {
56 print "1..0 # skipped: unsigned perl arithmetic is not sane";
57 eval { require Config; import Config };
59 if ($Config{d_quad} eq 'define') {
60 print " (common in 64-bit platforms)";
65 if ($max_uv_less3 =~ tr/0-9//c) {
66 print "1..0 # skipped: this perl stringifies large unsigned integers using E notation\n";
70 my $st_t = 4*4; # We try 4 initializers and 4 reporters
73 $num += 10**$_ - 4**$_ for 1.. $max_chain;
75 print "1..$num\n"; # In fact 15 times more subsubtests...
78 my $max_iv = int($max_uv/2);
81 my $l_uv = length $max_uv;
82 my $l_iv = length $max_iv;
84 # Hope: the first digits are good
85 my $larger_than_uv = substr 97 x 100, 0, $l_uv;
86 my $smaller_than_iv = substr 12 x 100, 0, $l_iv;
87 my $yet_smaller_than_iv = substr 97 x 100, 0, ($l_iv - 1);
89 my @list = (1, $yet_smaller_than_iv, $smaller_than_iv, $max_iv, $max_iv + 1,
90 $max_uv, $max_uv + 1);
91 unshift @list, (reverse map -$_, @list), 0; # 15 elts
92 @list = map "$_", @list; # Normalize
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.
101 my $max_uv_pp = "$max_uv"; $max_uv_pp++;
102 my $max_uv_p1 = "$max_uv"; $max_uv_p1+=0; $max_uv_p1++;
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 # precisision. This bites for 12 byte long doubles and 8 byte UVs
108 my $temp = $max_uv_p1;
110 {use integer; $max_uv_p1_as_iv = 0 + sprintf "%s", $temp}
111 my $max_uv_p1_as_uv = 0 | sprintf "%s", $temp;
113 my @opnames = split //, "-+UINPuinp";
115 # @list = map { 2->($_), 3->($_), 4->($_), 5->($_), } @list; # Prepare input
122 for my $num_chain (1..$max_chain) {
123 my @ops = map [split //], grep /[4-9]/,
124 map { sprintf "%0${num_chain}d", $_ } 0 .. 10**$num_chain - 1;
126 #@ops = ([]) unless $num_chain;
131 for my $first (2..5) {
132 for my $last (2..5) {
134 my @otherops = grep $_ <= 3, @$op;
135 my @curops = ($op,\@otherops);
137 for my $num (@list) {
141 for my $short (0, 1) {
142 # undef $inpt; # Forget all we had - some bugs were masked
144 $inpt = $num; # Try to not contaminate $num...
147 $inpt = $max_uv & $inpt; # U 2
148 } elsif ($first == 3) {
149 use integer; $inpt += $zero; # I 3
150 } elsif ($first == 4) {
151 $inpt += $zero; # N 4
153 $inpt = "$inpt"; # P 5
156 # Saves 20% of time - not with this logic:
159 #next if $num_chain > 1
160 # and "$tmp" ne "$tmp1"; # Already the coercion gives problems...
162 for my $curop (@{$curops[$short]}) {
167 } elsif ($curop == 1) {
170 $inpt = $max_uv & $inpt; # U 2
172 } elsif ($curop == 3) {
173 use integer; $inpt += $zero;
175 $inpt += $zero; # N 4
177 } elsif ($curop < 8) {
179 $inpt = "$inpt"; # P 5
180 } elsif ($curop == 6) {
181 $max_uv & $inpt; # u 6
183 use integer; $inpt + $zero;
185 } elsif ($curop == 8) {
193 $inpt = sprintf "%u", $inpt; # U 2
194 } elsif ($last == 3) {
195 $inpt = sprintf "%d", $inpt; # I 3
196 } elsif ($last == 4) {
197 $inpt = sprintf "%g", $inpt; # N 4
199 $inpt = "$inpt"; # P 5
203 if ($ans[0] ne $ans[1]) {
204 print "# '$ans[0]' ne '$ans[1]',\t$num\t=> @opnames[$first,@{$curops[0]},$last] vs @opnames[$first,@{$curops[1]},$last]\n";
205 # XXX ought to check that "+" was in the list of opnames
206 if ((($ans[0] eq $max_uv_pp) and ($ans[1] eq $max_uv_p1))
207 or (($ans[1] eq $max_uv_pp) and ($ans[0] eq $max_uv_p1))) {
208 # string ++ versus numeric ++. Tolerate this little
210 print "# ok, as string ++ of max_uv is \"$max_uv_pp\", numeric is $max_uv_p1\n"
211 } elsif ($opnames[$last] eq 'I' and $ans[1] eq "-1"
212 and $ans[0] eq $max_uv_p1_as_iv) {
213 # Max UV plus 1 is NV. This NV may stringify in E notation.
214 # And the number of decimal digits shown in E notation will depend
215 # on the binary digits in the mantissa. And it may be that
216 # (say) 18446744073709551616 in E notation is truncated to
217 # (say) 1.8446744073709551e+19 (say) which gets converted back
218 # as 1.8446744073709551000e+19
219 # ie 18446744073709551000
220 # which isn't the integer we first had.
221 # But each step of conversion is correct. So it's not an error.
222 # (Only shows up for 64 bit UVs and NVs with 64 bit mantissas,
223 # and on Crays (64 bit integers, 48 bit mantissas) IIRC)
224 print "# ok, \"$max_uv_p1\" correctly converts to IV \"$max_uv_p1_as_iv\"\n";
225 } elsif ($opnames[$last] eq 'U' and $ans[1] eq ~0
226 and $ans[0] eq $max_uv_p1_as_uv) {
228 print "# ok, \"$max_uv_p1\" correctly converts to UV \"$max_uv_p1_as_uv\"\n";
229 } elsif (grep {/^N$/} @opnames[@{$curops[0]}]
230 and $ans[0] == $ans[1] and $ans[0] <= ~0
231 # First must be in E notation (ie not just digits) and
232 # second must still be an integer.
233 # eg 1.84467440737095516e+19
234 # 1.84467440737095516e+19 for 64 bit mantissa is in the
235 # integer range, so 1.84467440737095516e+19 + 0 is treated
236 # as integer addition. [should it be?]
237 # and 18446744073709551600 + 0 is 18446744073709551600
238 # Which isn't the string you first thought of.
239 # I can't remember why there isn't symmetry in this
240 # exception, ie why only the first ops are tested for 'N'
241 and $ans[0] != /^-?\d+$/ and $ans[1] !~ /^-?\d+$/) {
242 print "# ok, numerically equal - notation changed due to adding zero\n";
249 print "not ok $test\n";