summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--methods_test.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/methods_test.go b/methods_test.go
index f5d5a9a..64af28c 100644
--- a/methods_test.go
+++ b/methods_test.go
@@ -30,6 +30,10 @@ type SumRun struct {
Sums ShortSum
}
+func (run SumRun) Size() int64 {
+ return int64(len(run.Sums)/(2*run.ChecksumSize)) * (4 * 1024)
+}
+
type SumRunWithGaps struct {
Addr int64
Size int64
@@ -47,13 +51,19 @@ func (sg SumRunWithGaps) EncodeJSON(w io.Writer) error {
return err
}
}
- if run.Addr > cur {
+ switch {
+ case run.Addr < cur:
+ return fmt.Errorf("invalid %T: addr went backwards: %v < %v", sg, run.Addr, cur)
+ case run.Addr > cur:
if _, err := fmt.Fprintf(w, `{"Gap":%d},`, run.Addr-cur); err != nil {
return err
}
- }
- if err := lowmemjson.NewEncoder(w).Encode(run); err != nil {
- return err
+ fallthrough
+ default:
+ if err := lowmemjson.NewEncoder(w).Encode(run); err != nil {
+ return err
+ }
+ cur = run.Addr + run.Size()
}
}
end := sg.Addr + sg.Size
@@ -110,14 +120,14 @@ func TestMethods(t *testing.T) {
{
ChecksumSize: 4,
Addr: 1095761920,
- Sums: "c160817cb5c72bbbe",
+ Sums: "c160817cb5c72bbb",
},
},
}
var buf bytes.Buffer
assert.NoError(t, lowmemjson.NewEncoder(&buf).Encode(in))
assert.Equal(t,
- `{"Addr":13631488,"Size":416033783808,"Runs":[{"Gap":1082130432},{"ChecksumSize":4,"Addr":1095761920,"Sums":"c160817cb5c72bbbe"},{"Gap":416033783808}]}`,
+ `{"Addr":13631488,"Size":416033783808,"Runs":[{"Gap":1082130432},{"ChecksumSize":4,"Addr":1095761920,"Sums":"c160817cb5c72bbb"},{"Gap":414951645184}]}`,
buf.String())
var out SumRunWithGaps
assert.NoError(t, lowmemjson.NewDecoder(&buf).Decode(&out))