Add a FailingAllocator tests for exercises with allocators

The standard library contains a FailingAllocator type (std.testing.FailingAllocator). It allows you to test that code properly handles failed allocations by failing after a set number of allocations. Here’s an example of how it works pulled from a mentoring session I had with @elshize on the proverb exercise:

test "cleanup" {
    var failing_allocator = std.testing.FailingAllocator.init(std.testing.allocator, 3);

    const input_array = [_][]const u8{ "nail", "shoe", "horse" };

    try std.testing.expectError(error.OutOfMemory, recite(failing_allocator.allocator(), &input_array));
}

This does make assumptions about the use of the allocator in the exercise, which may be an issue.

1 Like