PR libgcc/55451
[official-gcc.git] / libgo / go / compress / flate / reader_test.go
blob54ed788dbd3e50f3a13fbc353ca66dbc2ba8ffe7
1 // Copyright 2012 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.
5 package flate
7 import (
8 "bytes"
9 "io"
10 "io/ioutil"
11 "runtime"
12 "strings"
13 "testing"
16 func TestNlitOutOfRange(t *testing.T) {
17 // Trying to decode this bogus flate data, which has a Huffman table
18 // with nlit=288, should not panic.
19 io.Copy(ioutil.Discard, NewReader(strings.NewReader(
20 "\xfc\xfe\x36\xe7\x5e\x1c\xef\xb3\x55\x58\x77\xb6\x56\xb5\x43\xf4"+
21 "\x6f\xf2\xd2\xe6\x3d\x99\xa0\x85\x8c\x48\xeb\xf8\xda\x83\x04\x2a"+
22 "\x75\xc4\xf8\x0f\x12\x11\xb9\xb4\x4b\x09\xa0\xbe\x8b\x91\x4c")))
25 const (
26 digits = iota
27 twain
30 var testfiles = []string{
31 // Digits is the digits of the irrational number e. Its decimal representation
32 // does not repeat, but there are only 10 posible digits, so it should be
33 // reasonably compressible.
34 digits: "../testdata/e.txt",
35 // Twain is Project Gutenberg's edition of Mark Twain's classic English novel.
36 twain: "../testdata/Mark.Twain-Tom.Sawyer.txt",
39 func benchmarkDecode(b *testing.B, testfile, level, n int) {
40 b.StopTimer()
41 b.SetBytes(int64(n))
42 buf0, err := ioutil.ReadFile(testfiles[testfile])
43 if err != nil {
44 b.Fatal(err)
46 if len(buf0) == 0 {
47 b.Fatalf("test file %q has no data", testfiles[testfile])
49 compressed := new(bytes.Buffer)
50 w, err := NewWriter(compressed, level)
51 if err != nil {
52 b.Fatal(err)
54 for i := 0; i < n; i += len(buf0) {
55 if len(buf0) > n-i {
56 buf0 = buf0[:n-i]
58 io.Copy(w, bytes.NewBuffer(buf0))
60 w.Close()
61 buf1 := compressed.Bytes()
62 buf0, compressed, w = nil, nil, nil
63 runtime.GC()
64 b.StartTimer()
65 for i := 0; i < b.N; i++ {
66 io.Copy(ioutil.Discard, NewReader(bytes.NewBuffer(buf1)))
70 // These short names are so that gofmt doesn't break the BenchmarkXxx function
71 // bodies below over multiple lines.
72 const (
73 speed = BestSpeed
74 default_ = DefaultCompression
75 compress = BestCompression
78 func BenchmarkDecodeDigitsSpeed1e4(b *testing.B) { benchmarkDecode(b, digits, speed, 1e4) }
79 func BenchmarkDecodeDigitsSpeed1e5(b *testing.B) { benchmarkDecode(b, digits, speed, 1e5) }
80 func BenchmarkDecodeDigitsSpeed1e6(b *testing.B) { benchmarkDecode(b, digits, speed, 1e6) }
81 func BenchmarkDecodeDigitsDefault1e4(b *testing.B) { benchmarkDecode(b, digits, default_, 1e4) }
82 func BenchmarkDecodeDigitsDefault1e5(b *testing.B) { benchmarkDecode(b, digits, default_, 1e5) }
83 func BenchmarkDecodeDigitsDefault1e6(b *testing.B) { benchmarkDecode(b, digits, default_, 1e6) }
84 func BenchmarkDecodeDigitsCompress1e4(b *testing.B) { benchmarkDecode(b, digits, compress, 1e4) }
85 func BenchmarkDecodeDigitsCompress1e5(b *testing.B) { benchmarkDecode(b, digits, compress, 1e5) }
86 func BenchmarkDecodeDigitsCompress1e6(b *testing.B) { benchmarkDecode(b, digits, compress, 1e6) }
87 func BenchmarkDecodeTwainSpeed1e4(b *testing.B) { benchmarkDecode(b, twain, speed, 1e4) }
88 func BenchmarkDecodeTwainSpeed1e5(b *testing.B) { benchmarkDecode(b, twain, speed, 1e5) }
89 func BenchmarkDecodeTwainSpeed1e6(b *testing.B) { benchmarkDecode(b, twain, speed, 1e6) }
90 func BenchmarkDecodeTwainDefault1e4(b *testing.B) { benchmarkDecode(b, twain, default_, 1e4) }
91 func BenchmarkDecodeTwainDefault1e5(b *testing.B) { benchmarkDecode(b, twain, default_, 1e5) }
92 func BenchmarkDecodeTwainDefault1e6(b *testing.B) { benchmarkDecode(b, twain, default_, 1e6) }
93 func BenchmarkDecodeTwainCompress1e4(b *testing.B) { benchmarkDecode(b, twain, compress, 1e4) }
94 func BenchmarkDecodeTwainCompress1e5(b *testing.B) { benchmarkDecode(b, twain, compress, 1e5) }
95 func BenchmarkDecodeTwainCompress1e6(b *testing.B) { benchmarkDecode(b, twain, compress, 1e6) }