-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrac_test.go
77 lines (71 loc) · 1.62 KB
/
frac_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
package dectofrac
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewRatI(t *testing.T) {
type testCase struct {
expected string
float float64
iterations int64
}
tests := []testCase{
{"1/4", 0.25, 4},
{"1/4", 0.25, 100},
{"2/3", 2 / 3.0, 100},
{"2/3", 0.6666, 4},
{"3331/4997", 0.6666, 5},
{"2/3", 0.6667, 3},
{"6667/10000", 0.6667, 4},
{"1/7", 1 / 7.0, 7},
{"99/101", 99 / 101.0, 5},
{"108851651149874/111050674405427", 99 / 101.0, 6},
}
for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
assert.Equal(t, test.expected, NewRatI(test.float, test.iterations).String())
})
}
}
func TestNewRatP(t *testing.T) {
type testCase struct {
expected string
float float64
precision float64
}
tests := []testCase{
{"1/4", 0.25, 1e-3},
{"1/4", 0.25, 1e-3},
{"2/3", 2 / 3.0, 1e-3},
{"2/3", 0.6666, 1e-3},
{"3333/5000", 0.6666, 1e-4},
{"2/3", 0.6667, 1e-3},
{"6667/10000", 0.6667, 1e-4},
{"1/7", 1 / 7.0, 1e-3},
{"99/101", 99 / 101.0, 1e-2},
{"99/101", 99 / 101.0, 1e-11},
{"108851651149874/111050674405427", 99 / 101.0, 1e-14},
}
for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
assert.Equal(t, test.expected, NewRatP(test.float, test.precision).String())
})
}
}
func ExampleNewRatP() {
fmt.Println(NewRatP(0.6666, 0.01).String())
fmt.Println(NewRatP(0.981, 0.001).String())
fmt.Println(NewRatP(0.75, 0.01).String())
// Output:
// 2/3
// 981/1000
// 3/4
}
func ExampleNewRatI() {
fmt.Println(NewRatI(0.6667, 3).String())
fmt.Println(NewRatI(0.6667, 4).String())
// Output:
// 2/3
// 6667/10000
}