Search Apps Documentation Source Content File Folder Download Copy Actions Download

murmur3_64_test.gno

2.32 Kb · 101 lines
  1package murmur3_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/jeronimoalbi/murmur3"
  7)
  8
  9func TestHash64Sum64(t *testing.T) {
 10	// Test cases -> https://en.wikipedia.org/wiki/MurmurHash#Algorithm
 11	tests := []struct {
 12		name string
 13		data string
 14		want uint64
 15	}{
 16		{"empty", "", 0x00000000514e28b7},
 17		{"test", "test", 0xba6bd21399c02ae2},
 18		{"hello", "Hello, world!", 0xc0363e43aa5dc85b},
 19		{"pangram", "The quick brown fox jumps over the lazy dog", 0x2e4ff72378e69e27},
 20	}
 21
 22	for _, tt := range tests {
 23		t.Run(tt.name, func(t *testing.T) {
 24			got := murmur3.Sum64([]byte(tt.data))
 25			if got != tt.want {
 26				t.Errorf("Sum64(%q) = 0x%016x, want 0x%016x", tt.data, got, tt.want)
 27			}
 28		})
 29	}
 30}
 31
 32func TestHash64InterfaceSizes(t *testing.T) {
 33	h := murmur3.New64()
 34	if h.Size() != 8 {
 35		t.Errorf("Size() = %d, want 8", h.Size())
 36	}
 37	if h.BlockSize() != 4 {
 38		t.Errorf("BlockSize() = %d, want 4", h.BlockSize())
 39	}
 40}
 41
 42func TestHash64IncrementalWrite(t *testing.T) {
 43	want := uint64(13850326164366542939)
 44	data := []byte("Hello, world!")
 45
 46	// Hash one byte at a time
 47	h := murmur3.New64()
 48	for _, b := range data {
 49		h.Write([]byte{b})
 50	}
 51
 52	if got := h.Sum64(); got != want {
 53		t.Errorf("incremental Write got 0x%016x, want 0x%016x", got, want)
 54	}
 55
 56	// Hash in two parts
 57	h.Reset()
 58	h.Write(data[:5])
 59	h.Write(data[5:])
 60	if got := h.Sum64(); got != want {
 61		t.Errorf("two-part Write got 0x%016x, want 0x%016x", got, want)
 62	}
 63}
 64
 65func TestHash64Sum(t *testing.T) {
 66	h := murmur3.New64()
 67	h.Write([]byte("Hello, world!"))
 68	b := h.Sum(nil)
 69	if len(b) != 8 {
 70		t.Fatalf("Sum(nil) returned %d bytes, want 8", len(b))
 71	}
 72
 73	got := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
 74		uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
 75	want := uint64(13850326164366542939)
 76	if got != want {
 77		t.Errorf("Sum got 0x%016x, want 0x%016x", got, want)
 78	}
 79}
 80
 81func TestHash64SumDoesNotChangeState(t *testing.T) {
 82	h := murmur3.New64()
 83	h.Write([]byte("Hello"))
 84	s1 := h.Sum64()
 85	s2 := h.Sum64()
 86	if s1 != s2 {
 87		t.Errorf("Sum64 changed state: first 0x%016x, second 0x%016x", s1, s2)
 88	}
 89}
 90
 91func TestHash64Reset(t *testing.T) {
 92	h := murmur3.New64()
 93	h.Write([]byte("some data"))
 94	h.Reset()
 95	h.Write([]byte("Hello, world!"))
 96	got := h.Sum64()
 97	want := uint64(13850326164366542939)
 98	if got != want {
 99		t.Errorf("after Reset got 0x%016x, want 0x%016x", got, want)
100	}
101}