-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumber_test.go
121 lines (111 loc) · 2.68 KB
/
number_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package utils_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/dollarsignteam/go-utils"
)
func TestMin(t *testing.T) {
tests := []struct {
x float64
y float64
expected float64
}{
{x: 0, y: 1, expected: 0},
{x: 10, y: 1, expected: 1},
}
for _, test := range tests {
min := utils.Min(test.x, test.y)
assert.Equal(t, test.expected, min)
}
}
func TestMax(t *testing.T) {
tests := []struct {
x float64
y float64
expected float64
}{
{x: 0, y: 1, expected: 1},
{x: 10, y: 1, expected: 10},
}
for _, test := range tests {
max := utils.Max(test.x, test.y)
assert.Equal(t, test.expected, max)
}
}
func TestMinOf(t *testing.T) {
tests := []struct {
input []float64
expected float64
}{
{input: []float64{}, expected: 0},
{input: []float64{10, 2, 4, 1, 6, 8, 2}, expected: 1},
}
for _, test := range tests {
min := utils.MinOf(test.input)
assert.Equal(t, test.expected, min)
}
}
func TestMaxOf(t *testing.T) {
tests := []struct {
input []float64
expected float64
}{
{input: []float64{}, expected: 0},
{input: []float64{1, 2, 4, 10, 6, 8, 2}, expected: 10},
}
for _, test := range tests {
max := utils.MaxOf(test.input)
assert.Equal(t, test.expected, max)
}
}
func TestRandomInt64(t *testing.T) {
tests := []struct {
min int64
max int64
expectedMin int64
expectedMax int64
}{
{min: 5, max: 10, expectedMin: 5, expectedMax: 10},
{min: 5, max: -10, expectedMin: -10, expectedMax: 5},
}
for _, test := range tests {
result := utils.RandomInt64(test.min, test.max)
assert.GreaterOrEqual(t, result, test.expectedMin)
assert.LessOrEqual(t, result, test.expectedMax)
}
}
func TestRandomFloat64(t *testing.T) {
tests := []struct {
min float64
max float64
expectedMin float64
expectedMax float64
}{
{min: 5, max: 10, expectedMin: 5, expectedMax: 10},
{min: 5, max: -10, expectedMin: -10, expectedMax: 5},
}
for _, test := range tests {
result := utils.RandomFloat64(test.min, test.max)
assert.GreaterOrEqual(t, result, test.expectedMin)
assert.LessOrEqual(t, result, test.expectedMax)
}
}
func TestParseFloat64(t *testing.T) {
tests := []struct {
input string
expected float64
}{
{input: "123.45", expected: 123.45},
{input: "0.1234", expected: 0.1234},
{input: "-123.45", expected: -123.45},
{input: "-0.1234", expected: -0.1234},
{input: "+123.45", expected: 123.45},
{input: "+0.1234", expected: 0.1234},
{input: " 9,999,999,999.99 ", expected: 9999999999.99},
{input: "message", expected: 0},
}
for _, test := range tests {
result, _ := utils.ParseFloat64(test.input)
assert.Equal(t, test.expected, result)
}
}