Skip to content

Commit

Permalink
std.testing.expectEqual: {any} in print and move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poypoyan authored and alexrp committed Jan 29, 2025
1 parent 78b7a44 commit e528ab4
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/std/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
var i: usize = 0;
while (i < info.len) : (i += 1) {
if (!std.meta.eql(expected[i], actual[i])) {
print("index {} incorrect. expected {}, found {}\n", .{
print("index {d} incorrect. expected {any}, found {any}\n", .{
i, expected[i], actual[i],
});
return error.TestExpectedEqual;
Expand Down Expand Up @@ -214,6 +214,34 @@ test "expectEqual union with comptime-only field" {
try expectEqual(U{ .a = {} }, .a);
}

test "expectEqual nested array" {
const a = [2][2]f32{
[_]f32{ 1.0, 0.0 },
[_]f32{ 0.0, 1.0 },
};

const b = [2][2]f32{
[_]f32{ 1.0, 0.0 },
[_]f32{ 0.0, 1.0 },
};

try expectEqual(a, b);
}

test "expectEqual vector" {
const a: @Vector(4, u32) = @splat(4);
const b: @Vector(4, u32) = @splat(4);

try expectEqual(a, b);
}

test "expectEqual null" {
const a = .{null};
const b = @Vector(1, ?*u8){null};

try expectEqual(a, b);
}

/// This function is intended to be used only in tests. When the formatted result of the template
/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how
/// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing
Expand Down Expand Up @@ -584,27 +612,6 @@ pub fn tmpDir(opts: std.fs.Dir.OpenOptions) TmpDir {
};
}

test "expectEqual nested array" {
const a = [2][2]f32{
[_]f32{ 1.0, 0.0 },
[_]f32{ 0.0, 1.0 },
};

const b = [2][2]f32{
[_]f32{ 1.0, 0.0 },
[_]f32{ 0.0, 1.0 },
};

try expectEqual(a, b);
}

test "expectEqual vector" {
const a: @Vector(4, u32) = @splat(4);
const b: @Vector(4, u32) = @splat(4);

try expectEqual(a, b);
}

pub fn expectEqualStrings(expected: []const u8, actual: []const u8) !void {
if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| {
print("\n====== expected this output: =========\n", .{});
Expand Down

0 comments on commit e528ab4

Please sign in to comment.