aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-23 12:03:50 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-23 12:03:50 +0200
commitc47d2184b2e6a1e7dbd139ca47bb3aebcfeb754f (patch)
tree251a99c8bfbececaf797dde8629d7f43334cb210
parent03e3d1aa9eb6e6917d2a9e6b85f7dd6f92e20d04 (diff)
downloaddna-backup-c47d2184b2e6a1e7dbd139ca47bb3aebcfeb754f.tar.gz
dna-backup-c47d2184b2e6a1e7dbd139ca47bb3aebcfeb754f.zip
add String to Delta struct and use it in logs
Also switch to external package for slice_test as it was already a case of "black box testing".
-rw-r--r--repo.go4
-rw-r--r--slice/slice.go18
-rw-r--r--slice/slice_test.go67
3 files changed, 61 insertions, 28 deletions
diff --git a/repo.go b/repo.go
index ec5935d..e5a73ec 100644
--- a/repo.go
+++ b/repo.go
@@ -345,7 +345,7 @@ func slice2fileList(s slice.Slice) (ret []File) {
func (r *Repo) storeFileList(version int, list []File) {
dest := filepath.Join(r.path, fmt.Sprintf(versionFmt, version), filesName)
delta := slice.Diff(fileList2slice(r.files), fileList2slice(list))
- logger.Info("files delta del: ", len(delta.Del), ", ins: ", len(delta.Ins))
+ logger.Infof("files delta %s", delta.String())
storeBasicStruct(dest, utils.NopWriteWrapper, delta)
}
@@ -713,7 +713,7 @@ func slice2recipe(s slice.Slice) (ret []Chunk) {
func (r *Repo) storeRecipe(version int, recipe []Chunk) {
dest := filepath.Join(r.path, fmt.Sprintf(versionFmt, version), recipeName)
delta := slice.Diff(recipe2slice(r.recipe), recipe2slice(recipe))
- logger.Info("recipe delta del: ", len(delta.Del), ", ins:", len(delta.Ins))
+ logger.Infof("recipe delta %s", delta.String())
storeBasicStruct(dest, utils.NopWriteWrapper, delta)
}
diff --git a/slice/slice.go b/slice/slice.go
index 05441c8..e5b959e 100644
--- a/slice/slice.go
+++ b/slice/slice.go
@@ -1,6 +1,9 @@
package slice
-import "reflect"
+import (
+ "fmt"
+ "reflect"
+)
type Slice []interface{}
@@ -11,11 +14,24 @@ type Ins struct {
Value []interface{}
}
+type insData struct {
+ idx int
+ count int
+}
+
type Delta struct {
Del []Del
Ins []Ins
}
+func (d Delta) String() string {
+ data := make([]insData, len(d.Ins))
+ for i, ins := range d.Ins {
+ data[i] = insData{ins.Idx, len(ins.Value)}
+ }
+ return fmt.Sprintf("{Del: %d Ins: %+v}", d.Del, data)
+}
+
func Patch(source Slice, delta Delta) (target Slice) {
// apply Del part from patch to source into temp
size := len(source) - len(delta.Del)
diff --git a/slice/slice_test.go b/slice/slice_test.go
index d678fc7..8cb2a68 100644
--- a/slice/slice_test.go
+++ b/slice/slice_test.go
@@ -1,32 +1,33 @@
-package slice
+package slice_test
import (
"testing"
+ "github.com/n-peugnet/dna-backup/slice"
"github.com/n-peugnet/dna-backup/testutils"
)
func TestPatch(t *testing.T) {
- source := Slice{1, 2, 3, 4}
- target := Slice{2, 5, 3, 6, 4, 7, 8}
- patch := Diff(source, target)
- testutils.AssertSame(t, []Del{0}, patch.Del, "Patch del part")
- testutils.AssertSame(t, []Ins{
- {1, Slice{5}},
- {3, Slice{6}},
- {5, Slice{7, 8}},
+ source := slice.Slice{1, 2, 3, 4}
+ target := slice.Slice{2, 5, 3, 6, 4, 7, 8}
+ patch := slice.Diff(source, target)
+ testutils.AssertSame(t, []slice.Del{0}, patch.Del, "Patch del part")
+ testutils.AssertSame(t, []slice.Ins{
+ {1, slice.Slice{5}},
+ {3, slice.Slice{6}},
+ {5, slice.Slice{7, 8}},
}, patch.Ins, "Patch ins part")
- actual := Patch(source, patch)
+ actual := slice.Patch(source, patch)
testutils.AssertSame(t, target, actual, "Target obtained from patch application")
}
func TestEmptyPatch(t *testing.T) {
- source := Slice{1, 2, 3, 4}
- target := Slice{1, 2, 3, 4}
- patch := Diff(source, target)
- testutils.AssertSame(t, *new([]Del), patch.Del, "Patch del part")
- testutils.AssertSame(t, *new([]Ins), patch.Ins, "Patch ins part")
- actual := Patch(source, patch)
+ source := slice.Slice{1, 2, 3, 4}
+ target := slice.Slice{1, 2, 3, 4}
+ patch := slice.Diff(source, target)
+ testutils.AssertSame(t, *new([]slice.Del), patch.Del, "Patch del part")
+ testutils.AssertSame(t, *new([]slice.Ins), patch.Ins, "Patch ins part")
+ actual := slice.Patch(source, patch)
testutils.AssertSame(t, target, actual, "Target obtained from patch application")
}
@@ -36,15 +37,31 @@ type i struct {
func TestStruct(t *testing.T) {
c1, c2, c3, c4, c5, c6, c7, c8 := &i{1}, &i{2}, &i{3}, &i{4}, &i{5}, &i{6}, &i{7}, &i{8}
- source := Slice{c1, c2, c3, c4}
- target := Slice{&i{5}, c2, c5, c6, &i{4}, c7, &i{8}}
- patch := Diff(source, target)
- testutils.AssertSame(t, []Del{0, 2}, patch.Del, "Patch del part")
- testutils.AssertSame(t, []Ins{
- {0, Slice{c5}},
- {2, Slice{c5, c6}},
- {5, Slice{c7, c8}},
+ source := slice.Slice{c1, c2, c3, c4}
+ target := slice.Slice{&i{5}, c2, c5, c6, &i{4}, c7, &i{8}}
+ patch := slice.Diff(source, target)
+ testutils.AssertSame(t, []slice.Del{0, 2}, patch.Del, "Patch del part")
+ testutils.AssertSame(t, []slice.Ins{
+ {0, slice.Slice{c5}},
+ {2, slice.Slice{c5, c6}},
+ {5, slice.Slice{c7, c8}},
}, patch.Ins, "Patch ins part")
- actual := Patch(source, patch)
+ actual := slice.Patch(source, patch)
testutils.AssertSame(t, target, actual, "Target obtained from patch application")
}
+
+func TestDeltaString(t *testing.T) {
+ delta := slice.Delta{
+ Del: []slice.Del{0, 3, 4},
+ Ins: []slice.Ins{
+ {2, slice.Slice{6, 7, 8}},
+ {5, slice.Slice{5}},
+ },
+ }
+ testutils.AssertSame(
+ t,
+ "{Del: [0 3 4] Ins: [{idx:2 count:3} {idx:5 count:1}]}",
+ delta.String(),
+ "Delta string representation",
+ )
+}