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