github.com/hamba/avro/v2@v2.22.1-0.20240518180522-aff3955acf7d/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, test := range tests {
    32  		test := test
    33  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    34  			hash.Reset()
    35  			_, _ = hash.Write([]byte(test.in))
    36  
    37  			got := hash.Sum64()
    38  
    39  			assert.Equal(t, test.want, got)
    40  		})
    41  	}
    42  }
    43  
    44  func TestGoldenBytes(t *testing.T) {
    45  	tests := []struct {
    46  		in   string
    47  		want []byte
    48  	}{
    49  		{
    50  			in:   `"null"`,
    51  			want: []byte{0x63, 0xdd, 0x24, 0xe7, 0xcc, 0x25, 0x8f, 0x8a},
    52  		},
    53  		{
    54  			in:   `{"name":"foo","type":"fixed","size":15}`,
    55  			want: []byte{0x18, 0x60, 0x2e, 0xc3, 0xed, 0x31, 0xa5, 0x4},
    56  		},
    57  		{
    58  			in:   `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`,
    59  			want: []byte{0x6c, 0xd8, 0xea, 0xf1, 0xc9, 0x68, 0xa3, 0x3b},
    60  		},
    61  	}
    62  
    63  	hash := New()
    64  
    65  	for i, test := range tests {
    66  		test := test
    67  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    68  			hash.Reset()
    69  			_, _ = hash.Write([]byte(test.in))
    70  
    71  			got := make([]byte, 0, hash.Size())
    72  			got = hash.Sum(got)
    73  
    74  			assert.Equal(t, test.want, got)
    75  		})
    76  	}
    77  }
    78  
    79  func TestDigest_BlockSize(t *testing.T) {
    80  	hash := New()
    81  
    82  	assert.Equal(t, 1, hash.BlockSize())
    83  }
    84  
    85  func bench(b *testing.B, size int64) {
    86  	b.SetBytes(size)
    87  
    88  	h := New()
    89  	in := make([]byte, 0, h.Size())
    90  
    91  	data := make([]byte, size)
    92  	for i := range data {
    93  		data[i] = byte(i)
    94  	}
    95  
    96  	b.ReportAllocs()
    97  	b.ResetTimer()
    98  	for i := 0; i < b.N; i++ {
    99  		h.Reset()
   100  		_, _ = h.Write(data)
   101  		h.Sum(in)
   102  
   103  		in = in[:0]
   104  	}
   105  }
   106  
   107  func BenchmarkCrc64(b *testing.B) {
   108  	b.Run("64KB", func(b *testing.B) {
   109  		bench(b, 64<<10)
   110  	})
   111  	b.Run("4KB", func(b *testing.B) {
   112  		bench(b, 4<<10)
   113  	})
   114  	b.Run("1KB", func(b *testing.B) {
   115  		bench(b, 1<<10)
   116  	})
   117  }