-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_test.go
136 lines (126 loc) · 2.92 KB
/
calculator_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
package calculator_test
import (
"calculator"
"math"
"testing"
)
func TestManyAdd(t *testing.T) {
t.Parallel()
// var want float64 = 4
type testCase struct {
inputs []float64
want float64
}
testCases := []testCase{
{inputs: []float64{}, want: 0},
{inputs: []float64{2, 2, 2}, want: 6},
{inputs: []float64{5, 0, 5, 5}, want: 15},
}
for _, tc := range testCases {
got := calculator.AddMany(tc.inputs...)
if tc.want != got {
t.Errorf("AddMany(%f): want %f, got %f",
tc.inputs, tc.want, got)
}
}
}
func TestSubstractMany(t *testing.T) {
t.Parallel()
type testCase struct {
inputs []float64
want float64
}
testCases := []testCase{
{inputs: []float64{}, want: 0},
{inputs: []float64{3, 4, 5}, want: -6},
{inputs: []float64{10, 1, 2, 3}, want: 4},
}
for _, tc := range testCases {
got := calculator.SubstractMany(tc.inputs...)
if tc.want != got {
t.Errorf("SubstractMany %f): want %f, got %f",
tc.inputs, tc.want, got)
}
}
}
func TestMutiplyMany(t *testing.T) {
t.Parallel()
type testCase struct {
inputs []float64
want float64
}
testCases := []testCase{
{inputs: []float64{}, want: 0},
{inputs: []float64{2, 2, 2}, want: 8},
{inputs: []float64{10, -5, 2}, want: -100},
}
for _, tc := range testCases {
got := calculator.MultiplyMany(tc.inputs...)
if tc.want != got {
t.Errorf("Multiply %f): want %f, got %f",
tc.inputs, tc.want, got)
}
}
}
func TestDivideMany(t *testing.T) {
t.Parallel()
// var want float64 = 4
type testCase struct {
inputs []float64
want float64
}
testCases := []testCase{
{inputs: []float64{4, 2}, want: 2},
{inputs: []float64{10, 2, 5}, want: 1},
{inputs: []float64{1000, 10, 10}, want: 10},
}
for _, tc := range testCases {
got, err := calculator.DivideMany(tc.inputs...)
if err != nil {
t.Fatalf("Divide (%f) want no error for valid input, got %v", tc.inputs, err)
}
if tc.want != got {
t.Errorf("Divide %f): want %f, got %f",
tc.inputs, tc.want, got)
}
}
}
func TestDivideInvalid(t *testing.T) {
t.Parallel()
_, err := calculator.DivideMany(10, 5, 0, 7)
if err == nil {
t.Error("Divide(10, 5, 0, 7): want error for invalid input, got nil")
}
}
func TestSqrt(t *testing.T) {
t.Parallel()
type testCase struct {
a float64
want float64
}
testCases := []testCase{
{a: 4, want: 2},
{a: 9, want: 3},
{a: 3, want: 1.73},
}
for _, tc := range testCases {
got, err := calculator.Sqrt(tc.a)
if err != nil {
t.Fatalf("Sqrt (%f) want no error for valid input, got %v", tc.a, err)
}
if !closeEnough(tc.want, got, 0.01) {
t.Errorf("Sqrt(%f): want %f, got %f", tc.a, tc.want, got)
}
}
}
func TestSqrtInvalid(t *testing.T) {
t.Parallel()
// We use _ because we don't need the a value here
_, err := calculator.Sqrt(-2)
if err == nil {
t.Error("Sqrt(-2): want error for invalid input, got nil")
}
}
func closeEnough(a, b, tolerance float64) bool {
return math.Abs(a-b) <= tolerance
}