Skip to content

Commit

Permalink
implement Equal for IntervalDigest
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Mar 19, 2021
1 parent e0a317a commit 35ec90d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion namespace/digest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package namespace

import "fmt"
import (
"bytes"
"fmt"
)

type IntervalDigest struct {
min ID
Expand Down Expand Up @@ -40,6 +43,10 @@ func (d IntervalDigest) Bytes() []byte {
return append(append(d.min, d.max...), d.digest...)
}

func (d *IntervalDigest) Equal(to *IntervalDigest) bool {
return d.max.Equal(to.max) && d.min.Equal(to.min) && bytes.Equal(d.digest, to.digest)
}

func (d IntervalDigest) String() string {
return fmt.Sprintf(
`{
Expand Down
28 changes: 28 additions & 0 deletions namespace/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,31 @@ func TestIntervalDigest_String(t *testing.T) {
})
}
}

func TestIntervalDigest_Equal(t *testing.T) {
tests := []struct {
name string
one, two *IntervalDigest
want bool
}{
{
"equal",
&IntervalDigest{[]byte{0}, []byte{1}, []byte{}},
&IntervalDigest{[]byte{0}, []byte{1}, []byte{}},
true,
},
{
"unequal",
&IntervalDigest{[]byte{0}, []byte{1}, []byte{1, 0, 0}},
&IntervalDigest{[]byte{0}, []byte{1}, []byte{1, 1, 1}},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.one.Equal(tt.two); got != tt.want {
t.Errorf("Equal() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 35ec90d

Please sign in to comment.