diff --git a/exercises/practice/all-your-base/.meta/example.zig b/exercises/practice/all-your-base/.meta/example.zig index 6b87a3d5..dac584bf 100644 --- a/exercises/practice/all-your-base/.meta/example.zig +++ b/exercises/practice/all-your-base/.meta/example.zig @@ -19,16 +19,13 @@ fn toBase10(digits: []const u32, input_base: u32) u32 { } fn fromBase10(allocator: mem.Allocator, num: u32, output_base: u32) mem.Allocator.Error![]u32 { - if (num == 0) { - var res = [_]u32{0}; - return &res; - } var list = std.ArrayList(u32).init(allocator); var n = num; while (n > 0) { try list.append(n % output_base); n /= output_base; } + if (list.items.len == 0) try list.append(0); var result = try list.toOwnedSlice(); mem.reverse(u32, result); return result; diff --git a/exercises/practice/all-your-base/test_all_your_base.zig b/exercises/practice/all-your-base/test_all_your_base.zig index c73920c5..fb0854d4 100644 --- a/exercises/practice/all-your-base/test_all_your_base.zig +++ b/exercises/practice/all-your-base/test_all_your_base.zig @@ -91,6 +91,7 @@ test "empty list" { const input_base = 2; const output_base = 10; const actual = try convert(testing.allocator, &digits, input_base, output_base); + defer testing.allocator.free(actual); try testing.expectEqualSlices(u32, &expected, actual); } @@ -100,6 +101,7 @@ test "single zero" { const input_base = 10; const output_base = 2; const actual = try convert(testing.allocator, &digits, input_base, output_base); + defer testing.allocator.free(actual); try testing.expectEqualSlices(u32, &expected, actual); } @@ -109,6 +111,7 @@ test "multiple zeros" { const input_base = 10; const output_base = 2; const actual = try convert(testing.allocator, &digits, input_base, output_base); + defer testing.allocator.free(actual); try testing.expectEqualSlices(u32, &expected, actual); }