Compare commits
3 Commits
3569bcd485
...
080f31017d
Author | SHA1 | Date | |
---|---|---|---|
080f31017d | |||
a6a921cedc | |||
01e7a6fa26 |
@ -7,3 +7,17 @@ func Sum(numbers []int) int {
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func SumAllTails(numbersToSum ...[]int) []int {
|
||||
sums := make([]int, 0, len(numbersToSum))
|
||||
|
||||
for _, numbers := range numbersToSum {
|
||||
if len(numbers) == 0 {
|
||||
sums = append(sums, 0)
|
||||
} else {
|
||||
tail := numbers[1:]
|
||||
sums = append(sums, Sum(tail))
|
||||
}
|
||||
}
|
||||
return sums
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
|
||||
@ -27,3 +30,33 @@ func TestSum(t *testing.T) {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
//func TestSumAll(t *testing.T) {
|
||||
//got := SumAll([]int{1, 2}, []int{0, 9})
|
||||
//want := []int{3, 9}
|
||||
|
||||
//if !reflect.DeepEqual(got, want) {
|
||||
//t.Errorf("got %v want %v", got, want)
|
||||
//}
|
||||
//}
|
||||
|
||||
func TestSumAllTails(t *testing.T) {
|
||||
checkSums := func(t testing.TB, got, want []int) {
|
||||
t.Helper()
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got %v want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("make the sums of tails of", func(t *testing.T) {
|
||||
got := SumAllTails([]int{1, 2}, []int{0, 9})
|
||||
want := []int{2, 9}
|
||||
checkSums(t, got, want)
|
||||
})
|
||||
|
||||
t.Run("safely sum empty slices", func(t *testing.T) {
|
||||
got := SumAllTails([]int{}, []int{3, 4, 5})
|
||||
want := []int{0, 9}
|
||||
checkSums(t, got, want)
|
||||
})
|
||||
}
|
||||
|
35
pointers/wallet.go
Normal file
35
pointers/wallet.go
Normal file
@ -0,0 +1,35 @@
|
||||
package pointers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Bitcoin int
|
||||
|
||||
type Wallet struct {
|
||||
balance Bitcoin
|
||||
}
|
||||
|
||||
func (w *Wallet) Deposit(a Bitcoin) {
|
||||
w.balance += a
|
||||
}
|
||||
|
||||
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
|
||||
|
||||
func (w *Wallet) Withdraw(a Bitcoin) error {
|
||||
if a > w.balance {
|
||||
return ErrInsufficientFunds
|
||||
}
|
||||
|
||||
w.balance -= a
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w Wallet) Balance() Bitcoin {
|
||||
return w.balance
|
||||
}
|
||||
|
||||
func (b Bitcoin) String() string {
|
||||
return fmt.Sprintf("%d BTC", b)
|
||||
}
|
52
pointers/wallet_test.go
Normal file
52
pointers/wallet_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package pointers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWallet(t *testing.T) {
|
||||
assertBalance := func(t testing.TB, w Wallet, want Bitcoin) {
|
||||
t.Helper()
|
||||
balance := w.Balance()
|
||||
|
||||
if balance != want {
|
||||
t.Errorf("got %s want %s", balance, want)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("Deposit", func(t *testing.T) {
|
||||
wallet := Wallet{}
|
||||
wallet.Deposit(Bitcoin(10))
|
||||
want := Bitcoin(10)
|
||||
|
||||
assertBalance(t, wallet, want)
|
||||
})
|
||||
|
||||
t.Run("Withdraw", func(t *testing.T) {
|
||||
wallet := Wallet{balance: Bitcoin(20)}
|
||||
wallet.Withdraw(Bitcoin(10))
|
||||
|
||||
want := Bitcoin(10)
|
||||
assertBalance(t, wallet, want)
|
||||
})
|
||||
|
||||
assertError := func(t testing.TB, got, want error) {
|
||||
t.Helper()
|
||||
|
||||
if got == nil {
|
||||
t.Fatal("wanted an error but didn't get one")
|
||||
}
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("Withdraw insufficient funds", func(t *testing.T) {
|
||||
startingBalance := Bitcoin(20)
|
||||
wallet := Wallet{startingBalance}
|
||||
err := wallet.Withdraw(Bitcoin(100))
|
||||
assertError(t, err, ErrInsufficientFunds)
|
||||
assertBalance(t, wallet, startingBalance)
|
||||
})
|
||||
}
|
37
structs/shapes.go
Normal file
37
structs/shapes.go
Normal file
@ -0,0 +1,37 @@
|
||||
package structs
|
||||
|
||||
import "math"
|
||||
|
||||
type Shape interface {
|
||||
Area() float64
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
Width float64
|
||||
Height float64
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
Radius float64
|
||||
}
|
||||
|
||||
type Triangle struct {
|
||||
Base float64
|
||||
Height float64
|
||||
}
|
||||
|
||||
func Perimiter(r Rectangle) float64 {
|
||||
return 2 * (r.Width + r.Height)
|
||||
}
|
||||
|
||||
func (c Circle) Area() float64 {
|
||||
return math.Pi * c.Radius * c.Radius
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() float64 {
|
||||
return r.Width * r.Height
|
||||
}
|
||||
|
||||
func (t Triangle) Area() float64 {
|
||||
return t.Base * t.Height / 2
|
||||
}
|
38
structs/shapes_test.go
Normal file
38
structs/shapes_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package structs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPerimiter(t *testing.T) {
|
||||
rectangle := Rectangle{10.0, 10.0}
|
||||
got := Perimiter(rectangle)
|
||||
want := 40.0
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %.2f want %.2f", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArea(t *testing.T) {
|
||||
|
||||
areaTests := []struct {
|
||||
name string
|
||||
shape Shape
|
||||
hasArea float64
|
||||
}{
|
||||
{name: "Rectangle", shape: Rectangle{Width: 12, Height: 6}, hasArea: 72.0},
|
||||
{name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793},
|
||||
{name: "Triangle", shape: Triangle{Base: 12, Height: 6}, hasArea: 36.0},
|
||||
}
|
||||
|
||||
for _, tt := range areaTests {
|
||||
// using tt.name from the case to use it as the `t.Run` test name
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.shape.Area()
|
||||
if got != tt.hasArea {
|
||||
t.Errorf("%#v got %g want %g", tt.shape, got, tt.hasArea)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user