I reduced my solution of Protein Translation exercise on Zig track to this:
pub fn proteins(
allocator: std.mem.Allocator,
_: []const u8,
) Error![]Protein {
const result = try allocator.alloc(Protein, 3);
@memset(result, .lysine);
return result;
}
I got a bunch of failed tests as expected. But two tests unexpectedly complained about memory leaks:
28/31 test_protein_translation.test.Unknown amino acids, not part of a codon, can't translate...FAIL (TestExpectedError)
...
try testing.expectError(TranslationError.InvalidCodon, proteins(testing.allocator, "XYZ"));
^
[DebugAllocator] (err): memory address 0x7f8746720000 leaked:
...
29/31 test_protein_translation.test.Incomplete RNA sequence can't translate...FAIL (TestExpectedError)
...
try testing.expectError(TranslationError.InvalidCodon, proteins(testing.allocator, "AUGU"));
^
[DebugAllocator] (err): memory address 0x7f8746700000 leaked:
Tests did not take into account the happy path when they expected an error. The fix is not complicated:
test "Unknown amino acids, not part of a codon, can't translate" {
const actual = proteins(testing.allocator, "XYZ");
defer if (actual) |a| testing.allocator.free(a) else |_| {};
try testing.expectError(TranslationError.InvalidCodon, actual);
}
test "Incomplete RNA sequence can't translate" {
const actual = proteins(testing.allocator, "AUGU");
defer if (actual) |a| testing.allocator.free(a) else |_| {};
try testing.expectError(TranslationError.InvalidCodon, actual);
}