...

Source file src/compress/flate/inflate_test.go

Documentation: compress/flate

     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  	"bufio"
     9  	"bytes"
    10  	"io"
    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 := io.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  }
    99  
   100  func TestReaderReusesReaderBuffer(t *testing.T) {
   101  	encodedReader := bytes.NewReader([]byte{})
   102  	encodedNotByteReader := struct{ io.Reader }{encodedReader}
   103  
   104  	t.Run("BufferIsReused", func(t *testing.T) {
   105  		f := NewReader(encodedNotByteReader).(*decompressor)
   106  		bufioR, ok := f.r.(*bufio.Reader)
   107  		if !ok {
   108  			t.Fatalf("bufio.Reader should be created")
   109  		}
   110  		f.Reset(encodedNotByteReader, nil)
   111  		if bufioR != f.r {
   112  			t.Fatalf("bufio.Reader was not reused")
   113  		}
   114  	})
   115  	t.Run("BufferIsNotReusedWhenGotByteReader", func(t *testing.T) {
   116  		f := NewReader(encodedNotByteReader).(*decompressor)
   117  		if _, ok := f.r.(*bufio.Reader); !ok {
   118  			t.Fatalf("bufio.Reader should be created")
   119  		}
   120  		f.Reset(encodedReader, nil)
   121  		if f.r != encodedReader {
   122  			t.Fatalf("provided io.ByteReader should be used directly")
   123  		}
   124  	})
   125  	t.Run("BufferIsCreatedAfterByteReader", func(t *testing.T) {
   126  		for i, r := range []io.Reader{encodedReader, bufio.NewReader(encodedReader)} {
   127  			f := NewReader(r).(*decompressor)
   128  			if f.r != r {
   129  				t.Fatalf("provided io.ByteReader should be used directly, i=%d", i)
   130  			}
   131  			f.Reset(encodedNotByteReader, nil)
   132  			if _, ok := f.r.(*bufio.Reader); !ok {
   133  				t.Fatalf("bufio.Reader should be created, i=%d", i)
   134  			}
   135  		}
   136  	})
   137  }
   138  

View as plain text