github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/compress/flate/reader_test.go (about) 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. 4 5 package flate 6 7 import ( 8 "bytes" 9 "io" 10 "io/ioutil" 11 "runtime" 12 "strings" 13 "testing" 14 ) 15 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"))) 23 } 24 25 const ( 26 digits = iota 27 twain 28 ) 29 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 possible digits, so it should be 33 // reasonably compressible. 34 digits: "../testdata/e.txt", 35 // Twain is Mark Twain's classic English novel. 36 twain: "../testdata/Mark.Twain-Tom.Sawyer.txt", 37 } 38 39 func benchmarkDecode(b *testing.B, testfile, level, n int) { 40 b.ReportAllocs() 41 b.StopTimer() 42 b.SetBytes(int64(n)) 43 buf0, err := ioutil.ReadFile(testfiles[testfile]) 44 if err != nil { 45 b.Fatal(err) 46 } 47 if len(buf0) == 0 { 48 b.Fatalf("test file %q has no data", testfiles[testfile]) 49 } 50 compressed := new(bytes.Buffer) 51 w, err := NewWriter(compressed, level) 52 if err != nil { 53 b.Fatal(err) 54 } 55 for i := 0; i < n; i += len(buf0) { 56 if len(buf0) > n-i { 57 buf0 = buf0[:n-i] 58 } 59 io.Copy(w, bytes.NewReader(buf0)) 60 } 61 w.Close() 62 buf1 := compressed.Bytes() 63 buf0, compressed, w = nil, nil, nil 64 runtime.GC() 65 b.StartTimer() 66 for i := 0; i < b.N; i++ { 67 io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1))) 68 } 69 } 70 71 // These short names are so that gofmt doesn't break the BenchmarkXxx function 72 // bodies below over multiple lines. 73 const ( 74 speed = BestSpeed 75 default_ = DefaultCompression 76 compress = BestCompression 77 ) 78 79 func BenchmarkDecodeDigitsSpeed1e4(b *testing.B) { benchmarkDecode(b, digits, speed, 1e4) } 80 func BenchmarkDecodeDigitsSpeed1e5(b *testing.B) { benchmarkDecode(b, digits, speed, 1e5) } 81 func BenchmarkDecodeDigitsSpeed1e6(b *testing.B) { benchmarkDecode(b, digits, speed, 1e6) } 82 func BenchmarkDecodeDigitsDefault1e4(b *testing.B) { benchmarkDecode(b, digits, default_, 1e4) } 83 func BenchmarkDecodeDigitsDefault1e5(b *testing.B) { benchmarkDecode(b, digits, default_, 1e5) } 84 func BenchmarkDecodeDigitsDefault1e6(b *testing.B) { benchmarkDecode(b, digits, default_, 1e6) } 85 func BenchmarkDecodeDigitsCompress1e4(b *testing.B) { benchmarkDecode(b, digits, compress, 1e4) } 86 func BenchmarkDecodeDigitsCompress1e5(b *testing.B) { benchmarkDecode(b, digits, compress, 1e5) } 87 func BenchmarkDecodeDigitsCompress1e6(b *testing.B) { benchmarkDecode(b, digits, compress, 1e6) } 88 func BenchmarkDecodeTwainSpeed1e4(b *testing.B) { benchmarkDecode(b, twain, speed, 1e4) } 89 func BenchmarkDecodeTwainSpeed1e5(b *testing.B) { benchmarkDecode(b, twain, speed, 1e5) } 90 func BenchmarkDecodeTwainSpeed1e6(b *testing.B) { benchmarkDecode(b, twain, speed, 1e6) } 91 func BenchmarkDecodeTwainDefault1e4(b *testing.B) { benchmarkDecode(b, twain, default_, 1e4) } 92 func BenchmarkDecodeTwainDefault1e5(b *testing.B) { benchmarkDecode(b, twain, default_, 1e5) } 93 func BenchmarkDecodeTwainDefault1e6(b *testing.B) { benchmarkDecode(b, twain, default_, 1e6) } 94 func BenchmarkDecodeTwainCompress1e4(b *testing.B) { benchmarkDecode(b, twain, compress, 1e4) } 95 func BenchmarkDecodeTwainCompress1e5(b *testing.B) { benchmarkDecode(b, twain, compress, 1e5) } 96 func BenchmarkDecodeTwainCompress1e6(b *testing.B) { benchmarkDecode(b, twain, compress, 1e6) }