github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/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 "io" 9 "testing" 10 ) 11 12 type test struct { 13 ieee, castagnoli uint32 14 in string 15 } 16 17 var golden = []test{ 18 {0x0, 0x0, ""}, 19 {0xe8b7be43, 0xc1d04330, "a"}, 20 {0x9e83486d, 0xe2a22936, "ab"}, 21 {0x352441c2, 0x364b3fb7, "abc"}, 22 {0xed82cd11, 0x92c80a31, "abcd"}, 23 {0x8587d865, 0xc450d697, "abcde"}, 24 {0x4b8e39ef, 0x53bceff1, "abcdef"}, 25 {0x312a6aa6, 0xe627f441, "abcdefg"}, 26 {0xaeef2a50, 0xa9421b7, "abcdefgh"}, 27 {0x8da988af, 0x2ddc99fc, "abcdefghi"}, 28 {0x3981703a, 0xe6599437, "abcdefghij"}, 29 {0x6b9cdfe7, 0xb2cc01fe, "Discard medicine more than two years old."}, 30 {0xc90ef73f, 0xe28207f, "He who has a shady past knows that nice guys finish last."}, 31 {0xb902341f, 0xbe93f964, "I wouldn't marry him with a ten foot pole."}, 32 {0x42080e8, 0x9e3be0c3, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"}, 33 {0x154c6d11, 0xf505ef04, "The days of the digital watch are numbered. -Tom Stoppard"}, 34 {0x4c418325, 0x85d3dc82, "Nepal premier won't resign."}, 35 {0x33955150, 0xc5142380, "For every action there is an equal and opposite government program."}, 36 {0x26216a4b, 0x75eb77dd, "His money is twice tainted: 'taint yours and 'taint mine."}, 37 {0x1abbe45e, 0x91ebe9f7, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"}, 38 {0xc89a94f7, 0xf0b1168e, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"}, 39 {0xab3abe14, 0x572b74e2, "size: a.out: bad magic"}, 40 {0xbab102b6, 0x8a58a6d5, "The major problem is with sendmail. -Mark Horton"}, 41 {0x999149d7, 0x9c426c50, "Give me a rock, paper and scissors and I will move the world. CCFestoon"}, 42 {0x6d52a33c, 0x735400a4, "If the enemy is within range, then so are you."}, 43 {0x90631e8d, 0xbec49c95, "It's well we cannot hear the screams/That we create in others' dreams."}, 44 {0x78309130, 0xa95a2079, "You remind me of a TV show, but that's all right: I watch it anyway."}, 45 {0x7d0a377f, 0xde2e65c5, "C is as portable as Stonehedge!!"}, 46 {0x8c79fd79, 0x297a88ed, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"}, 47 {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"}, 48 {0x8e0bb443, 0xdcded527, "How can you write a big system without C++? -Paul Glick"}, 49 } 50 51 func TestGolden(t *testing.T) { 52 castagnoliTab := MakeTable(Castagnoli) 53 54 for _, g := range golden { 55 ieee := NewIEEE() 56 io.WriteString(ieee, g.in) 57 s := ieee.Sum32() 58 if s != g.ieee { 59 t.Errorf("IEEE(%s) = 0x%x want 0x%x", g.in, s, g.ieee) 60 } 61 62 castagnoli := New(castagnoliTab) 63 io.WriteString(castagnoli, g.in) 64 s = castagnoli.Sum32() 65 if s != g.castagnoli { 66 t.Errorf("Castagnoli(%s) = 0x%x want 0x%x", g.in, s, g.castagnoli) 67 } 68 69 if len(g.in) > 0 { 70 // The SSE4.2 implementation of this has code to deal 71 // with misaligned data so we ensure that we test that 72 // too. 73 castagnoli = New(castagnoliTab) 74 io.WriteString(castagnoli, g.in[:1]) 75 io.WriteString(castagnoli, g.in[1:]) 76 s = castagnoli.Sum32() 77 if s != g.castagnoli { 78 t.Errorf("Castagnoli[misaligned](%s) = 0x%x want 0x%x", g.in, s, g.castagnoli) 79 } 80 } 81 } 82 } 83 84 func BenchmarkCrc32KB(b *testing.B) { 85 b.SetBytes(1024) 86 data := make([]byte, 1024) 87 for i := range data { 88 data[i] = byte(i) 89 } 90 h := NewIEEE() 91 in := make([]byte, 0, h.Size()) 92 93 b.ResetTimer() 94 for i := 0; i < b.N; i++ { 95 h.Reset() 96 h.Write(data) 97 h.Sum(in) 98 } 99 }