Beispiel #1
0
func benchmarkDecode(b *testing.B, file string, level, n int) {
	b.StopTimer()
	b.SetBytes(int64(n))
	buf := testutil.MustLoadFile(file, n)
	w := new(bytes.Buffer)
	wr, err := NewWriter(w, &WriterConfig{Level: level})
	if err != nil {
		b.Fatalf("unexpected error: %v", err)
	}
	if _, err := wr.Write(buf); err != nil {
		b.Fatalf("unexpected error: %v", err)
	}
	if err := wr.Close(); err != nil {
		b.Fatalf("unexpected error: %v", err)
	}
	runtime.GC()
	b.ReportAllocs()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		r, err := NewReader(bytes.NewBuffer(w.Bytes()), nil)
		if err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
		if _, err := io.Copy(ioutil.Discard, r); err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
		if err := r.Close(); err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
	}
}
Beispiel #2
0
func testRoundTrip(t *testing.T, enc Encoder, dec Decoder) {
	type entry struct {
		name  string // Name of the test
		file  string // The input test file
		level int    // The size of the input
		size  int    // The compression level
	}
	var vectors []entry
	for _, f := range []string{
		"binary.bin", "digits.txt", "huffman.txt", "random.bin", "repeats.bin", "twain.txt", "zeros.bin",
	} {
		var l, s int = 6, 1e6
		vectors = append(vectors, entry{getName(f, l, s), f, l, s})
	}

	for i, v := range vectors {
		input := testutil.MustLoadFile("../../../testdata/"+v.file, v.size)
		buf := new(bytes.Buffer)
		wr := enc(buf, v.level)
		_, cpErr := io.Copy(wr, bytes.NewReader(input))
		if err := wr.Close(); err != nil {
			t.Errorf("test %d, %s: unexpected error: %v", i, v.name, err)
			continue
		}
		if cpErr != nil {
			t.Errorf("test %d, %s: unexpected error: %v", i, v.name, cpErr)
			continue
		}

		hash := crc32.NewIEEE()
		rd := dec(buf)
		cnt, cpErr := io.Copy(hash, rd)
		if err := rd.Close(); err != nil {
			t.Errorf("test %d, %s: unexpected error: %v", i, v.name, err)
			continue
		}
		if cpErr != nil {
			t.Errorf("test %d, %s: unexpected error: %v", i, v.name, cpErr)
			continue
		}

		sum := crc32.ChecksumIEEE(input)
		if int(cnt) != len(input) {
			t.Errorf("test %d, %s: mismatching count: got %d, want %d", i, v.name, cnt, len(input))
		}
		if hash.Sum32() != sum {
			t.Errorf("test %d, %s: mismatching checksum: got 0x%08x, want 0x%08x", i, v.name, hash.Sum32(), sum)
		}
	}
}
Beispiel #3
0
func TestRoundTrip(t *testing.T) {
	var vectors = []struct {
		input []byte
	}{
		{input: testutil.MustLoadFile(binary, -1)},
		{input: testutil.MustLoadFile(digits, -1)},
		{input: testutil.MustLoadFile(huffman, -1)},
		{input: testutil.MustLoadFile(random, -1)},
		{input: testutil.MustLoadFile(repeats, -1)},
		{input: testutil.MustLoadFile(twain, -1)},
		{input: testutil.MustLoadFile(zeros, -1)},
	}

	for i, v := range vectors {
		var buf bytes.Buffer
		wr, _ := flate.NewWriter(&buf, flate.DefaultCompression)
		cnt, err := io.Copy(wr, bytes.NewReader(v.input))
		if err != nil {
			t.Errorf("test %d, write error: got %v", i, err)
		}
		if cnt != int64(len(v.input)) {
			t.Errorf("test %d, write count mismatch: got %d, want %d", i, cnt, len(v.input))
		}
		if err := wr.Close(); err != nil {
			t.Errorf("test %d, close error: got %v", i, err)
		}

		// Write a canary byte to ensure this does not get read.
		buf.WriteByte(0x7a)

		rd, err := NewReader(&struct{ compress.ByteReader }{&buf}, nil)
		if err != nil {
			t.Errorf("test %d, NewReader error: got %v", i, err)
		}
		output, err := ioutil.ReadAll(rd)
		if err != nil {
			t.Errorf("test %d, read error: got %v", i, err)
		}
		if !bytes.Equal(output, v.input) {
			t.Errorf("test %d, output data mismatch", i)
		}
		if err := wr.Close(); err != nil {
			t.Errorf("test %d, close error: got %v", i, err)
		}

		// Read back the canary byte.
		if v, _ := buf.ReadByte(); v != 0x7a {
			t.Errorf("Read consumed more data than necessary")
		}
	}
}
Beispiel #4
0
func TestRoundTrip(t *testing.T) {
	var vectors = []struct {
		input []byte
	}{
		{input: testutil.MustLoadFile(binary, -1)},
		{input: testutil.MustLoadFile(digits, -1)},
		{input: testutil.MustLoadFile(huffman, -1)},
		{input: testutil.MustLoadFile(random, -1)},
		{input: testutil.MustLoadFile(repeats, -1)},
		{input: testutil.MustLoadFile(twain, -1)},
		{input: testutil.MustLoadFile(zeros, -1)},
	}

	for i, v := range vectors {
		var buf bytes.Buffer
		wr, err := NewWriter(&buf, nil)
		if err != nil {
			t.Errorf("test %d, NewWriter error: got %v", i, err)
		}
		cnt, err := io.Copy(wr, bytes.NewReader(v.input))
		if err != nil {
			t.Errorf("test %d, write error: got %v", i, err)
		}
		if cnt != int64(len(v.input)) {
			t.Errorf("test %d, write count mismatch: got %d, want %d", i, cnt, len(v.input))
		}
		if err := wr.Close(); err != nil {
			t.Errorf("test %d, close error: got %v", i, err)
		}

		rd, err := NewReader(&buf, nil)
		if err != nil {
			t.Errorf("test %d, NewReader error: got %v", i, err)
		}
		output, err := ioutil.ReadAll(rd)
		if err != nil {
			t.Errorf("test %d, read error: got %v", i, err)
		}
		t.Log(len(output), len(v.input))

		if !bytes.Equal(output, v.input) {
			t.Errorf("test %d, output data mismatch", i)
		}
	}
}
Beispiel #5
0
func benchmarkEncode(b *testing.B, file string, level, n int) {
	b.StopTimer()
	b.SetBytes(int64(n))
	buf := testutil.MustLoadFile(file, n)
	runtime.GC()
	b.ReportAllocs()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		w, err := NewWriter(ioutil.Discard, &WriterConfig{Level: level})
		if err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
		if _, err := io.Copy(w, bytes.NewBuffer(buf)); err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
		if err := w.Close(); err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
	}
}