fix(tests): fix buffer tests
This commit is contained in:
parent
12842b6b3b
commit
633cc5a1c1
2 changed files with 35 additions and 18 deletions
|
@ -695,17 +695,17 @@ const Node = union(enum) {
|
||||||
return Node.new(a, try merge_in_place(leaves[0..mid], a), try merge_in_place(leaves[mid..], a));
|
return Node.new(a, try merge_in_place(leaves[0..mid], a), try merge_in_place(leaves[mid..], a));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_line(self: *const Node, line: usize, result: *ArrayList(u8)) !void {
|
pub fn get_line(self: *const Node, line: usize, result: *ArrayList(u8), mtrx: Metrix) !void {
|
||||||
const Ctx = struct {
|
const Ctx = struct {
|
||||||
line: *ArrayList(u8),
|
line: *ArrayList(u8),
|
||||||
fn walker(ctx_: *anyopaque, leaf: *const Leaf) Walker {
|
fn walker(ctx_: *anyopaque, leaf: *const Leaf, _: Metrix) Walker {
|
||||||
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
|
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
|
||||||
ctx.line.appendSlice(leaf.buf) catch |e| return .{ .err = e };
|
ctx.line.appendSlice(leaf.buf) catch |e| return .{ .err = e };
|
||||||
return if (!leaf.eol) Walker.keep_walking else Walker.stop;
|
return if (!leaf.eol) Walker.keep_walking else Walker.stop;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var ctx: Ctx = .{ .line = result };
|
var ctx: Ctx = .{ .line = result };
|
||||||
const found = self.walk_from_line_begin_const(line, Ctx.walker, &ctx) catch false;
|
const found = self.walk_from_line_begin_const(line, Ctx.walker, &ctx, mtrx) catch false;
|
||||||
return if (!found) error.NotFound;
|
return if (!found) error.NotFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,23 @@ const Buffer = @import("Buffer");
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
const a = std.testing.allocator;
|
const a = std.testing.allocator;
|
||||||
|
|
||||||
|
fn metrics() Buffer.Metrix {
|
||||||
|
return .{
|
||||||
|
.ctx = undefined,
|
||||||
|
.egc_length = struct {
|
||||||
|
fn f(_: *const anyopaque, _: []const u8, colcount: *c_int, _: usize) usize {
|
||||||
|
colcount.* = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}.f,
|
||||||
|
.egc_chunk_width = struct {
|
||||||
|
fn f(_: *const anyopaque, chunk_: []const u8, _: usize) usize {
|
||||||
|
return chunk_.len;
|
||||||
|
}
|
||||||
|
}.f,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn get_big_doc() !*Buffer {
|
fn get_big_doc() !*Buffer {
|
||||||
const BigDocGen = struct {
|
const BigDocGen = struct {
|
||||||
line_num: usize = 0,
|
line_num: usize = 0,
|
||||||
|
@ -83,7 +100,7 @@ test "buffer" {
|
||||||
|
|
||||||
fn get_line(buf: *const Buffer, line: usize) ![]const u8 {
|
fn get_line(buf: *const Buffer, line: usize) ![]const u8 {
|
||||||
var result = ArrayList(u8).init(a);
|
var result = ArrayList(u8).init(a);
|
||||||
try buf.root.get_line(line, &result);
|
try buf.root.get_line(line, &result, metrics());
|
||||||
return result.toOwnedSlice();
|
return result.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +147,8 @@ test "line_len" {
|
||||||
defer buffer.deinit();
|
defer buffer.deinit();
|
||||||
buffer.update(try buffer.load_from_string(doc));
|
buffer.update(try buffer.load_from_string(doc));
|
||||||
|
|
||||||
try std.testing.expectEqual(try buffer.root.line_width(0), 8);
|
try std.testing.expectEqual(try buffer.root.line_width(0, metrics()), 8);
|
||||||
try std.testing.expectEqual(try buffer.root.line_width(1), 5);
|
try std.testing.expectEqual(try buffer.root.line_width(1, metrics()), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "del_chars" {
|
test "del_chars" {
|
||||||
|
@ -153,12 +170,12 @@ test "del_chars" {
|
||||||
defer buffer.deinit();
|
defer buffer.deinit();
|
||||||
buffer.update(try buffer.load_from_string(doc));
|
buffer.update(try buffer.load_from_string(doc));
|
||||||
|
|
||||||
buffer.update(try buffer.root.del_chars(3, try buffer.root.line_width(3) - 1, 1, buffer.a));
|
buffer.update(try buffer.root.del_chars(3, try buffer.root.line_width(3, metrics()) - 1, 1, buffer.a, metrics()));
|
||||||
const line3 = try get_line(buffer, 3);
|
const line3 = try get_line(buffer, 3);
|
||||||
defer a.free(line3);
|
defer a.free(line3);
|
||||||
try std.testing.expect(std.mem.eql(u8, line3, "us"));
|
try std.testing.expect(std.mem.eql(u8, line3, "us"));
|
||||||
|
|
||||||
buffer.update(try buffer.root.del_chars(3, 0, 7, buffer.a));
|
buffer.update(try buffer.root.del_chars(3, 0, 7, buffer.a, metrics()));
|
||||||
const line3_1 = try get_line(buffer, 3);
|
const line3_1 = try get_line(buffer, 3);
|
||||||
defer a.free(line3_1);
|
defer a.free(line3_1);
|
||||||
try std.testing.expect(std.mem.eql(u8, line3_1, "your"));
|
try std.testing.expect(std.mem.eql(u8, line3_1, "your"));
|
||||||
|
@ -166,7 +183,7 @@ test "del_chars" {
|
||||||
// try buffer.rebalance();
|
// try buffer.rebalance();
|
||||||
// try std.testing.expect(buffer.is_balanced());
|
// try std.testing.expect(buffer.is_balanced());
|
||||||
|
|
||||||
buffer.update(try buffer.root.del_chars(0, try buffer.root.line_width(0) - 1, 2, buffer.a));
|
buffer.update(try buffer.root.del_chars(0, try buffer.root.line_width(0, metrics()) - 1, 2, buffer.a, metrics()));
|
||||||
const line0 = try get_line(buffer, 0);
|
const line0 = try get_line(buffer, 0);
|
||||||
defer a.free(line0);
|
defer a.free(line0);
|
||||||
try std.testing.expect(std.mem.eql(u8, line0, "All youropes"));
|
try std.testing.expect(std.mem.eql(u8, line0, "All youropes"));
|
||||||
|
@ -197,7 +214,7 @@ test "del_chars2" {
|
||||||
defer buffer.deinit();
|
defer buffer.deinit();
|
||||||
buffer.update(try buffer.load_from_string(doc));
|
buffer.update(try buffer.load_from_string(doc));
|
||||||
|
|
||||||
buffer.update(try buffer.root.del_chars(2, try buffer.root.line_width(2) - 3, 6, buffer.a));
|
buffer.update(try buffer.root.del_chars(2, try buffer.root.line_width(2, metrics()) - 3, 6, buffer.a, metrics()));
|
||||||
|
|
||||||
try check_line(buffer, 2, "are belong!");
|
try check_line(buffer, 2, "are belong!");
|
||||||
try check_line(buffer, 3, "All your");
|
try check_line(buffer, 3, "All your");
|
||||||
|
@ -216,56 +233,56 @@ test "insert_chars" {
|
||||||
defer a.free(line0);
|
defer a.free(line0);
|
||||||
try std.testing.expect(std.mem.eql(u8, line0, "B"));
|
try std.testing.expect(std.mem.eql(u8, line0, "B"));
|
||||||
|
|
||||||
_, _, const root = try buffer.root.insert_chars(0, 0, "1", buffer.a);
|
_, _, var root = try buffer.root.insert_chars(0, 0, "1", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line1 = try get_line(buffer, 0);
|
const line1 = try get_line(buffer, 0);
|
||||||
defer a.free(line1);
|
defer a.free(line1);
|
||||||
try std.testing.expect(std.mem.eql(u8, line1, "1B"));
|
try std.testing.expect(std.mem.eql(u8, line1, "1B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 1, "2", buffer.a);
|
_, _, root = try root.insert_chars(0, 1, "2", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line2 = try get_line(buffer, 0);
|
const line2 = try get_line(buffer, 0);
|
||||||
defer a.free(line2);
|
defer a.free(line2);
|
||||||
try std.testing.expect(std.mem.eql(u8, line2, "12B"));
|
try std.testing.expect(std.mem.eql(u8, line2, "12B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 2, "3", buffer.a);
|
_, _, root = try root.insert_chars(0, 2, "3", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line3 = try get_line(buffer, 0);
|
const line3 = try get_line(buffer, 0);
|
||||||
defer a.free(line3);
|
defer a.free(line3);
|
||||||
try std.testing.expect(std.mem.eql(u8, line3, "123B"));
|
try std.testing.expect(std.mem.eql(u8, line3, "123B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 3, "4", buffer.a);
|
_, _, root = try root.insert_chars(0, 3, "4", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line4 = try get_line(buffer, 0);
|
const line4 = try get_line(buffer, 0);
|
||||||
defer a.free(line4);
|
defer a.free(line4);
|
||||||
try std.testing.expect(std.mem.eql(u8, line4, "1234B"));
|
try std.testing.expect(std.mem.eql(u8, line4, "1234B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 4, "5", buffer.a);
|
_, _, root = try root.insert_chars(0, 4, "5", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line5 = try get_line(buffer, 0);
|
const line5 = try get_line(buffer, 0);
|
||||||
defer a.free(line5);
|
defer a.free(line5);
|
||||||
try std.testing.expect(std.mem.eql(u8, line5, "12345B"));
|
try std.testing.expect(std.mem.eql(u8, line5, "12345B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 5, "6", buffer.a);
|
_, _, root = try root.insert_chars(0, 5, "6", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line6 = try get_line(buffer, 0);
|
const line6 = try get_line(buffer, 0);
|
||||||
defer a.free(line6);
|
defer a.free(line6);
|
||||||
try std.testing.expect(std.mem.eql(u8, line6, "123456B"));
|
try std.testing.expect(std.mem.eql(u8, line6, "123456B"));
|
||||||
|
|
||||||
_, _, root = try root.insert_chars(0, 6, "7", buffer.a);
|
_, _, root = try root.insert_chars(0, 6, "7", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line7 = try get_line(buffer, 0);
|
const line7 = try get_line(buffer, 0);
|
||||||
defer a.free(line7);
|
defer a.free(line7);
|
||||||
try std.testing.expect(std.mem.eql(u8, line7, "1234567B"));
|
try std.testing.expect(std.mem.eql(u8, line7, "1234567B"));
|
||||||
|
|
||||||
const line, const col, root = try buffer.root.insert_chars(0, 7, "8\n9", buffer.a);
|
const line, const col, root = try buffer.root.insert_chars(0, 7, "8\n9", buffer.a, metrics());
|
||||||
buffer.update(root);
|
buffer.update(root);
|
||||||
|
|
||||||
const line8 = try get_line(buffer, 0);
|
const line8 = try get_line(buffer, 0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue