|
| 1 | +const std = @import("std"); |
| 2 | +const delta_encode = @import("git/delta_encode.zig"); |
| 3 | + |
| 4 | +test "createDelta produces valid delta for similar content" { |
| 5 | + const allocator = std.testing.allocator; |
| 6 | + const base = "Hello world this is a test file with some content that is fairly long to ensure delta encoding works properly"; |
| 7 | + const target = "Hello world this is a test file with some content that is fairly long to ensure delta encoding works properly - modified slightly"; |
| 8 | + |
| 9 | + const delta = try delta_encode.createDelta(allocator, base, target); |
| 10 | + defer allocator.free(delta); |
| 11 | + |
| 12 | + // Delta should be smaller than the target |
| 13 | + try std.testing.expect(delta.len < target.len); |
| 14 | + // Delta should not be empty |
| 15 | + try std.testing.expect(delta.len > 0); |
| 16 | +} |
| 17 | + |
| 18 | +test "createDelta for identical content" { |
| 19 | + const allocator = std.testing.allocator; |
| 20 | + const base = "identical content here"; |
| 21 | + const target = "identical content here"; |
| 22 | + |
| 23 | + const delta = try delta_encode.createDelta(allocator, base, target); |
| 24 | + defer allocator.free(delta); |
| 25 | + |
| 26 | + try std.testing.expect(delta.len > 0); |
| 27 | + // Delta for identical content should be very small (just header + copy) |
| 28 | + try std.testing.expect(delta.len < target.len); |
| 29 | +} |
| 30 | + |
| 31 | +test "createDelta for completely different content" { |
| 32 | + const allocator = std.testing.allocator; |
| 33 | + const base = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 34 | + const target = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; |
| 35 | + |
| 36 | + const delta = try delta_encode.createDelta(allocator, base, target); |
| 37 | + defer allocator.free(delta); |
| 38 | + |
| 39 | + // Even for different content, delta should be produced |
| 40 | + try std.testing.expect(delta.len > 0); |
| 41 | +} |
| 42 | + |
| 43 | +test "delta encoding saves space for similar blobs" { |
| 44 | + const allocator = std.testing.allocator; |
| 45 | + |
| 46 | + // Simulate similar blob objects (like successive versions of a file) |
| 47 | + const blob1 = "line 1: this is the first line\nline 2: this is the second line\nline 3: this is the third line\nline 4: this is the fourth line\nline 5: this is the fifth line\n"; |
| 48 | + const blob2 = "line 1: this is the first line\nline 2: this is the MODIFIED second line\nline 3: this is the third line\nline 4: this is the fourth line\nline 5: this is the fifth line\nline 6: added a new line\n"; |
| 49 | + |
| 50 | + const delta = try delta_encode.createDelta(allocator, blob1, blob2); |
| 51 | + defer allocator.free(delta); |
| 52 | + |
| 53 | + // Delta should be significantly smaller than full target |
| 54 | + try std.testing.expect(delta.len < blob2.len); |
| 55 | +} |
| 56 | + |
| 57 | +test "delta window selects best base" { |
| 58 | + // Verify that among multiple candidates, a closer match produces a smaller delta |
| 59 | + const allocator = std.testing.allocator; |
| 60 | + |
| 61 | + const target = "The quick brown fox jumps over the lazy dog. Extra stuff here for padding to make it long enough."; |
| 62 | + const bad_base = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
| 63 | + const good_base = "The quick brown fox jumps over the lazy cat. Extra stuff here for padding to make it long enough."; |
| 64 | + |
| 65 | + const delta_bad = try delta_encode.createDelta(allocator, bad_base, target); |
| 66 | + defer allocator.free(delta_bad); |
| 67 | + const delta_good = try delta_encode.createDelta(allocator, good_base, target); |
| 68 | + defer allocator.free(delta_good); |
| 69 | + |
| 70 | + // Good base should produce smaller delta |
| 71 | + try std.testing.expect(delta_good.len < delta_bad.len); |
| 72 | +} |
0 commit comments