File: crc_test.go

package info (click to toggle)
etcd 3.5.16-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,892 kB
  • sloc: sh: 3,139; makefile: 478
file content (59 lines) | stat: -rw-r--r-- 1,692 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package crc

import (
	"hash/crc32"
	"reflect"
	"testing"
)

// TestHash32 tests that Hash32 provided by this package can take an initial
// crc and behaves exactly the same as the standard one in the following calls.
func TestHash32(t *testing.T) {
	stdhash := crc32.New(crc32.IEEETable)
	if _, err := stdhash.Write([]byte("test data")); err != nil {
		t.Fatalf("unexpected write error: %v", err)
	}
	// create a new hash with stdhash.Sum32() as initial crc
	hash := New(stdhash.Sum32(), crc32.IEEETable)

	wsize := stdhash.Size()
	if g := hash.Size(); g != wsize {
		t.Errorf("size = %d, want %d", g, wsize)
	}
	wbsize := stdhash.BlockSize()
	if g := hash.BlockSize(); g != wbsize {
		t.Errorf("block size = %d, want %d", g, wbsize)
	}
	wsum32 := stdhash.Sum32()
	if g := hash.Sum32(); g != wsum32 {
		t.Errorf("Sum32 = %d, want %d", g, wsum32)
	}
	wsum := stdhash.Sum(make([]byte, 32))
	if g := hash.Sum(make([]byte, 32)); !reflect.DeepEqual(g, wsum) {
		t.Errorf("sum = %v, want %v", g, wsum)
	}

	// write something
	if _, err := stdhash.Write([]byte("test data")); err != nil {
		t.Fatalf("unexpected write error: %v", err)
	}
	if _, err := hash.Write([]byte("test data")); err != nil {
		t.Fatalf("unexpected write error: %v", err)
	}
	wsum32 = stdhash.Sum32()
	if g := hash.Sum32(); g != wsum32 {
		t.Errorf("Sum32 after write = %d, want %d", g, wsum32)
	}

	// reset
	stdhash.Reset()
	hash.Reset()
	wsum32 = stdhash.Sum32()
	if g := hash.Sum32(); g != wsum32 {
		t.Errorf("Sum32 after reset = %d, want %d", g, wsum32)
	}
}