github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/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 "io" 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 func TestGolden(t *testing.T) { 54 castagnoliTab := MakeTable(Castagnoli) 55 56 for _, g := range golden { 57 ieee := NewIEEE() 58 io.WriteString(ieee, g.in) 59 s := ieee.Sum32() 60 if s != g.ieee { 61 t.Errorf("IEEE(%s) = 0x%x want 0x%x", g.in, s, g.ieee) 62 } 63 64 castagnoli := New(castagnoliTab) 65 io.WriteString(castagnoli, g.in) 66 s = castagnoli.Sum32() 67 if s != g.castagnoli { 68 t.Errorf("Castagnoli(%s) = 0x%x want 0x%x", g.in, s, g.castagnoli) 69 } 70 71 // The SSE4.2 implementation of this has code to deal 72 // with misaligned data so we ensure that we test that 73 // too. 74 for delta := 1; delta <= 7; delta++ { 75 if len(g.in) > delta { 76 in := []byte(g.in) 77 castagnoli = New(castagnoliTab) 78 castagnoli.Write(in[:delta]) 79 castagnoli.Write(in[delta:]) 80 s = castagnoli.Sum32() 81 if s != g.castagnoli { 82 t.Errorf("Castagnoli[misaligned](%s) = 0x%x want 0x%x", g.in, s, g.castagnoli) 83 } 84 } 85 } 86 } 87 } 88 89 func TestCastagnoliSSE42(t *testing.T) { 90 if !sse42 { 91 t.Skip("SSE42 not supported") 92 } 93 94 // Init the SSE42 tables. 95 MakeTable(Castagnoli) 96 97 // Manually init the software implementation to compare against. 98 castagnoliTable = makeTable(Castagnoli) 99 castagnoliTable8 = makeTable8(Castagnoli) 100 101 // The optimized SSE4.2 implementation behaves differently for different 102 // lengths (especially around multiples of K*3). Crosscheck against the 103 // software implementation for various lengths. 104 for _, base := range []int{castagnoliK1, castagnoliK2, castagnoliK1 + castagnoliK2} { 105 for _, baseMult := range []int{2, 3, 5, 6, 9, 30} { 106 for _, variation := range []int{0, 1, 2, 3, 4, 7, 10, 16, 32, 50, 128} { 107 for _, varMult := range []int{-2, -1, +1, +2} { 108 length := base*baseMult + variation*varMult 109 p := make([]byte, length) 110 _, _ = rand.Read(p) 111 crcInit := uint32(rand.Int63()) 112 correct := updateSlicingBy8(crcInit, castagnoliTable8, p) 113 result := updateCastagnoli(crcInit, p) 114 if result != correct { 115 t.Errorf("SSE42 implementation = 0x%x want 0x%x (buffer length %d)", 116 result, correct, len(p)) 117 } 118 } 119 } 120 } 121 } 122 } 123 124 func BenchmarkIEEECrc40B(b *testing.B) { 125 benchmark(b, NewIEEE(), 40, 0) 126 } 127 128 func BenchmarkIEEECrc1KB(b *testing.B) { 129 benchmark(b, NewIEEE(), 1<<10, 0) 130 } 131 132 func BenchmarkIEEECrc4KB(b *testing.B) { 133 benchmark(b, NewIEEE(), 4<<10, 0) 134 } 135 136 func BenchmarkIEEECrc32KB(b *testing.B) { 137 benchmark(b, NewIEEE(), 32<<10, 0) 138 } 139 140 func BenchmarkCastagnoliCrc15B(b *testing.B) { 141 benchmark(b, New(MakeTable(Castagnoli)), 15, 0) 142 } 143 144 func BenchmarkCastagnoliCrc15BMisaligned(b *testing.B) { 145 benchmark(b, New(MakeTable(Castagnoli)), 15, 1) 146 } 147 148 func BenchmarkCastagnoliCrc40B(b *testing.B) { 149 benchmark(b, New(MakeTable(Castagnoli)), 40, 0) 150 } 151 152 func BenchmarkCastagnoliCrc40BMisaligned(b *testing.B) { 153 benchmark(b, New(MakeTable(Castagnoli)), 40, 1) 154 } 155 156 func BenchmarkCastagnoliCrc512(b *testing.B) { 157 benchmark(b, New(MakeTable(Castagnoli)), 512, 0) 158 } 159 160 func BenchmarkCastagnoliCrc512Misaligned(b *testing.B) { 161 benchmark(b, New(MakeTable(Castagnoli)), 512, 1) 162 } 163 164 func BenchmarkCastagnoliCrc1KB(b *testing.B) { 165 benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 0) 166 } 167 168 func BenchmarkCastagnoliCrc1KBMisaligned(b *testing.B) { 169 benchmark(b, New(MakeTable(Castagnoli)), 1<<10, 1) 170 } 171 172 func BenchmarkCastagnoliCrc4KB(b *testing.B) { 173 benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 0) 174 } 175 176 func BenchmarkCastagnoliCrc4KBMisaligned(b *testing.B) { 177 benchmark(b, New(MakeTable(Castagnoli)), 4<<10, 1) 178 } 179 180 func BenchmarkCastagnoliCrc32KB(b *testing.B) { 181 benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 0) 182 } 183 184 func BenchmarkCastagnoliCrc32KBMisaligned(b *testing.B) { 185 benchmark(b, New(MakeTable(Castagnoli)), 32<<10, 1) 186 } 187 188 func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) { 189 b.SetBytes(n) 190 data := make([]byte, n+alignment) 191 data = data[alignment:] 192 for i := range data { 193 data[i] = byte(i) 194 } 195 in := make([]byte, 0, h.Size()) 196 197 // Warm up 198 h.Reset() 199 h.Write(data) 200 h.Sum(in) 201 202 b.ResetTimer() 203 for i := 0; i < b.N; i++ { 204 h.Reset() 205 h.Write(data) 206 h.Sum(in) 207 } 208 }