Explorar el Código

internal/xxh32: updated tests

Pierre Curto hace 6 años
padre
commit
17c5579070
Se han modificado 3 ficheros con 53 adiciones y 64 borrados
  1. 1 0
      go.mod
  2. 9 0
      go.sum
  3. 43 64
      internal/xxh32/xxh32zero_test.go

+ 1 - 0
go.mod

@@ -2,6 +2,7 @@ module github.com/pierrec/lz4
 
 require (
 	code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c
+	github.com/frankban/quicktest v1.4.0
 	github.com/onsi/ginkgo v1.8.0 // indirect
 	github.com/onsi/gomega v1.5.0 // indirect
 	github.com/pierrec/cmdflag v0.0.2

+ 9 - 0
go.sum

@@ -3,12 +3,21 @@ code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c/go.mod h1:wN/zk
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/frankban/quicktest v1.4.0 h1:rCSCih1FnSWJEel/eub9wclBSqpF2F/PuvxUWGWnbO8=
+github.com/frankban/quicktest v1.4.0/go.mod h1:36zfPVQyHxymz4cH7wlDmVwDrJuljRB60qkgn7rorfQ=
 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
+github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
 github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

+ 43 - 64
internal/xxh32/xxh32zero_test.go

@@ -6,40 +6,30 @@ import (
 	"hash/fnv"
 	"testing"
 
+	qt "github.com/frankban/quicktest"
 	"github.com/pierrec/lz4/internal/xxh32"
 )
 
 type test struct {
-	sum             uint32
-	data, printable string
+	sum  uint32
+	data string
 }
 
 var testdata = []test{
-	// {0x02cc5d05, "", ""},
-	// {0x550d7456, "a", ""},
-	// {0x4999fc53, "ab", ""},
-	// {0x32d153ff, "abc", ""},
-	// {0xa3643705, "abcd", ""},
-	// {0x9738f19b, "abcde", ""},
-	// {0x8b7cd587, "abcdef", ""},
-	// {0x9dd093b3, "abcdefg", ""},
-	// {0x0bb3c6bb, "abcdefgh", ""},
-	// {0xd03c13fd, "abcdefghi", ""},
-	// {0x8b988cfe, "abcdefghij", ""},
-	{0x9d2d8b62, "abcdefghijklmnop", ""},
-	// {0x42ae804d, "abcdefghijklmnopqrstuvwxyz0123456789", ""},
-	// {0x62b4ed00, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", ""},
-}
-
-func init() {
-	for i := range testdata {
-		d := &testdata[i]
-		if len(d.data) > 20 {
-			d.printable = d.data[:20]
-		} else {
-			d.printable = d.data
-		}
-	}
+	{0x02cc5d05, ""},
+	{0x550d7456, "a"},
+	{0x4999fc53, "ab"},
+	{0x32d153ff, "abc"},
+	{0xa3643705, "abcd"},
+	{0x9738f19b, "abcde"},
+	{0x8b7cd587, "abcdef"},
+	{0x9dd093b3, "abcdefg"},
+	{0x0bb3c6bb, "abcdefgh"},
+	{0xd03c13fd, "abcdefghi"},
+	{0x8b988cfe, "abcdefghij"},
+	{0x9d2d8b62, "abcdefghijklmnop"},
+	{0x42ae804d, "abcdefghijklmnopqrstuvwxyz0123456789"},
+	{0x62b4ed00, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
 }
 
 func TestZeroBlockSize(t *testing.T) {
@@ -57,69 +47,58 @@ func TestZeroSize(t *testing.T) {
 }
 
 func TestZeroData(t *testing.T) {
-	for i, td := range testdata {
+	c := qt.New(t)
+	for _, td := range testdata {
 		var xxh xxh32.XXHZero
 		data := []byte(td.data)
-		xxh.Write(data)
-		if h := xxh.Sum32(); h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.printable, h, td.sum)
-			t.FailNow()
-		}
-		if h := xxh32.ChecksumZero(data); h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.printable, h, td.sum)
-			t.FailNow()
-		}
+		_, _ = xxh.Write(data)
+
+		c.Assert(xxh.Sum32(), qt.Equals, td.sum)
+		c.Assert(xxh32.ChecksumZero(data), qt.Equals, td.sum)
 	}
 }
 
 func TestZeroSplitData(t *testing.T) {
-	for i, td := range testdata {
+	c := qt.New(t)
+	for _, td := range testdata {
 		var xxh xxh32.XXHZero
 		data := []byte(td.data)
 		l := len(data) / 2
-		xxh.Write(data[0:l])
-		xxh.Write(data[l:])
-		h := xxh.Sum32()
-		if h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.printable, h, td.sum)
-			t.FailNow()
-		}
+		_, _ = xxh.Write(data[0:l])
+		_, _ = xxh.Write(data[l:])
+
+		c.Assert(xxh.Sum32(), qt.Equals, td.sum)
 	}
 }
 
 func TestZeroSum(t *testing.T) {
-	for i, td := range testdata {
+	c := qt.New(t)
+	for _, td := range testdata {
 		var xxh xxh32.XXHZero
 		data := []byte(td.data)
-		xxh.Write(data)
+		_, _ = xxh.Write(data)
 		b := xxh.Sum(data)
-		if h := binary.LittleEndian.Uint32(b[len(data):]); h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.printable, h, td.sum)
-			t.FailNow()
-		}
+		h := binary.LittleEndian.Uint32(b[len(data):])
+		c.Assert(h, qt.Equals, td.sum)
 	}
 }
 
 func TestZeroChecksum(t *testing.T) {
-	for i, td := range testdata {
+	c := qt.New(t)
+	for _, td := range testdata {
 		data := []byte(td.data)
 		h := xxh32.ChecksumZero(data)
-		if h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.printable, h, td.sum)
-			t.FailNow()
-		}
+		c.Assert(h, qt.Equals, td.sum)
 	}
 }
 
 func TestZeroReset(t *testing.T) {
+	c := qt.New(t)
 	var xxh xxh32.XXHZero
-	for i, td := range testdata {
-		xxh.Write([]byte(td.data))
+	for _, td := range testdata {
+		_, _ = xxh.Write([]byte(td.data))
 		h := xxh.Sum32()
-		if h != td.sum {
-			t.Errorf("test %d: xxh32(%s)=0x%x expected 0x%x", i, td.data[:40], h, td.sum)
-			t.FailNow()
-		}
+		c.Assert(h, qt.Equals, td.sum)
 		xxh.Reset()
 	}
 }
@@ -132,7 +111,7 @@ var testdata1 = []byte(testdata[len(testdata)-1].data)
 func Benchmark_XXH32(b *testing.B) {
 	var h xxh32.XXHZero
 	for n := 0; n < b.N; n++ {
-		h.Write(testdata1)
+		_, _ = h.Write(testdata1)
 		h.Sum32()
 		h.Reset()
 	}
@@ -154,7 +133,7 @@ func Benchmark_CRC32(b *testing.B) {
 func Benchmark_Fnv32(b *testing.B) {
 	h := fnv.New32()
 	for i := 0; i < b.N; i++ {
-		h.Write(testdata1)
+		_, _ = h.Write(testdata1)
 		h.Sum32()
 		h.Reset()
 	}