learning_go/calculator/calculator_test.go
2025-06-30 06:58:29 +03:00

87 lines
1.6 KiB
Go
Executable File

package calculator_test
import (
"calculator"
"testing"
)
func TestAdd(t *testing.T) {
t.Parallel()
type TestCase struct {
a, b int
want int
}
testCases := []TestCase{
{a: 2, b: 2, want: 4},
{a: 5, b: 0, want: 5},
{a: 1, b: 1, want: 2},
}
for _, tc := range testCases {
got := calculator.Add(tc.a, tc.b)
if tc.want != got {
t.Errorf("Add(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}
func TestSubtract(t *testing.T) {
t.Parallel()
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 5,b: 3,want: 2},
{a: 4,b: 3,want: 1},
{a: 10,b: 3,want: 7},
}
for _,tc := range testCases {
got := calculator.Subtract(tc.a, tc.b)
if tc.want != got {
t.Errorf("Subtract(%d, %d): want %d, got %d", tc.a,tc.b,tc.want, got)
}
}
}
func TestMultiply(t *testing.T) {
t.Parallel()
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 5,b: 3,want: 15},
{a: 4,b: 3,want: 12},
{a: 10,b: 3,want: 30},
}
for _,tc := range testCases {
got := calculator.Multiply(tc.a,tc.b)
if tc.want != got {
t.Errorf("Multiply(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}
func TestDivide(t *testing.T) {
t.Parallel()
type testCase struct {
a,b int
want int
}
testCases := []testCase {
{a: 6,b: 3,want: 2},
{a: 12,b: 3,want: 4},
{a: 10,b: 2,want: 5},
}
for _,tc := range testCases {
got,err := calculator.Divide(tc.a,tc.b)
if err != nil {
t.Errorf("WTF r u doing?")
}
if tc.want != got {
t.Errorf("Multiply(%d, %d): want %d, got %d", tc.a, tc.b, tc.want, got)
}
}
}