Source file
src/math/big/rat_test.go
1
2
3
4
5 package big
6
7 import (
8 "math"
9 "testing"
10 )
11
12 func TestZeroRat(t *testing.T) {
13 var x, y, z Rat
14 y.SetFrac64(0, 42)
15
16 if x.Cmp(&y) != 0 {
17 t.Errorf("x and y should be both equal and zero")
18 }
19
20 if s := x.String(); s != "0/1" {
21 t.Errorf("got x = %s, want 0/1", s)
22 }
23
24 if s := x.RatString(); s != "0" {
25 t.Errorf("got x = %s, want 0", s)
26 }
27
28 z.Add(&x, &y)
29 if s := z.RatString(); s != "0" {
30 t.Errorf("got x+y = %s, want 0", s)
31 }
32
33 z.Sub(&x, &y)
34 if s := z.RatString(); s != "0" {
35 t.Errorf("got x-y = %s, want 0", s)
36 }
37
38 z.Mul(&x, &y)
39 if s := z.RatString(); s != "0" {
40 t.Errorf("got x*y = %s, want 0", s)
41 }
42
43
44 defer func() {
45 if s := recover(); s == nil || s.(string) != "division by zero" {
46 panic(s)
47 }
48 }()
49 z.Quo(&x, &y)
50 }
51
52 func TestRatSign(t *testing.T) {
53 zero := NewRat(0, 1)
54 for _, a := range setStringTests {
55 x, ok := new(Rat).SetString(a.in)
56 if !ok {
57 continue
58 }
59 s := x.Sign()
60 e := x.Cmp(zero)
61 if s != e {
62 t.Errorf("got %d; want %d for z = %v", s, e, &x)
63 }
64 }
65 }
66
67 var ratCmpTests = []struct {
68 rat1, rat2 string
69 out int
70 }{
71 {"0", "0/1", 0},
72 {"1/1", "1", 0},
73 {"-1", "-2/2", 0},
74 {"1", "0", 1},
75 {"0/1", "1/1", -1},
76 {"-5/1434770811533343057144", "-5/1434770811533343057145", -1},
77 {"49832350382626108453/8964749413", "49832350382626108454/8964749413", -1},
78 {"-37414950961700930/7204075375675961", "37414950961700930/7204075375675961", -1},
79 {"37414950961700930/7204075375675961", "74829901923401860/14408150751351922", 0},
80 }
81
82 func TestRatCmp(t *testing.T) {
83 for i, test := range ratCmpTests {
84 x, _ := new(Rat).SetString(test.rat1)
85 y, _ := new(Rat).SetString(test.rat2)
86
87 out := x.Cmp(y)
88 if out != test.out {
89 t.Errorf("#%d got out = %v; want %v", i, out, test.out)
90 }
91 }
92 }
93
94 func TestIsInt(t *testing.T) {
95 one := NewInt(1)
96 for _, a := range setStringTests {
97 x, ok := new(Rat).SetString(a.in)
98 if !ok {
99 continue
100 }
101 i := x.IsInt()
102 e := x.Denom().Cmp(one) == 0
103 if i != e {
104 t.Errorf("got IsInt(%v) == %v; want %v", x, i, e)
105 }
106 }
107 }
108
109 func TestRatAbs(t *testing.T) {
110 zero := new(Rat)
111 for _, a := range setStringTests {
112 x, ok := new(Rat).SetString(a.in)
113 if !ok {
114 continue
115 }
116 e := new(Rat).Set(x)
117 if e.Cmp(zero) < 0 {
118 e.Sub(zero, e)
119 }
120 z := new(Rat).Abs(x)
121 if z.Cmp(e) != 0 {
122 t.Errorf("got Abs(%v) = %v; want %v", x, z, e)
123 }
124 }
125 }
126
127 func TestRatNeg(t *testing.T) {
128 zero := new(Rat)
129 for _, a := range setStringTests {
130 x, ok := new(Rat).SetString(a.in)
131 if !ok {
132 continue
133 }
134 e := new(Rat).Sub(zero, x)
135 z := new(Rat).Neg(x)
136 if z.Cmp(e) != 0 {
137 t.Errorf("got Neg(%v) = %v; want %v", x, z, e)
138 }
139 }
140 }
141
142 func TestRatInv(t *testing.T) {
143 zero := new(Rat)
144 for _, a := range setStringTests {
145 x, ok := new(Rat).SetString(a.in)
146 if !ok {
147 continue
148 }
149 if x.Cmp(zero) == 0 {
150 continue
151 }
152 e := new(Rat).SetFrac(x.Denom(), x.Num())
153 z := new(Rat).Inv(x)
154 if z.Cmp(e) != 0 {
155 t.Errorf("got Inv(%v) = %v; want %v", x, z, e)
156 }
157 }
158 }
159
160 type ratBinFun func(z, x, y *Rat) *Rat
161 type ratBinArg struct {
162 x, y, z string
163 }
164
165 func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) {
166 x, _ := new(Rat).SetString(a.x)
167 y, _ := new(Rat).SetString(a.y)
168 z, _ := new(Rat).SetString(a.z)
169 out := f(new(Rat), x, y)
170
171 if out.Cmp(z) != 0 {
172 t.Errorf("%s #%d got %s want %s", name, i, out, z)
173 }
174 }
175
176 var ratBinTests = []struct {
177 x, y string
178 sum, prod string
179 }{
180 {"0", "0", "0", "0"},
181 {"0", "1", "1", "0"},
182 {"-1", "0", "-1", "0"},
183 {"-1", "1", "0", "-1"},
184 {"1", "1", "2", "1"},
185 {"1/2", "1/2", "1", "1/4"},
186 {"1/4", "1/3", "7/12", "1/12"},
187 {"2/5", "-14/3", "-64/15", "-28/15"},
188 {"4707/49292519774798173060", "-3367/70976135186689855734", "84058377121001851123459/1749296273614329067191168098769082663020", "-1760941/388732505247628681598037355282018369560"},
189 {"-61204110018146728334/3", "-31052192278051565633/2", "-215564796870448153567/6", "950260896245257153059642991192710872711/3"},
190 {"-854857841473707320655/4237645934602118692642972629634714039", "-18/31750379913563777419", "-27/133467566250814981", "15387441146526731771790/134546868362786310073779084329032722548987800600710485341"},
191 {"618575745270541348005638912139/19198433543745179392300736", "-19948846211000086/637313996471", "27674141753240653/30123979153216", "-6169936206128396568797607742807090270137721977/6117715203873571641674006593837351328"},
192 {"-3/26206484091896184128", "5/2848423294177090248", "15310893822118706237/9330894968229805033368778458685147968", "-5/24882386581946146755650075889827061248"},
193 {"26946729/330400702820", "41563965/225583428284", "1238218672302860271/4658307703098666660055", "224002580204097/14906584649915733312176"},
194 {"-8259900599013409474/7", "-84829337473700364773/56707961321161574960", "-468402123685491748914621885145127724451/396955729248131024720", "350340947706464153265156004876107029701/198477864624065512360"},
195 {"575775209696864/1320203974639986246357", "29/712593081308", "410331716733912717985762465/940768218243776489278275419794956", "808/45524274987585732633"},
196 {"1786597389946320496771/2066653520653241", "6269770/1992362624741777", "3559549865190272133656109052308126637/4117523232840525481453983149257", "8967230/3296219033"},
197 {"-36459180403360509753/32150500941194292113930", "9381566963714/9633539", "301622077145533298008420642898530153/309723104686531919656937098270", "-3784609207827/3426986245"},
198 }
199
200 func TestRatBin(t *testing.T) {
201 for i, test := range ratBinTests {
202 arg := ratBinArg{test.x, test.y, test.sum}
203 testRatBin(t, i, "Add", (*Rat).Add, arg)
204
205 arg = ratBinArg{test.y, test.x, test.sum}
206 testRatBin(t, i, "Add symmetric", (*Rat).Add, arg)
207
208 arg = ratBinArg{test.sum, test.x, test.y}
209 testRatBin(t, i, "Sub", (*Rat).Sub, arg)
210
211 arg = ratBinArg{test.sum, test.y, test.x}
212 testRatBin(t, i, "Sub symmetric", (*Rat).Sub, arg)
213
214 arg = ratBinArg{test.x, test.y, test.prod}
215 testRatBin(t, i, "Mul", (*Rat).Mul, arg)
216
217 arg = ratBinArg{test.y, test.x, test.prod}
218 testRatBin(t, i, "Mul symmetric", (*Rat).Mul, arg)
219
220 if test.x != "0" {
221 arg = ratBinArg{test.prod, test.x, test.y}
222 testRatBin(t, i, "Quo", (*Rat).Quo, arg)
223 }
224
225 if test.y != "0" {
226 arg = ratBinArg{test.prod, test.y, test.x}
227 testRatBin(t, i, "Quo symmetric", (*Rat).Quo, arg)
228 }
229 }
230 }
231
232 func TestIssue820(t *testing.T) {
233 x := NewRat(3, 1)
234 y := NewRat(2, 1)
235 z := y.Quo(x, y)
236 q := NewRat(3, 2)
237 if z.Cmp(q) != 0 {
238 t.Errorf("got %s want %s", z, q)
239 }
240
241 y = NewRat(3, 1)
242 x = NewRat(2, 1)
243 z = y.Quo(x, y)
244 q = NewRat(2, 3)
245 if z.Cmp(q) != 0 {
246 t.Errorf("got %s want %s", z, q)
247 }
248
249 x = NewRat(3, 1)
250 z = x.Quo(x, x)
251 q = NewRat(3, 3)
252 if z.Cmp(q) != 0 {
253 t.Errorf("got %s want %s", z, q)
254 }
255 }
256
257 var setFrac64Tests = []struct {
258 a, b int64
259 out string
260 }{
261 {0, 1, "0"},
262 {0, -1, "0"},
263 {1, 1, "1"},
264 {-1, 1, "-1"},
265 {1, -1, "-1"},
266 {-1, -1, "1"},
267 {-9223372036854775808, -9223372036854775808, "1"},
268 }
269
270 func TestRatSetFrac64Rat(t *testing.T) {
271 for i, test := range setFrac64Tests {
272 x := new(Rat).SetFrac64(test.a, test.b)
273 if x.RatString() != test.out {
274 t.Errorf("#%d got %s want %s", i, x.RatString(), test.out)
275 }
276 }
277 }
278
279 func TestIssue2379(t *testing.T) {
280
281 q := NewRat(3, 2)
282 x := new(Rat)
283 x.SetFrac(NewInt(3), NewInt(2))
284 if x.Cmp(q) != 0 {
285 t.Errorf("1) got %s want %s", x, q)
286 }
287
288
289 x = NewRat(2, 3)
290 x.SetFrac(NewInt(3), x.Num())
291 if x.Cmp(q) != 0 {
292 t.Errorf("2) got %s want %s", x, q)
293 }
294
295
296 x = NewRat(2, 3)
297 x.SetFrac(x.Denom(), NewInt(2))
298 if x.Cmp(q) != 0 {
299 t.Errorf("3) got %s want %s", x, q)
300 }
301
302
303 x = NewRat(2, 3)
304 x.SetFrac(x.Denom(), x.Num())
305 if x.Cmp(q) != 0 {
306 t.Errorf("4) got %s want %s", x, q)
307 }
308
309
310 q = NewRat(1, 1)
311 x = new(Rat)
312 n := NewInt(7)
313 x.SetFrac(n, n)
314 if x.Cmp(q) != 0 {
315 t.Errorf("5) got %s want %s", x, q)
316 }
317 }
318
319 func TestIssue3521(t *testing.T) {
320 a := new(Int)
321 b := new(Int)
322 a.SetString("64375784358435883458348587", 0)
323 b.SetString("4789759874531", 0)
324
325
326 zero := new(Rat)
327 one := NewInt(1)
328 if zero.Denom().Cmp(one) != 0 {
329 t.Errorf("0) got %s want %s", zero.Denom(), one)
330 }
331
332
333 s := &zero.b
334 d := zero.Denom()
335 if d == s {
336 t.Errorf("1a) got %s (%p) == %s (%p) want different *Int values", d, d, s, s)
337 }
338
339
340 d1 := zero.Denom()
341 d2 := zero.Denom()
342 if d1 == d2 {
343 t.Errorf("1b) got %s (%p) == %s (%p) want different *Int values", d1, d1, d2, d2)
344 }
345
346
347 x := new(Rat)
348 x.Set(x)
349 s = &x.b
350 d = x.Denom()
351 if d != s {
352 t.Errorf("1c) got %s (%p) != %s (%p) want identical *Int values", d, d, s, s)
353 }
354
355
356 x.Denom().Set(new(Int).Neg(b))
357 if x.Cmp(zero) != 0 {
358 t.Errorf("1d) got %s want %s", x, zero)
359 }
360
361
362 x.Num().Set(a)
363 qab := new(Rat).SetFrac(a, b)
364 if x.Cmp(qab) != 0 {
365 t.Errorf("1e) got %s want %s", x, qab)
366 }
367
368
369 x.SetFrac64(10, 2)
370 x.Denom().SetInt64(3)
371 q53 := NewRat(5, 3)
372 if x.Cmp(q53) != 0 {
373 t.Errorf("2a) got %s want %s", x, q53)
374 }
375
376
377 x = NewRat(10, 2)
378 x.Denom().SetInt64(3)
379 if x.Cmp(q53) != 0 {
380 t.Errorf("2b) got %s want %s", x, q53)
381 }
382
383
384 x.SetFrac(a, b)
385 a = x.Num()
386 b = x.Denom()
387 a.SetInt64(5)
388 b.SetInt64(3)
389 if x.Cmp(q53) != 0 {
390 t.Errorf("3) got %s want %s", x, q53)
391 }
392 }
393
394 func TestFloat32Distribution(t *testing.T) {
395
396
397
398 var add = []int64{
399 0,
400 1,
401 3,
402 5,
403 7,
404 9,
405 11,
406 }
407 var winc, einc = uint64(5), 15
408 if *long {
409 winc, einc = uint64(1), 1
410 }
411
412 for _, sign := range "+-" {
413 for _, a := range add {
414 for wid := uint64(0); wid < 30; wid += winc {
415 b := 1<<wid + a
416 if sign == '-' {
417 b = -b
418 }
419 for exp := -150; exp < 150; exp += einc {
420 num, den := NewInt(b), NewInt(1)
421 if exp > 0 {
422 num.Lsh(num, uint(exp))
423 } else {
424 den.Lsh(den, uint(-exp))
425 }
426 r := new(Rat).SetFrac(num, den)
427 f, _ := r.Float32()
428
429 if !checkIsBestApprox32(t, f, r) {
430
431 t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)",
432 b, exp, f, f, math.Ldexp(float64(b), exp), r)
433 }
434
435 checkNonLossyRoundtrip32(t, f)
436 }
437 }
438 }
439 }
440 }
441
442 func TestFloat64Distribution(t *testing.T) {
443
444
445
446 var add = []int64{
447 0,
448 1,
449 3,
450 5,
451 7,
452 9,
453 11,
454 }
455 var winc, einc = uint64(10), 500
456 if *long {
457 winc, einc = uint64(1), 1
458 }
459
460 for _, sign := range "+-" {
461 for _, a := range add {
462 for wid := uint64(0); wid < 60; wid += winc {
463 b := 1<<wid + a
464 if sign == '-' {
465 b = -b
466 }
467 for exp := -1100; exp < 1100; exp += einc {
468 num, den := NewInt(b), NewInt(1)
469 if exp > 0 {
470 num.Lsh(num, uint(exp))
471 } else {
472 den.Lsh(den, uint(-exp))
473 }
474 r := new(Rat).SetFrac(num, den)
475 f, _ := r.Float64()
476
477 if !checkIsBestApprox64(t, f, r) {
478
479 t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)",
480 b, exp, f, f, math.Ldexp(float64(b), exp), r)
481 }
482
483 checkNonLossyRoundtrip64(t, f)
484 }
485 }
486 }
487 }
488 }
489
490
491
492 func TestSetFloat64NonFinite(t *testing.T) {
493 for _, f := range []float64{math.NaN(), math.Inf(+1), math.Inf(-1)} {
494 var r Rat
495 if r2 := r.SetFloat64(f); r2 != nil {
496 t.Errorf("SetFloat64(%g) was %v, want nil", f, r2)
497 }
498 }
499 }
500
501
502
503 func checkNonLossyRoundtrip32(t *testing.T, f float32) {
504 if !isFinite(float64(f)) {
505 return
506 }
507 r := new(Rat).SetFloat64(float64(f))
508 if r == nil {
509 t.Errorf("Rat.SetFloat64(float64(%g) (%b)) == nil", f, f)
510 return
511 }
512 f2, exact := r.Float32()
513 if f != f2 || !exact {
514 t.Errorf("Rat.SetFloat64(float64(%g)).Float32() = %g (%b), %v, want %g (%b), %v; delta = %b",
515 f, f2, f2, exact, f, f, true, f2-f)
516 }
517 }
518
519
520
521 func checkNonLossyRoundtrip64(t *testing.T, f float64) {
522 if !isFinite(f) {
523 return
524 }
525 r := new(Rat).SetFloat64(f)
526 if r == nil {
527 t.Errorf("Rat.SetFloat64(%g (%b)) == nil", f, f)
528 return
529 }
530 f2, exact := r.Float64()
531 if f != f2 || !exact {
532 t.Errorf("Rat.SetFloat64(%g).Float64() = %g (%b), %v, want %g (%b), %v; delta = %b",
533 f, f2, f2, exact, f, f, true, f2-f)
534 }
535 }
536
537
538 func delta(r *Rat, f float64) *Rat {
539 d := new(Rat).Sub(r, new(Rat).SetFloat64(f))
540 return d.Abs(d)
541 }
542
543
544
545
546 func checkIsBestApprox32(t *testing.T, f float32, r *Rat) bool {
547 if math.Abs(float64(f)) >= math.MaxFloat32 {
548
549
550 return true
551 }
552
553
554 f0 := math.Nextafter32(f, float32(math.Inf(-1)))
555 f1 := math.Nextafter32(f, float32(math.Inf(+1)))
556
557
558 df := delta(r, float64(f))
559 df0 := delta(r, float64(f0))
560 df1 := delta(r, float64(f1))
561 if df.Cmp(df0) > 0 {
562 t.Errorf("Rat(%v).Float32() = %g (%b), but previous float32 %g (%b) is closer", r, f, f, f0, f0)
563 return false
564 }
565 if df.Cmp(df1) > 0 {
566 t.Errorf("Rat(%v).Float32() = %g (%b), but next float32 %g (%b) is closer", r, f, f, f1, f1)
567 return false
568 }
569 if df.Cmp(df0) == 0 && !isEven32(f) {
570 t.Errorf("Rat(%v).Float32() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f0, f0)
571 return false
572 }
573 if df.Cmp(df1) == 0 && !isEven32(f) {
574 t.Errorf("Rat(%v).Float32() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f1, f1)
575 return false
576 }
577 return true
578 }
579
580
581
582
583 func checkIsBestApprox64(t *testing.T, f float64, r *Rat) bool {
584 if math.Abs(f) >= math.MaxFloat64 {
585
586
587 return true
588 }
589
590
591 f0 := math.Nextafter(f, math.Inf(-1))
592 f1 := math.Nextafter(f, math.Inf(+1))
593
594
595 df := delta(r, f)
596 df0 := delta(r, f0)
597 df1 := delta(r, f1)
598 if df.Cmp(df0) > 0 {
599 t.Errorf("Rat(%v).Float64() = %g (%b), but previous float64 %g (%b) is closer", r, f, f, f0, f0)
600 return false
601 }
602 if df.Cmp(df1) > 0 {
603 t.Errorf("Rat(%v).Float64() = %g (%b), but next float64 %g (%b) is closer", r, f, f, f1, f1)
604 return false
605 }
606 if df.Cmp(df0) == 0 && !isEven64(f) {
607 t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f0, f0)
608 return false
609 }
610 if df.Cmp(df1) == 0 && !isEven64(f) {
611 t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f1, f1)
612 return false
613 }
614 return true
615 }
616
617 func isEven32(f float32) bool { return math.Float32bits(f)&1 == 0 }
618 func isEven64(f float64) bool { return math.Float64bits(f)&1 == 0 }
619
620 func TestIsFinite(t *testing.T) {
621 finites := []float64{
622 1.0 / 3,
623 4891559871276714924261e+222,
624 math.MaxFloat64,
625 math.SmallestNonzeroFloat64,
626 -math.MaxFloat64,
627 -math.SmallestNonzeroFloat64,
628 }
629 for _, f := range finites {
630 if !isFinite(f) {
631 t.Errorf("!IsFinite(%g (%b))", f, f)
632 }
633 }
634 nonfinites := []float64{
635 math.NaN(),
636 math.Inf(-1),
637 math.Inf(+1),
638 }
639 for _, f := range nonfinites {
640 if isFinite(f) {
641 t.Errorf("IsFinite(%g, (%b))", f, f)
642 }
643 }
644 }
645
646 func TestRatSetInt64(t *testing.T) {
647 var testCases = []int64{
648 0,
649 1,
650 -1,
651 12345,
652 -98765,
653 math.MaxInt64,
654 math.MinInt64,
655 }
656 var r = new(Rat)
657 for i, want := range testCases {
658 r.SetInt64(want)
659 if !r.IsInt() {
660 t.Errorf("#%d: Rat.SetInt64(%d) is not an integer", i, want)
661 }
662 num := r.Num()
663 if !num.IsInt64() {
664 t.Errorf("#%d: Rat.SetInt64(%d) numerator is not an int64", i, want)
665 }
666 got := num.Int64()
667 if got != want {
668 t.Errorf("#%d: Rat.SetInt64(%d) = %d, but expected %d", i, want, got, want)
669 }
670 }
671 }
672
673 func TestRatSetUint64(t *testing.T) {
674 var testCases = []uint64{
675 0,
676 1,
677 12345,
678 ^uint64(0),
679 }
680 var r = new(Rat)
681 for i, want := range testCases {
682 r.SetUint64(want)
683 if !r.IsInt() {
684 t.Errorf("#%d: Rat.SetUint64(%d) is not an integer", i, want)
685 }
686 num := r.Num()
687 if !num.IsUint64() {
688 t.Errorf("#%d: Rat.SetUint64(%d) numerator is not a uint64", i, want)
689 }
690 got := num.Uint64()
691 if got != want {
692 t.Errorf("#%d: Rat.SetUint64(%d) = %d, but expected %d", i, want, got, want)
693 }
694 }
695 }
696
697 func BenchmarkRatCmp(b *testing.B) {
698 x, y := NewRat(4, 1), NewRat(7, 2)
699 for i := 0; i < b.N; i++ {
700 x.Cmp(y)
701 }
702 }
703
704
705
706 func TestIssue34919(t *testing.T) {
707 for _, acc := range []struct {
708 name string
709 f func(*Rat)
710 }{
711 {"Float32", func(x *Rat) { x.Float32() }},
712 {"Float64", func(x *Rat) { x.Float64() }},
713 {"Inv", func(x *Rat) { new(Rat).Inv(x) }},
714 {"Sign", func(x *Rat) { x.Sign() }},
715 {"IsInt", func(x *Rat) { x.IsInt() }},
716 {"Num", func(x *Rat) { x.Num() }},
717
718 } {
719
720
721
722 r := &Rat{Int{abs: nat{991}}, Int{abs: make(nat, 0, 1)}}
723 acc.f(r)
724 if d := r.b.abs[:1][0]; d != 0 {
725 t.Errorf("%s modified denominator: got %d, want 0", acc.name, d)
726 }
727 }
728 }
729
730 func TestDenomRace(t *testing.T) {
731 x := NewRat(1, 2)
732 const N = 3
733 c := make(chan bool, N)
734 for i := 0; i < N; i++ {
735 go func() {
736
737
738 x.Denom()
739 new(Float).SetRat(x)
740 c <- true
741 }()
742 }
743 for i := 0; i < N; i++ {
744 <-c
745 }
746 }
747
View as plain text