crc_test.go 1.7 KB

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