expectEqualSlices
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.
Function parameters
Parameters
- T:type
- expected:[]const T
- actual:[]const T
Type definitions in this namespace
Types
- Reader
- A `std.Io.Reader` that writes a predetermined list of buffers during `stream`.
- ReaderIndirect
- A `std.Io.Reader` that gets its data from another `std.Io.Reader`, and always
This function is intended to be used only in tests.
Functions
- expectError
- This function is intended to be used only in tests.
- expectEqual
- This function is intended to be used only in tests.
- expectFmt
- This function is intended to be used only in tests.
- expectApproxEqAbs
- This function is intended to be used only in tests.
- expectApproxEqRel
- This function is intended to be used only in tests.
- expectEqualSlices
- This function is intended to be used only in tests.
- expectEqualSentinel
- This function is intended to be used only in tests.
- expect
- This function is intended to be used only in tests.
- expectEqualDeep
- This function is intended to be used only in tests.
- checkAllAllocationFailures
- Exhaustively check that allocation failures within `test_fn` are handled without
- refAllDecls
- Given a type, references all the declarations inside, so that the semantic analyzer sees them.
- refAllDeclsRecursive
- Given a type, recursively references all the declarations inside, so that the semantic analyzer sees them.
- fuzz
- Inline to avoid coverage instrumentation.
Provides deterministic randomness in unit tests.
Values
- random_seed
- Provides deterministic randomness in unit tests.
- failing_allocator
- = failing_allocator_instance.allocator()
- allocator
- This should only be used in temporary test programs.
- log_level
- TODO https://github.com/ziglang/zig/issues/5738
- backend_can_print
- = switch (builtin.zig_backend) { .stage2_aarch64, .stage2_powerpc, .stage2_riscv64, .stage2_spirv, => false, else => true, }
Source
Implementation
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;
}