Skip to content

Commit

Permalink
Add 3 new modes of calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMcKenzieSmarty committed Sep 17, 2024
1 parent db99ec1 commit 681f4fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions calc/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ type Calculator interface {
}

type Addition struct{}
type Subtraction struct{}
type Multiplication struct{}
type Division struct{}

func (Addition) Calculate(a, b int) int {
return a + b
}
func (Subtraction) Calculate(a, b int) int { return a - b }
func (Multiplication) Calculate(a, b int) int { return a * b }
func (Division) Calculate(a, b int) int { return a / b }
28 changes: 27 additions & 1 deletion calc/calc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package calc

import "testing"
import (
"testing"
)

func TestAddition_Calculate(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -36,6 +38,30 @@ func TestAddition(t *testing.T) {

}

func TestSubtraction(t *testing.T) {
var calculator Subtraction
assertEqual(t, 0, calculator.Calculate(0, 0))
assertEqual(t, 1, calculator.Calculate(0, -1))
assertEqual(t, 2, calculator.Calculate(4, 2))
assertEqual(t, -4, calculator.Calculate(0, 4))
}

func TestMultiplication(t *testing.T) {
var calculator Multiplication
assertEqual(t, 0, calculator.Calculate(0, 0))
assertEqual(t, 0, calculator.Calculate(0, -1))
assertEqual(t, 8, calculator.Calculate(4, 2))
assertEqual(t, 0, calculator.Calculate(0, 4))
}

func TestDivision(t *testing.T) {
var calculator Division
assertEqual(t, 1, calculator.Calculate(1, 1))
assertEqual(t, 0, calculator.Calculate(0, -1))
assertEqual(t, 2, calculator.Calculate(4, 2))
assertEqual(t, -4, calculator.Calculate(16, -4))
}

func assertEqual(t *testing.T, expected, actual any) {
if actual != expected {
t.Helper() //so that the stack trace shows the actual test that failed
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"reflect"
"testing"

"calc-lib/calc"
"calc-lib/handler"
calc "calc-lib/calc"
handler "calc-lib/handler"
)

func assertEqual(t *testing.T, expected, actual any) {
Expand Down

0 comments on commit 681f4fa

Please sign in to comment.