forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_test.go
More file actions
67 lines (63 loc) · 1.33 KB
/
float_test.go
File metadata and controls
67 lines (63 loc) · 1.33 KB
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
package math
import (
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
func TestPercentage(t *testing.T) {
testCases := []struct {
name string
numerator *big.Int
denominator *big.Int
percentage *big.Float
fail bool
}{
{
name: "positive percentage",
numerator: big.NewInt(165),
denominator: big.NewInt(1000),
percentage: big.NewFloat(16.5),
fail: false,
},
{
name: "negative percentage",
numerator: big.NewInt(-165),
denominator: big.NewInt(1000),
percentage: big.NewFloat(-16.5),
fail: false,
},
{
name: "zero denominator",
numerator: big.NewInt(1),
denominator: big.NewInt(0),
percentage: nil,
fail: true,
},
{
name: "nil numerator",
numerator: nil,
denominator: big.NewInt(1000),
percentage: nil,
fail: true,
},
{
name: "nil denominator",
numerator: big.NewInt(165),
denominator: nil,
percentage: nil,
fail: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
percentage := Percentage(tc.numerator, tc.denominator)
fmt.Printf("percentage: %v\n", percentage)
if tc.fail {
require.Nil(t, percentage)
} else {
require.True(t, percentage.Cmp(tc.percentage) == 0)
}
})
}
}