github.com/hamba/avro@v1.8.0/pkg/crc64/crc64_test.go (about) 1 package crc64 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGolden(t *testing.T) { 11 tests := []struct { 12 in string 13 want uint64 14 }{ 15 { 16 in: `"null"`, 17 want: 7195948357588979594, 18 }, 19 { 20 in: `{"name":"foo","type":"fixed","size":15}`, 21 want: 1756455273707447556, 22 }, 23 { 24 in: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`, 25 want: 7843277075252814651, 26 }, 27 } 28 29 hash := New() 30 31 for i, tt := range tests { 32 t.Run(strconv.Itoa(i), func(t *testing.T) { 33 hash.Reset() 34 _, _ = hash.Write([]byte(tt.in)) 35 36 got := hash.Sum64() 37 38 assert.Equal(t, tt.want, got) 39 }) 40 } 41 } 42 43 func TestGoldenBytes(t *testing.T) { 44 tests := []struct { 45 in string 46 want []byte 47 }{ 48 { 49 in: `"null"`, 50 want: []byte{0x63, 0xdd, 0x24, 0xe7, 0xcc, 0x25, 0x8f, 0x8a}, 51 }, 52 { 53 in: `{"name":"foo","type":"fixed","size":15}`, 54 want: []byte{0x18, 0x60, 0x2e, 0xc3, 0xed, 0x31, 0xa5, 0x4}, 55 }, 56 { 57 in: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`, 58 want: []byte{0x6c, 0xd8, 0xea, 0xf1, 0xc9, 0x68, 0xa3, 0x3b}, 59 }, 60 } 61 62 hash := New() 63 64 for i, tt := range tests { 65 t.Run(strconv.Itoa(i), func(t *testing.T) { 66 hash.Reset() 67 _, _ = hash.Write([]byte(tt.in)) 68 69 got := make([]byte, 0, hash.Size()) 70 got = hash.Sum(got) 71 72 assert.Equal(t, tt.want, got) 73 }) 74 } 75 } 76 77 func TestDigest_BlockSize(t *testing.T) { 78 hash := New() 79 80 assert.Equal(t, 1, hash.BlockSize()) 81 } 82 83 func bench(b *testing.B, size int64) { 84 b.SetBytes(size) 85 86 h := New() 87 in := make([]byte, 0, h.Size()) 88 89 data := make([]byte, size) 90 for i := range data { 91 data[i] = byte(i) 92 } 93 94 b.ReportAllocs() 95 b.ResetTimer() 96 for i := 0; i < b.N; i++ { 97 h.Reset() 98 _, _ = h.Write(data) 99 h.Sum(in) 100 101 in = in[:0] 102 } 103 } 104 105 func BenchmarkCrc64(b *testing.B) { 106 b.Run("64KB", func(b *testing.B) { 107 bench(b, 64<<10) 108 }) 109 b.Run("4KB", func(b *testing.B) { 110 bench(b, 4<<10) 111 }) 112 b.Run("1KB", func(b *testing.B) { 113 bench(b, 1<<10) 114 }) 115 }