github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/compress/lzw/writer_test.go (about)

     1  // Copyright 2011 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 lzw
     6  
     7  import (
     8  	"fmt"
     9  	"internal/testenv"
    10  	"io"
    11  	"math"
    12  	"os"
    13  	"runtime"
    14  	"testing"
    15  )
    16  
    17  var filenames = []string{
    18  	"../testdata/gettysburg.txt",
    19  	"../testdata/e.txt",
    20  	"../testdata/pi.txt",
    21  }
    22  
    23  // testFile tests that compressing and then decompressing the given file with
    24  // the given options yields equivalent bytes to the original file.
    25  func testFile(t *testing.T, fn string, order Order, litWidth int) {
    26  	// Read the file, as golden output.
    27  	golden, err := os.Open(fn)
    28  	if err != nil {
    29  		t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
    30  		return
    31  	}
    32  	defer golden.Close()
    33  
    34  	// Read the file again, and push it through a pipe that compresses at the write end, and decompresses at the read end.
    35  	raw, err := os.Open(fn)
    36  	if err != nil {
    37  		t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err)
    38  		return
    39  	}
    40  
    41  	piper, pipew := io.Pipe()
    42  	defer piper.Close()
    43  	go func() {
    44  		defer raw.Close()
    45  		defer pipew.Close()
    46  		lzww := NewWriter(pipew, order, litWidth)
    47  		defer lzww.Close()
    48  		var b [4096]byte
    49  		for {
    50  			n, err0 := raw.Read(b[:])
    51  			if err0 != nil && err0 != io.EOF {
    52  				t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
    53  				return
    54  			}
    55  			_, err1 := lzww.Write(b[:n])
    56  			if err1 != nil {
    57  				t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
    58  				return
    59  			}
    60  			if err0 == io.EOF {
    61  				break
    62  			}
    63  		}
    64  	}()
    65  	lzwr := NewReader(piper, order, litWidth)
    66  	defer lzwr.Close()
    67  
    68  	// Compare the two.
    69  	b0, err0 := io.ReadAll(golden)
    70  	b1, err1 := io.ReadAll(lzwr)
    71  	if err0 != nil {
    72  		t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err0)
    73  		return
    74  	}
    75  	if err1 != nil {
    76  		t.Errorf("%s (order=%d litWidth=%d): %v", fn, order, litWidth, err1)
    77  		return
    78  	}
    79  	if len(b1) != len(b0) {
    80  		t.Errorf("%s (order=%d litWidth=%d): length mismatch %d != %d", fn, order, litWidth, len(b1), len(b0))
    81  		return
    82  	}
    83  	for i := 0; i < len(b0); i++ {
    84  		if b1[i] != b0[i] {
    85  			t.Errorf("%s (order=%d litWidth=%d): mismatch at %d, 0x%02x != 0x%02x\n", fn, order, litWidth, i, b1[i], b0[i])
    86  			return
    87  		}
    88  	}
    89  }
    90  
    91  func TestWriter(t *testing.T) {
    92  	for _, filename := range filenames {
    93  		for _, order := range [...]Order{LSB, MSB} {
    94  			// The test data "2.71828 etcetera" is ASCII text requiring at least 6 bits.
    95  			for litWidth := 6; litWidth <= 8; litWidth++ {
    96  				if filename == "../testdata/gettysburg.txt" && litWidth == 6 {
    97  					continue
    98  				}
    99  				testFile(t, filename, order, litWidth)
   100  			}
   101  		}
   102  		if testing.Short() && testenv.Builder() == "" {
   103  			break
   104  		}
   105  	}
   106  }
   107  
   108  func TestWriterReturnValues(t *testing.T) {
   109  	w := NewWriter(io.Discard, LSB, 8)
   110  	n, err := w.Write([]byte("asdf"))
   111  	if n != 4 || err != nil {
   112  		t.Errorf("got %d, %v, want 4, nil", n, err)
   113  	}
   114  }
   115  
   116  func TestSmallLitWidth(t *testing.T) {
   117  	w := NewWriter(io.Discard, LSB, 2)
   118  	if _, err := w.Write([]byte{0x03}); err != nil {
   119  		t.Fatalf("write a byte < 1<<2: %v", err)
   120  	}
   121  	if _, err := w.Write([]byte{0x04}); err == nil {
   122  		t.Fatal("write a byte >= 1<<2: got nil error, want non-nil")
   123  	}
   124  }
   125  
   126  func BenchmarkEncoder(b *testing.B) {
   127  	buf, err := os.ReadFile("../testdata/e.txt")
   128  	if err != nil {
   129  		b.Fatal(err)
   130  	}
   131  	if len(buf) == 0 {
   132  		b.Fatalf("test file has no data")
   133  	}
   134  
   135  	for e := 4; e <= 6; e++ {
   136  		n := int(math.Pow10(e))
   137  		buf0 := buf
   138  		buf1 := make([]byte, n)
   139  		for i := 0; i < n; i += len(buf0) {
   140  			if len(buf0) > n-i {
   141  				buf0 = buf0[:n-i]
   142  			}
   143  			copy(buf1[i:], buf0)
   144  		}
   145  		buf0 = nil
   146  		runtime.GC()
   147  		b.Run(fmt.Sprint("1e", e), func(b *testing.B) {
   148  			b.SetBytes(int64(n))
   149  			for i := 0; i < b.N; i++ {
   150  				w := NewWriter(io.Discard, LSB, 8)
   151  				w.Write(buf1)
   152  				w.Close()
   153  			}
   154  		})
   155  	}
   156  }