Function expectEqualSlices [src]
This function is intended to be used only in tests. When the two slices are not
equal, prints diagnostics to stderr to show exactly how they are not equal (with
the differences highlighted in red), then returns a test failure error.
The colorized output is optional and controlled by the return of std.Io.tty.detectConfig().
If your inputs are UTF-8 encoded strings, consider calling expectEqualStrings instead.
Prototype
pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) !void
Parameters
T: type
expected: []const T
actual: []const T
Source
pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) !void {
const diff_index: usize = diff_index: {
const shortest = @min(expected.len, actual.len);
var index: usize = 0;
while (index < shortest) : (index += 1) {
if (!std.meta.eql(actual[index], expected[index])) break :diff_index index;
}
break :diff_index if (expected.len == actual.len) return else shortest;
};
if (!backend_can_print) return error.TestExpectedEqual;
const stderr_w = std.debug.lockStderrWriter(&.{});
defer std.debug.unlockStderrWriter();
failEqualSlices(T, expected, actual, diff_index, stderr_w) catch {};
return error.TestExpectedEqual;
}