This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix behavior of <=> on bigints
[perl5.git] / t / lib / bigintpm.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Config;
9 use Math::BigInt;
10
11 $test = 0;
12 $| = 1;
13 print "1..247\n";
14 while (<DATA>) {
15        chop;
16        if (s/^&//) {
17                $f = $_;
18        } else {
19                ++$test;
20                @args = split(/:/,$_,99);
21                $ans = pop(@args);
22                $try = "\$x = new Math::BigInt \"$args[0]\";";
23                if ($f eq "bnorm"){
24                    $try .= "\$x+0;";
25                } elsif ($f eq "bneg") {
26                    $try .= "-\$x;";
27                } elsif ($f eq "babs") {
28                    $try .= "abs \$x;";
29                } else {
30                    $try .= "\$y = new Math::BigInt \"$args[1]\";";
31                    if ($f eq bcmp){
32                        $try .= "\$x <=> \$y;";
33                    }elsif ($f eq badd){
34                        $try .= "\$x + \$y;";
35                    }elsif ($f eq bsub){
36                        $try .= "\$x - \$y;";
37                    }elsif ($f eq bmul){
38                        $try .= "\$x * \$y;";
39                    }elsif ($f eq bdiv){
40                        $try .= "\$x / \$y;";
41                    }elsif ($f eq bmod){
42                        $try .= "\$x % \$y;";
43                    }elsif ($f eq bgcd){
44                        $try .= "Math::BigInt::bgcd(\$x, \$y);";
45                    } else { warn "Unknown op"; }
46                }
47                #print ">>>",$try,"<<<\n";
48                $ans1 = eval $try;
49                if ("$ans1" eq $ans) { #bug!
50                        print "ok $test\n";
51                } else {
52                        print "not ok $test\n";
53                        print "# '$try' expected: '$ans' got: '$ans1'\n";
54                }
55        }
56
57 __END__
58 &bnorm
59 abc:NaN
60    1 a:NaN
61 1bcd2:NaN
62 11111b:NaN
63 +1z:NaN
64 -1z:NaN
65 0:+0
66 +0:+0
67 +00:+0
68 +0 0 0:+0
69 000000  0000000   00000:+0
70 -0:+0
71 -0000:+0
72 +1:+1
73 +01:+1
74 +001:+1
75 +00000100000:+100000
76 123456789:+123456789
77 -1:-1
78 -01:-1
79 -001:-1
80 -123456789:-123456789
81 -00000100000:-100000
82 &bneg
83 abd:NaN
84 +0:+0
85 +1:-1
86 -1:+1
87 +123456789:-123456789
88 -123456789:+123456789
89 &babs
90 abc:NaN
91 +0:+0
92 +1:+1
93 -1:+1
94 +123456789:+123456789
95 -123456789:+123456789
96 &bcmp
97 abc:abc:NaN
98 abc:+0:NaN
99 +0:abc:NaN
100 +0:+0:+0
101 -1:+0:-1
102 +0:-1:+1
103 +1:+0:+1
104 +0:+1:-1
105 -1:+1:-1
106 +1:-1:+1
107 -1:-1:+0
108 +1:+1:+0
109 +123:+123:+0
110 +123:+12:+1
111 +12:+123:-1
112 -123:-123:+0
113 -123:-12:-1
114 -12:-123:+1
115 +123:+124:-1
116 +124:+123:+1
117 -123:-124:+1
118 -124:-123:-1
119 +100:+5:+1
120 &badd
121 abc:abc:NaN
122 abc:+0:NaN
123 +0:abc:NaN
124 +0:+0:+0
125 +1:+0:+1
126 +0:+1:+1
127 +1:+1:+2
128 -1:+0:-1
129 +0:-1:-1
130 -1:-1:-2
131 -1:+1:+0
132 +1:-1:+0
133 +9:+1:+10
134 +99:+1:+100
135 +999:+1:+1000
136 +9999:+1:+10000
137 +99999:+1:+100000
138 +999999:+1:+1000000
139 +9999999:+1:+10000000
140 +99999999:+1:+100000000
141 +999999999:+1:+1000000000
142 +9999999999:+1:+10000000000
143 +99999999999:+1:+100000000000
144 +10:-1:+9
145 +100:-1:+99
146 +1000:-1:+999
147 +10000:-1:+9999
148 +100000:-1:+99999
149 +1000000:-1:+999999
150 +10000000:-1:+9999999
151 +100000000:-1:+99999999
152 +1000000000:-1:+999999999
153 +10000000000:-1:+9999999999
154 +123456789:+987654321:+1111111110
155 -123456789:+987654321:+864197532
156 -123456789:-987654321:-1111111110
157 +123456789:-987654321:-864197532
158 &bsub
159 abc:abc:NaN
160 abc:+0:NaN
161 +0:abc:NaN
162 +0:+0:+0
163 +1:+0:+1
164 +0:+1:-1
165 +1:+1:+0
166 -1:+0:-1
167 +0:-1:+1
168 -1:-1:+0
169 -1:+1:-2
170 +1:-1:+2
171 +9:+1:+8
172 +99:+1:+98
173 +999:+1:+998
174 +9999:+1:+9998
175 +99999:+1:+99998
176 +999999:+1:+999998
177 +9999999:+1:+9999998
178 +99999999:+1:+99999998
179 +999999999:+1:+999999998
180 +9999999999:+1:+9999999998
181 +99999999999:+1:+99999999998
182 +10:-1:+11
183 +100:-1:+101
184 +1000:-1:+1001
185 +10000:-1:+10001
186 +100000:-1:+100001
187 +1000000:-1:+1000001
188 +10000000:-1:+10000001
189 +100000000:-1:+100000001
190 +1000000000:-1:+1000000001
191 +10000000000:-1:+10000000001
192 +123456789:+987654321:-864197532
193 -123456789:+987654321:-1111111110
194 -123456789:-987654321:+864197532
195 +123456789:-987654321:+1111111110
196 &bmul
197 abc:abc:NaN
198 abc:+0:NaN
199 +0:abc:NaN
200 +0:+0:+0
201 +0:+1:+0
202 +1:+0:+0
203 +0:-1:+0
204 -1:+0:+0
205 +123456789123456789:+0:+0
206 +0:+123456789123456789:+0
207 -1:-1:+1
208 -1:+1:-1
209 +1:-1:-1
210 +1:+1:+1
211 +2:+3:+6
212 -2:+3:-6
213 +2:-3:-6
214 -2:-3:+6
215 +111:+111:+12321
216 +10101:+10101:+102030201
217 +1001001:+1001001:+1002003002001
218 +100010001:+100010001:+10002000300020001
219 +10000100001:+10000100001:+100002000030000200001
220 +11111111111:+9:+99999999999
221 +22222222222:+9:+199999999998
222 +33333333333:+9:+299999999997
223 +44444444444:+9:+399999999996
224 +55555555555:+9:+499999999995
225 +66666666666:+9:+599999999994
226 +77777777777:+9:+699999999993
227 +88888888888:+9:+799999999992
228 +99999999999:+9:+899999999991
229 &bdiv
230 abc:abc:NaN
231 abc:+1:abc:NaN
232 +1:abc:NaN
233 +0:+0:NaN
234 +0:+1:+0
235 +1:+0:NaN
236 +0:-1:+0
237 -1:+0:NaN
238 +1:+1:+1
239 -1:-1:+1
240 +1:-1:-1
241 -1:+1:-1
242 +1:+2:+0
243 +2:+1:+2
244 +1000000000:+9:+111111111
245 +2000000000:+9:+222222222
246 +3000000000:+9:+333333333
247 +4000000000:+9:+444444444
248 +5000000000:+9:+555555555
249 +6000000000:+9:+666666666
250 +7000000000:+9:+777777777
251 +8000000000:+9:+888888888
252 +9000000000:+9:+1000000000
253 +35500000:+113:+314159
254 +71000000:+226:+314159
255 +106500000:+339:+314159
256 +1000000000:+3:+333333333
257 +10:+5:+2
258 +100:+4:+25
259 +1000:+8:+125
260 +10000:+16:+625
261 +999999999999:+9:+111111111111
262 +999999999999:+99:+10101010101
263 +999999999999:+999:+1001001001
264 +999999999999:+9999:+100010001
265 +999999999999999:+99999:+10000100001
266 &bmod
267 abc:abc:NaN
268 abc:+1:abc:NaN
269 +1:abc:NaN
270 +0:+0:NaN
271 +0:+1:+0
272 +1:+0:NaN
273 +0:-1:+0
274 -1:+0:NaN
275 +1:+1:+0
276 -1:-1:+0
277 +1:-1:+0
278 -1:+1:+0
279 +1:+2:+1
280 +2:+1:+0
281 +1000000000:+9:+1
282 +2000000000:+9:+2
283 +3000000000:+9:+3
284 +4000000000:+9:+4
285 +5000000000:+9:+5
286 +6000000000:+9:+6
287 +7000000000:+9:+7
288 +8000000000:+9:+8
289 +9000000000:+9:+0
290 +35500000:+113:+33
291 +71000000:+226:+66
292 +106500000:+339:+99
293 +1000000000:+3:+1
294 +10:+5:+0
295 +100:+4:+0
296 +1000:+8:+0
297 +10000:+16:+0
298 +999999999999:+9:+0
299 +999999999999:+99:+0
300 +999999999999:+999:+0
301 +999999999999:+9999:+0
302 +999999999999999:+99999:+0
303 &bgcd
304 abc:abc:NaN
305 abc:+0:NaN
306 +0:abc:NaN
307 +0:+0:+0
308 +0:+1:+1
309 +1:+0:+1
310 +1:+1:+1
311 +2:+3:+1
312 +3:+2:+1
313 +100:+625:+25
314 +4096:+81:+1