tiny.game.golden
Defined in tiny.game.
An exact byte comparison between a saved reference output and a new one.
API (2)
Actions
Public operations.
compare: Compares a new output,actual, with its reference,expected, byte by byte.
Types and contracts
Public types and contracts.
Comparison: The result of comparing a new output with its reference: whether they match, the first offset at which they differ, and both lengths.
Source
Source: fun/game/src/golden.zig
zig
//! An exact byte comparison between a saved reference output and a new one. A test wants to know//! whether a run produced the same output as its saved reference, and the first offset at which the//! two differ. Parsing both outputs to find a meaningful difference takes work that only a run//! whose bytes differ needs. `compare` walks both outputs byte by byte and returns whether they//! match, the offset of the first difference and both lengths (`Comparison`). When one output//! matches the start of the other, the first difference is at the shorter output's length. A caller//! runs the byte comparison first and parses both outputs only when they differ.const std = @import("std");/// The result of comparing a new output with its reference: whether they match, the first offset at/// which they differ, and both lengths. A test reads `equal` to decide whether it has to look/// closer at the two outputs.pub const Comparison = struct { /// True when both outputs have the same length and the same bytes. equal: bool, /// The offset of the first byte at which the outputs differ, or `null` when they match. When /// one output matches the start of the other, the offset is the shorter output's length. first_difference: ?usize, /// The length of the reference output in bytes. expected_bytes: usize, /// The length of the new output in bytes. actual_bytes: usize,};/// Compares a new output, `actual`, with its reference, `expected`, byte by byte. A test calls it/// on its saved reference and the output of a fresh run. The call stops at the first differing/// byte. The call allocates nothing.pub fn compare(expected: []const u8, actual: []const u8) Comparison { const extent = @min(expected.len, actual.len); for (expected[0..extent], actual[0..extent], 0..) |a, b, i| { if (a != b) return .{ .equal = false, .first_difference = i, .expected_bytes = expected.len, .actual_bytes = actual.len }; } return .{ .equal = expected.len == actual.len, .first_difference = if (expected.len == actual.len) null else extent, .expected_bytes = expected.len, .actual_bytes = actual.len, };}test "goldens compare exact output bytes" { try std.testing.expect(compare("abc", "abc").equal); try std.testing.expectEqual(@as(?usize, 2), compare("abc", "abd").first_difference); try std.testing.expectEqual(@as(?usize, 3), compare("abc", "abcd").first_difference);}Source: fun/game/src/root.zig:117
zig
pub const golden = @import("golden.zig");Audit
| Definitions | 3 |
|---|---|
| Public names | 3 |
| Members | 4 |
| Version | 26.7.0 |
| Revision | daab053ee433 |