add xxhash 64

This commit is contained in:
tursom 2023-08-01 16:04:19 +08:00
parent 271efb99b3
commit e461eca3fc
5 changed files with 100 additions and 1 deletions

32
checksum/xxh.go Normal file
View File

@ -0,0 +1,32 @@
package checksum
import (
"github.com/cespare/xxhash"
"hash"
)
type (
Xxh64 struct {
d hash.Hash
}
)
func (m *Xxh64) String() string {
return "xxhash64"
}
func (m *Xxh64) Append(data []byte) {
if m.d == nil {
m.d = xxhash.New()
}
m.d.Write(data)
}
func (m *Xxh64) Finish() []byte {
if m.d == nil {
m.d = xxhash.New()
}
return m.d.Sum(nil)
}

57
checksum/xxhash64_test.go Normal file
View File

@ -0,0 +1,57 @@
package checksum
import (
"github.com/cespare/xxhash"
"github.com/tursom/checksum/assert"
"hash"
"testing"
)
func TestXxh64_Finish(t *testing.T) {
type fields struct {
d hash.Hash
}
tests := []struct {
name string
fields fields
builder func(m *Xxh64)
want []byte
}{
{
"nil",
fields{},
nil,
xxhash.New().Sum(nil),
},
{
"hello",
fields{},
func(m *Xxh64) {
m.Append([]byte("hello"))
},
xxh64Sum([]byte("hello")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Xxh64{
d: tt.fields.d,
}
if tt.builder != nil {
tt.builder(m)
}
assert.Equals(t, m.Finish(), tt.want)
})
}
}
func xxh64Sum(b []byte) []byte {
h := xxhash.New()
_, _ = h.Write(b)
return h.Sum(nil)
}
func TestXxh64_String(t *testing.T) {
assert.Equals(t, (&Xxh64{}).String(), "xxhash64")
}

View File

@ -31,6 +31,7 @@ var (
func() checksum.Func { return &checksum.Fnv64a{} },
func() checksum.Func { return &checksum.Fnv128{} },
func() checksum.Func { return &checksum.Fnv128a{} },
func() checksum.Func { return &checksum.Xxh64{} },
}
)

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/tursom/checksum
go 1.20
require github.com/twmb/murmur3 v1.1.8
require (
github.com/cespare/xxhash v1.1.0
github.com/twmb/murmur3 v1.1.8
)

6
go.sum
View File

@ -1,2 +1,8 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/twmb/murmur3 v1.1.8 h1:8Yt9taO/WN3l08xErzjeschgZU2QSrwm1kclYq+0aRg=
github.com/twmb/murmur3 v1.1.8/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=