diff options
author | Antonio Navarro Perez <antnavper@gmail.com> | 2016-12-09 17:52:03 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-09 17:52:03 +0100 |
commit | 3ba5019e406ab25ee0a658dd2166fa4ac53c52a3 (patch) | |
tree | a46974e4b0761cacef025af770edd44df8f23cd4 /plumbing/format/packfile/delta_test.go | |
parent | f36a76ed05755b5fa574f85f695dd3a9c26b48c3 (diff) | |
download | go-git-3ba5019e406ab25ee0a658dd2166fa4ac53c52a3.tar.gz |
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
Diffstat (limited to 'plumbing/format/packfile/delta_test.go')
-rw-r--r-- | plumbing/format/packfile/delta_test.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/plumbing/format/packfile/delta_test.go b/plumbing/format/packfile/delta_test.go new file mode 100644 index 0000000..4ef9b70 --- /dev/null +++ b/plumbing/format/packfile/delta_test.go @@ -0,0 +1,93 @@ +package packfile + +import ( + "fmt" + + . "gopkg.in/check.v1" +) + +type DeltaSuite struct { + testCases []deltaTest +} + +var _ = Suite(&DeltaSuite{}) + +type piece struct { + val string + times int +} + +type deltaTest struct { + description string + base []piece + target []piece +} + +func (s *DeltaSuite) SetUpSuite(c *C) { + s.testCases = []deltaTest{{ + description: "distinct file", + base: []piece{{"0", 300}}, + target: []piece{{"2", 200}}, + }, { + description: "same file", + base: []piece{{"1", 3000}}, + target: []piece{{"1", 3000}}, + }, { + description: "small file", + base: []piece{{"1", 3}}, + target: []piece{{"1", 3}, {"0", 1}}, + }, { + description: "big file", + base: []piece{{"1", 300000}}, + target: []piece{{"1", 30000}, {"0", 1000000}}, + }, { + description: "add elements before", + base: []piece{{"0", 200}}, + target: []piece{{"1", 300}, {"0", 200}}, + }, { + description: "add 10 times more elements at the end", + base: []piece{{"1", 300}, {"0", 200}}, + target: []piece{{"0", 2000}}, + }, { + description: "add elements between", + base: []piece{{"0", 400}}, + target: []piece{{"0", 200}, {"1", 200}, {"0", 200}}, + }, { + description: "add elements after", + base: []piece{{"0", 200}}, + target: []piece{{"0", 200}, {"1", 200}}, + }, { + description: "modify elements at the end", + base: []piece{{"1", 300}, {"0", 200}}, + target: []piece{{"0", 100}}, + }, { + description: "complex modification", + base: []piece{{"0", 3}, {"1", 40}, {"2", 30}, {"3", 2}, + {"4", 400}, {"5", 23}}, + target: []piece{{"1", 30}, {"2", 20}, {"7", 40}, {"4", 400}, + {"5", 10}}, + }} +} + +func (s *DeltaSuite) TestAddDelta(c *C) { + for _, t := range s.testCases { + baseBuf := genBytes(t.base) + targetBuf := genBytes(t.target) + delta := DiffDelta(baseBuf, targetBuf) + result := PatchDelta(baseBuf, delta) + + c.Log(fmt.Printf("Executing test case: %s\n", t.description)) + c.Assert(result, DeepEquals, targetBuf) + } +} + +func genBytes(elements []piece) []byte { + var result []byte + for _, e := range elements { + for i := 0; i < e.times; i++ { + result = append(result, e.val...) + } + } + + return result +} |