Skip to content

Commit 46367ff

Browse files
bottle-song error reporting (#505)
We allow errors to be returned from recite() This change breaks existing community solutions.
1 parent 1ef7775 commit 46367ff

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Instructions append
2+
3+
## Detect invalid input
4+
5+
Use [std.debug.assert][assert] or return an [error][error].
6+
7+
[error]: https://ziglang.org/documentation/master/#Errors
8+
[assert]: https://ziglang.org/documentation/master/std/#std.debug.assert

exercises/practice/bottle-song/.meta/example.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ fn appendLine(buffer: []u8, offset: *usize, num_bottles: u32) void {
3030
appendString(buffer, offset, hanging_on);
3131
}
3232

33-
pub fn recite(buffer: []u8, start_bottles: u32, take_down: u32) []const u8 {
33+
pub fn recite(buffer: []u8, start_bottles: u32, take_down: u32) ![]const u8 {
34+
std.debug.assert(1 <= take_down);
35+
std.debug.assert(take_down <= start_bottles);
36+
std.debug.assert(start_bottles <= 10);
3437
var offset: usize = 0;
3538

3639
var num_bottles = start_bottles;

exercises/practice/bottle-song/bottle_song.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub fn recite(buffer: []u8, start_bottles: u32, take_down: u32) []const u8 {
1+
pub fn recite(buffer: []u8, start_bottles: u32, take_down: u32) ![]const u8 {
22
_ = buffer;
33
_ = start_bottles;
44
_ = take_down;

exercises/practice/bottle-song/test_bottle_song.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test "verse-single verse-first generic verse" {
1212
\\And if one green bottle should accidentally fall,
1313
\\There'll be nine green bottles hanging on the wall.
1414
;
15-
const actual = bottle_song.recite(&buffer, 10, 1);
15+
const actual = try bottle_song.recite(&buffer, 10, 1);
1616
try testing.expectEqualStrings(expected, actual);
1717
}
1818

@@ -25,7 +25,7 @@ test "verse-single verse-last generic verse" {
2525
\\And if one green bottle should accidentally fall,
2626
\\There'll be two green bottles hanging on the wall.
2727
;
28-
const actual = bottle_song.recite(&buffer, 3, 1);
28+
const actual = try bottle_song.recite(&buffer, 3, 1);
2929
try testing.expectEqualStrings(expected, actual);
3030
}
3131

@@ -38,7 +38,7 @@ test "verse-single verse-verse with 2 bottles" {
3838
\\And if one green bottle should accidentally fall,
3939
\\There'll be one green bottle hanging on the wall.
4040
;
41-
const actual = bottle_song.recite(&buffer, 2, 1);
41+
const actual = try bottle_song.recite(&buffer, 2, 1);
4242
try testing.expectEqualStrings(expected, actual);
4343
}
4444

@@ -51,7 +51,7 @@ test "verse-single verse-verse with 1 bottle" {
5151
\\And if one green bottle should accidentally fall,
5252
\\There'll be no green bottles hanging on the wall.
5353
;
54-
const actual = bottle_song.recite(&buffer, 1, 1);
54+
const actual = try bottle_song.recite(&buffer, 1, 1);
5555
try testing.expectEqualStrings(expected, actual);
5656
}
5757

@@ -69,7 +69,7 @@ test "lyrics-multiple verses-first two verses" {
6969
\\And if one green bottle should accidentally fall,
7070
\\There'll be eight green bottles hanging on the wall.
7171
;
72-
const actual = bottle_song.recite(&buffer, 10, 2);
72+
const actual = try bottle_song.recite(&buffer, 10, 2);
7373
try testing.expectEqualStrings(expected, actual);
7474
}
7575

@@ -92,7 +92,7 @@ test "lyrics-multiple verses-last three verses" {
9292
\\And if one green bottle should accidentally fall,
9393
\\There'll be no green bottles hanging on the wall.
9494
;
95-
const actual = bottle_song.recite(&buffer, 3, 3);
95+
const actual = try bottle_song.recite(&buffer, 3, 3);
9696
try testing.expectEqualStrings(expected, actual);
9797
}
9898

@@ -150,6 +150,6 @@ test "lyrics-multiple verses-all verses" {
150150
\\And if one green bottle should accidentally fall,
151151
\\There'll be no green bottles hanging on the wall.
152152
;
153-
const actual = bottle_song.recite(&buffer, 10, 10);
153+
const actual = try bottle_song.recite(&buffer, 10, 10);
154154
try testing.expectEqualStrings(expected, actual);
155155
}

0 commit comments

Comments
 (0)