Skip to content

Commit 2fb91ca

Browse files
authored
Sync two-bucket tests (#605)
1 parent 96ebd03 commit 2fb91ca

3 files changed

Lines changed: 128 additions & 61 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
return {
2+
module_name = 'two_bucket',
3+
4+
generate_test = function(case)
5+
if case.expected.error then
6+
local template = [[
7+
assert.has_error(function()
8+
two_bucket.measure({
9+
bucket_one_capacity = %d, --
10+
bucket_two_capacity = %d, --
11+
goal_volume = %d, --
12+
start_bucket = %d --
13+
})
14+
end)]]
15+
16+
return template:format(case.input.bucketOne, case.input.bucketTwo, case.input.goal,
17+
case.input.startBucket == 'one' and 1 or 2)
18+
else
19+
local template = [[
20+
assert.are.same(two_bucket.measure({
21+
bucket_one_capacity = %d, --
22+
bucket_two_capacity = %d, --
23+
goal_volume = %d, --
24+
start_bucket = %d --
25+
}), {
26+
moves = %d, --
27+
other_bucket_volume = %d, --
28+
goal_bucket_number = %d --
29+
})]]
30+
31+
return template:format(case.input.bucketOne, case.input.bucketTwo, case.input.goal,
32+
case.input.startBucket == 'one' and 1 or 2, case.expected.moves, case.expected.otherBucket,
33+
case.expected.goalBucket == 'one' and 1 or 2)
34+
end
35+
end
36+
}

exercises/practice/two-bucket/.meta/tests.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ description = "Measure one step using bucket one of size 1 and bucket two of siz
2727
[eb329c63-5540-4735-b30b-97f7f4df0f84]
2828
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
2929

30+
[58d70152-bf2b-46bb-ad54-be58ebe94c03]
31+
description = "Measure using bucket one much bigger than bucket two"
32+
33+
[9dbe6499-caa5-4a58-b5ce-c988d71b8981]
34+
description = "Measure using bucket one much smaller than bucket two"
35+
3036
[449be72d-b10a-4f4b-a959-ca741e333b72]
3137
description = "Not possible to reach the goal"
3238

exercises/practice/two-bucket/two-bucket_spec.lua

Lines changed: 86 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,143 @@
1-
local two_bucket = require 'two-bucket'
1+
local two_bucket = require('two-bucket')
22

3-
-- LuaFormatter off
43
describe('two-bucket', function()
54
it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket one', function()
65
assert.are.same(two_bucket.measure({
7-
bucket_one_capacity = 3,
8-
bucket_two_capacity = 5,
9-
goal_volume = 1,
10-
start_bucket = 1
6+
bucket_one_capacity = 3, --
7+
bucket_two_capacity = 5, --
8+
goal_volume = 1, --
9+
start_bucket = 1 --
1110
}), {
12-
moves = 4,
13-
other_bucket_volume = 5,
14-
goal_bucket_number = 1
11+
moves = 4, --
12+
other_bucket_volume = 5, --
13+
goal_bucket_number = 1 --
1514
})
1615
end)
1716

1817
it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket two', function()
1918
assert.are.same(two_bucket.measure({
20-
bucket_one_capacity = 3,
21-
bucket_two_capacity = 5,
22-
goal_volume = 1,
23-
start_bucket = 2
19+
bucket_one_capacity = 3, --
20+
bucket_two_capacity = 5, --
21+
goal_volume = 1, --
22+
start_bucket = 2 --
2423
}), {
25-
moves = 8,
26-
other_bucket_volume = 3,
27-
goal_bucket_number = 2
24+
moves = 8, --
25+
other_bucket_volume = 3, --
26+
goal_bucket_number = 2 --
2827
})
2928
end)
3029

3130
it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket one', function()
3231
assert.are.same(two_bucket.measure({
33-
bucket_one_capacity = 7,
34-
bucket_two_capacity = 11,
35-
goal_volume = 2,
36-
start_bucket = 1
32+
bucket_one_capacity = 7, --
33+
bucket_two_capacity = 11, --
34+
goal_volume = 2, --
35+
start_bucket = 1 --
3736
}), {
38-
moves = 14,
39-
other_bucket_volume = 11,
40-
goal_bucket_number = 1
37+
moves = 14, --
38+
other_bucket_volume = 11, --
39+
goal_bucket_number = 1 --
4140
})
4241
end)
4342

4443
it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket two', function()
4544
assert.are.same(two_bucket.measure({
46-
bucket_one_capacity = 7,
47-
bucket_two_capacity = 11,
48-
goal_volume = 2,
49-
start_bucket = 2
45+
bucket_one_capacity = 7, --
46+
bucket_two_capacity = 11, --
47+
goal_volume = 2, --
48+
start_bucket = 2 --
5049
}), {
51-
moves = 18,
52-
other_bucket_volume = 7,
53-
goal_bucket_number = 2
50+
moves = 18, --
51+
other_bucket_volume = 7, --
52+
goal_bucket_number = 2 --
5453
})
5554
end)
5655

5756
it('measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two', function()
5857
assert.are.same(two_bucket.measure({
59-
bucket_one_capacity = 1,
60-
bucket_two_capacity = 3,
61-
goal_volume = 3,
62-
start_bucket = 2
58+
bucket_one_capacity = 1, --
59+
bucket_two_capacity = 3, --
60+
goal_volume = 3, --
61+
start_bucket = 2 --
6362
}), {
64-
moves = 1,
65-
other_bucket_volume = 0,
66-
goal_bucket_number = 2
63+
moves = 1, --
64+
other_bucket_volume = 0, --
65+
goal_bucket_number = 2 --
6766
})
6867
end)
6968

70-
it('measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two', function()
69+
it('measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two',
70+
function()
7171
assert.are.same(two_bucket.measure({
72-
bucket_one_capacity = 2,
73-
bucket_two_capacity = 3,
74-
goal_volume = 3,
75-
start_bucket = 1
72+
bucket_one_capacity = 2, --
73+
bucket_two_capacity = 3, --
74+
goal_volume = 3, --
75+
start_bucket = 1 --
7676
}), {
77-
moves = 2,
78-
other_bucket_volume = 2,
79-
goal_bucket_number = 2
77+
moves = 2, --
78+
other_bucket_volume = 2, --
79+
goal_bucket_number = 2 --
80+
})
81+
end)
82+
83+
it('measure using bucket one much bigger than bucket two', function()
84+
assert.are.same(two_bucket.measure({
85+
bucket_one_capacity = 5, --
86+
bucket_two_capacity = 1, --
87+
goal_volume = 2, --
88+
start_bucket = 1 --
89+
}), {
90+
moves = 6, --
91+
other_bucket_volume = 1, --
92+
goal_bucket_number = 1 --
93+
})
94+
end)
95+
96+
it('measure using bucket one much smaller than bucket two', function()
97+
assert.are.same(two_bucket.measure({
98+
bucket_one_capacity = 3, --
99+
bucket_two_capacity = 15, --
100+
goal_volume = 9, --
101+
start_bucket = 1 --
102+
}), {
103+
moves = 6, --
104+
other_bucket_volume = 0, --
105+
goal_bucket_number = 2 --
80106
})
81107
end)
82108

83109
it('not possible to reach the goal', function()
84110
assert.has_error(function()
85111
two_bucket.measure({
86-
bucket_one_capacity = 6,
87-
bucket_two_capacity = 15,
88-
goal_volume = 5,
89-
start_bucket = 1
112+
bucket_one_capacity = 6, --
113+
bucket_two_capacity = 15, --
114+
goal_volume = 5, --
115+
start_bucket = 1 --
90116
})
91117
end)
92118
end)
93119

94120
it('with the same buckets but a different goal, then it is possible', function()
95121
assert.are.same(two_bucket.measure({
96-
bucket_one_capacity = 6,
97-
bucket_two_capacity = 15,
98-
goal_volume = 9,
99-
start_bucket = 1
122+
bucket_one_capacity = 6, --
123+
bucket_two_capacity = 15, --
124+
goal_volume = 9, --
125+
start_bucket = 1 --
100126
}), {
101-
moves = 10,
102-
other_bucket_volume = 0,
103-
goal_bucket_number = 2
127+
moves = 10, --
128+
other_bucket_volume = 0, --
129+
goal_bucket_number = 2 --
104130
})
105131
end)
106132

107133
it('goal larger than both buckets is impossible', function()
108134
assert.has_error(function()
109135
two_bucket.measure({
110-
bucket_one_capacity = 5,
111-
bucket_two_capacity = 7,
112-
goal_volume = 8,
113-
start_bucket = 1
136+
bucket_one_capacity = 5, --
137+
bucket_two_capacity = 7, --
138+
goal_volume = 8, --
139+
start_bucket = 1 --
114140
})
115141
end)
116142
end)
117143
end)
118-
-- LuaFormatter on

0 commit comments

Comments
 (0)