-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_test.go
148 lines (124 loc) · 2.7 KB
/
arithmetic_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package arithmetic
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDivideIntReturnFloat(t *testing.T) {
testData := []struct {
name string
numerator int
denominator int
result float64
}{
{
"whole number (6/3 = 2)",
6,
3,
2,
},
{
"Fractions (1/3 = 0.33*)",
1,
3,
0.3333333333333333,
},
}
for _, data := range testData {
t.Run(data.name, func(t *testing.T) {
assert.Equal(t, data.result, DivideIntsReturnFloat(data.numerator, data.denominator))
})
}
}
func BenchmarkDivideIntsReturnFloat(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DivideIntsReturnFloat(1, 3)
}
}
func BenchmarkDivideIntsReturnFloatNative(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = float64(1) / float64(3)
}
}
func TestMinInt(t *testing.T) {
t.Run("min value first", func(t *testing.T) {
assert.Equal(t, -3, MinInt(-3, 2))
})
t.Run("min value last", func(t *testing.T) {
assert.Equal(t, -231, MinInt(4, -231))
})
}
func BenchmarkMinInt(b *testing.B) {
first := 31
second := 42
for i := 0; i < b.N; i++ {
_ = MinInt(first, second)
}
}
func BenchmarkMathMin(b *testing.B) {
first := 31
second := 42
for i := 0; i < b.N; i++ {
_ = math.Min(float64(first), float64(second))
}
}
func TestMinInts(t *testing.T) {
t.Run("one value", func(t *testing.T) {
assert.Equal(t, 1, MinInts(1))
})
t.Run("two values", func(t *testing.T) {
assert.Equal(t, 1, MinInts(2, 1))
})
t.Run("five values sorted", func(t *testing.T) {
assert.Equal(t, 1, MinInts(1, 5, 4, 3, 2, 1))
})
t.Run("five values unsorted", func(t *testing.T) {
assert.Equal(t, 1, MinInts(1, 4, 1, 2, 3, 5))
})
}
func BenchmarkMinInts(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = MinInts(42, 90, 30)
}
}
func TestMaxInt(t *testing.T) {
t.Run("max value first", func(t *testing.T) {
assert.Equal(t, 2, MaxInt(2, -1))
})
t.Run("max value last", func(t *testing.T) {
assert.Equal(t, 4, MaxInt(-4, 4))
})
}
func BenchmarkMaxInt(b *testing.B) {
first := 31
second := 42
for i := 0; i < b.N; i++ {
_ = MaxInt(first, second)
}
}
func BenchmarkMathMax(b *testing.B) {
first := 31
second := 42
for i := 0; i < b.N; i++ {
_ = math.Max(float64(first), float64(second))
}
}
func TestMaxInts(t *testing.T) {
t.Run("one value", func(t *testing.T) {
assert.Equal(t, 1, MaxInts(1))
})
t.Run("two values", func(t *testing.T) {
assert.Equal(t, 2, MaxInts(2, 1))
})
t.Run("five values sorted", func(t *testing.T) {
assert.Equal(t, 5, MaxInts(1, 5, 4, 3, 2, 1))
})
t.Run("five values unsorted", func(t *testing.T) {
assert.Equal(t, 5, MaxInts(1, 4, 1, 2, 3, 5))
})
}
func BenchmarkMaxInts(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = MaxInts(42, 90, 30)
}
}