Consistently use tt to name a *testing.T

This commit is contained in:
Nigel Tao committed 2019-09-15 22:26:01 +10:00
1 parent 8357a74a8c
commit 59b9dee61a
8 files changed
+150 -150

No files matched your search

+12 -12
View File
@@ -41,7 +41,7 @@ func clone(b []byte) []byte {
return append([]byte(nil), b...) return append([]byte(nil), b...)
} }
func Test(t *testing.T, func Test(tt *testing.T,
smallestValidMaxEncodedLen int, smallestValidMaxEncodedLen int,
cut func(io.Writer, []byte, int) (int, int, error), cut func(io.Writer, []byte, int) (int, int, error),
newReader func(io.Reader) (io.ReadCloser, error), newReader func(io.Reader) (io.ReadCloser, error),
@@ -50,12 +50,12 @@ func Test(t *testing.T,
for _, filename := range filenames { for _, filename := range filenames {
full, err := ioutil.ReadFile("../../test/data/" + filename) full, err := ioutil.ReadFile("../../test/data/" + filename)
if err != nil { if err != nil {
t.Errorf("f=%q: ReadFile: %v", filename, err) tt.Errorf("f=%q: ReadFile: %v", filename, err)
continue continue
} }
fullDecodedLen, err := calculateDecodedLen(full, newReader) fullDecodedLen, err := calculateDecodedLen(full, newReader)
if err != nil { if err != nil {
t.Errorf("f=%q: calculateDecodedLen: %v", filename, err) tt.Errorf("f=%q: calculateDecodedLen: %v", filename, err)
continue continue
} }
@@ -94,16 +94,16 @@ func Test(t *testing.T,
encoded := clone(full) encoded := clone(full)
encLen, decLen, err := cut(w0, encoded, maxEncodedLen) encLen, decLen, err := cut(w0, encoded, maxEncodedLen)
if err != nil { if err != nil {
t.Errorf("f=%q, mEL=%d: cut: %v", filename, maxEncodedLen, err) tt.Errorf("f=%q, mEL=%d: cut: %v", filename, maxEncodedLen, err)
continue continue
} }
if encLen > maxEncodedLen { if encLen > maxEncodedLen {
t.Errorf("f=%q, mEL=%d: encLen vs max: got %d, want <= %d", tt.Errorf("f=%q, mEL=%d: encLen vs max: got %d, want <= %d",
filename, maxEncodedLen, encLen, maxEncodedLen) filename, maxEncodedLen, encLen, maxEncodedLen)
continue continue
} }
if encLen > len(encoded) { if encLen > len(encoded) {
t.Errorf("f=%q, mEL=%d: encLen vs len: got %d, want <= %d", tt.Errorf("f=%q, mEL=%d: encLen vs len: got %d, want <= %d",
filename, maxEncodedLen, encLen, len(encoded)) filename, maxEncodedLen, encLen, len(encoded))
continue continue
} }
@@ -111,31 +111,31 @@ func Test(t *testing.T,
w1 := &bytes.Buffer{} w1 := &bytes.Buffer{}
r, err := newReader(bytes.NewReader(encoded[:encLen])) r, err := newReader(bytes.NewReader(encoded[:encLen]))
if err != nil { if err != nil {
t.Errorf("f=%q, mEL=%d: newReader: %v", filename, maxEncodedLen, err) tt.Errorf("f=%q, mEL=%d: newReader: %v", filename, maxEncodedLen, err)
continue continue
} }
if n, err := io.Copy(w1, r); err != nil { if n, err := io.Copy(w1, r); err != nil {
t.Errorf("f=%q, mEL=%d: io.Copy: %v", filename, maxEncodedLen, err) tt.Errorf("f=%q, mEL=%d: io.Copy: %v", filename, maxEncodedLen, err)
continue continue
} else if n != int64(decLen) { } else if n != int64(decLen) {
t.Errorf("f=%q, mEL=%d: io.Copy: got %d, want %d", tt.Errorf("f=%q, mEL=%d: io.Copy: got %d, want %d",
filename, maxEncodedLen, n, decLen) filename, maxEncodedLen, n, decLen)
continue continue
} }
if !bytes.Equal(w0.Bytes(), w1.Bytes()) { if !bytes.Equal(w0.Bytes(), w1.Bytes()) {
t.Errorf("f=%q, mEL=%d: decoded bytes were not equal", filename, maxEncodedLen) tt.Errorf("f=%q, mEL=%d: decoded bytes were not equal", filename, maxEncodedLen)
continue continue
} }
if (maxEncodedLen == len(encoded)) && (int64(decLen) != fullDecodedLen) { if (maxEncodedLen == len(encoded)) && (int64(decLen) != fullDecodedLen) {
t.Errorf("f=%q, mEL=%d: full decode: got %d, want %d", tt.Errorf("f=%q, mEL=%d: full decode: got %d, want %d",
filename, maxEncodedLen, decLen, fullDecodedLen) filename, maxEncodedLen, decLen, fullDecodedLen)
continue continue
} }
if err := r.Close(); err != nil { if err := r.Close(); err != nil {
t.Errorf("f=%q, mEL=%d: r.Close: %v", filename, maxEncodedLen, err) tt.Errorf("f=%q, mEL=%d: r.Close: %v", filename, maxEncodedLen, err)
continue continue
} }
} }
+8 -8
View File
@@ -36,9 +36,9 @@ const (
uncompressedMore = "More!\n" uncompressedMore = "More!\n"
) )
func TestRoundTrip(t *testing.T) { func TestRoundTrip(tt *testing.T) {
if !cgoEnabled { if !cgoEnabled {
t.Skip("cgo is not enabled") tt.Skip("cgo is not enabled")
} }
wr := &WriterRecycler{} wr := &WriterRecycler{}
@@ -59,16 +59,16 @@ func TestRoundTrip(t *testing.T) {
w.Reset(buf, nil, 0) w.Reset(buf, nil, 0)
if _, err := w.Write([]byte(uncompressedMore)); err != nil { if _, err := w.Write([]byte(uncompressedMore)); err != nil {
w.Close() w.Close()
t.Fatalf("i=%d: Write: %v", i, err) tt.Fatalf("i=%d: Write: %v", i, err)
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatalf("i=%d: Close: %v", i, err) tt.Fatalf("i=%d: Close: %v", i, err)
} }
} }
compressed := buf.String() compressed := buf.String()
if compressed != compressedMore { if compressed != compressedMore {
t.Fatalf("i=%d: compressed\ngot % 02x\nwant % 02x", i, compressed, compressedMore) tt.Fatalf("i=%d: compressed\ngot % 02x\nwant % 02x", i, compressed, compressedMore)
} }
// Uncompress. // Uncompress.
@@ -77,14 +77,14 @@ func TestRoundTrip(t *testing.T) {
gotBytes, err := ioutil.ReadAll(r) gotBytes, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
r.Close() r.Close()
t.Fatalf("i=%d: ReadAll: %v", i, err) tt.Fatalf("i=%d: ReadAll: %v", i, err)
} }
if got, want := string(gotBytes), uncompressedMore; got != want { if got, want := string(gotBytes), uncompressedMore; got != want {
r.Close() r.Close()
t.Fatalf("i=%d:\ngot %q\nwant %q", i, got, want) tt.Fatalf("i=%d:\ngot %q\nwant %q", i, got, want)
} }
if err := r.Close(); err != nil { if err := r.Close(); err != nil {
t.Fatalf("i=%d: Close: %v", i, err) tt.Fatalf("i=%d: Close: %v", i, err)
} }
} }
} }
+14 -14
View File
@@ -47,39 +47,39 @@ func makePure() resetReadCloser {
return r.(resetReadCloser) return r.(resetReadCloser)
} }
func testReadAll(t *testing.T, r resetReadCloser, src io.Reader, dict []byte, want string) { func testReadAll(tt *testing.T, r resetReadCloser, src io.Reader, dict []byte, want string) {
t.Helper() tt.Helper()
if !cgoEnabled { if !cgoEnabled {
t.Skip("cgo is not enabled") tt.Skip("cgo is not enabled")
} }
if err := r.Reset(src, dict); err != nil { if err := r.Reset(src, dict); err != nil {
t.Fatalf("Reset: %v", err) tt.Fatalf("Reset: %v", err)
} }
got, err := ioutil.ReadAll(r) got, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
t.Fatalf("ReadAll: %v", err) tt.Fatalf("ReadAll: %v", err)
} }
if err := r.Close(); err != nil { if err := r.Close(); err != nil {
t.Fatalf("Close: %v", err) tt.Fatalf("Close: %v", err)
} }
if string(got) != want { if string(got) != want {
t.Fatalf("got %q, want %q", got, want) tt.Fatalf("got %q, want %q", got, want)
} }
} }
func testReader(t *testing.T, r resetReadCloser) { func testReader(tt *testing.T, r resetReadCloser) {
testReadAll(t, r, strings.NewReader(compressedMore), nil, "More!\n") testReadAll(tt, r, strings.NewReader(compressedMore), nil, "More!\n")
testReadAll(t, r, strings.NewReader(compressedSheep), dictSheep, "Two sheep.\n") testReadAll(tt, r, strings.NewReader(compressedSheep), dictSheep, "Two sheep.\n")
testReadAll(t, r, strings.NewReader(compressedMore), nil, "More!\n") testReadAll(tt, r, strings.NewReader(compressedMore), nil, "More!\n")
testReadAll(t, r, strings.NewReader(compressedSheep), dictSheep, "Two sheep.\n") testReadAll(tt, r, strings.NewReader(compressedSheep), dictSheep, "Two sheep.\n")
} }
func TestCgo(t *testing.T) { testReader(t, &Reader{}) } func TestCgo(tt *testing.T) { testReader(tt, &Reader{}) }
func TestPure(t *testing.T) { testReader(t, makePure()) } func TestPure(tt *testing.T) { testReader(tt, makePure()) }
func benchmarkReader(b *testing.B, r resetReadCloser) { func benchmarkReader(b *testing.B, r resetReadCloser) {
if !cgoEnabled { if !cgoEnabled {
+8 -8
View File
@@ -42,9 +42,9 @@ const (
uncompressedMore = "More!\n" uncompressedMore = "More!\n"
) )
func TestRoundTrip(t *testing.T) { func TestRoundTrip(tt *testing.T) {
if !cgoEnabled { if !cgoEnabled {
t.Skip("cgo is not enabled") tt.Skip("cgo is not enabled")
} }
wr := &WriterRecycler{} wr := &WriterRecycler{}
@@ -65,16 +65,16 @@ func TestRoundTrip(t *testing.T) {
w.Reset(buf, nil, 0) w.Reset(buf, nil, 0)
if _, err := w.Write([]byte(uncompressedMore)); err != nil { if _, err := w.Write([]byte(uncompressedMore)); err != nil {
w.Close() w.Close()
t.Fatalf("i=%d: Write: %v", i, err) tt.Fatalf("i=%d: Write: %v", i, err)
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatalf("i=%d: Close: %v", i, err) tt.Fatalf("i=%d: Close: %v", i, err)
} }
} }
compressed := buf.String() compressed := buf.String()
if (compressed != compressedMore0) && (compressed != compressedMore1) { if (compressed != compressedMore0) && (compressed != compressedMore1) {
t.Fatalf("i=%d: compressed\ngot % 02x\nwant % 02x\nor % 02x", tt.Fatalf("i=%d: compressed\ngot % 02x\nwant % 02x\nor % 02x",
i, compressed, compressedMore0, compressedMore1) i, compressed, compressedMore0, compressedMore1)
} }
@@ -84,14 +84,14 @@ func TestRoundTrip(t *testing.T) {
gotBytes, err := ioutil.ReadAll(r) gotBytes, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
r.Close() r.Close()
t.Fatalf("i=%d: ReadAll: %v", i, err) tt.Fatalf("i=%d: ReadAll: %v", i, err)
} }
if got, want := string(gotBytes), uncompressedMore; got != want { if got, want := string(gotBytes), uncompressedMore; got != want {
r.Close() r.Close()
t.Fatalf("i=%d:\ngot %q\nwant %q", i, got, want) tt.Fatalf("i=%d:\ngot %q\nwant %q", i, got, want)
} }
if err := r.Close(); err != nil { if err := r.Close(); err != nil {
t.Fatalf("i=%d: Close: %v", i, err) tt.Fatalf("i=%d: Close: %v", i, err)
} }
} }
} }
+18 -18
View File
@@ -24,8 +24,8 @@ import (
"github.com/google/wuffs/internal/testcut" "github.com/google/wuffs/internal/testcut"
) )
func TestCut(t *testing.T) { func TestCut(tt *testing.T) {
testcut.Test(t, SmallestValidMaxEncodedLen, Cut, newReader, []string{ testcut.Test(tt, SmallestValidMaxEncodedLen, Cut, newReader, []string{
"artificial/deflate-backref-crosses-blocks.deflate", "artificial/deflate-backref-crosses-blocks.deflate",
"artificial/deflate-distance-32768.deflate", "artificial/deflate-distance-32768.deflate",
"romeo.txt.deflate", "romeo.txt.deflate",
@@ -50,7 +50,7 @@ func decodeFlate(src []byte) string {
return string(dst) return string(dst)
} }
func TestHuffmanDecode(t *testing.T) { func TestHuffmanDecode(tt *testing.T) {
// This exercises the "ABCDEFGH" example from RFC 1951 section 3.2.2, // This exercises the "ABCDEFGH" example from RFC 1951 section 3.2.2,
// discussed in the "type huffman" doc comment. // discussed in the "type huffman" doc comment.
@@ -116,7 +116,7 @@ func TestHuffmanDecode(t *testing.T) {
} }
if got, want := string(decoded), src; got != want { if got, want := string(decoded), src; got != want {
t.Fatalf("got %q, want %q", got, want) tt.Fatalf("got %q, want %q", got, want)
} }
} }
@@ -140,28 +140,28 @@ const (
"\x4e\x4c\xef\x37\xf5\x44\x63\x9f\xdc\xfe\x00" "\x4e\x4c\xef\x37\xf5\x44\x63\x9f\xdc\xfe\x00"
) )
func TestReplaceHuffmanWithStored(t *testing.T) { func TestReplaceHuffmanWithStored(tt *testing.T) {
const dec = degenerateHuffmanDec const dec = degenerateHuffmanDec
const enc = degenerateHuffmanEnc const enc = degenerateHuffmanEnc
if (len(dec) != 64) || (len(enc) != 59) { if (len(dec) != 64) || (len(enc) != 59) {
panic("inconsistent const string lengths") panic("inconsistent const string lengths")
} }
if got, want := decodeFlate([]byte(enc)), dec; got != want { if got, want := decodeFlate([]byte(enc)), dec; got != want {
t.Fatalf("before Cut: got %q, want %q", got, want) tt.Fatalf("before Cut: got %q, want %q", got, want)
} }
for i := 4; i <= 59; i += 5 { for i := 4; i <= 59; i += 5 {
b := []byte(enc) b := []byte(enc)
encLen, decLen, err := Cut(nil, b, i) encLen, decLen, err := Cut(nil, b, i)
if err != nil { if err != nil {
t.Errorf("i=%d: %v", i, err) tt.Errorf("i=%d: %v", i, err)
continue continue
} }
if encLen < 1 { if encLen < 1 {
t.Errorf("i=%d: encLen: got %d, want >= 1", i, encLen) tt.Errorf("i=%d: encLen: got %d, want >= 1", i, encLen)
continue continue
} else if encLen > len(enc) { } else if encLen > len(enc) {
t.Errorf("i=%d: encLen: got %d, want <= %d", i, encLen, len(enc)) tt.Errorf("i=%d: encLen: got %d, want <= %d", i, encLen, len(enc))
continue continue
} }
// If we can make some progress (decLen > 0), even if the input uses a // If we can make some progress (decLen > 0), even if the input uses a
@@ -170,15 +170,15 @@ func TestReplaceHuffmanWithStored(t *testing.T) {
// Regardless of whether the cut form uses a Huffman or Stored block, // Regardless of whether the cut form uses a Huffman or Stored block,
// we should be able to produce at least i-5 bytes of decoded output. // we should be able to produce at least i-5 bytes of decoded output.
if (decLen > 0) && (i > 5) && (decLen < i-5) { if (decLen > 0) && (i > 5) && (decLen < i-5) {
t.Errorf("i=%d: decLen: got %d, want >= %d", i, decLen, i-5) tt.Errorf("i=%d: decLen: got %d, want >= %d", i, decLen, i-5)
continue continue
} else if decLen > len(dec) { } else if decLen > len(dec) {
t.Errorf("i=%d: decLen: got %d, want <= %d", i, decLen, len(dec)) tt.Errorf("i=%d: decLen: got %d, want <= %d", i, decLen, len(dec))
continue continue
} }
if got, want := decodeFlate(b[:encLen]), dec[:decLen]; got != want { if got, want := decodeFlate(b[:encLen]), dec[:decLen]; got != want {
t.Errorf("i=%d: after Cut: got %q, want %q", i, got, want) tt.Errorf("i=%d: after Cut: got %q, want %q", i, got, want)
continue continue
} }
@@ -199,13 +199,13 @@ func TestReplaceHuffmanWithStored(t *testing.T) {
} }
if got != want { if got != want {
t.Errorf("i=%d: block type: got %d, want %d", i, got, want) tt.Errorf("i=%d: block type: got %d, want %d", i, got, want)
} }
} }
} }
} }
func TestDegenerateHuffmanUnused(t *testing.T) { func TestDegenerateHuffmanUnused(tt *testing.T) {
const dec = degenerateHuffmanDec const dec = degenerateHuffmanDec
const enc = degenerateHuffmanEnc const enc = degenerateHuffmanEnc
@@ -218,14 +218,14 @@ func TestDegenerateHuffmanUnused(t *testing.T) {
b := []byte(enc) b := []byte(enc)
encLen, decLen, err := Cut(nil, b, len(enc)-1) encLen, decLen, err := Cut(nil, b, len(enc)-1)
if err != nil { if err != nil {
t.Fatalf("Cut: %v", err) tt.Fatalf("Cut: %v", err)
} else if encLen != len(enc)-1 { } else if encLen != len(enc)-1 {
t.Fatalf("encLen: got %d, want %d", encLen, len(enc)-1) tt.Fatalf("encLen: got %d, want %d", encLen, len(enc)-1)
} else if decLen != len(dec)-n { } else if decLen != len(dec)-n {
t.Fatalf("decLen: got %d, want %d", decLen, len(dec)-n) tt.Fatalf("decLen: got %d, want %d", decLen, len(dec)-n)
} }
if got, want := decodeFlate(b[:encLen]), dec[:decLen]; got != want { if got, want := decodeFlate(b[:encLen]), dec[:decLen]; got != want {
t.Fatalf("after Cut: got %q, want %q", got, want) tt.Fatalf("after Cut: got %q, want %q", got, want)
} }
} }
+63 -63
View File
@@ -121,79 +121,79 @@ const writerWantILAStartCPageSize128 = "" +
"00000080 52 72 72 53 73 41 61 61 42 62 62 62 43 63 63 63 |RrrSsAaaBbbbCccc|\n" + "00000080 52 72 72 53 73 41 61 61 42 62 62 62 43 63 63 63 |RrrSsAaaBbbbCccc|\n" +
"00000090 63 63 63 63 63 63 31 32 |cccccc12|\n" "00000090 63 63 63 63 63 63 31 32 |cccccc12|\n"
func TestWriterILAEndEmpty(t *testing.T) { func TestWriterILAEndEmpty(tt *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 0, true); err != nil { if err := testWriter(IndexLocationAtEnd, nil, 0, true); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartEmpty(t *testing.T) { func TestWriterILAStartEmpty(tt *testing.T) {
tempFile := &bytes.Buffer{} tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 0, true); err != nil { if err := testWriter(IndexLocationAtStart, tempFile, 0, true); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAEndNoTempFile(t *testing.T) { func TestWriterILAEndNoTempFile(tt *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 0, false); err != nil { if err := testWriter(IndexLocationAtEnd, nil, 0, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAEndMemTempFile(t *testing.T) { func TestWriterILAEndMemTempFile(tt *testing.T) {
tempFile := &bytes.Buffer{} tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtEnd, tempFile, 0, false); err == nil { if err := testWriter(IndexLocationAtEnd, tempFile, 0, false); err == nil {
t.Fatal("err: got nil, want non-nil") tt.Fatal("err: got nil, want non-nil")
} else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtEnd requires") { } else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtEnd requires") {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartNoTempFile(t *testing.T) { func TestWriterILAStartNoTempFile(tt *testing.T) {
if err := testWriter(IndexLocationAtStart, nil, 0, false); err == nil { if err := testWriter(IndexLocationAtStart, nil, 0, false); err == nil {
t.Fatal("err: got nil, want non-nil") tt.Fatal("err: got nil, want non-nil")
} else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtStart requires") { } else if !strings.HasPrefix(err.Error(), "rac: IndexLocationAtStart requires") {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartMemTempFile(t *testing.T) { func TestWriterILAStartMemTempFile(tt *testing.T) {
tempFile := &bytes.Buffer{} tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 0, false); err != nil { if err := testWriter(IndexLocationAtStart, tempFile, 0, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartRealTempFile(t *testing.T) { func TestWriterILAStartRealTempFile(tt *testing.T) {
f, err := ioutil.TempFile("", "rac_test") f, err := ioutil.TempFile("", "rac_test")
if err != nil { if err != nil {
t.Fatalf("TempFile: %v", err) tt.Fatalf("TempFile: %v", err)
} }
defer os.Remove(f.Name()) defer os.Remove(f.Name())
defer f.Close() defer f.Close()
if err := testWriter(IndexLocationAtStart, f, 0, false); err != nil { if err := testWriter(IndexLocationAtStart, f, 0, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAEndCPageSize8(t *testing.T) { func TestWriterILAEndCPageSize8(tt *testing.T) {
if err := testWriter(IndexLocationAtEnd, nil, 8, false); err != nil { if err := testWriter(IndexLocationAtEnd, nil, 8, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartCPageSize4(t *testing.T) { func TestWriterILAStartCPageSize4(tt *testing.T) {
tempFile := &bytes.Buffer{} tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 4, false); err != nil { if err := testWriter(IndexLocationAtStart, tempFile, 4, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
func TestWriterILAStartCPageSize128(t *testing.T) { func TestWriterILAStartCPageSize128(tt *testing.T) {
tempFile := &bytes.Buffer{} tempFile := &bytes.Buffer{}
if err := testWriter(IndexLocationAtStart, tempFile, 128, false); err != nil { if err := testWriter(IndexLocationAtStart, tempFile, 128, false); err != nil {
t.Fatal(err) tt.Fatal(err)
} }
} }
@@ -247,7 +247,7 @@ func testWriter(iloc IndexLocation, tempFile io.ReadWriter, cPageSize uint64, em
return nil return nil
} }
func TestMultiLevelIndex(t *testing.T) { func TestMultiLevelIndex(tt *testing.T) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := &ChunkWriter{ w := &ChunkWriter{
Writer: buf, Writer: buf,
@@ -293,12 +293,12 @@ func TestMultiLevelIndex(t *testing.T) {
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err) tt.Fatalf("Close: %v", err)
} }
encoded := buf.Bytes() encoded := buf.Bytes()
if got, want := len(encoded), 0x13E2; got != want { if got, want := len(encoded), 0x13E2; got != want {
t.Fatalf("len(encoded): got 0x%X, want 0x%X", got, want) tt.Fatalf("len(encoded): got 0x%X, want 0x%X", got, want)
} }
gotHexDump := hex.Dump(encoded) gotHexDump := hex.Dump(encoded)
@@ -352,7 +352,7 @@ func TestMultiLevelIndex(t *testing.T) {
"000013e0 30 33 |03|\n" "000013e0 30 33 |03|\n"
if gotHexDump != wantHexDump { if gotHexDump != wantHexDump {
t.Fatalf("\ngot:\n%s\nwant:\n%s", gotHexDump, wantHexDump) tt.Fatalf("\ngot:\n%s\nwant:\n%s", gotHexDump, wantHexDump)
} }
r := &ChunkReader{ r := &ChunkReader{
@@ -365,7 +365,7 @@ func TestMultiLevelIndex(t *testing.T) {
if err == io.EOF { if err == io.EOF {
break break
} else if err != nil { } else if err != nil {
t.Fatalf("NextChunk: %v", err) tt.Fatalf("NextChunk: %v", err)
} }
p0, p1 := c.CPrimary[0], c.CPrimary[1] p0, p1 := c.CPrimary[0], c.CPrimary[1]
if (0 <= p0) && (p0 <= p1) && (p1 <= int64(len(encoded))) { if (0 <= p0) && (p0 <= p1) && (p1 <= int64(len(encoded))) {
@@ -377,11 +377,11 @@ func TestMultiLevelIndex(t *testing.T) {
} }
} }
if !bytes.Equal(gotPrimaries, primaries) { if !bytes.Equal(gotPrimaries, primaries) {
t.Fatalf("\ngot:\n%s\nwant:\n%s", gotPrimaries, primaries) tt.Fatalf("\ngot:\n%s\nwant:\n%s", gotPrimaries, primaries)
} }
} }
func TestWriter1000Chunks(t *testing.T) { func TestWriter1000Chunks(tt *testing.T) {
loop: loop:
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
@@ -403,7 +403,7 @@ loop:
} }
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Errorf("i=%d: Close: %v", i, err) tt.Errorf("i=%d: Close: %v", i, err)
continue loop continue loop
} }
@@ -415,12 +415,12 @@ loop:
for n := 0; ; n++ { for n := 0; ; n++ {
if _, err := r.NextChunk(); err == io.EOF { if _, err := r.NextChunk(); err == io.EOF {
if n != 1000 { if n != 1000 {
t.Errorf("i=%d: number of chunks: got %d, want %d", i, n, 1000) tt.Errorf("i=%d: number of chunks: got %d, want %d", i, n, 1000)
continue loop continue loop
} }
break break
} else if err != nil { } else if err != nil {
t.Errorf("i=%d: NextChunk: %v", i, err) tt.Errorf("i=%d: NextChunk: %v", i, err)
continue loop continue loop
} }
} }
@@ -436,7 +436,7 @@ func printChunks(chunks []Chunk) string {
return strings.Join(ss, "\n") return strings.Join(ss, "\n")
} }
func TestChunkReader(t *testing.T) { func TestChunkReader(tt *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
compressed []byte compressed []byte
@@ -482,10 +482,10 @@ loop:
} }
if gotDecompressedSize, err := r.DecompressedSize(); err != nil { if gotDecompressedSize, err := r.DecompressedSize(); err != nil {
t.Errorf("%q test case: %v", tc.name, err) tt.Errorf("%q test case: %v", tc.name, err)
continue loop continue loop
} else if gotDecompressedSize != wantDecompressedSize { } else if gotDecompressedSize != wantDecompressedSize {
t.Errorf("%q test case: DecompressedSize: got %d, want %d", tt.Errorf("%q test case: DecompressedSize: got %d, want %d",
tc.name, gotDecompressedSize, wantDecompressedSize) tc.name, gotDecompressedSize, wantDecompressedSize)
continue loop continue loop
} }
@@ -498,12 +498,12 @@ loop:
if err == io.EOF { if err == io.EOF {
break break
} else if err != nil { } else if err != nil {
t.Errorf("%q test case: NextChunk: %v", tc.name, err) tt.Errorf("%q test case: NextChunk: %v", tc.name, err)
continue loop continue loop
} }
if c.DRange[0] != prevDRange1 { if c.DRange[0] != prevDRange1 {
t.Errorf("%q test case: NextChunk: DRange[0]: got %d, want %d", tt.Errorf("%q test case: NextChunk: DRange[0]: got %d, want %d",
tc.name, c.DRange[0], prevDRange1) tc.name, c.DRange[0], prevDRange1)
continue loop continue loop
} }
@@ -519,41 +519,41 @@ loop:
if tc.name == "Empty" { if tc.name == "Empty" {
if len(gotChunks) != 0 { if len(gotChunks) != 0 {
t.Errorf("%q test case: NextChunk: got non-empty, want empty", tc.name) tt.Errorf("%q test case: NextChunk: got non-empty, want empty", tc.name)
} }
continue loop continue loop
} }
gotDescription := description.String() gotDescription := description.String()
if gotDescription != wantDescription { if gotDescription != wantDescription {
t.Errorf("%q test case: NextChunk:\n got\n%s\n which is\n%s\n want\n%s", tt.Errorf("%q test case: NextChunk:\n got\n%s\n which is\n%s\n want\n%s",
tc.name, printChunks(gotChunks), gotDescription, wantDescription) tc.name, printChunks(gotChunks), gotDescription, wantDescription)
continue loop continue loop
} }
// NextChunk should return io.EOF. // NextChunk should return io.EOF.
if _, err := r.NextChunk(); err != io.EOF { if _, err := r.NextChunk(); err != io.EOF {
t.Errorf("%q test case: NextChunk: got %v, want io.EOF", tc.name, err) tt.Errorf("%q test case: NextChunk: got %v, want io.EOF", tc.name, err)
continue loop continue loop
} }
if err := r.SeekToChunkContaining(0x30); err != nil { if err := r.SeekToChunkContaining(0x30); err != nil {
t.Errorf("%q test case: SeekToChunkContaining: %v", tc.name, err) tt.Errorf("%q test case: SeekToChunkContaining: %v", tc.name, err)
continue loop continue loop
} }
// NextChunk should return the "Bb..." chunk. // NextChunk should return the "Bb..." chunk.
if c, err := r.NextChunk(); err != nil { if c, err := r.NextChunk(); err != nil {
t.Errorf("%q test case: NextChunk: %v", tc.name, err) tt.Errorf("%q test case: NextChunk: %v", tc.name, err)
continue loop continue loop
} else if got, want := snippet(c.CPrimary), "Bb..."; got != want { } else if got, want := snippet(c.CPrimary), "Bb..."; got != want {
t.Errorf("%q test case: NextChunk: got %q, want %q", tc.name, got, want) tt.Errorf("%q test case: NextChunk: got %q, want %q", tc.name, got, want)
continue loop continue loop
} }
} }
} }
func TestReaderEmpty(t *testing.T) { func TestReaderEmpty(tt *testing.T) {
encoded := undoHexDump(writerWantEmpty) encoded := undoHexDump(writerWantEmpty)
r := &Reader{ r := &Reader{
ReadSeeker: bytes.NewReader(encoded), ReadSeeker: bytes.NewReader(encoded),
@@ -562,24 +562,24 @@ func TestReaderEmpty(t *testing.T) {
defer r.Close() defer r.Close()
got, err := ioutil.ReadAll(r) got, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
t.Fatalf("ReadAll: %v", err) tt.Fatalf("ReadAll: %v", err)
} }
if len(got) != 0 { if len(got) != 0 {
t.Fatalf("got %q, want %q", got, []byte(nil)) tt.Fatalf("got %q, want %q", got, []byte(nil))
} }
} }
func TestReaderZeroes(t *testing.T) { func TestReaderZeroes(tt *testing.T) {
const dSize = 7 const dSize = 7
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := &ChunkWriter{ w := &ChunkWriter{
Writer: buf, Writer: buf,
} }
if err := w.AddChunk(dSize, CodecZeroes, nil, 0, 0); err != nil { if err := w.AddChunk(dSize, CodecZeroes, nil, 0, 0); err != nil {
t.Fatalf("AddChunk: %v", err) tt.Fatalf("AddChunk: %v", err)
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err) tt.Fatalf("Close: %v", err)
} }
encoded := buf.Bytes() encoded := buf.Bytes()
r := &Reader{ r := &Reader{
@@ -589,15 +589,15 @@ func TestReaderZeroes(t *testing.T) {
defer r.Close() defer r.Close()
got, err := ioutil.ReadAll(r) got, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
t.Fatalf("ReadAll: %v", err) tt.Fatalf("ReadAll: %v", err)
} }
want := make([]byte, dSize) want := make([]byte, dSize)
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
t.Fatalf("got %q, want %q", got, want) tt.Fatalf("got %q, want %q", got, want)
} }
} }
func TestLongCodec(t *testing.T) { func TestLongCodec(tt *testing.T) {
const codec = Codec(0x80000000326F646D) // "mdo2" backwards, with a high bit. const codec = Codec(0x80000000326F646D) // "mdo2" backwards, with a high bit.
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := &ChunkWriter{ w := &ChunkWriter{
@@ -606,13 +606,13 @@ func TestLongCodec(t *testing.T) {
TempFile: &bytes.Buffer{}, TempFile: &bytes.Buffer{},
} }
if err := w.AddChunk(0x66, codec, []byte{0xAA, 0xBB}, 0, 0); err != nil { if err := w.AddChunk(0x66, codec, []byte{0xAA, 0xBB}, 0, 0); err != nil {
t.Fatalf("AddChunk: %v", err) tt.Fatalf("AddChunk: %v", err)
} }
if err := w.AddChunk(0x77, codec, []byte{0xCC}, 0, 0); err != nil { if err := w.AddChunk(0x77, codec, []byte{0xCC}, 0, 0); err != nil {
t.Fatalf("AddChunk: %v", err) tt.Fatalf("AddChunk: %v", err)
} }
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err) tt.Fatalf("Close: %v", err)
} }
encoded := buf.Bytes() encoded := buf.Bytes()
@@ -626,7 +626,7 @@ func TestLongCodec(t *testing.T) {
"00000040 aa bb cc |...|\n" "00000040 aa bb cc |...|\n"
if gotHexDump != wantHexDump { if gotHexDump != wantHexDump {
t.Fatalf("\ngot:\n%s\nwant:\n%s", gotHexDump, wantHexDump) tt.Fatalf("\ngot:\n%s\nwant:\n%s", gotHexDump, wantHexDump)
} }
r := &ChunkReader{ r := &ChunkReader{
@@ -636,18 +636,18 @@ func TestLongCodec(t *testing.T) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
c, err := r.NextChunk() c, err := r.NextChunk()
if err != nil { if err != nil {
t.Fatalf("i=%d: %v", i, err) tt.Fatalf("i=%d: %v", i, err)
} }
if got, want := c.Codec, codec; got != want { if got, want := c.Codec, codec; got != want {
t.Fatalf("i=%d: Codec: got 0x%X, want 0x%X", i, got, want) tt.Fatalf("i=%d: Codec: got 0x%X, want 0x%X", i, got, want)
} }
} }
if _, err := r.NextChunk(); err != io.EOF { if _, err := r.NextChunk(); err != io.EOF {
t.Fatalf("got %v, want %v", err, io.EOF) tt.Fatalf("got %v, want %v", err, io.EOF)
} }
} }
func TestFindChunkContaining(t *testing.T) { func TestFindChunkContaining(tt *testing.T) {
rng := rand.New(rand.NewSource(1)) rng := rand.New(rand.NewSource(1))
arity, dptrs := 0, [256]int64{} arity, dptrs := 0, [256]int64{}
@@ -694,7 +694,7 @@ func TestFindChunkContaining(t *testing.T) {
node[5] = uint8(checksum >> 8) node[5] = uint8(checksum >> 8)
if !node.valid() { if !node.valid() {
t.Fatalf("i=%d: invalid node", i) tt.Fatalf("i=%d: invalid node", i)
} }
for k := 0; k < 100; k++ { for k := 0; k < 100; k++ {
@@ -702,7 +702,7 @@ func TestFindChunkContaining(t *testing.T) {
got := node.findChunkContaining(dptr, 0) got := node.findChunkContaining(dptr, 0)
want := simpleFCC(dptr) want := simpleFCC(dptr)
if got != want { if got != want {
t.Fatalf("i=%d, k=%d: got %d, want %d", i, k, got, want) tt.Fatalf("i=%d, k=%d: got %d, want %d", i, k, got, want)
} }
} }
} }
+25 -25
View File
@@ -89,20 +89,20 @@ func racDecompress(compressed []byte) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func testReader(t *testing.T, decoded string, encoded string) { func testReader(tt *testing.T, decoded string, encoded string) {
g, err := racDecompress([]byte(encoded)) g, err := racDecompress([]byte(encoded))
if err != nil { if err != nil {
t.Fatalf("racDecompress: %v", err) tt.Fatalf("racDecompress: %v", err)
} }
if got, want := string(g), decoded; got != want { if got, want := string(g), decoded; got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want) tt.Fatalf("got:\n%s\nwant:\n%s", got, want)
} }
} }
func TestReaderSansDictionary(t *testing.T) { testReader(t, decodedMore, encodedMore) } func TestReaderSansDictionary(tt *testing.T) { testReader(tt, decodedMore, encodedMore) }
func TestReaderWithDictionary(t *testing.T) { testReader(t, decodedSheep, encodedSheep) } func TestReaderWithDictionary(tt *testing.T) { testReader(tt, decodedSheep, encodedSheep) }
func TestReaderConcatenation(t *testing.T) { func TestReaderConcatenation(tt *testing.T) {
// Create a RAC file whose decoding is the concatenation of two other RAC // Create a RAC file whose decoding is the concatenation of two other RAC
// file's decoding. The resultant RAC file's contents (the encoded form) is // file's decoding. The resultant RAC file's contents (the encoded form) is
// the concatenation of the two RAC files, plus a new root node. // the concatenation of the two RAC files, plus a new root node.
@@ -171,13 +171,13 @@ func TestReaderConcatenation(t *testing.T) {
buf[0x05] = byte(checksum >> 8) buf[0x05] = byte(checksum >> 8)
// Test the concatenation. // Test the concatenation.
testReader(t, testReader(tt,
decodedSheep+decodedMore, decodedSheep+decodedMore,
encodedSheep+encodedMore+string(buf[:]), encodedSheep+encodedMore+string(buf[:]),
) )
} }
func TestZeroedBytes(t *testing.T) { func TestZeroedBytes(tt *testing.T) {
original := make([]byte, 32) original := make([]byte, 32)
original[0] = 'a' original[0] = 'a'
original[1] = 'b' original[1] = 'b'
@@ -195,7 +195,7 @@ func TestZeroedBytes(t *testing.T) {
compressed, err := racCompress(original, cChunkSize, dChunkSize, nil) compressed, err := racCompress(original, cChunkSize, dChunkSize, nil)
if err != nil { if err != nil {
t.Fatalf("i=%d: racCompress: %v", i, err) tt.Fatalf("i=%d: racCompress: %v", i, err)
} }
r := &rac.Reader{ r := &rac.Reader{
@@ -212,22 +212,22 @@ func TestZeroedBytes(t *testing.T) {
} }
if _, err := r.Seek(int64(j), io.SeekStart); err != nil { if _, err := r.Seek(int64(j), io.SeekStart); err != nil {
t.Errorf("i=%d, j=%d: Seek: %v", i, j, err) tt.Errorf("i=%d, j=%d: Seek: %v", i, j, err)
continue continue
} }
if _, err := io.ReadFull(r, got); err != nil { if _, err := io.ReadFull(r, got); err != nil {
t.Errorf("i=%d, j=%d: ReadFull: %v", i, j, err) tt.Errorf("i=%d, j=%d: ReadFull: %v", i, j, err)
continue continue
} }
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
t.Errorf("i=%d, j=%d: got\n% 02x\nwant\n% 02x", i, j, got, want) tt.Errorf("i=%d, j=%d: got\n% 02x\nwant\n% 02x", i, j, got, want)
continue continue
} }
} }
} }
} }
func TestSharedDictionary(t *testing.T) { func TestSharedDictionary(tt *testing.T) {
// Make some "dictionary" data that, as an independent chunk, does not // Make some "dictionary" data that, as an independent chunk, does not
// compress very well. // compress very well.
const n = 256 const n = 256
@@ -254,20 +254,20 @@ func TestSharedDictionary(t *testing.T) {
// Compress. // Compress.
compressed, err := racCompress(original, 0, n, resourcesData) compressed, err := racCompress(original, 0, n, resourcesData)
if err != nil { if err != nil {
t.Fatalf("i=%d: racCompress: %v", i, err) tt.Fatalf("i=%d: racCompress: %v", i, err)
} }
if len(compressed) == 0 { if len(compressed) == 0 {
t.Fatalf("i=%d: compressed form is empty", i) tt.Fatalf("i=%d: compressed form is empty", i)
} }
compressedLengths[i] = len(compressed) compressedLengths[i] = len(compressed)
// Decompress. // Decompress.
decompressed, err := racDecompress(compressed) decompressed, err := racDecompress(compressed)
if err != nil { if err != nil {
t.Fatalf("i=%d: racDecompress: %v", i, err) tt.Fatalf("i=%d: racDecompress: %v", i, err)
} }
if !bytes.Equal(decompressed, original) { if !bytes.Equal(decompressed, original) {
t.Fatalf("i=%d: racDecompress: round trip did not match original", i) tt.Fatalf("i=%d: racDecompress: round trip did not match original", i)
} }
} }
@@ -275,7 +275,7 @@ func TestSharedDictionary(t *testing.T) {
// exact value depends on the Zlib compression algorithm, but we should // exact value depends on the Zlib compression algorithm, but we should
// expect at least a 4x improvement. // expect at least a 4x improvement.
if ratio := compressedLengths[0] / compressedLengths[1]; ratio < 4 { if ratio := compressedLengths[0] / compressedLengths[1]; ratio < 4 {
t.Fatalf("ratio: got %dx, want at least 4x", ratio) tt.Fatalf("ratio: got %dx, want at least 4x", ratio)
} }
} }
@@ -301,7 +301,7 @@ func (r *rsWithReadAt) ReadAt(p []byte, o int64) (int, error) { return r.r.ReadA
// testReadSeeker tests that decoding from rs works, regardless of whether rs // testReadSeeker tests that decoding from rs works, regardless of whether rs
// implements the optional ReadAt method. If rs does implement io.ReaderAt then // implements the optional ReadAt method. If rs does implement io.ReaderAt then
// its Read and Seek methods should never be called. // its Read and Seek methods should never be called.
func testReadSeeker(t *testing.T, rs io.ReadSeeker) { func testReadSeeker(tt *testing.T, rs io.ReadSeeker) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
r := &rac.Reader{ r := &rac.Reader{
ReadSeeker: rs, ReadSeeker: rs,
@@ -310,17 +310,17 @@ func testReadSeeker(t *testing.T, rs io.ReadSeeker) {
} }
defer r.Close() defer r.Close()
if _, err := io.Copy(buf, r); err != nil { if _, err := io.Copy(buf, r); err != nil {
t.Fatalf("io.Copy: %v", err) tt.Fatalf("io.Copy: %v", err)
} }
if got, want := buf.String(), decodedSheep; got != want { if got, want := buf.String(), decodedSheep; got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want) tt.Fatalf("got:\n%s\nwant:\n%s", got, want)
} }
} }
func TestReadSeekerSansReadAt(t *testing.T) { func TestReadSeekerSansReadAt(tt *testing.T) {
testReadSeeker(t, &rsSansReadAt{strings.NewReader(encodedSheep)}) testReadSeeker(tt, &rsSansReadAt{strings.NewReader(encodedSheep)})
} }
func TestReadSeekerWithReadAt(t *testing.T) { func TestReadSeekerWithReadAt(tt *testing.T) {
testReadSeeker(t, &rsWithReadAt{strings.NewReader(encodedSheep)}) testReadSeeker(tt, &rsWithReadAt{strings.NewReader(encodedSheep)})
} }
+2 -2
View File
@@ -21,8 +21,8 @@ import (
"github.com/google/wuffs/internal/testcut" "github.com/google/wuffs/internal/testcut"
) )
func TestCut(t *testing.T) { func TestCut(tt *testing.T) {
testcut.Test(t, SmallestValidMaxEncodedLen, Cut, zlib.NewReader, []string{ testcut.Test(tt, SmallestValidMaxEncodedLen, Cut, zlib.NewReader, []string{
"midsummer.txt.zlib", "midsummer.txt.zlib",
"pi.txt.zlib", "pi.txt.zlib",
"romeo.txt.zlib", "romeo.txt.zlib",