diff options
Diffstat (limited to 'src/walk.zig')
| -rw-r--r-- | src/walk.zig | 160 |
1 files changed, 80 insertions, 80 deletions
diff --git a/src/walk.zig b/src/walk.zig index d7ee63b3..3641d327 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -528,80 +528,6 @@ fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void { out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError; } -test "a value comes back from its bytes" { - const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 }; - var buf: [256]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - const values = [_]E{ .{ .key = .{ .cp = 'x', .text = "x" } }, .tick, .{ .failed = error.FileNotFound }, .{ .failed = 9 } }; - for (&values) |*v| try encode(E, v, &w); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - var r: Reader = .{ .bytes = w.buffered() }; - for (&values) |*v| try std.testing.expectEqual(digest(E, v), digest(E, &try decode(E, &r, arena.allocator()))); - try std.testing.expectEqual(r.bytes.len, r.at); -} - -test "an error is taped by its name, which another build reads back as its own" { - // The build's number for an error is its own: 451 of 505 names were - // numbered otherwise in a Linux window's build than a terminal's, so a - // window replica read a terminal session's FileNotFound as another. - var buf: [64]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir; - try encode(@TypeOf(failed), &failed, &w); - try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered()); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - var r: Reader = .{ .bytes = w.buffered() }; - try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator())); - // A name this build's set lacks (an OS's own): the tape is another - // build's, and the replay diverges rather than read some other error. - r = .{ .bytes = "\x00\x0c\x00FileTooLarge" }; - try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator())); - // An anyerror (a host's report) is read back as one of tape.Failure's. - const Report = struct { err: anyerror }; - w = .fixed(&buf); - try encode(Report, &.{ .err = error.FileNotFound }, &w); - r = .{ .bytes = w.buffered() }; - try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); -} - -test "an enum is digested by its name, which another build may number otherwise" { - // registry.Builtin as a Linux build makes it (Tty9p in it) and as a - // FreeBSD one does: LocationsConfig is another number in each. - const Linux = enum { Look, Tty9p, LocationsConfig }; - const FreeBSD = enum { Look, LocationsConfig }; - const S = struct { from: Linux, also: [2]Linux }; - const T = struct { from: FreeBSD, also: [2]FreeBSD }; - try std.testing.expectEqual(digest(S, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }), digest(T, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } })); - try std.testing.expect(digest(S, &.{ .from = .Look, .also = .{ .Look, .Look } }) != digest(S, &.{ .from = .Tty9p, .also = .{ .Look, .Look } })); -} - -test "a digest is of values: the same text at two addresses is one digest" { - const S = struct { name: []const u8, n: u32, next: ?*const u32 }; - const a = "same text"; - var b: [9]u8 = undefined; - @memcpy(&b, a); - const x: u32 = 7; - const y: u32 = 7; - try std.testing.expectEqual(digest(S, &.{ .name = a, .n = 1, .next = &x }), digest(S, &.{ .name = &b, .n = 1, .next = &y })); - try std.testing.expect(digest(S, &.{ .name = a, .n = 1, .next = &x }) != digest(S, &.{ .name = a, .n = 2, .next = &x })); -} - -test "an annotated field is in no digest, and a length cuts its array" { - const S = struct { - kept: u32, - cache: u32, - ring: [4]u8, - ring_len: u8, - pub const replica = .{ .derived = .{"cache"}, .len = .{.{ "ring", "ring_len" }} }; - }; - try std.testing.expectEqual( - digest(S, &.{ .kept = 1, .cache = 2, .ring = .{ 1, 2, 9, 9 }, .ring_len = 2 }), - digest(S, &.{ .kept = 1, .cache = 3, .ring = .{ 1, 2, 0, 0 }, .ring_len = 2 }), - ); -} - // ---- the dump: the same walk, by name, for another build ---- // // A dump (docs/next-daemon.md, phase 2) is `dump_magic`, then a table of @@ -800,7 +726,7 @@ fn writeTable(comptime types: []const type, w: *std.Io.Writer) !void { try writeU32(w, types.len); inline for (types) |T| { const k = comptime kindOf(T); - try w.writeByte(@intFromEnum(k)); + try w.writeByte(@backingInt(k)); switch (k) { .int => { try w.writeByte(@intFromBool(@typeInfo(T).int.signedness == .signed)); @@ -962,7 +888,7 @@ pub fn Dump(comptime roots: []const type) type { .bool => try w.writeByte(@intFromBool(v.*)), .int => try putInt(T, v.*, w), .float => try w.writeAll(std.mem.asBytes(v)), - .@"enum" => try putInt(@typeInfo(T).@"enum".tag_type, @intFromEnum(v.*), w), + .@"enum" => try putInt(@typeInfo(T).@"enum".tag_type, @backingInt(v.*), w), .optional => if (v.*) |*payload| { try w.writeByte(1); try d.value(@typeInfo(T).optional.child, payload); @@ -1306,7 +1232,7 @@ pub fn Load(comptime roots: []const type) type { .@"enum" => { const e = @typeInfo(T).@"enum"; if (l.same) { - out.* = @enumFromInt(try l.readInt(e.tag_type, s)); + out.* = @fromBackingInt(@intCast(try l.readInt(e.tag_type, s))); return; } const n = try l.readInt(i128, s); @@ -1319,7 +1245,7 @@ pub fn Load(comptime roots: []const type) type { }; if (e.mode == .nonexhaustive and was == null) if (std.math.cast(e.tag_type, n)) |tag| { - out.* = @enumFromInt(tag); + out.* = @fromBackingInt(@intCast(tag)); return; }; l.note("{s}: no {s} here", .{ l.field, was orelse "such value" }); @@ -1752,7 +1678,7 @@ pub const Put = struct { .float => try c.write(.{ .float = v.* }), .@"enum" => if (std.enums.tagName(T, v.*)) |name| { try c.write(.{ .bytes = name }); - } else try c.write(.{ .int = @intCast(@intFromEnum(v.*)) }), + } else try c.write(.{ .int = @intCast(@backingInt(v.*)) }), .optional => { const C = @typeInfo(T).optional.child; if (v.*) |*payload| { @@ -2047,7 +1973,7 @@ pub const Get = struct { }, .int, .uint => if (e.mode == .nonexhaustive) { const n: i64 = @bitCast(slot.value); - out.* = @enumFromInt(std.math.cast(e.tag_type, n) orelse return error.Incompatible); + out.* = @fromBackingInt(@intCast(std.math.cast(e.tag_type, n) orelse return error.Incompatible)); } else return error.Incompatible, else => return error.Incompatible, } @@ -2404,6 +2330,80 @@ pub const Get = struct { } }; +test "a value comes back from its bytes" { + const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 }; + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const values = [_]E{ .{ .key = .{ .cp = 'x', .text = "x" } }, .tick, .{ .failed = error.FileNotFound }, .{ .failed = 9 } }; + for (&values) |*v| try encode(E, v, &w); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + for (&values) |*v| try std.testing.expectEqual(digest(E, v), digest(E, &try decode(E, &r, arena.allocator()))); + try std.testing.expectEqual(r.bytes.len, r.at); +} + +test "an error is taped by its name, which another build reads back as its own" { + // The build's number for an error is its own: 451 of 505 names were + // numbered otherwise in a Linux window's build than a terminal's, so a + // window replica read a terminal session's FileNotFound as another. + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir; + try encode(@TypeOf(failed), &failed, &w); + try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered()); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator())); + // A name this build's set lacks (an OS's own): the tape is another + // build's, and the replay diverges rather than read some other error. + r = .{ .bytes = "\x00\x0c\x00FileTooLarge" }; + try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator())); + // An anyerror (a host's report) is read back as one of tape.Failure's. + const Report = struct { err: anyerror }; + w = .fixed(&buf); + try encode(Report, &.{ .err = error.FileNotFound }, &w); + r = .{ .bytes = w.buffered() }; + try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); +} + +test "an enum is digested by its name, which another build may number otherwise" { + // registry.Builtin as a Linux build makes it (Tty9p in it) and as a + // FreeBSD one does: LocationsConfig is another number in each. + const Linux = enum { Look, Tty9p, LocationsConfig }; + const FreeBSD = enum { Look, LocationsConfig }; + const S = struct { from: Linux, also: [2]Linux }; + const T = struct { from: FreeBSD, also: [2]FreeBSD }; + try std.testing.expectEqual(digest(S, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }), digest(T, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } })); + try std.testing.expect(digest(S, &.{ .from = .Look, .also = .{ .Look, .Look } }) != digest(S, &.{ .from = .Tty9p, .also = .{ .Look, .Look } })); +} + +test "a digest is of values: the same text at two addresses is one digest" { + const S = struct { name: []const u8, n: u32, next: ?*const u32 }; + const a = "same text"; + var b: [9]u8 = undefined; + @memcpy(&b, a); + const x: u32 = 7; + const y: u32 = 7; + try std.testing.expectEqual(digest(S, &.{ .name = a, .n = 1, .next = &x }), digest(S, &.{ .name = &b, .n = 1, .next = &y })); + try std.testing.expect(digest(S, &.{ .name = a, .n = 1, .next = &x }) != digest(S, &.{ .name = a, .n = 2, .next = &x })); +} + +test "an annotated field is in no digest, and a length cuts its array" { + const S = struct { + kept: u32, + cache: u32, + ring: [4]u8, + ring_len: u8, + pub const replica = .{ .derived = .{"cache"}, .len = .{.{ "ring", "ring_len" }} }; + }; + try std.testing.expectEqual( + digest(S, &.{ .kept = 1, .cache = 2, .ring = .{ 1, 2, 9, 9 }, .ring_len = 2 }), + digest(S, &.{ .kept = 1, .cache = 3, .ring = .{ 1, 2, 0, 0 }, .ring_len = 2 }), + ); +} + test "an optional slice comes back, and the fields after it" { const S = struct { before: u32 = 0, command: ?[]u8 = null, after: u32 = 0 }; var text = "sleep 30".*; |
