github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/src/hash/crc32/crc32_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 crc32
     6  
     7  import (
     8  	"hash"
     9  	"math/rand"
    10  	"testing"
    11  )
    12  
    13  type test struct {
    14  	ieee, castagnoli uint32
    15  	in               string
    16  }
    17  
    18  var golden = []test{
    19  	{0x0, 0x0, ""},
    20  	{0xe8b7be43, 0xc1d04330, "a"},
    21  	{0x9e83486d, 0xe2a22936, "ab"},
    22  	{0x352441c2, 0x364b3fb7, "abc"},
    23  	{0xed82cd11, 0x92c80a31, "abcd"},
    24  	{0x8587d865, 0xc450d697, "abcde"},
    25  	{0x4b8e39ef, 0x53bceff1, "abcdef"},
    26  	{0x312a6aa6, 0xe627f441, "abcdefg"},
    27  	{0xaeef2a50, 0xa9421b7, "abcdefgh"},
    28  	{0x8da988af, 0x2ddc99fc, "abcdefghi"},
    29  	{0x3981703a, 0xe6599437, "abcdefghij"},
    30  	{0x6b9cdfe7, 0xb2cc01fe, "Discard medicine more than two years old."},
    31  	{0xc90ef73f, 0xe28207f, "He who has a shady past knows that nice guys finish last."},
    32  	{0xb902341f, 0xbe93f964, "I wouldn't marry him with a ten foot pole."},
    33  	{0x42080e8, 0x9e3be0c3, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
    34  	{0x154c6d11, 0xf505ef04, "The days of the digital watch are numbered.  -Tom Stoppard"},
    35  	{0x4c418325, 0x85d3dc82, "Nepal premier won't resign."},
    36  	{0x33955150, 0xc5142380, "For every action there is an equal and opposite government program."},
    37  	{0x26216a4b, 0x75eb77dd, "His money is twice tainted: 'taint yours and 'taint mine."},
    38  	{0x1abbe45e, 0x91ebe9f7, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
    39  	{0xc89a94f7, 0xf0b1168e, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
    40  	{0xab3abe14, 0x572b74e2, "size:  a.out:  bad magic"},
    41  	{0xbab102b6, 0x8a58a6d5, "The major problem is with sendmail.  -Mark Horton"},
    42  	{0x999149d7, 0x9c426c50, "Give me a rock, paper and scissors and I will move the world.  CCFestoon"},
    43  	{0x6d52a33c, 0x735400a4, "If the enemy is within range, then so are you."},
    44  	{0x90631e8d, 0xbec49c95, "It's well we cannot hear the screams/That we create in others' dreams."},
    45  	{0x78309130, 0xa95a2079, "You remind me of a TV show, but that's all right: I watch it anyway."},
    46  	{0x7d0a377f, 0xde2e65c5, "C is as portable as Stonehedge!!"},
    47  	{0x8c79fd79, 0x297a88ed, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
    48  	{0xa20b7167, 0x66ed1d8b, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction.  Lewis-Randall Rule"},
    49  	{0x8e0bb443, 0xdcded527, "How can you write a big system without C++?  -Paul Glick"},
    50  }
    51  
    52  // testGoldenIEEE verifies that the given function returns
    53  // correct IEEE checksums.
    54  func testGoldenIEEE(t *testing.T, crcFunc func(b []byte) uint32) {
    55  	for _, g := range golden {
    56  		if crc := crcFunc([]byte(g.in)); crc != g.ieee {
    57  			t.Errorf("IEEE(%s) = 0x%x want 0x%x", g.in, crc, g.ieee)
    58  		}
    59  	}
    60  }
    61  
    62  // testGoldenCastagnoli verifies that the given function returns
    63  // correct IEEE checksums.
    64  func testGoldenCastagnoli(t *testing.T, crcFunc func(b []byte) uint32) {
    65  	for _, g := range golden {
    66  		if crc := crcFunc([]byte(g.in)); crc != g.castagnoli {
    67  			t.Errorf("Castagnoli(%s) = 0x%x want 0x%x", g.in, crc, g.castagnoli)
    68  		}
    69  	}
    70  }
    71  
    72  // testCrossCheck generates random buffers of various lengths and verifies that
    73  // the two "update" functions return the same result.
    74  func testCrossCheck(t *testing.T, crcFunc1, crcFunc2 func(crc uint32, b []byte) uint32) {
    75  	// The AMD64 implementation has some cutoffs at lengths 168*3=504 and
    76  	// 1344*3=4032. We should make sure lengths around these values are in the
    77  	// list.
    78  	lengths := []int{0, 1, 2, 3, 4, 5, 10, 16, 50, 100, 128,
    79  		500, 501, 502, 503, 504, 505, 512, 1000, 1024, 2000,
    80  		4030, 4031, 4032, 4033, 4036, 4040, 4048, 4096, 5000, 10000}
    81  	for _, length := range lengths {
    82  		p := make([]byte, length)
    83  		_, _ = rand.Read(p)
    84  		crcInit := uint32(rand.Int63())
    85  		crc1 := crcFunc1(crcInit, p)
    86  		crc2 := crcFunc2(crcInit, p)
    87  		if crc1 != crc2 {
    88  			t.Errorf("mismatch: 0x%x vs 0x%x (buffer length %d)", crc1, crc2, length)
    89  		}
    90  	}
    91  }
    92  
    93  // TestSimple tests the simple generic algorithm.
    94  func TestSimple(t *testing.T) {
    95  	tab := simpleMakeTable(IEEE)
    96  	testGoldenIEEE(t, func(b []byte) uint32 {
    97  		return simpleUpdate(0, tab, b)
    98  	})
    99  
   100  	tab = simpleMakeTable(Castagnoli)
   101  	testGoldenCastagnoli(t, func(b []byte) uint32 {
   102  		return simpleUpdate(0, tab, b)
   103  	})
   104  }
   105  
   106  // TestSimple tests the slicing-by-8 algorithm.
   107  func TestSlicing(t *testing.T) {
   108  	tab := slicingMakeTable(IEEE)
   109  	testGoldenIEEE(t, func(b []byte) uint32 {
   110  		return slicingUpdate(0, tab, b)
   111  	})
   112  
   113  	tab = slicingMakeTable(Castagnoli)
   114  	testGoldenCastagnoli(t, func(b []byte) uint32 {
   115  		return slicingUpdate(0, tab, b)
   116  	})
   117  
   118  	// Cross-check various polys against the simple algorithm.
   119  	for _, poly := range []uint32{IEEE, Castagnoli, Koopman, 0xD5828281} {
   120  		t1 := simpleMakeTable(poly)
   121  		f1 := func(crc uint32, b []byte) uint32 {
   122  			return simpleUpdate(crc, t1, b)
   123  		}
   124  		t2 := slicingMakeTable(poly)
   125  		f2 := func(crc uint32, b []byte) uint32 {
   126  			return slicingUpdate(crc, t2, b)
   127  		}
   128  		testCrossCheck(t, f1, f2)
   129  	}
   130  }
   131  
   132  func TestArchIEEE(t *testing.T) {
   133  	if !archAvailableIEEE() {
   134  		t.Skip("Arch-specific IEEE not available.")
   135  	}
   136  	archInitIEEE()
   137  	slicingTable := slicingMakeTable(IEEE)
   138  	testCrossCheck(t, archUpdateIEEE, func(crc uint32, b []byte) uint32 {
   139  		return slicingUpdate(crc, slicingTable, b)
   140  	})
   141  }
   142  
   143  func TestArchCastagnoli(t *testing.T) {
   144  	if !archAvailableCastagnoli() {
   145  		t.Skip("Arch-specific Castagnoli not available.")
   146  	}
   147  	archInitCastagnoli()
   148  	slicingTable := slicingMakeTable(Castagnoli)
   149  	testCrossCheck(t, archUpdateCastagnoli, func(crc uint32, b []byte) uint32 {
   150  		return slicingUpdate(crc, slicingTable, b)
   151  	})
   152  }
   153  
   154  func TestGolden(t *testing.T) {
   155  	testGoldenIEEE(t, ChecksumIEEE)
   156  
   157  	// Some implementations have special code to deal with misaligned
   158  	// data; test that as well.
   159  	for delta := 1; delta <= 7; delta++ {
   160  		testGoldenIEEE(t, func(b []byte) uint32 {
   161  			ieee := NewIEEE()
   162  			d := delta
   163  			if d >= len(b) {
   164  				d = len(b)
   165  			}
   166  			ieee.Write(b[:d])
   167  			ieee.Write(b[d:])
   168  			return ieee.Sum32()
   169  		})
   170  	}
   171  
   172  	castagnoliTab := MakeTable(Castagnoli)
   173  	if castagnoliTab == nil {
   174  		t.Errorf("nil Castagnoli Table")
   175  	}
   176  
   177  	testGoldenCastagnoli(t, func(b []byte) uint32 {
   178  		castagnoli := New(castagnoliTab)
   179  		castagnoli.Write(b)
   180  		return castagnoli.Sum32()
   181  	})
   182  
   183  	// Some implementations have special code to deal with misaligned
   184  	// data; test that as well.
   185  	for delta := 1; delta <= 7; delta++ {
   186  		testGoldenCastagnoli(t, func(b []byte) uint32 {
   187  			castagnoli := New(castagnoliTab)
   188  			d := delta
   189  			if d >= len(b) {
   190  				d = len(b)
   191  			}
   192  			castagnoli.Write(b[:d])
   193  			castagnoli.Write(b[d:])
   194  			return castagnoli.Sum32()
   195  		})
   196  	}
   197  }
   198  
   199  func BenchmarkIEEECrc40B(b *testing.B) {
   200  	benchmark(b, NewIEEE(), 40, 0)
   201  }
   202  
   203  func BenchmarkIEEECrc1KB(b *testing.B) {
   204  	benchmark(b, NewIEEE(), 1<<10, 0)
   205  }
   206  
   207  func BenchmarkIEEECrc4KB(b *testing.B) {
   208  	benchmark(b, NewIEEE(), 4<<10, 0)
   209  }
   210  
   211  func BenchmarkIEEECrc32KB(b *testing.B) {
   212  	benchmark(b, NewIEEE(), 32<<10, 0)
   213  }
   214  
   215  func BenchmarkCastagnoliCrc15B(b *testing.B) {
   216  	benchmark(b, New(MakeTable(Castagnoli)), 15, 0)
   217  }
   218  
   219  func BenchmarkCastagnoliCrc15BMisaligned(b *testing.B) {
   220  	benchmark(b, New(MakeTable(Castagnoli)), 15, 1)
   221  }
   222  
   223  func BenchmarkCastagnoliCrc40B(b *testing.B) {
   224  	benchmark(b, New(MakeTable(Castagnoli)), 40, 0)
   225  }
   226  
   227  func BenchmarkCastagnoliCrc40BMisaligned(b *testing.B) {
   228  	benchmark(b, New(MakeTable(Castagnoli)), 40, 1)
   229  }
   230  
   231  func BenchmarkCastagnoliCrc512(b *testing.B) {
   232  	benchmark(b, New(MakeTable(Castagnoli)), 512, 0)
   233  }
   234  
   235  func BenchmarkCastagnoliCrc512Misaligned(b *testing.B) {
   236  	benchmark(b, New(MakeTable(Castagnoli)), 512, 1)
   237  }
   238  
   239  func BenchmarkCastagnoliCrc1KB(b *testing.B) {
   240  	benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 0)
   241  }
   242  
   243  func BenchmarkCastagnoliCrc1KBMisaligned(b *testing.B) {
   244  	benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 1)
   245  }
   246  
   247  func BenchmarkCastagnoliCrc4KB(b *testing.B) {
   248  	benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 0)
   249  }
   250  
   251  func BenchmarkCastagnoliCrc4KBMisaligned(b *testing.B) {
   252  	benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 1)
   253  }
   254  
   255  func BenchmarkCastagnoliCrc32KB(b *testing.B) {
   256  	benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 0)
   257  }
   258  
   259  func BenchmarkCastagnoliCrc32KBMisaligned(b *testing.B) {
   260  	benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 1)
   261  }
   262  
   263  func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) {
   264  	b.SetBytes(n)
   265  	data := make([]byte, n+alignment)
   266  	data = data[alignment:]
   267  	for i := range data {
   268  		data[i] = byte(i)
   269  	}
   270  	in := make([]byte, 0, h.Size())
   271  
   272  	// Warm up
   273  	h.Reset()
   274  	h.Write(data)
   275  	h.Sum(in)
   276  
   277  	b.ResetTimer()
   278  	for i := 0; i < b.N; i++ {
   279  		h.Reset()
   280  		h.Write(data)
   281  		h.Sum(in)
   282  	}
   283  }