Skip to content

Commit adc485b

Browse files
Series returned fixed-length slices (#520)
The slice length is specified as part of the type. The stub already used the desired return type, but the tests and example solution did not.
1 parent 92a9981 commit adc485b

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

exercises/practice/series/.meta/example.zig

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
const std = @import("std");
2-
const fmt = std.fmt;
32
const mem = std.mem;
43

5-
fn free(allocator: mem.Allocator, array_list: *std.ArrayList([]u8)) void {
6-
for (array_list.items) |item| {
7-
allocator.free(item);
8-
}
9-
array_list.deinit(allocator);
10-
}
11-
12-
pub fn slices(comptime slice_length: usize, allocator: mem.Allocator, series: []const u8) mem.Allocator.Error![][]u8 {
4+
pub fn slices(comptime slice_length: usize, allocator: mem.Allocator, series: []const u8) mem.Allocator.Error![][slice_length]u8 {
135
comptime {
146
std.debug.assert(slice_length > 0);
157
}
168

17-
var array_list = std.ArrayList([]u8).empty;
18-
errdefer free(allocator, &array_list);
9+
var array_list = std.ArrayList([slice_length]u8).empty;
10+
errdefer array_list.deinit(allocator);
1911
if (slice_length <= series.len) {
2012
for (slice_length..(series.len + 1)) |i| {
21-
try array_list.append(allocator, try fmt.allocPrint(allocator, "{s}", .{series[(i - slice_length)..i]}));
13+
var slice: [slice_length]u8 = undefined;
14+
@memcpy(slice[0..], series[(i - slice_length)..i]);
15+
try array_list.append(allocator, slice);
2216
}
2317
}
2418
return array_list.toOwnedSlice(allocator);

exercises/practice/series/test_series.zig

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,115 @@ const testing = std.testing;
33

44
const slices = @import("series.zig").slices;
55

6-
fn free(arr: [][]u8) void {
7-
for (arr) |s| {
8-
testing.allocator.free(s);
9-
}
10-
testing.allocator.free(arr);
11-
}
12-
136
test "slices of one from one" {
147
const series = "1";
15-
const expected = [_][]const u8{"1"};
8+
const expected = [_][1]u8{
9+
"1"[0..1].*, //
10+
};
1611
const actual = try slices(1, testing.allocator, series);
17-
defer free(actual);
12+
defer testing.allocator.free(actual);
1813
try testing.expectEqual(expected.len, actual.len);
1914
for (expected, 0..) |expected_slice, i| {
20-
try testing.expectEqualStrings(expected_slice, actual[i]);
15+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
2116
}
2217
}
2318

2419
test "slices of one from two" {
2520
const series = "12";
26-
const expected = [_][]const u8{ "1", "2" };
21+
const expected = [_][1]u8{
22+
"1"[0..1].*, //
23+
"2"[0..1].*, //
24+
};
2725
const actual = try slices(1, testing.allocator, series);
28-
defer free(actual);
26+
defer testing.allocator.free(actual);
2927
try testing.expectEqual(expected.len, actual.len);
3028
for (expected, 0..) |expected_slice, i| {
31-
try testing.expectEqualStrings(expected_slice, actual[i]);
29+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
3230
}
3331
}
3432

3533
test "slices of two" {
3634
const series = "35";
37-
const expected = [_][]const u8{"35"};
35+
const expected = [_][2]u8{
36+
"35"[0..2].*, //
37+
};
3838
const actual = try slices(2, testing.allocator, series);
39-
defer free(actual);
39+
defer testing.allocator.free(actual);
4040
try testing.expectEqual(expected.len, actual.len);
4141
for (expected, 0..) |expected_slice, i| {
42-
try testing.expectEqualStrings(expected_slice, actual[i]);
42+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
4343
}
4444
}
4545

4646
test "slices of two overlap" {
4747
const series = "9142";
48-
const expected = [_][]const u8{ "91", "14", "42" };
48+
const expected = [_][2]u8{
49+
"91"[0..2].*, //
50+
"14"[0..2].*, //
51+
"42"[0..2].*, //
52+
};
4953
const actual = try slices(2, testing.allocator, series);
50-
defer free(actual);
54+
defer testing.allocator.free(actual);
5155
try testing.expectEqual(expected.len, actual.len);
5256
for (expected, 0..) |expected_slice, i| {
53-
try testing.expectEqualStrings(expected_slice, actual[i]);
57+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
5458
}
5559
}
5660

5761
test "slices can include duplicates" {
5862
const series = "777777";
59-
const expected = [_][]const u8{ "777", "777", "777", "777" };
63+
const expected = [_][3]u8{
64+
"777"[0..3].*, //
65+
"777"[0..3].*, //
66+
"777"[0..3].*, //
67+
"777"[0..3].*, //
68+
};
6069
const actual = try slices(3, testing.allocator, series);
61-
defer free(actual);
70+
defer testing.allocator.free(actual);
6271
try testing.expectEqual(expected.len, actual.len);
6372
for (expected, 0..) |expected_slice, i| {
64-
try testing.expectEqualStrings(expected_slice, actual[i]);
73+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
6574
}
6675
}
6776

6877
test "slices of a long series" {
6978
const series = "918493904243";
70-
const expected = [_][]const u8{ "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243" };
79+
const expected = [_][5]u8{
80+
"91849"[0..5].*, //
81+
"18493"[0..5].*, //
82+
"84939"[0..5].*, //
83+
"49390"[0..5].*, //
84+
"93904"[0..5].*, //
85+
"39042"[0..5].*, //
86+
"90424"[0..5].*, //
87+
"04243"[0..5].*, //
88+
};
7189
const actual = try slices(5, testing.allocator, series);
72-
defer free(actual);
90+
defer testing.allocator.free(actual);
7391
try testing.expectEqual(expected.len, actual.len);
7492
for (expected, 0..) |expected_slice, i| {
75-
try testing.expectEqualStrings(expected_slice, actual[i]);
93+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
7694
}
7795
}
7896

7997
test "slice length is too large" {
8098
const series = "12345";
81-
const expected = [_][]const u8{};
99+
const expected = [_][6]u8{};
82100
const actual = try slices(6, testing.allocator, series);
83-
defer free(actual);
101+
defer testing.allocator.free(actual);
84102
try testing.expectEqual(expected.len, actual.len);
85103
for (expected, 0..) |expected_slice, i| {
86-
try testing.expectEqualStrings(expected_slice, actual[i]);
104+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
87105
}
88106
}
89107

90108
test "slice length is way too large" {
91109
const series = "12345";
92-
const expected = [_][]const u8{};
110+
const expected = [_][42]u8{};
93111
const actual = try slices(42, testing.allocator, series);
94-
defer free(actual);
112+
defer testing.allocator.free(actual);
95113
try testing.expectEqual(expected.len, actual.len);
96114
for (expected, 0..) |expected_slice, i| {
97-
try testing.expectEqualStrings(expected_slice, actual[i]);
115+
try testing.expectEqualStrings(&expected_slice, &actual[i]);
98116
}
99117
}

0 commit comments

Comments
 (0)