...

Source file src/compress/flate/reader_test.go

Documentation: compress/flate

		 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  	"os"
		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(io.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  var suites = []struct{ name, file string }{
		26  	// Digits is the digits of the irrational number e. Its decimal representation
		27  	// does not repeat, but there are only 10 possible digits, so it should be
		28  	// reasonably compressible.
		29  	{"Digits", "../testdata/e.txt"},
		30  	// Newton is Isaac Newtons's educational text on Opticks.
		31  	{"Newton", "../../testdata/Isaac.Newton-Opticks.txt"},
		32  }
		33  
		34  func BenchmarkDecode(b *testing.B) {
		35  	doBench(b, func(b *testing.B, buf0 []byte, level, n int) {
		36  		b.ReportAllocs()
		37  		b.StopTimer()
		38  		b.SetBytes(int64(n))
		39  
		40  		compressed := new(bytes.Buffer)
		41  		w, err := NewWriter(compressed, level)
		42  		if err != nil {
		43  			b.Fatal(err)
		44  		}
		45  		for i := 0; i < n; i += len(buf0) {
		46  			if len(buf0) > n-i {
		47  				buf0 = buf0[:n-i]
		48  			}
		49  			io.Copy(w, bytes.NewReader(buf0))
		50  		}
		51  		w.Close()
		52  		buf1 := compressed.Bytes()
		53  		buf0, compressed, w = nil, nil, nil
		54  		runtime.GC()
		55  		b.StartTimer()
		56  		for i := 0; i < b.N; i++ {
		57  			io.Copy(io.Discard, NewReader(bytes.NewReader(buf1)))
		58  		}
		59  	})
		60  }
		61  
		62  var levelTests = []struct {
		63  	name	string
		64  	level int
		65  }{
		66  	{"Huffman", HuffmanOnly},
		67  	{"Speed", BestSpeed},
		68  	{"Default", DefaultCompression},
		69  	{"Compression", BestCompression},
		70  }
		71  
		72  var sizes = []struct {
		73  	name string
		74  	n		int
		75  }{
		76  	{"1e4", 1e4},
		77  	{"1e5", 1e5},
		78  	{"1e6", 1e6},
		79  }
		80  
		81  func doBench(b *testing.B, f func(b *testing.B, buf []byte, level, n int)) {
		82  	for _, suite := range suites {
		83  		buf, err := os.ReadFile(suite.file)
		84  		if err != nil {
		85  			b.Fatal(err)
		86  		}
		87  		if len(buf) == 0 {
		88  			b.Fatalf("test file %q has no data", suite.file)
		89  		}
		90  		for _, l := range levelTests {
		91  			for _, s := range sizes {
		92  				b.Run(suite.name+"/"+l.name+"/"+s.name, func(b *testing.B) {
		93  					f(b, buf, l.level, s.n)
		94  				})
		95  			}
		96  		}
		97  	}
		98  }
		99  

View as plain text