first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package operator
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Add returns the sum of a and b.
|
||||
func Add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// Sub returns the difference a - b.
|
||||
func Sub(a, b int) int {
|
||||
return a - b
|
||||
}
|
||||
|
||||
// Mul returns the product of a and b.
|
||||
func Mul(a, b int) int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
// Div returns the division a / b as float64. Returns an error on division by zero.
|
||||
func Div(a, b int) (float64, error) {
|
||||
if b == 0 {
|
||||
return 0, fmt.Errorf("divide by zero")
|
||||
}
|
||||
return float64(a) / float64(b), nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package operator
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
got := Add(2, 3)
|
||||
if got != 5 {
|
||||
t.Fatalf("Add(2,3) = %d; want 5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
got := Sub(5, 3)
|
||||
if got != 2 {
|
||||
t.Fatalf("Sub(5,3) = %d; want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMul(t *testing.T) {
|
||||
got := Mul(4, 3)
|
||||
if got != 12 {
|
||||
t.Fatalf("Mul(4,3) = %d; want 12", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiv(t *testing.T) {
|
||||
got, err := Div(7, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != 3.5 {
|
||||
t.Fatalf("Div(7,2) = %v; want 3.5", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDivByZero(t *testing.T) {
|
||||
_, err := Div(1, 0)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error when dividing by zero")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user