forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank_test.go
58 lines (50 loc) · 966 Bytes
/
bank_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package bank
import (
"fmt"
"testing"
)
func TestBank(t *testing.T) {
done := make(chan struct{})
// Alice
go func() {
Deposit(200)
Withdraw(200)
fmt.Println("=", Balance())
done <- struct{}{}
}()
// Bob
go func() {
Deposit(50)
Withdraw(50)
Deposit(100)
done <- struct{}{}
}()
// Wait for both transactions.
<-done
<-done
if got, want := Balance(), 100; got != want {
t.Errorf("Balance = %d, want %d", got, want)
}
}
func TestWithdrawal(t *testing.T) {
b1 := Balance()
ok := Withdraw(50)
if !ok {
t.Errorf("ok = false, want true. balance = %d", Balance())
}
expected := b1 - 50
if b2 := Balance(); b2 != expected {
t.Errorf("balance = %d, want %d", b2, expected)
}
}
func TestWithdrawalFailsIfInsufficientFunds(t *testing.T) {
b1 := Balance()
ok := Withdraw(b1 + 1)
b2 := Balance()
if ok {
t.Errorf("ok = true, want false. balance = %d", b2)
}
if b2 != b1 {
t.Errorf("balance = %d, want %d", b2, b1)
}
}