go.etcd.io/etcd@v3.3.27+incompatible/pkg/crc/crc_test.go (about)

     1  // Copyright 2009 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 crc
     6  
     7  import (
     8  	"hash/crc32"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  // TestHash32 tests that Hash32 provided by this package can take an initial
    14  // crc and behaves exactly the same as the standard one in the following calls.
    15  func TestHash32(t *testing.T) {
    16  	stdhash := crc32.New(crc32.IEEETable)
    17  	if _, err := stdhash.Write([]byte("test data")); err != nil {
    18  		t.Fatalf("unexpected write error: %v", err)
    19  	}
    20  	// create a new hash with stdhash.Sum32() as initial crc
    21  	hash := New(stdhash.Sum32(), crc32.IEEETable)
    22  
    23  	wsize := stdhash.Size()
    24  	if g := hash.Size(); g != wsize {
    25  		t.Errorf("size = %d, want %d", g, wsize)
    26  	}
    27  	wbsize := stdhash.BlockSize()
    28  	if g := hash.BlockSize(); g != wbsize {
    29  		t.Errorf("block size = %d, want %d", g, wbsize)
    30  	}
    31  	wsum32 := stdhash.Sum32()
    32  	if g := hash.Sum32(); g != wsum32 {
    33  		t.Errorf("Sum32 = %d, want %d", g, wsum32)
    34  	}
    35  	wsum := stdhash.Sum(make([]byte, 32))
    36  	if g := hash.Sum(make([]byte, 32)); !reflect.DeepEqual(g, wsum) {
    37  		t.Errorf("sum = %v, want %v", g, wsum)
    38  	}
    39  
    40  	// write something
    41  	if _, err := stdhash.Write([]byte("test data")); err != nil {
    42  		t.Fatalf("unexpected write error: %v", err)
    43  	}
    44  	if _, err := hash.Write([]byte("test data")); err != nil {
    45  		t.Fatalf("unexpected write error: %v", err)
    46  	}
    47  	wsum32 = stdhash.Sum32()
    48  	if g := hash.Sum32(); g != wsum32 {
    49  		t.Errorf("Sum32 after write = %d, want %d", g, wsum32)
    50  	}
    51  
    52  	// reset
    53  	stdhash.Reset()
    54  	hash.Reset()
    55  	wsum32 = stdhash.Sum32()
    56  	if g := hash.Sum32(); g != wsum32 {
    57  		t.Errorf("Sum32 after reset = %d, want %d", g, wsum32)
    58  	}
    59  }