Skip to content

Commit

Permalink
testing: use "eql", not "equal(s)"
Browse files Browse the repository at this point in the history
This is more consistent with the stdlib everywhere other than `std.testing`.

Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
slimsag committed Sep 4, 2023
1 parent 2781d4a commit 6b5a999
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ fn ExpectFloat(comptime T: type) type {
expected: T,

/// Approximate (absolute epsilon tolerence) equality
pub fn equal(e: *const @This(), actual: T) !void {
try e.equalApprox(actual, math.eps(T));
pub fn eql(e: *const @This(), actual: T) !void {
try e.eqlApprox(actual, math.eps(T));
}

/// Approximate (tolerence) equality
pub fn equalApprox(e: *const @This(), actual: T, tolerance: T) !void {
pub fn eqlApprox(e: *const @This(), actual: T, tolerance: T) !void {
try testing.expectApproxEqAbs(e.expected, actual, tolerance);
}

/// Bitwise equality
pub fn equalBinary(e: *const @This(), actual: T) !void {
pub fn eqlBinary(e: *const @This(), actual: T) !void {
try testing.expectEqual(e.expected, actual);
}
};
Expand All @@ -32,23 +32,23 @@ fn ExpectVector(comptime T: type) type {
expected: T,

/// Approximate (absolute epsilon tolerence) equality
pub fn equal(e: *const @This(), actual: T) !void {
try e.equalApprox(actual, math.eps(Elem));
pub fn eql(e: *const @This(), actual: T) !void {
try e.eqlApprox(actual, math.eps(Elem));
}

/// Approximate (tolerence) equality
pub fn equalApprox(e: *const @This(), actual: T, tolerance: Elem) !void {
pub fn eqlApprox(e: *const @This(), actual: T, tolerance: Elem) !void {
var i: usize = 0;
while (i < len) : (i += 1) {
if (!math.equals(Elem, e.expected[i], actual[i], tolerance)) {
if (!math.eql(Elem, e.expected[i], actual[i], tolerance)) {
std.debug.print("vector[{}] actual {}, not within absolute tolerance {} of expected {}\n", .{ i, actual[i], tolerance, e.expected[i] });
return error.TestExpectEqualEps;
}
}
}

/// Bitwise equality
pub fn equalBinary(e: *const @This(), actual: T) !void {
pub fn eqlBinary(e: *const @This(), actual: T) !void {
try testing.expectEqual(e.expected, actual);
}
};
Expand All @@ -59,23 +59,23 @@ fn ExpectVecMat(comptime T: type) type {
expected: T,

/// Approximate (absolute epsilon tolerence) equality
pub fn equal(e: *const @This(), actual: T) !void {
try e.equalApprox(actual, math.eps(T.T));
pub fn eql(e: *const @This(), actual: T) !void {
try e.eqlApprox(actual, math.eps(T.T));
}

/// Approximate (tolerence) equality
pub fn equalApprox(e: *const @This(), actual: T, tolerance: T.T) !void {
pub fn eqlApprox(e: *const @This(), actual: T, tolerance: T.T) !void {
var i: usize = 0;
while (i < T.n) : (i += 1) {
if (!math.equals(T.T, e.expected.v[i], actual.v[i], tolerance)) {
if (!math.eql(T.T, e.expected.v[i], actual.v[i], tolerance)) {
std.debug.print("vector[{}] actual {}, not within absolute tolerance {} of expected {}\n", .{ i, actual.v[i], tolerance, e.expected.v[i] });
return error.TestExpectEqualEps;
}
}
}

/// Bitwise equality
pub fn equalBinary(e: *const @This(), actual: T) !void {
pub fn eqlBinary(e: *const @This(), actual: T) !void {
try testing.expectEqual(e.expected.v, actual.v);
}
};
Expand All @@ -84,7 +84,7 @@ fn ExpectVecMat(comptime T: type) type {
fn ExpectComptime(comptime T: type) type {
return struct {
expected: T,
pub fn equal(comptime e: *const @This(), comptime actual: T) !void {
pub fn eql(comptime e: *const @This(), comptime actual: T) !void {
try testing.expectEqual(e.expected, actual);
}
};
Expand All @@ -94,11 +94,11 @@ fn ExpectBytes(comptime T: type) type {
return struct {
expected: T,

pub fn equal(comptime e: *const @This(), comptime actual: T) !void {
pub fn eql(comptime e: *const @This(), comptime actual: T) !void {
try testing.expectEqualStrings(e.expected, actual);
}

pub fn equalBinary(comptime e: *const @This(), comptime actual: T) !void {
pub fn eqlBinary(comptime e: *const @This(), comptime actual: T) !void {
try testing.expectEqual(e.expected, actual);
}
};
Expand All @@ -119,7 +119,7 @@ fn Expect(comptime T: type) type {
// Generic equality
return struct {
expected: T,
pub fn equal(e: *const @This(), actual: T) !void {
pub fn eql(e: *const @This(), actual: T) !void {
try testing.expectEqual(e.expected, actual);
}
};
Expand All @@ -133,42 +133,42 @@ fn Expect(comptime T: type) type {
///
/// Floats, mach.math.Vec, and mach.math.Mat types support:
///
/// * `.equal(v)` (epsilon equality)
/// * `.equalApprox(v, tolerence)` (specific tolerence equality)
/// * `.equalBinary(v)` binary equality
/// * `.eql(v)` (epsilon equality)
/// * `.eqlApprox(v, tolerence)` (specific tolerence equality)
/// * `.eqlBinary(v)` binary equality
///
/// All other types support only `.equal(v)` binary equality.
/// All other types support only `.eql(v)` binary equality.
///
/// Comparisons with std.testing:
///
/// ```diff
/// -std.testing.expectEqual(@as(u32, 1337), actual())
/// +mach.testing.expect(u32, 1337).equal(actual())
/// +mach.testing.expect(u32, 1337).eql(actual())
/// ```
///
/// ```diff
/// -std.testing.expectApproxEqAbs(@as(f32, 1.0), actual(), std.math.floatEps(f32))
/// +mach.testing.expect(f32, 1.0).equal(actual())
/// +mach.testing.expect(f32, 1.0).eql(actual())
/// ```
///
/// ```diff
/// -std.testing.expectApproxEqAbs(@as(f32, 1.0), actual(), 0.1)
/// +mach.testing.expect(f32, 1.0).equalApprox(actual(), 0.1)
/// +mach.testing.expect(f32, 1.0).eqlApprox(actual(), 0.1)
/// ```
///
/// ```diff
/// -std.testing.expectEqual(@as(f32, 1.0), actual())
/// +mach.testing.expect(f32, 1.0).equalBinary(actual())
/// +mach.testing.expect(f32, 1.0).eqlBinary(actual())
/// ```
///
/// ```diff
/// -std.testing.expectEqual(@as([]const u8, byte_array), actual())
/// +mach.testing.expect([]const u8, byte_array).equalBinary(actual())
/// +mach.testing.expect([]const u8, byte_array).eqlBinary(actual())
/// ```
///
/// ```diff
/// -std.testing.expectEqualStrings("foo", actual())
/// +mach.testing.expect([]const u8, "foo").equal(actual())
/// +mach.testing.expect([]const u8, "foo").eql(actual())
/// ```
///
/// Note that std.testing cannot handle @Vector approximate equality at all, while mach.testing uses
Expand Down

0 comments on commit 6b5a999

Please sign in to comment.