Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bitfield/ftl-fundamentals
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: sdoyle88/ftl-fundamentals
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Dec 15, 2020

  1. Working through Go Book

    sdoyle88 committed Dec 15, 2020
    Copy the full SHA
    d9bc6d2 View commit details

Commits on Jan 16, 2021

  1. Updated based on exercises

    sdoyle88 committed Jan 16, 2021
    Copy the full SHA
    0f9974f View commit details
Showing with 103 additions and 13 deletions.
  1. +20 −1 calculator.go
  2. +83 −12 calculator_test.go
21 changes: 20 additions & 1 deletion calculator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// Package calculator provides a library for simple calculations in Go.
package calculator

import (
"fmt"
)

// Add takes two numbers and returns the result of adding them together.
func Add(a, b float64) float64 {
return a + b
return a + b
}

// Subtract takes two numbers and returns the result of subtracting the second
// from the first.
func Subtract(a, b float64) float64 {
return b - a
}

// Multiply takes two numbers and returns the result of mulitipling the second
// from the first.
func Multiply(a, b float64) float64 {
return a * b
}

// Divide takes two numbers and returns the result of dividing the second
// from the first.
func Divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("bad inputs %f, %f: division by zero", a, b)
}
return a / b, nil
}
95 changes: 83 additions & 12 deletions calculator_test.go
Original file line number Diff line number Diff line change
@@ -7,18 +7,89 @@ import (

func TestAdd(t *testing.T) {
t.Parallel()
var want float64 = 4
got := calculator.Add(2, 2)
if want != got {
t.Errorf("want %f, got %f", want, got)
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 TestSubtract(t *testing.T) {
// t.Parallel()
// var want float64 = 2
// got := calculator.Subtract(4, 2)
// if want != got {
// t.Errorf("want %f, got %f", 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)
}
}
}