github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/hash/fnv/fnv_test.go (about)

     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/binary"
    10  	"hash"
    11  	"testing"
    12  )
    13  
    14  type golden struct {
    15  	sum  []byte
    16  	text string
    17  }
    18  
    19  var golden32 = []golden{
    20  	{[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
    21  	{[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a"},
    22  	{[]byte{0x70, 0x77, 0x2d, 0x38}, "ab"},
    23  	{[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc"},
    24  }
    25  
    26  var golden32a = []golden{
    27  	{[]byte{0x81, 0x1c, 0x9d, 0xc5}, ""},
    28  	{[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a"},
    29  	{[]byte{0x4d, 0x25, 0x05, 0xca}, "ab"},
    30  	{[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc"},
    31  }
    32  
    33  var golden64 = []golden{
    34  	{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
    35  	{[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a"},
    36  	{[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab"},
    37  	{[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc"},
    38  }
    39  
    40  var golden64a = []golden{
    41  	{[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, ""},
    42  	{[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a"},
    43  	{[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab"},
    44  	{[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc"},
    45  }
    46  
    47  var golden128 = []golden{
    48  	{[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, ""},
    49  	{[]byte{0xd2, 0x28, 0xcb, 0x69, 0x10, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x14, 0x1e}, "a"},
    50  	{[]byte{0x8, 0x80, 0x94, 0x5a, 0xee, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0x26, 0xc0, 0x88}, "ab"},
    51  	{[]byte{0xa6, 0x8b, 0xb2, 0xa4, 0x34, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x78, 0xc6, 0xae, 0xe7, 0x3b}, "abc"},
    52  }
    53  
    54  var golden128a = []golden{
    55  	{[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, ""},
    56  	{[]byte{0xd2, 0x28, 0xcb, 0x69, 0x6f, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x89, 0x64}, "a"},
    57  	{[]byte{0x08, 0x80, 0x95, 0x44, 0xbb, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0xb6, 0x9a, 0x62}, "ab"},
    58  	{[]byte{0xa6, 0x8d, 0x62, 0x2c, 0xec, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x79, 0x77, 0xaf, 0x7f, 0x3b}, "abc"},
    59  }
    60  
    61  func TestGolden32(t *testing.T) {
    62  	testGolden(t, New32(), golden32)
    63  }
    64  
    65  func TestGolden32a(t *testing.T) {
    66  	testGolden(t, New32a(), golden32a)
    67  }
    68  
    69  func TestGolden64(t *testing.T) {
    70  	testGolden(t, New64(), golden64)
    71  }
    72  
    73  func TestGolden64a(t *testing.T) {
    74  	testGolden(t, New64a(), golden64a)
    75  }
    76  
    77  func TestGolden128(t *testing.T) {
    78  	testGolden(t, New128(), golden128)
    79  }
    80  
    81  func TestGolden128a(t *testing.T) {
    82  	testGolden(t, New128a(), golden128a)
    83  }
    84  
    85  func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
    86  	for _, g := range gold {
    87  		hash.Reset()
    88  		done, error := hash.Write([]byte(g.text))
    89  		if error != nil {
    90  			t.Fatalf("write error: %s", error)
    91  		}
    92  		if done != len(g.text) {
    93  			t.Fatalf("wrote only %d out of %d bytes", done, len(g.text))
    94  		}
    95  		if actual := hash.Sum(nil); !bytes.Equal(g.sum, actual) {
    96  			t.Errorf("hash(%q) = 0x%x want 0x%x", g.text, actual, g.sum)
    97  		}
    98  	}
    99  }
   100  
   101  func TestIntegrity32(t *testing.T) {
   102  	testIntegrity(t, New32())
   103  }
   104  
   105  func TestIntegrity32a(t *testing.T) {
   106  	testIntegrity(t, New32a())
   107  }
   108  
   109  func TestIntegrity64(t *testing.T) {
   110  	testIntegrity(t, New64())
   111  }
   112  
   113  func TestIntegrity64a(t *testing.T) {
   114  	testIntegrity(t, New64a())
   115  }
   116  func TestIntegrity128(t *testing.T) {
   117  	testIntegrity(t, New128())
   118  }
   119  
   120  func TestIntegrity128a(t *testing.T) {
   121  	testIntegrity(t, New128a())
   122  }
   123  
   124  func testIntegrity(t *testing.T, h hash.Hash) {
   125  	data := []byte{'1', '2', 3, 4, 5}
   126  	h.Write(data)
   127  	sum := h.Sum(nil)
   128  
   129  	if size := h.Size(); size != len(sum) {
   130  		t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
   131  	}
   132  
   133  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   134  		t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
   135  	}
   136  
   137  	h.Reset()
   138  	h.Write(data)
   139  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   140  		t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
   141  	}
   142  
   143  	h.Reset()
   144  	h.Write(data[:2])
   145  	h.Write(data[2:])
   146  	if a := h.Sum(nil); !bytes.Equal(sum, a) {
   147  		t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
   148  	}
   149  
   150  	switch h.Size() {
   151  	case 4:
   152  		sum32 := h.(hash.Hash32).Sum32()
   153  		if sum32 != binary.BigEndian.Uint32(sum) {
   154  			t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
   155  		}
   156  	case 8:
   157  		sum64 := h.(hash.Hash64).Sum64()
   158  		if sum64 != binary.BigEndian.Uint64(sum) {
   159  			t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
   160  		}
   161  	case 16:
   162  		// There's no Sum128 function, so we don't need to test anything here.
   163  	}
   164  }
   165  
   166  func BenchmarkFnv32KB(b *testing.B) {
   167  	benchmarkKB(b, New32())
   168  }
   169  
   170  func BenchmarkFnv32aKB(b *testing.B) {
   171  	benchmarkKB(b, New32a())
   172  }
   173  
   174  func BenchmarkFnv64KB(b *testing.B) {
   175  	benchmarkKB(b, New64())
   176  }
   177  
   178  func BenchmarkFnv64aKB(b *testing.B) {
   179  	benchmarkKB(b, New64a())
   180  }
   181  
   182  func BenchmarkFnv128KB(b *testing.B) {
   183  	benchmarkKB(b, New128())
   184  }
   185  
   186  func BenchmarkFnv128aKB(b *testing.B) {
   187  	benchmarkKB(b, New128a())
   188  }
   189  
   190  func benchmarkKB(b *testing.B, h hash.Hash) {
   191  	b.SetBytes(1024)
   192  	data := make([]byte, 1024)
   193  	for i := range data {
   194  		data[i] = byte(i)
   195  	}
   196  	in := make([]byte, 0, h.Size())
   197  
   198  	b.ResetTimer()
   199  	for i := 0; i < b.N; i++ {
   200  		h.Reset()
   201  		h.Write(data)
   202  		h.Sum(in)
   203  	}
   204  }