Source file src/compress/flate/inflate_test.go

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

View as plain text