Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions xxhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (d *Digest) ResetWithSeed(seed uint64) {
d.n = 0
}

// Copy returns a copy of the Digest.
func (d *Digest) Copy() *Digest {
digest := *d
copy(digest.mem[:], d.mem[:])
return &digest
}

// Size always returns 8 bytes.
func (d *Digest) Size() int { return 8 }

Expand Down
17 changes: 17 additions & 0 deletions xxhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ func TestAllocs(t *testing.T) {
})
}

func TestCopy(t *testing.T) {
want := Sum64String("abcdef")

d0 := New()
d0.WriteString("abc")
d1 := d0.Copy()
d0.WriteString("def")
d1.WriteString("def")

if got := d0.Sum64(); got != want {
t.Fatalf("hashing with original Digest, got 0x%x; want 0x%x", got, want)
}
if got := d1.Sum64(); got != want {
t.Fatalf("hashing with copied Digest, got 0x%x; want 0x%x", got, want)
}
}

func testAllocs(t *testing.T, fn func()) {
t.Helper()
if allocs := int(testing.AllocsPerRun(10, fn)); allocs > 0 {
Expand Down