github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/compress/flate/inflate_test.go (about)

     1  // Copyright 2014 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  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestReset(t *testing.T) {
    16  	ss := []string{
    17  		"lorem ipsum izzle fo rizzle",
    18  		"the quick brown fox jumped over",
    19  	}
    20  
    21  	deflated := make([]bytes.Buffer, 2)
    22  	for i, s := range ss {
    23  		w, _ := NewWriter(&deflated[i], 1)
    24  		w.Write([]byte(s))
    25  		w.Close()
    26  	}
    27  
    28  	inflated := make([]bytes.Buffer, 2)
    29  
    30  	f := NewReader(&deflated[0])
    31  	io.Copy(&inflated[0], f)
    32  	f.(Resetter).Reset(&deflated[1], nil)
    33  	io.Copy(&inflated[1], f)
    34  	f.Close()
    35  
    36  	for i, s := range ss {
    37  		if s != inflated[i].String() {
    38  			t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, inflated[i], s)
    39  		}
    40  	}
    41  }
    42  
    43  func TestReaderTruncated(t *testing.T) {
    44  	vectors := []struct{ input, output string }{
    45  		{"\x00", ""},
    46  		{"\x00\f", ""},
    47  		{"\x00\f\x00", ""},
    48  		{"\x00\f\x00\xf3\xff", ""},
    49  		{"\x00\f\x00\xf3\xffhello", "hello"},
    50  		{"\x00\f\x00\xf3\xffhello, world", "hello, world"},
    51  		{"\x02", ""},
    52  		{"\xf2H\xcd", "He"},
    53  		{"\xf2H͙0a\u0084\t", "Hel\x90\x90\x90\x90\x90"},
    54  		{"\xf2H͙0a\u0084\t\x00", "Hel\x90\x90\x90\x90\x90"},
    55  	}
    56  
    57  	for i, v := range vectors {
    58  		r := strings.NewReader(v.input)
    59  		zr := NewReader(r)
    60  		b, err := ioutil.ReadAll(zr)
    61  		if err != io.ErrUnexpectedEOF {
    62  			t.Errorf("test %d, error mismatch: got %v, want io.ErrUnexpectedEOF", i, err)
    63  		}
    64  		if string(b) != v.output {
    65  			t.Errorf("test %d, output mismatch: got %q, want %q", i, b, v.output)
    66  		}
    67  	}
    68  }
    69  
    70  func TestResetDict(t *testing.T) {
    71  	dict := []byte("the lorem fox")
    72  	ss := []string{
    73  		"lorem ipsum izzle fo rizzle",
    74  		"the quick brown fox jumped over",
    75  	}
    76  
    77  	deflated := make([]bytes.Buffer, len(ss))
    78  	for i, s := range ss {
    79  		w, _ := NewWriterDict(&deflated[i], DefaultCompression, dict)
    80  		w.Write([]byte(s))
    81  		w.Close()
    82  	}
    83  
    84  	inflated := make([]bytes.Buffer, len(ss))
    85  
    86  	f := NewReader(nil)
    87  	for i := range inflated {
    88  		f.(Resetter).Reset(&deflated[i], dict)
    89  		io.Copy(&inflated[i], f)
    90  	}
    91  	f.Close()
    92  
    93  	for i, s := range ss {
    94  		if s != inflated[i].String() {
    95  			t.Errorf("inflated[%d]:\ngot  %q\nwant %q", i, inflated[i], s)
    96  		}
    97  	}
    98  }