Compare commits
No commits in common. "080f31017d0fff91bc49ba1a9c0055107dae47da" and "3569bcd48594d9141e713b9ca5f91a0f3c401e80" have entirely different histories.
080f31017d
...
3569bcd485
@ -7,17 +7,3 @@ func Sum(numbers []int) int {
|
|||||||
}
|
}
|
||||||
return sum
|
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,9 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSum(t *testing.T) {
|
func TestSum(t *testing.T) {
|
||||||
|
|
||||||
@ -30,33 +27,3 @@ 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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
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