-
-
Notifications
You must be signed in to change notification settings - Fork 654
/
two_bucket_test.go
83 lines (78 loc) · 2.2 KB
/
two_bucket_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package twobucket
import "testing"
func TestSolve(t *testing.T) {
for _, tc := range append(testCases, errorTestCases...) {
runTestCase(t, tc)
}
}
func runTestCase(t *testing.T, tc bucketTestCase) {
t.Run(tc.description, func(t *testing.T) {
g, m, other, err := Solve(tc.bucketOne, tc.bucketTwo, tc.goal, tc.startBucket)
switch {
case tc.expectedError != "":
if err == nil {
t.Fatalf("Solve(%d,%d,%d,%q) expected error, got:%q,%d,%d", tc.bucketOne, tc.bucketTwo, tc.goal, tc.startBucket, g, m, other)
}
case err != nil:
t.Fatalf("Solve(%d,%d,%d,%q) returned error: %v, want:%q,%d,%d", tc.bucketOne, tc.bucketTwo, tc.goal, tc.startBucket, err, tc.goalBucket, tc.moves, tc.otherBucket)
case g != tc.goalBucket || m != tc.moves || other != tc.otherBucket:
t.Fatalf("Solve(%d,%d,%d,%q) = %q,%d,%d, want:%q,%d,%d", tc.bucketOne, tc.bucketTwo, tc.goal, tc.startBucket, g, m, other, tc.goalBucket, tc.moves, tc.otherBucket)
}
})
}
func BenchmarkSolve(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
for _, tc := range append(testCases, errorTestCases...) {
Solve(tc.bucketOne, tc.bucketTwo, tc.goal, tc.startBucket)
}
}
}
var errorTestCases = []bucketTestCase{
{
description: "Invalid first bucket size",
bucketOne: 0,
bucketTwo: 5,
goal: 5,
startBucket: "one",
goalBucket: "one",
moves: 1,
otherBucket: 0,
expectedError: "invalid first bucket size",
},
{
description: "Invalid second bucket size",
bucketOne: 3,
bucketTwo: 0,
goal: 3,
startBucket: "one",
goalBucket: "one",
moves: 1,
otherBucket: 0,
expectedError: "invalid second bucket size",
},
{
description: "Invalid goal amount",
bucketOne: 1,
bucketTwo: 1,
goal: 0,
startBucket: "one",
goalBucket: "one",
moves: 0,
otherBucket: 1,
expectedError: "invalid goal amount",
},
{
description: "Invalid start bucket name",
bucketOne: 3,
bucketTwo: 5,
goal: 1,
startBucket: "three",
goalBucket: "one",
moves: 4,
otherBucket: 5,
expectedError: "invalid start bucket name",
},
}