Source file src/hash/fnv/fnv_test.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fnv
     6  
     7  import (
     8  	"bytes"
     9  	"encoding"
    10  	"encoding/binary"
    11  	"hash"
    12  	"internal/testhash"
    13  	"io"
    14  	"testing"
    15  )
    16  
    17  func TestHashInterface(t *testing.T) {
    18  	type test struct {
    19  		name string
    20  		fn   func() hash.Hash
    21  	}
    22  	fn32 := func(fn func() hash.Hash32) func() hash.Hash {
    23  		return func() hash.Hash { return fn() }
    24  	}
    25  	fn64 := func(fn func() hash.Hash64) func() hash.Hash {
    26  		return func() hash.Hash { return fn() }
    27  	}
    28  	tests := []test{
    29  		{"32", fn32(New32)},
    30  		{"32a", fn32(New32a)},
    31  		{"64", fn64(New64)},
    32  		{"64a", fn64(New64a)},
    33  		{"128", New128},
    34  		{"128a", New128a},
    35  	}
    36  	for _, test := range tests {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			testhash.TestHash(t, test.fn)
    39  		})
    40  	}
    41  }
    42  
    43  type golden struct {
    44  	out       []byte
    45  	in        string
    46  	halfState string // marshaled hash state after first half of in written, used by TestGoldenMarshal
    47  }
    48  
    49  var golden32 = []golden{
    50  	{[]byte{0x81, 0x1c, 0x9d, 0xc5}, "", "fnv\x01\x81\x1c\x9d\xc5"},
    51  	{[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a", "fnv\x01\x81\x1c\x9d\xc5"},
    52  	{[]byte{0x70, 0x77, 0x2d, 0x38}, "ab", "fnv\x01\x05\f]~"},
    53  	{[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc", "fnv\x01\x05\f]~"},
    54  }
    55  
    56  var golden32a = []golden{
    57  	{[]byte{0x81, 0x1c, 0x9d, 0xc5}, "", "fnv\x02\x81\x1c\x9d\xc5"},
    58  	{[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a", "fnv\x02\x81\x1c\x9d\xc5"},
    59  	{[]byte{0x4d, 0x25, 0x05, 0xca}, "ab", "fnv\x02\xe4\f),"},
    60  	{[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc", "fnv\x02\xe4\f),"},
    61  }
    62  
    63  var golden64 = []golden{
    64  	{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, "", "fnv\x03\xcb\xf2\x9c\xe4\x84\"#%"},
    65  	{[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a", "fnv\x03\xcb\xf2\x9c\xe4\x84\"#%"},
    66  	{[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab", "fnv\x03\xafc\xbdL\x86\x01\xb7\xbe"},
    67  	{[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc", "fnv\x03\xafc\xbdL\x86\x01\xb7\xbe"},
    68  }
    69  
    70  var golden64a = []golden{
    71  	{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, "", "fnv\x04\xcb\xf2\x9c\xe4\x84\"#%"},
    72  	{[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a", "fnv\x04\xcb\xf2\x9c\xe4\x84\"#%"},
    73  	{[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab", "fnv\x04\xafc\xdcL\x86\x01\xec\x8c"},
    74  	{[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc", "fnv\x04\xafc\xdcL\x86\x01\xec\x8c"},
    75  }
    76  
    77  var golden128 = []golden{
    78  	{[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, "", "fnv\x05lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
    79  	{[]byte{0xd2, 0x28, 0xcb, 0x69, 0x10, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x14, 0x1e}, "a", "fnv\x05lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
    80  	{[]byte{0x8, 0x80, 0x94, 0x5a, 0xee, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0x26, 0xc0, 0x88}, "ab", "fnv\x05\xd2(\xcbi\x10\x1a\x8c\xafx\x91+pNJ\x14\x1e"},
    81  	{[]byte{0xa6, 0x8b, 0xb2, 0xa4, 0x34, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x78, 0xc6, 0xae, 0xe7, 0x3b}, "abc", "fnv\x05\xd2(\xcbi\x10\x1a\x8c\xafx\x91+pNJ\x14\x1e"},
    82  }
    83  
    84  var golden128a = []golden{
    85  	{[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, "", "fnv\x06lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
    86  	{[]byte{0xd2, 0x28, 0xcb, 0x69, 0x6f, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x89, 0x64}, "a", "fnv\x06lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
    87  	{[]byte{0x08, 0x80, 0x95, 0x44, 0xbb, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0xb6, 0x9a, 0x62}, "ab", "fnv\x06\xd2(\xcbio\x1a\x8c\xafx\x91+pNJ\x89d"},
    88  	{[]byte{0xa6, 0x8d, 0x62, 0x2c, 0xec, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x79, 0x77, 0xaf, 0x7f, 0x3b}, "abc", "fnv\x06\xd2(\xcbio\x1a\x8c\xafx\x91+pNJ\x89d"},
    89  }
    90  
    91  func TestGolden32(t *testing.T) {
    92  	testGolden(t, New32(), golden32)
    93  }
    94  
    95  func TestGolden32a(t *testing.T) {
    96  	testGolden(t, New32a(), golden32a)
    97  }
    98  
    99  func TestGolden64(t *testing.T) {
   100  	testGolden(t, New64(), golden64)
   101  }
   102  
   103  func TestGolden64a(t *testing.T) {
   104  	testGolden(t, New64a(), golden64a)
   105  }
   106  
   107  func TestGolden128(t *testing.T) {
   108  	testGolden(t, New128(), golden128)
   109  }
   110  
   111  func TestGolden128a(t *testing.T) {
   112  	testGolden(t, New128a(), golden128a)
   113  }
   114  
   115  func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
   116  	for _, g := range gold {
   117  		hash.Reset()
   118  		done, error := hash.Write([]byte(g.in))
   119  		if error != nil {
   120  			t.Fatalf("write error: %s", error)
   121  		}
   122  		if done != len(g.in) {
   123  			t.Fatalf("wrote only %d out of %d bytes", done, len(g.in))
   124  		}
   125  		if actual := hash.Sum(nil); !bytes.Equal(g.out, actual) {
   126  			t.Errorf("hash(%q) = 0x%x want 0x%x", g.in, actual, g.out)
   127  		}
   128  	}
   129  }
   130  
   131  func TestGoldenMarshal(t *testing.T) {
   132  	tests := []struct {
   133  		name    string
   134  		newHash func() hash.Hash
   135  		gold    []golden
   136  	}{
   137  		{"32", func() hash.Hash { return New32() }, golden32},
   138  		{"32a", func() hash.Hash { return New32a() }, golden32a},
   139  		{"64", func() hash.Hash { return New64() }, golden64},
   140  		{"64a", func() hash.Hash { return New64a() }, golden64a},
   141  		{"128", func() hash.Hash { return New128() }, golden128},
   142  		{"128a", func() hash.Hash { return New128a() }, golden128a},
   143  	}
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			for _, g := range tt.gold {
   147  				h := tt.newHash()
   148  				h2 := tt.newHash()
   149  
   150  				io.WriteString(h, g.in[:len(g.in)/2])
   151  
   152  				state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
   153  				if err != nil {
   154  					t.Errorf("could not marshal: %v", err)
   155  					continue
   156  				}
   157  
   158  				stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
   159  				if err != nil {
   160  					t.Errorf("could not marshal: %v", err)
   161  					continue
   162  				}
   163  				stateAppend = stateAppend[4:]
   164  
   165  				if string(state) != g.halfState {
   166  					t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
   167  					continue
   168  				}
   169  
   170  				if string(stateAppend) != g.halfState {
   171  					t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
   172  					continue
   173  				}
   174  
   175  				if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
   176  					t.Errorf("could not unmarshal: %v", err)
   177  					continue
   178  				}
   179  
   180  				io.WriteString(h, g.in[len(g.in)/2:])
   181  				io.WriteString(h2, g.in[len(g.in)/2:])
   182  
   183  				if actual, actual2 := h.Sum(nil), h2.Sum(nil); !bytes.Equal(actual, actual2) {
   184  					t.Errorf("hash(%q) = 0x%x != marshaled 0x%x", g.in, actual, actual2)
   185  				}
   186  			}
   187  		})
   188  	}
   189  }
   190  
   191  func TestIntegrity32(t *testing.T) {
   192  	testIntegrity(t, New32())
   193  }
   194  
   195  func TestIntegrity32a(t *testing.T) {
   196  	testIntegrity(t, New32a())
   197  }
   198  
   199  func TestIntegrity64(t *testing.T) {
   200  	testIntegrity(t, New64())
   201  }
   202  
   203  func TestIntegrity64a(t *testing.T) {
   204  	testIntegrity(t, New64a())
   205  }
   206  func TestIntegrity128(t *testing.T) {
   207  	testIntegrity(t, New128())
   208  }
   209  
   210  func TestIntegrity128a(t *testing.T) {
   211  	testIntegrity(t, New128a())
   212  }
   213  
   214  func testIntegrity(t *testing.T, h hash.Hash) {
   215  	data := []byte{'1', '2', 3, 4, 5}
   216  	h.Write(data)
   217  	sum := h.Sum(nil)
   218  
   219  	if size := h.Size(); size != len(sum) {
   220  		t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
   221  	}
   222  
   223  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   224  		t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
   225  	}
   226  
   227  	h.Reset()
   228  	h.Write(data)
   229  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   230  		t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
   231  	}
   232  
   233  	h.Reset()
   234  	h.Write(data[:2])
   235  	h.Write(data[2:])
   236  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   237  		t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
   238  	}
   239  
   240  	switch h.Size() {
   241  	case 4:
   242  		sum32 := h.(hash.Hash32).Sum32()
   243  		if sum32 != binary.BigEndian.Uint32(sum) {
   244  			t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
   245  		}
   246  	case 8:
   247  		sum64 := h.(hash.Hash64).Sum64()
   248  		if sum64 != binary.BigEndian.Uint64(sum) {
   249  			t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
   250  		}
   251  	case 16:
   252  		// There's no Sum128 function, so we don't need to test anything here.
   253  	}
   254  }
   255  
   256  func BenchmarkFnv32KB(b *testing.B) {
   257  	benchmarkKB(b, New32())
   258  }
   259  
   260  func BenchmarkFnv32aKB(b *testing.B) {
   261  	benchmarkKB(b, New32a())
   262  }
   263  
   264  func BenchmarkFnv64KB(b *testing.B) {
   265  	benchmarkKB(b, New64())
   266  }
   267  
   268  func BenchmarkFnv64aKB(b *testing.B) {
   269  	benchmarkKB(b, New64a())
   270  }
   271  
   272  func BenchmarkFnv128KB(b *testing.B) {
   273  	benchmarkKB(b, New128())
   274  }
   275  
   276  func BenchmarkFnv128aKB(b *testing.B) {
   277  	benchmarkKB(b, New128a())
   278  }
   279  
   280  func benchmarkKB(b *testing.B, h hash.Hash) {
   281  	b.SetBytes(1024)
   282  	data := make([]byte, 1024)
   283  	for i := range data {
   284  		data[i] = byte(i)
   285  	}
   286  	in := make([]byte, 0, h.Size())
   287  
   288  	b.ResetTimer()
   289  	for i := 0; i < b.N; i++ {
   290  		h.Reset()
   291  		h.Write(data)
   292  		h.Sum(in)
   293  	}
   294  }
   295  

View as plain text