Skip to content

Commit 03f28c8

Browse files
food-chain,house,twelve-days error reporting
We allow errors to be returned from recite() This change breaks existing community solutions.
1 parent b5e825d commit 03f28c8

File tree

11 files changed

+77
-44
lines changed

11 files changed

+77
-44
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/food-chain/.meta/example.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ fn appendString(buffer: []u8, offset: *usize, str: []const u8) void {
2929
offset.* += str.len;
3030
}
3131

32-
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) []const u8 {
32+
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) ![]const u8 {
33+
std.debug.assert(start_verse >= 1);
34+
std.debug.assert(end_verse >= start_verse);
35+
std.debug.assert(end_verse <= 8);
3336
var offset: usize = 0;
3437

3538
for (start_verse..(end_verse + 1)) |verse| {

exercises/practice/food-chain/test_food_chain.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test "fly" {
1010
\\I know an old lady who swallowed a fly.
1111
\\I don't know why she swallowed the fly. Perhaps she'll die.
1212
;
13-
const actual = food_chain.recite(&buffer, 1, 1);
13+
const actual = try food_chain.recite(&buffer, 1, 1);
1414
try testing.expectEqualStrings(expected, actual);
1515
}
1616

@@ -23,7 +23,7 @@ test "spider" {
2323
\\She swallowed the spider to catch the fly.
2424
\\I don't know why she swallowed the fly. Perhaps she'll die.
2525
;
26-
const actual = food_chain.recite(&buffer, 2, 2);
26+
const actual = try food_chain.recite(&buffer, 2, 2);
2727
try testing.expectEqualStrings(expected, actual);
2828
}
2929

@@ -37,7 +37,7 @@ test "bird" {
3737
\\She swallowed the spider to catch the fly.
3838
\\I don't know why she swallowed the fly. Perhaps she'll die.
3939
;
40-
const actual = food_chain.recite(&buffer, 3, 3);
40+
const actual = try food_chain.recite(&buffer, 3, 3);
4141
try testing.expectEqualStrings(expected, actual);
4242
}
4343

@@ -52,7 +52,7 @@ test "cat" {
5252
\\She swallowed the spider to catch the fly.
5353
\\I don't know why she swallowed the fly. Perhaps she'll die.
5454
;
55-
const actual = food_chain.recite(&buffer, 4, 4);
55+
const actual = try food_chain.recite(&buffer, 4, 4);
5656
try testing.expectEqualStrings(expected, actual);
5757
}
5858

@@ -68,7 +68,7 @@ test "dog" {
6868
\\She swallowed the spider to catch the fly.
6969
\\I don't know why she swallowed the fly. Perhaps she'll die.
7070
;
71-
const actual = food_chain.recite(&buffer, 5, 5);
71+
const actual = try food_chain.recite(&buffer, 5, 5);
7272
try testing.expectEqualStrings(expected, actual);
7373
}
7474

@@ -85,7 +85,7 @@ test "goat" {
8585
\\She swallowed the spider to catch the fly.
8686
\\I don't know why she swallowed the fly. Perhaps she'll die.
8787
;
88-
const actual = food_chain.recite(&buffer, 6, 6);
88+
const actual = try food_chain.recite(&buffer, 6, 6);
8989
try testing.expectEqualStrings(expected, actual);
9090
}
9191

@@ -103,7 +103,7 @@ test "cow" {
103103
\\She swallowed the spider to catch the fly.
104104
\\I don't know why she swallowed the fly. Perhaps she'll die.
105105
;
106-
const actual = food_chain.recite(&buffer, 7, 7);
106+
const actual = try food_chain.recite(&buffer, 7, 7);
107107
try testing.expectEqualStrings(expected, actual);
108108
}
109109

@@ -114,7 +114,7 @@ test "horse" {
114114
\\I know an old lady who swallowed a horse.
115115
\\She's dead, of course!
116116
;
117-
const actual = food_chain.recite(&buffer, 8, 8);
117+
const actual = try food_chain.recite(&buffer, 8, 8);
118118
try testing.expectEqualStrings(expected, actual);
119119
}
120120

@@ -136,7 +136,7 @@ test "multiple verses" {
136136
\\She swallowed the spider to catch the fly.
137137
\\I don't know why she swallowed the fly. Perhaps she'll die.
138138
;
139-
const actual = food_chain.recite(&buffer, 1, 3);
139+
const actual = try food_chain.recite(&buffer, 1, 3);
140140
try testing.expectEqualStrings(expected, actual);
141141
}
142142

@@ -195,6 +195,6 @@ test "full song" {
195195
\\I know an old lady who swallowed a horse.
196196
\\She's dead, of course!
197197
;
198-
const actual = food_chain.recite(&buffer, 1, 8);
198+
const actual = try food_chain.recite(&buffer, 1, 8);
199199
try testing.expectEqualStrings(expected, actual);
200200
}
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/house/.meta/example.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ fn appendString(buffer: []u8, offset: *usize, str: []const u8) void {
1111
offset.* += str.len;
1212
}
1313

14-
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) []const u8 {
14+
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) ![]const u8 {
15+
std.debug.assert(start_verse >= 1);
16+
std.debug.assert(end_verse >= start_verse);
17+
std.debug.assert(end_verse <= 12);
1518
var offset: usize = 0;
1619

1720
for (start_verse..(end_verse + 1)) |verse| {

exercises/practice/house/house.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_verse: u32, end_verse: u32) []const u8 {
1+
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) ![]const u8 {
22
_ = buffer;
33
_ = start_verse;
44
_ = end_verse;

exercises/practice/house/test_house.zig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test "verse one - the house that jack built" {
99
const expected: []const u8 =
1010
\\This is the house that Jack built.
1111
;
12-
const actual = house.recite(&buffer, 1, 1);
12+
const actual = try house.recite(&buffer, 1, 1);
1313
try testing.expectEqualStrings(expected, actual);
1414
}
1515

@@ -19,7 +19,7 @@ test "verse two - the malt that lay" {
1919
const expected: []const u8 =
2020
\\This is the malt that lay in the house that Jack built.
2121
;
22-
const actual = house.recite(&buffer, 2, 2);
22+
const actual = try house.recite(&buffer, 2, 2);
2323
try testing.expectEqualStrings(expected, actual);
2424
}
2525

@@ -29,7 +29,7 @@ test "verse three - the rat that ate" {
2929
const expected: []const u8 =
3030
\\This is the rat that ate the malt that lay in the house that Jack built.
3131
;
32-
const actual = house.recite(&buffer, 3, 3);
32+
const actual = try house.recite(&buffer, 3, 3);
3333
try testing.expectEqualStrings(expected, actual);
3434
}
3535

@@ -39,7 +39,7 @@ test "verse four - the cat that killed" {
3939
const expected: []const u8 =
4040
\\This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
4141
;
42-
const actual = house.recite(&buffer, 4, 4);
42+
const actual = try house.recite(&buffer, 4, 4);
4343
try testing.expectEqualStrings(expected, actual);
4444
}
4545

@@ -49,7 +49,7 @@ test "verse five - the dog that worried" {
4949
const expected: []const u8 =
5050
\\This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
5151
;
52-
const actual = house.recite(&buffer, 5, 5);
52+
const actual = try house.recite(&buffer, 5, 5);
5353
try testing.expectEqualStrings(expected, actual);
5454
}
5555

@@ -59,7 +59,7 @@ test "verse six - the cow with the crumpled horn" {
5959
const expected: []const u8 =
6060
\\This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
6161
;
62-
const actual = house.recite(&buffer, 6, 6);
62+
const actual = try house.recite(&buffer, 6, 6);
6363
try testing.expectEqualStrings(expected, actual);
6464
}
6565

@@ -69,7 +69,7 @@ test "verse seven - the maiden all forlorn" {
6969
const expected: []const u8 =
7070
\\This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
7171
;
72-
const actual = house.recite(&buffer, 7, 7);
72+
const actual = try house.recite(&buffer, 7, 7);
7373
try testing.expectEqualStrings(expected, actual);
7474
}
7575

@@ -79,7 +79,7 @@ test "verse eight - the man all tattered and torn" {
7979
const expected: []const u8 =
8080
\\This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
8181
;
82-
const actual = house.recite(&buffer, 8, 8);
82+
const actual = try house.recite(&buffer, 8, 8);
8383
try testing.expectEqualStrings(expected, actual);
8484
}
8585

@@ -89,7 +89,7 @@ test "verse nine - the priest all shaven and shorn" {
8989
const expected: []const u8 =
9090
\\This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
9191
;
92-
const actual = house.recite(&buffer, 9, 9);
92+
const actual = try house.recite(&buffer, 9, 9);
9393
try testing.expectEqualStrings(expected, actual);
9494
}
9595

@@ -99,7 +99,7 @@ test "verse 10 - the rooster that crowed in the morn" {
9999
const expected: []const u8 =
100100
\\This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
101101
;
102-
const actual = house.recite(&buffer, 10, 10);
102+
const actual = try house.recite(&buffer, 10, 10);
103103
try testing.expectEqualStrings(expected, actual);
104104
}
105105

@@ -109,7 +109,7 @@ test "verse 11 - the farmer sowing his corn" {
109109
const expected: []const u8 =
110110
\\This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
111111
;
112-
const actual = house.recite(&buffer, 11, 11);
112+
const actual = try house.recite(&buffer, 11, 11);
113113
try testing.expectEqualStrings(expected, actual);
114114
}
115115

@@ -119,7 +119,7 @@ test "verse 12 - the horse and the hound and the horn" {
119119
const expected: []const u8 =
120120
\\This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
121121
;
122-
const actual = house.recite(&buffer, 12, 12);
122+
const actual = try house.recite(&buffer, 12, 12);
123123
try testing.expectEqualStrings(expected, actual);
124124
}
125125

@@ -133,7 +133,7 @@ test "multiple verses" {
133133
\\This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
134134
\\This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
135135
;
136-
const actual = house.recite(&buffer, 4, 8);
136+
const actual = try house.recite(&buffer, 4, 8);
137137
try testing.expectEqualStrings(expected, actual);
138138
}
139139

@@ -154,6 +154,6 @@ test "full rhyme" {
154154
\\This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
155155
\\This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
156156
;
157-
const actual = house.recite(&buffer, 1, 12);
157+
const actual = try house.recite(&buffer, 1, 12);
158158
try testing.expectEqualStrings(expected, actual);
159159
}
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/twelve-days/.meta/example.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ fn appendString(buffer: []u8, offset: *usize, str: []const u8) void {
1515
offset.* += str.len;
1616
}
1717

18-
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) []const u8 {
18+
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) ![]const u8 {
19+
std.debug.assert(start_verse >= 1);
20+
std.debug.assert(end_verse >= start_verse);
21+
std.debug.assert(end_verse <= 12);
1922
var offset: usize = 0;
2023

2124
for (start_verse..(end_verse + 1)) |verse| {

0 commit comments

Comments
 (0)