summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/rollback.zig103
1 files changed, 43 insertions, 60 deletions
diff --git a/src/rollback.zig b/src/rollback.zig
index cc10d2cc..06260812 100644
--- a/src/rollback.zig
+++ b/src/rollback.zig
@@ -522,36 +522,50 @@ test "a copy put back is the core as it was; one let go keeps what was done" {
try std.testing.expectEqual(@as(usize, 0), e.live.count());
}
-test "a view's slot while it is live keeps no pointer a rollback could put old bytes back through" {
+test "a view's slot, live or drawn outside a step (a replica's own, another live), keeps no pointer a rollback could put old bytes back through" {
// The C heap: a freed chunk comes back to the next allocation its size.
- var e: Epoch = .{};
- defer e.deinit();
- const gpa = e.allocator();
- const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
- defer p.deinit();
- p.pump(.{}) catch {};
- _ = p.openView() orelse return error.NoView;
- // View 0's column tag has a text of its own; out to view 1 and back.
- const text = "the column tag of view 0, its own";
- p.col_tags[0].own = try gpa.dupe(u8, text);
- p.toView(1);
- p.toView(0);
- // The live view drops it, and its memory is another's: something the
- // copy has no business saving.
- const was = p.col_tags[0].own.?;
- gpa.free(was);
- p.col_tags[0].own = null;
- const other = try gpa.alloc(u8, text.len);
- defer gpa.free(other);
- if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another
- @memset(other, 'a');
- var c: Copy = .{};
- defer c.deinit();
- try c.take(&e, p);
- // Written in place while the copy is held, then the copy put back.
- @memset(other, 'b');
- c.restore();
- try std.testing.expect(std.mem.allEqual(u8, other, 'b'));
+ const Read = struct {
+ fn read(_: void, _: *Pardes) anyerror!void {}
+ };
+ var skipped = false;
+ for ([_]bool{ false, true }) |drawn| {
+ var e: Epoch = .{};
+ defer e.deinit();
+ const gpa = e.allocator();
+ const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
+ defer p.deinit();
+ p.pump(.{}) catch {};
+ _ = p.openView() orelse return error.NoView;
+ // View 0's column tag has a text of its own; out to view 1 and back,
+ // or view 1 read (as a replica client's frontend reads its own) while
+ // view 0 is live: 0 parked and put back.
+ const text = "the column tag of view 0, its own";
+ p.col_tags[0].own = try gpa.dupe(u8, text);
+ if (drawn) try pardes.views.within(p, 1, {}, Read.read) else {
+ p.toView(1);
+ p.toView(0);
+ }
+ // The live view drops it, and its memory is another's: something the
+ // copy has no business saving.
+ const was = p.col_tags[0].own.?;
+ gpa.free(was);
+ p.col_tags[0].own = null;
+ const other = try gpa.alloc(u8, text.len);
+ defer gpa.free(other);
+ if (other.ptr != was.ptr) { // the heap handed out another
+ skipped = true;
+ continue;
+ }
+ @memset(other, 'a');
+ var c: Copy = .{};
+ defer c.deinit();
+ try c.take(&e, p);
+ // Written in place while the copy is held, then the copy put back.
+ @memset(other, 'b');
+ c.restore();
+ try std.testing.expect(std.mem.allEqual(u8, other, 'b'));
+ }
+ if (skipped) return error.SkipZigTest;
}
test "a register's texts are pinned, not copied, and come back whole" {
@@ -648,34 +662,3 @@ test "a rollback leaves the store and Recent as a speculation left them: they li
p.deinit();
try std.testing.expectEqual(@as(usize, 0), e.live.count());
}
-
-test "a view drawn outside a step (a replica's own, another live) leaves the live view's slot holding nothing a rollback could put old bytes back through" {
- var e: Epoch = .{};
- defer e.deinit();
- const gpa = e.allocator();
- const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
- defer p.deinit();
- p.pump(.{}) catch {};
- _ = p.openView() orelse return error.NoView;
- const text = "the column tag of view 0, its own";
- p.col_tags[0].own = try gpa.dupe(u8, text);
- // View 1 read (as a replica client's frontend reads its own) while view
- // 0 is live: 0 parked and put back.
- const Read = struct {
- fn read(_: void, _: *Pardes) anyerror!void {}
- };
- try pardes.views.within(p, 1, {}, Read.read);
- const was = p.col_tags[0].own.?;
- gpa.free(was);
- p.col_tags[0].own = null;
- const other = try gpa.alloc(u8, text.len);
- defer gpa.free(other);
- if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another
- @memset(other, 'a');
- var c: Copy = .{};
- defer c.deinit();
- try c.take(&e, p);
- @memset(other, 'b');
- c.restore();
- try std.testing.expect(std.mem.allEqual(u8, other, 'b'));
-}