Skip to content
Merged
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
17 changes: 17 additions & 0 deletions exercises/practice/all-your-base/test_all_your_base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ test "15-bit integer" {
try testing.expectEqualSlices(u32, &expected, actual);
}

test "empty list - second call returns different memory" {
// The `convert` doc comment says that the caller owns the returned memory.
// `convert` must always return memory that can be freed.
// Test that `convert` does not return a slice that references a global array.
const expected = [_]u32{0};
const digits = [_]u32{};
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);
actual[0] = 1; // Modify the output!
const again = try convert(testing.allocator, &digits, input_base, output_base);
defer testing.allocator.free(again);
try testing.expectEqualSlices(u32, &expected, again);
}

test "empty list" {
const expected = [_]u32{0};
const digits = [_]u32{};
Expand Down