Use approximate equality for float comparisons
parent
bb607e0999
commit
4af4e18725
|
|
@ -1371,6 +1371,9 @@ test "adjusted sizes" {
|
|||
}
|
||||
|
||||
test "face metrics" {
|
||||
// The web canvas backend doesn't calculate face metrics, only cell metrics
|
||||
if (options.backend != .web_canvas) return error.SkipZigTest;
|
||||
|
||||
const testing = std.testing;
|
||||
const alloc = testing.allocator;
|
||||
const narrowFont = font.embedded.cozette;
|
||||
|
|
@ -1403,13 +1406,12 @@ test "face metrics" {
|
|||
.size_adjustment = .none,
|
||||
});
|
||||
|
||||
const narrowMetrics = (try c.getFace(narrowIndex)).getMetrics();
|
||||
const wideMetrics = (try c.getFace(wideIndex)).getMetrics();
|
||||
const narrowMetrics: font.Metrics.FaceMetrics = (try c.getFace(narrowIndex)).getMetrics();
|
||||
const wideMetrics: font.Metrics.FaceMetrics = (try c.getFace(wideIndex)).getMetrics();
|
||||
|
||||
// Verify provided/measured metrics. Measured
|
||||
// values are backend-dependent due to hinting.
|
||||
if (options.backend != .web_canvas) {
|
||||
try std.testing.expectEqual(font.Metrics.FaceMetrics{
|
||||
const narrowMetricsExpected = font.Metrics.FaceMetrics{
|
||||
.px_per_em = 16.0,
|
||||
.cell_width = switch (options.backend) {
|
||||
.freetype,
|
||||
|
|
@ -1442,8 +1444,8 @@ test "face metrics" {
|
|||
=> 16.0,
|
||||
.web_canvas => unreachable,
|
||||
},
|
||||
}, narrowMetrics);
|
||||
try std.testing.expectEqual(font.Metrics.FaceMetrics{
|
||||
};
|
||||
const wideMetricsExpected = font.Metrics.FaceMetrics{
|
||||
.px_per_em = 16.0,
|
||||
.cell_width = switch (options.backend) {
|
||||
.freetype,
|
||||
|
|
@ -1476,7 +1478,36 @@ test "face metrics" {
|
|||
=> 15.472000000000001,
|
||||
.web_canvas => unreachable,
|
||||
},
|
||||
}, wideMetrics);
|
||||
};
|
||||
|
||||
inline for (
|
||||
.{ narrowMetricsExpected, wideMetricsExpected },
|
||||
.{ narrowMetrics, wideMetrics },
|
||||
) |metricsExpected, metricsActual| {
|
||||
inline for (@typeInfo(font.Metrics.FaceMetrics).@"struct".fields) |field| {
|
||||
const expected = @field(metricsExpected, field.name);
|
||||
const actual = @field(metricsActual, field.name);
|
||||
// Unwrap optional fields
|
||||
const expectedValue, const actualValue = unwrap: switch (@typeInfo(field.type)) {
|
||||
.optional => |Tinfo| {
|
||||
if (expected) |expectedValue| {
|
||||
const actualValue = actual orelse std.math.nan(Tinfo.child);
|
||||
break :unwrap .{ expectedValue, actualValue };
|
||||
}
|
||||
// Null values can be compared directly
|
||||
try std.testing.expectEqual(expected, actual);
|
||||
continue;
|
||||
},
|
||||
else => break :unwrap .{ expected, actual },
|
||||
};
|
||||
// All non-null values are floats
|
||||
const eps = std.math.floatEps(@TypeOf(actualValue - expectedValue));
|
||||
try std.testing.expectApproxEqRel(
|
||||
expectedValue,
|
||||
actualValue,
|
||||
std.math.sqrt(eps),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Verify estimated metrics. icWidth() should equal the smaller of
|
||||
|
|
|
|||
Loading…
Reference in New Issue