forked from bitfield/ftl-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_test.go
95 lines (81 loc) · 1.83 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
package calculator_test
import (
"calculator"
"testing"
)
func TestAdd(t *testing.T) {
t.Parallel()
type testCase struct {
a, b float64
want float64
}
testCases := []testCase{
{a: 2, b: 2, want: 4},
{a: 2, b: 3, want: 5},
{a: 5, b: 5, want: 10},
}
for _, tc := range testCases {
got := calculator.Add(tc.a, tc.b)
if tc.want != got {
t.Errorf("Add(%f,%f): want %f, got %f", tc.a, tc.b, tc.want, got)
}
}
}
func TestSubtract(t *testing.T) {
t.Parallel()
type testCase struct {
a, b float64
want float64
}
testCases := []testCase{
{a: 2, b: 2, want: 0},
{a: 2, b: 3, want: -1},
{a: 5, b: 3, want: 2},
}
for _, tc := range testCases {
got := calculator.Subtract(tc.b, tc.a)
if tc.want != got {
t.Errorf("Subtract(%f,%f): want %f, got %f", tc.b, tc.a, tc.want, got)
}
}
}
func TestMultiply(t *testing.T) {
t.Parallel()
type testCase struct {
a, b float64
want float64
}
testCases := []testCase{
{a: 2, b: 2, want: 4},
{a: 2, b: 3, want: 6},
{a: 5, b: 3, want: 15},
}
for _, tc := range testCases {
got := calculator.Multiply(tc.a, tc.b)
if tc.want != got {
t.Errorf("Multiply(%f,%f): want %f, got %f", tc.a, tc.b, tc.want, got)
}
}
}
func TestDivide(t *testing.T) {
t.Parallel()
type testCase struct {
a, b float64
want float64
errExpected bool
}
testCases := []testCase{
{a: 2, b: 2, want: 1, errExpected: false},
{a: 2, b: 0, want: 0, errExpected: true},
{a: 6, b: 3, want: 2, errExpected: false},
}
for _, tc := range testCases {
got, err := calculator.Divide(tc.a, tc.b)
errReceived := err != nil
if tc.errExpected != errReceived {
t.Fatalf("Divide (%f,%f): unexpected error status: %v", tc.a, tc.b, errReceived)
} else if !tc.errExpected && tc.want != got {
t.Errorf("Divide(%f,%f): want %f, got %f", tc.a, tc.b, tc.want, got)
}
}
}