Skip to content

Commit d360348

Browse files
committed
diffCleanupMerge tests
1 parent feeba61 commit d360348

1 file changed

Lines changed: 121 additions & 9 deletions

File tree

DiffMatchPatch.zig

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ fn diffCleanupMerge(allocator: std.mem.Allocator, diffs: *std.ArrayListUnmanaged
673673
std.mem.copy(u8, nt, diffs.items[ii].text);
674674
std.mem.copy(u8, nt[diffs.items[ii].text.len..], text_insert.items[0..common_length]);
675675

676-
allocator.free(diffs.items[ii].text);
676+
// allocator.free(diffs.items[ii].text);
677677
diffs.items[ii].text = nt;
678678
} else {
679679
// diffs.Insert(0, new Diff(Operation.EQUAL,
@@ -1225,6 +1225,10 @@ fn diffCommonOverlap(text1_in: []const u8, text2_in: []const u8) usize {
12251225
// // }
12261226
// }
12271227

1228+
// TODO: Allocate all text in diffs to
1229+
// not cause segfault while freeing; not a problem
1230+
// at the moment because we don't free anything :P
1231+
12281232
test diffCommonPrefix {
12291233
// Detect any common suffix.
12301234
try testing.expectEqual(@as(usize, 0), diffCommonPrefix("abc", "xyz")); // Null case
@@ -1432,27 +1436,53 @@ test diffCleanupMerge {
14321436
var diffs = std.ArrayListUnmanaged(Diff){};
14331437
try std.testing.expectEqualDeep(@as([]const Diff, &[0]Diff{}), diffs.items); // Null case
14341438

1435-
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .insert, .text = "c" } });
1439+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1440+
.{ .operation = .equal, .text = "a" },
1441+
.{ .operation = .delete, .text = "b" },
1442+
.{ .operation = .insert, .text = "c" },
1443+
});
14361444
try diffCleanupMerge(arena.allocator(), &diffs);
1437-
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .insert, .text = "c" } }), diffs.items); // No change case
1445+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1446+
.{ .operation = .equal, .text = "a" },
1447+
.{ .operation = .delete, .text = "b" },
1448+
.{ .operation = .insert, .text = "c" },
1449+
}), diffs.items); // No change case
14381450

14391451
diffs.items.len = 0;
14401452

1441-
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .equal, .text = "a" }, .{ .operation = .equal, .text = "b" }, .{ .operation = .equal, .text = "c" } });
1453+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1454+
.{ .operation = .equal, .text = "a" },
1455+
.{ .operation = .equal, .text = "b" },
1456+
.{ .operation = .equal, .text = "c" },
1457+
});
14421458
try diffCleanupMerge(arena.allocator(), &diffs);
1443-
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .equal, .text = "abc" }}), diffs.items); // Merge equalities
1459+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1460+
.{ .operation = .equal, .text = "abc" },
1461+
}), diffs.items); // Merge equalities
14441462

14451463
diffs.items.len = 0;
14461464

1447-
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .delete, .text = "a" }, .{ .operation = .delete, .text = "b" }, .{ .operation = .delete, .text = "c" } });
1465+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1466+
.{ .operation = .delete, .text = "a" },
1467+
.{ .operation = .delete, .text = "b" },
1468+
.{ .operation = .delete, .text = "c" },
1469+
});
14481470
try diffCleanupMerge(arena.allocator(), &diffs);
1449-
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .delete, .text = "abc" }}), diffs.items); // Merge deletions
1471+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1472+
.{ .operation = .delete, .text = "abc" },
1473+
}), diffs.items); // Merge deletions
14501474

14511475
diffs.items.len = 0;
14521476

1453-
try diffs.appendSlice(arena.allocator(), &[_]Diff{ .{ .operation = .insert, .text = "a" }, .{ .operation = .insert, .text = "b" }, .{ .operation = .insert, .text = "c" } });
1477+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1478+
.{ .operation = .insert, .text = "a" },
1479+
.{ .operation = .insert, .text = "b" },
1480+
.{ .operation = .insert, .text = "c" },
1481+
});
14541482
try diffCleanupMerge(arena.allocator(), &diffs);
1455-
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{.{ .operation = .insert, .text = "abc" }}), diffs.items); // Merge insertions
1483+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1484+
.{ .operation = .insert, .text = "abc" },
1485+
}), diffs.items); // Merge insertions
14561486

14571487
diffs.items.len = 0;
14581488

@@ -1502,6 +1532,88 @@ test diffCleanupMerge {
15021532
.{ .operation = .insert, .text = "b" },
15031533
.{ .operation = .equal, .text = "cy" },
15041534
}), diffs.items); // Prefix and suffix detection with equalities
1535+
1536+
diffs.items.len = 0;
1537+
1538+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1539+
.{ .operation = .equal, .text = "a" },
1540+
.{ .operation = .insert, .text = "ba" },
1541+
.{ .operation = .equal, .text = "c" },
1542+
});
1543+
try diffCleanupMerge(arena.allocator(), &diffs);
1544+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1545+
.{ .operation = .insert, .text = "ab" },
1546+
.{ .operation = .equal, .text = "ac" },
1547+
}), diffs.items); // Slide edit left
1548+
1549+
diffs.items.len = 0;
1550+
1551+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1552+
.{ .operation = .equal, .text = "c" },
1553+
.{ .operation = .insert, .text = "ab" },
1554+
.{ .operation = .equal, .text = "a" },
1555+
});
1556+
try diffCleanupMerge(arena.allocator(), &diffs);
1557+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1558+
.{ .operation = .equal, .text = "ca" },
1559+
.{ .operation = .insert, .text = "ba" },
1560+
}), diffs.items); // Slide edit right
1561+
1562+
diffs.items.len = 0;
1563+
1564+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1565+
Diff.init(.equal, "a"),
1566+
Diff.init(.delete, "b"),
1567+
Diff.init(.equal, "c"),
1568+
Diff.init(.delete, "ac"),
1569+
Diff.init(.equal, "x"),
1570+
});
1571+
try diffCleanupMerge(arena.allocator(), &diffs);
1572+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1573+
Diff.init(.delete, "abc"),
1574+
Diff.init(.equal, "acx"),
1575+
}), diffs.items); // Slide edit left recursive
1576+
1577+
diffs.items.len = 0;
1578+
1579+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1580+
Diff.init(.equal, "x"),
1581+
Diff.init(.delete, "ca"),
1582+
Diff.init(.equal, "c"),
1583+
Diff.init(.delete, "b"),
1584+
Diff.init(.equal, "a"),
1585+
});
1586+
try diffCleanupMerge(arena.allocator(), &diffs);
1587+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1588+
Diff.init(.equal, "xca"),
1589+
Diff.init(.delete, "cba"),
1590+
}), diffs.items); // Slide edit right recursive
1591+
1592+
diffs.items.len = 0;
1593+
1594+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1595+
Diff.init(.delete, "b"),
1596+
Diff.init(.insert, "ab"),
1597+
Diff.init(.equal, "c"),
1598+
});
1599+
try diffCleanupMerge(arena.allocator(), &diffs);
1600+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1601+
Diff.init(.insert, "a"),
1602+
Diff.init(.equal, "bc"),
1603+
}), diffs.items); // Empty merge
1604+
1605+
diffs.items.len = 0;
1606+
1607+
try diffs.appendSlice(arena.allocator(), &[_]Diff{
1608+
Diff.init(.equal, ""),
1609+
Diff.init(.insert, "a"),
1610+
Diff.init(.equal, "b"),
1611+
});
1612+
try diffCleanupMerge(arena.allocator(), &diffs);
1613+
try std.testing.expectEqualDeep(@as([]const Diff, &[_]Diff{
1614+
Diff.init(.insert, "a"),
1615+
Diff.init(.equal, "b"),
1616+
}), diffs.items); // Empty equality
15051617
}
15061618

15071619
fn rebuildtexts(allocator: std.mem.Allocator, diffs: std.ArrayListUnmanaged(Diff)) ![2][]const u8 {

0 commit comments

Comments
 (0)