github.com/sandwich-go/boost@v1.3.29/xcompress/gzip_test.go (about)

     1  package xcompress
     2  
     3  import (
     4  	"github.com/sandwich-go/boost/xrand"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"testing"
     7  )
     8  
     9  func getTestFrames() [][]byte {
    10  	return [][]byte{
    11  		nil,
    12  		[]byte(""),
    13  		[]byte("time.Duration,[]time.Duration,map[string]*Redis此类的非基础类型的slice或者map都需要辅助指明类型"),
    14  		[]byte(xrand.String(100)),
    15  	}
    16  }
    17  
    18  func testFlatAndInflate(c Compressor, frame []byte) {
    19  	out, err1 := c.Flat(frame)
    20  	So(err1, ShouldBeNil)
    21  	So(len(out), ShouldNotBeEmpty)
    22  
    23  	in, err2 := c.Inflate(out)
    24  	So(err2, ShouldBeNil)
    25  	So(in, ShouldResemble, frame)
    26  }
    27  
    28  func testNFlatAndInflate(c Compressor, frame []byte, maxTimes uint32) {
    29  	var err error
    30  	var out = frame
    31  	var n = maxTimes
    32  	var i uint32
    33  	for i = 0; i < n; i++ {
    34  		out, err = c.Flat(out)
    35  		So(err, ShouldBeNil)
    36  		So(len(out), ShouldNotBeEmpty)
    37  	}
    38  	for i = 0; i < n; i++ {
    39  		out, err = c.Inflate(out)
    40  		So(err, ShouldBeNil)
    41  	}
    42  	So(out, ShouldResemble, frame)
    43  }
    44  
    45  func TestGZIP(t *testing.T) {
    46  	Convey("init gzip compressor", t, func() {
    47  		c, err := newGzipCompressor(BestCompression + 1)
    48  		So(err, ShouldNotBeNil)
    49  		So(c, ShouldBeNil)
    50  
    51  		c, err = newGzipCompressor(HuffmanOnly - 1)
    52  		So(err, ShouldNotBeNil)
    53  		So(c, ShouldBeNil)
    54  
    55  		c, err = newGzipCompressor(HuffmanOnly)
    56  		So(err, ShouldBeNil)
    57  		So(c, ShouldNotBeNil)
    58  	})
    59  
    60  	Convey("gzip flat/inflate", t, func() {
    61  		for _, frame := range getTestFrames() {
    62  			for lvl := HuffmanOnly; lvl <= BestCompression; lvl++ {
    63  				c, err0 := newGzipCompressor(lvl)
    64  				So(err0, ShouldBeNil)
    65  				So(c, ShouldNotBeNil)
    66  
    67  				testFlatAndInflate(c, frame)
    68  			}
    69  		}
    70  	})
    71  
    72  	Convey("gzip flat/inflate n times", t, func() {
    73  		for _, frame := range getTestFrames() {
    74  			for lvl := HuffmanOnly; lvl <= BestCompression; lvl++ {
    75  				c, err := newGzipCompressor(lvl)
    76  				So(err, ShouldBeNil)
    77  				So(c, ShouldNotBeNil)
    78  
    79  				testNFlatAndInflate(c, frame, 10)
    80  			}
    81  		}
    82  	})
    83  }