mirror of
https://github.com/minio/minio.git
synced 2025-11-10 05:59:43 -05:00
Crc32c re-implementation with Convenience functions
- Sum32([]byte) --> uint32 - Sum(io.Reader) --> uint32, error
This commit is contained in:
@@ -1,24 +1,74 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package crc32c
|
||||
|
||||
import (
|
||||
. "gopkg.in/check.v1"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type MySuite struct{}
|
||||
|
||||
var _ = Suite(&MySuite{})
|
||||
|
||||
func (s *MySuite) TestCrc32c(c *C) {
|
||||
data_1 := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
|
||||
crc, err := Crc32c(data_1)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
data_2 := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
|
||||
newcrc, newerr := Crc32c(data_2)
|
||||
c.Assert(newerr, IsNil)
|
||||
|
||||
c.Assert(crc, Equals, newcrc)
|
||||
type test struct {
|
||||
castagnoli uint32
|
||||
in string
|
||||
}
|
||||
|
||||
var golden = []test{
|
||||
{0x0, ""},
|
||||
{0x93ad1061, "a"},
|
||||
{0x13c35ee4, "ab"},
|
||||
{0x562f9ccd, "abc"},
|
||||
{0xdaaf41f6, "abcd"},
|
||||
{0x8122a0a2, "abcde"},
|
||||
{0x496937b, "abcdef"},
|
||||
{0x5d199e2c, "abcdefg"},
|
||||
{0x86bc933d, "abcdefgh"},
|
||||
{0x9639f15f, "abcdefghi"},
|
||||
{0x584645c, "abcdefghij"},
|
||||
{0x8c13a060, "Discard medicine more than two years old."},
|
||||
{0x629077d4, "He who has a shady past knows that nice guys finish last."},
|
||||
{0xd20036a4, "I wouldn't marry him with a ten foot pole."},
|
||||
{0xf283b768, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
|
||||
{0x9cd61a9f, "The days of the digital watch are numbered. -Tom Stoppard"},
|
||||
{0x637702f5, "Nepal premier won't resign."},
|
||||
{0x6c595588, "For every action there is an equal and opposite government program."},
|
||||
{0x19532076, "His money is twice tainted: 'taint yours and 'taint mine."},
|
||||
{0x9b82c857, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
|
||||
{0x2b485952, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
|
||||
{0xd3d0980c, "size: a.out: bad magic"},
|
||||
{0x12aad0bb, "The major problem is with sendmail. -Mark Horton"},
|
||||
{0x83a0339b, "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
|
||||
{0x1eb28fde, "If the enemy is within range, then so are you."},
|
||||
{0xce34d559, "It's well we cannot hear the screams/That we create in others' dreams."},
|
||||
{0x71576691, "You remind me of a TV show, but that's all right: I watch it anyway."},
|
||||
{0x54bf536f, "C is as portable as Stonehedge!!"},
|
||||
{0x2313a94d, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
|
||||
{0x9d4e3629, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
|
||||
{0xc9991fb9, "How can you write a big system without C++? -Paul Glick"},
|
||||
}
|
||||
|
||||
func TestGolden(t *testing.T) {
|
||||
for _, g := range golden {
|
||||
s := Sum32([]byte(g.in))
|
||||
if s != g.castagnoli {
|
||||
t.Errorf("Castagnoli(%s) = 0x%x want 0x%x", g.in, s, g.castagnoli)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCrc32KB(b *testing.B) {
|
||||
b.SetBytes(1024)
|
||||
data := make([]byte, 1024)
|
||||
for i := range data {
|
||||
data[i] = byte(i)
|
||||
}
|
||||
h := New()
|
||||
in := make([]byte, 0, h.Size())
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
h.Reset()
|
||||
h.Write(data)
|
||||
h.Sum(in)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user