Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions exercises/practice/two-bucket/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
return {
module_name = 'two_bucket',

generate_test = function(case)
if case.expected.error then
local template = [[
assert.has_error(function()
two_bucket.measure({
bucket_one_capacity = %d, --
bucket_two_capacity = %d, --
goal_volume = %d, --
start_bucket = %d --
})
end)]]

return template:format(case.input.bucketOne, case.input.bucketTwo, case.input.goal,
case.input.startBucket == 'one' and 1 or 2)
else
local template = [[
assert.are.same(two_bucket.measure({
bucket_one_capacity = %d, --
bucket_two_capacity = %d, --
goal_volume = %d, --
start_bucket = %d --
}), {
moves = %d, --
other_bucket_volume = %d, --
goal_bucket_number = %d --
})]]

return template:format(case.input.bucketOne, case.input.bucketTwo, case.input.goal,
case.input.startBucket == 'one' and 1 or 2, case.expected.moves, case.expected.otherBucket,
case.expected.goalBucket == 'one' and 1 or 2)
end
end
}
6 changes: 6 additions & 0 deletions exercises/practice/two-bucket/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ description = "Measure one step using bucket one of size 1 and bucket two of siz
[eb329c63-5540-4735-b30b-97f7f4df0f84]
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"

[58d70152-bf2b-46bb-ad54-be58ebe94c03]
description = "Measure using bucket one much bigger than bucket two"

[9dbe6499-caa5-4a58-b5ce-c988d71b8981]
description = "Measure using bucket one much smaller than bucket two"

[449be72d-b10a-4f4b-a959-ca741e333b72]
description = "Not possible to reach the goal"

Expand Down
147 changes: 86 additions & 61 deletions exercises/practice/two-bucket/two-bucket_spec.lua
Original file line number Diff line number Diff line change
@@ -1,118 +1,143 @@
local two_bucket = require 'two-bucket'
local two_bucket = require('two-bucket')

-- LuaFormatter off
describe('two-bucket', function()
it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket one', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 3,
bucket_two_capacity = 5,
goal_volume = 1,
start_bucket = 1
bucket_one_capacity = 3, --
bucket_two_capacity = 5, --
goal_volume = 1, --
start_bucket = 1 --
Comment on lines +6 to +9
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These empty comments force favorable formatting

}), {
moves = 4,
other_bucket_volume = 5,
goal_bucket_number = 1
moves = 4, --
other_bucket_volume = 5, --
goal_bucket_number = 1 --
})
end)

it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket two', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 3,
bucket_two_capacity = 5,
goal_volume = 1,
start_bucket = 2
bucket_one_capacity = 3, --
bucket_two_capacity = 5, --
goal_volume = 1, --
start_bucket = 2 --
}), {
moves = 8,
other_bucket_volume = 3,
goal_bucket_number = 2
moves = 8, --
other_bucket_volume = 3, --
goal_bucket_number = 2 --
})
end)

it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket one', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 7,
bucket_two_capacity = 11,
goal_volume = 2,
start_bucket = 1
bucket_one_capacity = 7, --
bucket_two_capacity = 11, --
goal_volume = 2, --
start_bucket = 1 --
}), {
moves = 14,
other_bucket_volume = 11,
goal_bucket_number = 1
moves = 14, --
other_bucket_volume = 11, --
goal_bucket_number = 1 --
})
end)

it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket two', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 7,
bucket_two_capacity = 11,
goal_volume = 2,
start_bucket = 2
bucket_one_capacity = 7, --
bucket_two_capacity = 11, --
goal_volume = 2, --
start_bucket = 2 --
}), {
moves = 18,
other_bucket_volume = 7,
goal_bucket_number = 2
moves = 18, --
other_bucket_volume = 7, --
goal_bucket_number = 2 --
})
end)

it('measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 1,
bucket_two_capacity = 3,
goal_volume = 3,
start_bucket = 2
bucket_one_capacity = 1, --
bucket_two_capacity = 3, --
goal_volume = 3, --
start_bucket = 2 --
}), {
moves = 1,
other_bucket_volume = 0,
goal_bucket_number = 2
moves = 1, --
other_bucket_volume = 0, --
goal_bucket_number = 2 --
})
end)

it('measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two', function()
it('measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two',
function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 2,
bucket_two_capacity = 3,
goal_volume = 3,
start_bucket = 1
bucket_one_capacity = 2, --
bucket_two_capacity = 3, --
goal_volume = 3, --
start_bucket = 1 --
}), {
moves = 2,
other_bucket_volume = 2,
goal_bucket_number = 2
moves = 2, --
other_bucket_volume = 2, --
goal_bucket_number = 2 --
})
end)

it('measure using bucket one much bigger than bucket two', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 5, --
bucket_two_capacity = 1, --
goal_volume = 2, --
start_bucket = 1 --
}), {
moves = 6, --
other_bucket_volume = 1, --
goal_bucket_number = 1 --
})
end)

it('measure using bucket one much smaller than bucket two', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 3, --
bucket_two_capacity = 15, --
goal_volume = 9, --
start_bucket = 1 --
}), {
moves = 6, --
other_bucket_volume = 0, --
goal_bucket_number = 2 --
})
end)
Comment on lines +83 to 107
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests are new


it('not possible to reach the goal', function()
assert.has_error(function()
two_bucket.measure({
bucket_one_capacity = 6,
bucket_two_capacity = 15,
goal_volume = 5,
start_bucket = 1
bucket_one_capacity = 6, --
bucket_two_capacity = 15, --
goal_volume = 5, --
start_bucket = 1 --
})
end)
end)

it('with the same buckets but a different goal, then it is possible', function()
assert.are.same(two_bucket.measure({
bucket_one_capacity = 6,
bucket_two_capacity = 15,
goal_volume = 9,
start_bucket = 1
bucket_one_capacity = 6, --
bucket_two_capacity = 15, --
goal_volume = 9, --
start_bucket = 1 --
}), {
moves = 10,
other_bucket_volume = 0,
goal_bucket_number = 2
moves = 10, --
other_bucket_volume = 0, --
goal_bucket_number = 2 --
})
end)

it('goal larger than both buckets is impossible', function()
assert.has_error(function()
two_bucket.measure({
bucket_one_capacity = 5,
bucket_two_capacity = 7,
goal_volume = 8,
start_bucket = 1
bucket_one_capacity = 5, --
bucket_two_capacity = 7, --
goal_volume = 8, --
start_bucket = 1 --
})
end)
end)
end)
-- LuaFormatter on
Loading