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