Skip to content

exercises(all-your-base): test: add test for global array #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2023
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