141 lines
1.9 KiB
Go
141 lines
1.9 KiB
Go
package calculator
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type testData struct {
|
|
want float64
|
|
a float64
|
|
b float64
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
td := []testData{
|
|
testData{
|
|
want: 4,
|
|
a: 2,
|
|
b: 2,
|
|
},
|
|
testData{
|
|
want: 2,
|
|
a: 1,
|
|
b: 1,
|
|
},
|
|
testData{
|
|
want: 3,
|
|
a: 1,
|
|
b: 2,
|
|
},
|
|
testData{
|
|
want: 0,
|
|
a: -1,
|
|
b: 1,
|
|
},
|
|
testData{
|
|
want: -1.5,
|
|
a: -1,
|
|
b: -.5,
|
|
},
|
|
}
|
|
for _, data := range td {
|
|
t.Run(fmt.Sprintf("(a:%f,b:%f)->want:%f", data.a, data.b, data.want), func(t *testing.T) {
|
|
got := Add(data.a, data.b)
|
|
if data.want != got {
|
|
t.Errorf("want %f, got %f", data.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSubtract(t *testing.T) {
|
|
t.Parallel()
|
|
td := []testData{
|
|
{
|
|
want: -2,
|
|
a: 4,
|
|
b: 2,
|
|
},
|
|
{
|
|
want: 2,
|
|
a: 2,
|
|
b: 4,
|
|
},
|
|
{
|
|
want: 0,
|
|
a: 4,
|
|
b: 4,
|
|
},
|
|
{
|
|
want: 1.5,
|
|
a: 1,
|
|
b: 2.5,
|
|
},
|
|
{
|
|
want: -1.5,
|
|
a: -1,
|
|
b: -2.5,
|
|
},
|
|
}
|
|
for _, data := range td {
|
|
t.Run(fmt.Sprintf("(a:%f,b:%f)->want:%f", data.a, data.b, data.want), func(t *testing.T) {
|
|
got := Subtract(data.a, data.b)
|
|
if data.want != got {
|
|
t.Errorf("want %f, got %f", data.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMultiply(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
want := 8.
|
|
got := Multiply(2, 4)
|
|
if want != got {
|
|
t.Errorf("want %f, got %f", want, got)
|
|
}
|
|
}
|
|
|
|
func TestMultiplyOverflows(t *testing.T) {
|
|
t.Parallel()
|
|
td := []testData{
|
|
{
|
|
want: 2,
|
|
a: 1,
|
|
b: 2,
|
|
},
|
|
{
|
|
want: 8,
|
|
a: 2,
|
|
b: 4,
|
|
},
|
|
{
|
|
want: -8,
|
|
a: -2,
|
|
b: 4,
|
|
},
|
|
{
|
|
want: 1.5,
|
|
a: 0.5,
|
|
b: 3,
|
|
},
|
|
{
|
|
want: 0,
|
|
a: 8,
|
|
b: 0,
|
|
},
|
|
}
|
|
for _, data := range td {
|
|
t.Run(fmt.Sprintf("(a:%f,b:%f)->want:%f", data.a, data.b, data.want), func(t *testing.T) {
|
|
got := Multiply(data.a, data.b)
|
|
if data.want != got {
|
|
t.Errorf("want %f, got %f", data.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|