-
Notifications
You must be signed in to change notification settings - Fork 2
/
finite_test.go
90 lines (79 loc) · 1.89 KB
/
finite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package rounding
import (
"math/big"
"math/rand"
"testing"
)
func TestFinite(t *testing.T) {
test := func(x, y int64, finite bool) {
if Finite(big.NewRat(x, y)) != finite {
t.Errorf("Expected Finite(%v, %v) == %v", x, y, finite)
}
}
test(1318, 185, false)
test(66, 100, true)
test(1, 2, true)
test(-1, 2, true)
test(1, 7, false)
test(-1, 7, false)
}
func TestFinitePrec(t *testing.T) {
test := func(x, y int64, prec int) {
if actual := FinitePrec(big.NewRat(x, y)); actual != prec {
t.Errorf("Expected FinitePrec(%v, %v) == %v, not %v", x, y, prec, actual)
}
}
test(1, 16, 4)
test(-1, 16, 4)
test(1, 2, 1)
test(5, 1, 0)
}
func TestFinitePrecPanic(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Expected FinitePrec(2, 3) to panic")
}
}()
FinitePrec(big.NewRat(2, 3))
}
func TestFiniteString(t *testing.T) {
test := func(x, y int64, s string) {
if actual := FiniteString(big.NewRat(x, y)); actual != s {
t.Errorf("Expected FiniteString(%v, %v) == %#v, not %#v", x, y, s, actual)
}
}
test(1, 16, "0.0625")
test(66, 100, "0.66")
test(5, 1, "5")
}
func TestFiniteStringMin(t *testing.T) {
test := func(x, y int64, prec int, s string) {
if actual := FiniteStringMin(big.NewRat(x, y), prec); actual != s {
t.Errorf("Expected FiniteStringMin(%v, %v, %v) == %#v, not %#v", x, y, prec, s, actual)
}
}
test(60, 100, 2, "0.60")
test(5, 1, 2, "5.00")
test(-5, 1, 2, "-5.00")
}
func BenchmarkFinite(b *testing.B) {
x := new(big.Rat)
for i := 0; i < b.N; i++ {
b.StopTimer()
x.SetFrac64(rand.Int63n(200)-100, rand.Int63n(100)+1)
b.StartTimer()
Finite(x)
}
}
func BenchmarkFinitePrec(b *testing.B) {
x := new(big.Rat)
for i := 0; i < b.N; i++ {
b.StopTimer()
x.SetFrac64(rand.Int63n(200)-100, rand.Int63n(100)+1)
for !Finite(x) {
x.SetFrac64(rand.Int63n(200)-100, rand.Int63n(100)+1)
}
b.StartTimer()
FinitePrec(x)
}
}