summaryrefslogtreecommitdiff
path: root/core/draw.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/draw.zig')
-rw-r--r--core/draw.zig149
1 files changed, 149 insertions, 0 deletions
diff --git a/core/draw.zig b/core/draw.zig
index dac6673e..88011373 100644
--- a/core/draw.zig
+++ b/core/draw.zig
@@ -1337,3 +1337,152 @@ test "a body with context rows is painted once, and the grid's rows are what a p
// The layer's own rows run past the grid's.
try std.testing.expect(layer.rows > body.rect.h);
}
+
+// The class kept closed: a frame changes nothing a step reads. What a frame
+// writes of a core is `.local` or `.derived` (in no digest and no dump: a
+// pane's notice bands, the geometry's wrap map and context rows, a markdown
+// text's tables), so a step that reads one must find what its own state
+// makes. Here one core draws after every input, each frame at its own time
+// after its step's, and one joined from its dump never draws: each input's
+// step leaves the two with one digest. A field a frame leaves that a step
+// then reads otherwise than a core that did not draw makes it, differs here.
+// Not seen: a core joined later (its authority's reads are the tape's, which
+// this has none of: the soak's late joins), and what no input here reaches.
+test "a frame changes nothing a step reads: a core drawing after every input and one never drawing agree" {
+ const gpa = std.testing.allocator;
+ const steps: usize = if (std.c.getenv("PARDES_FUZZ_STEPS")) |v| std.fmt.parseInt(usize, std.mem.span(v), 10) catch 600 else 600;
+ const seed: u64 = if (std.c.getenv("PARDES_FUZZ_SEED")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 11 else 11;
+ var prng = std.Random.DefaultPrng.init(seed);
+ const random = prng.random();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ for (0..4500) |i| try text.print(gpa, "line {d} {s}\n", .{ i, std.mem.asBytes(&@as([20][6]u8, @splat("words ".*)))[0 .. 6 * (i % 20)] });
+ try text.appendSlice(gpa, "\n| name | qty | note |\n|:--|--:|:-:|\n| fig | 1 | ripe |\n| apple || |\n| a longer name | 12345 | x |\n\n## end\n");
+ const metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 };
+ const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 90, .rows = 30 });
+ defer a.deinit();
+ a.update(.{ .resize = .{ .cols = 90, .rows = 30, .row_metrics = metrics } });
+ const pane = try a.setTestFile(text.items);
+ a.gpa.free(pane.file.?.path);
+ pane.file.?.path = try a.gpa.dupe(u8, "/notes.md");
+ a.newScratchBelow(0);
+ a.view().active = 0;
+ pane.file.?.scroll = 4490;
+ pane.body.cur_row = 4502;
+ a.sync();
+ const first = try pardes.dump.dumpBytes(a);
+ defer gpa.free(first);
+ const b = try pardes.dump.load(gpa, a.opts, first, null);
+ defer b.deinit();
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const commands = [_][]const u8{ "Wrap", "Collapse", "Del", "Undo", "Redo" };
+ var now: u64 = std.time.ns_per_s;
+ for (0..steps) |i| {
+ const v = a.view();
+ // Most presses on the body around the table at the text's end.
+ const body = v.rects[0];
+ const ev: pardes.Event = switch (random.uintLessThan(u8, 13)) {
+ 0...4 => .{ .key = .{ .cp = switch (random.uintLessThan(u8, 16)) {
+ 0 => pardes.Key.escape,
+ 1 => 'i',
+ 2 => 'G',
+ 3 => 'g',
+ 4 => ' ',
+ 5 => 'd',
+ 6 => 'w',
+ 7 => 'b',
+ 8 => 'u',
+ 9, 10 => 'j',
+ 11, 12 => 'k',
+ 13 => 'h',
+ 14 => 'l',
+ else => 'x',
+ } } },
+ 5...7 => .{ .mouse = .{
+ .button = if (random.uintLessThan(u8, 4) == 0) .wheel_up else if (random.uintLessThan(u8, 4) == 0) .wheel_down else .left,
+ .kind = switch (random.uintLessThan(u8, 3)) {
+ 0 => .press,
+ 1 => .drag,
+ else => .release,
+ },
+ .col = if (random.boolean()) random.uintLessThan(u16, v.screen_w) else body.x + random.uintLessThan(u16, @max(1, @min(body.w, 40))),
+ .row = if (random.boolean()) random.uintLessThan(u16, v.screen_h) else body.y + random.uintLessThan(u16, @max(1, body.h)),
+ } },
+ 8 => .{ .command = commands[random.uintLessThan(usize, commands.len)] },
+ 9 => .{ .paste = "| new | row |\n" },
+ // The text changed on disk: a step changes it whole.
+ 10 => .{ .file_changed = .{ .pane = 0, .bytes = text.items[0 .. text.items.len - random.uintLessThan(usize, 40)] } },
+ else => blk: {
+ now += random.uintLessThan(u64, 2 * std.time.ns_per_s);
+ break :blk .tick;
+ },
+ };
+ for ([_]*Pardes{ a, b }) |p| {
+ p.clock.now_ns = now;
+ p.clock.stepped_ns = now;
+ p.update(ev);
+ }
+ // A frame is drawn at its own time, after its step's.
+ now += random.uintLessThan(u64, std.time.ns_per_s);
+ a.clock.now_ns = now;
+ b.clock.now_ns = now;
+ _ = arena.reset(.retain_capacity);
+ _ = try a.render(arena.allocator());
+ a.presented();
+ b.presented();
+ if (apart(a, b)) {
+ std.debug.print("seed {d}, step {d}: a {s} left them apart\n", .{ seed, i, @tagName(ev) });
+ return error.DigestsDiffer;
+ }
+ }
+}
+
+/// Whether two cores' digests differ, field by field (the panes' and their
+/// files' too), the fields that do named; but for the three that differ
+/// between two cores in one test process whatever they do: a message's
+/// stamp and a text's mtime, read off the wall clock with no tape, and a
+/// scratch pane's cwd, inherited by pointer on one and owned bytes on one
+/// loaded from a dump.
+fn apart(a: *const Pardes, b: *const Pardes) bool {
+ var any = false;
+ const names = comptime pardes.walk.fieldNames(Pardes);
+ var da: [names.len]u64 = undefined;
+ var db: [names.len]u64 = undefined;
+ _ = pardes.walk.fieldDigests(Pardes, a, &da);
+ _ = pardes.walk.fieldDigests(Pardes, b, &db);
+ for (names, da, db) |name, x, y| if (x != y and !std.mem.eql(u8, name, "panes")) {
+ std.debug.print(" {s}\n", .{name});
+ any = true;
+ };
+ const pane_names = comptime pardes.walk.fieldNames(Pane);
+ for (a.panes, b.panes, 0..) |pa, pb, id| {
+ if ((pa == null) != (pb == null)) {
+ std.debug.print(" pane {d} open on one\n", .{id});
+ any = true;
+ }
+ const x = pa orelse continue;
+ const y = pb orelse continue;
+ var pda: [pane_names.len]u64 = undefined;
+ var pdb: [pane_names.len]u64 = undefined;
+ _ = pardes.walk.fieldDigests(Pane, x, &pda);
+ _ = pardes.walk.fieldDigests(Pane, y, &pdb);
+ for (pane_names, pda, pdb) |name, u, v| if (u != v and !std.mem.eql(u8, name, "msg_said_ms") and !std.mem.eql(u8, name, "cwd")) {
+ if (std.mem.eql(u8, name, "file")) if (x.file) |*fx| if (y.file) |*fy| {
+ const file_names = comptime pardes.walk.fieldNames(panes.File.State);
+ var fda: [file_names.len]u64 = undefined;
+ var fdb: [file_names.len]u64 = undefined;
+ _ = pardes.walk.fieldDigests(panes.File.State, fx, &fda);
+ _ = pardes.walk.fieldDigests(panes.File.State, fy, &fdb);
+ for (file_names, fda, fdb) |file_name, q, r| if (q != r and !std.mem.eql(u8, file_name, "mtime")) {
+ std.debug.print(" pane {d}.file.{s}\n", .{ id, file_name });
+ any = true;
+ };
+ continue;
+ };
+ std.debug.print(" pane {d}.{s}\n", .{ id, name });
+ any = true;
+ };
+ }
+ return any;
+}