Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit f0106fa

Browse files
ajnavarromcuadros
authored andcommitted
packfile: delta diff implementation (#159)
* packfile: delta diff implementation - Renamed delta.go to patch_delta.go - Added diff_delta.go file - Added tests that creates a diff and then tries to patch it * Requested changes
1 parent 7efc7ce commit f0106fa

File tree

4 files changed

+637
-0
lines changed

4 files changed

+637
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package packfile
2+
3+
import (
4+
"fmt"
5+
6+
. "gopkg.in/check.v1"
7+
)
8+
9+
type DeltaSuite struct {
10+
testCases []deltaTest
11+
}
12+
13+
var _ = Suite(&DeltaSuite{})
14+
15+
type piece struct {
16+
val string
17+
times int
18+
}
19+
20+
type deltaTest struct {
21+
description string
22+
base []piece
23+
target []piece
24+
}
25+
26+
func (s *DeltaSuite) SetUpSuite(c *C) {
27+
s.testCases = []deltaTest{{
28+
description: "distinct file",
29+
base: []piece{{"0", 300}},
30+
target: []piece{{"2", 200}},
31+
}, {
32+
description: "same file",
33+
base: []piece{{"1", 3000}},
34+
target: []piece{{"1", 3000}},
35+
}, {
36+
description: "small file",
37+
base: []piece{{"1", 3}},
38+
target: []piece{{"1", 3}, {"0", 1}},
39+
}, {
40+
description: "big file",
41+
base: []piece{{"1", 300000}},
42+
target: []piece{{"1", 30000}, {"0", 1000000}},
43+
}, {
44+
description: "add elements before",
45+
base: []piece{{"0", 200}},
46+
target: []piece{{"1", 300}, {"0", 200}},
47+
}, {
48+
description: "add 10 times more elements at the end",
49+
base: []piece{{"1", 300}, {"0", 200}},
50+
target: []piece{{"0", 2000}},
51+
}, {
52+
description: "add elements between",
53+
base: []piece{{"0", 400}},
54+
target: []piece{{"0", 200}, {"1", 200}, {"0", 200}},
55+
}, {
56+
description: "add elements after",
57+
base: []piece{{"0", 200}},
58+
target: []piece{{"0", 200}, {"1", 200}},
59+
}, {
60+
description: "modify elements at the end",
61+
base: []piece{{"1", 300}, {"0", 200}},
62+
target: []piece{{"0", 100}},
63+
}, {
64+
description: "complex modification",
65+
base: []piece{{"0", 3}, {"1", 40}, {"2", 30}, {"3", 2},
66+
{"4", 400}, {"5", 23}},
67+
target: []piece{{"1", 30}, {"2", 20}, {"7", 40}, {"4", 400},
68+
{"5", 10}},
69+
}}
70+
}
71+
72+
func (s *DeltaSuite) TestAddDelta(c *C) {
73+
for _, t := range s.testCases {
74+
baseBuf := genBytes(t.base)
75+
targetBuf := genBytes(t.target)
76+
delta := DiffDelta(baseBuf, targetBuf)
77+
result := PatchDelta(baseBuf, delta)
78+
79+
c.Log(fmt.Printf("Executing test case: %s\n", t.description))
80+
c.Assert(result, DeepEquals, targetBuf)
81+
}
82+
}
83+
84+
func genBytes(elements []piece) []byte {
85+
var result []byte
86+
for _, e := range elements {
87+
for i := 0; i < e.times; i++ {
88+
result = append(result, e.val...)
89+
}
90+
}
91+
92+
return result
93+
}

0 commit comments

Comments
 (0)