github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/compress/lzw/reader_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  	"bytes"
     9  	"io"
    10  	"io/ioutil"
    11  	"runtime"
    12  	"strconv"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  type lzwTest struct {
    18  	desc       string
    19  	raw        string
    20  	compressed string
    21  	err        error
    22  }
    23  
    24  var lzwTests = []lzwTest{
    25  	{
    26  		"empty;LSB;8",
    27  		"",
    28  		"\x01\x01",
    29  		nil,
    30  	},
    31  	{
    32  		"empty;MSB;8",
    33  		"",
    34  		"\x80\x80",
    35  		nil,
    36  	},
    37  	{
    38  		"tobe;LSB;7",
    39  		"TOBEORNOTTOBEORTOBEORNOT",
    40  		"\x54\x4f\x42\x45\x4f\x52\x4e\x4f\x54\x82\x84\x86\x8b\x85\x87\x89\x81",
    41  		nil,
    42  	},
    43  	{
    44  		"tobe;LSB;8",
    45  		"TOBEORNOTTOBEORTOBEORNOT",
    46  		"\x54\x9e\x08\x29\xf2\x44\x8a\x93\x27\x54\x04\x12\x34\xb8\xb0\xe0\xc1\x84\x01\x01",
    47  		nil,
    48  	},
    49  	{
    50  		"tobe;MSB;7",
    51  		"TOBEORNOTTOBEORTOBEORNOT",
    52  		"\x54\x4f\x42\x45\x4f\x52\x4e\x4f\x54\x82\x84\x86\x8b\x85\x87\x89\x81",
    53  		nil,
    54  	},
    55  	{
    56  		"tobe;MSB;8",
    57  		"TOBEORNOTTOBEORTOBEORNOT",
    58  		"\x2a\x13\xc8\x44\x52\x79\x48\x9c\x4f\x2a\x40\xa0\x90\x68\x5c\x16\x0f\x09\x80\x80",
    59  		nil,
    60  	},
    61  	{
    62  		"tobe-truncated;LSB;8",
    63  		"TOBEORNOTTOBEORTOBEORNOT",
    64  		"\x54\x9e\x08\x29\xf2\x44\x8a\x93\x27\x54\x04",
    65  		io.ErrUnexpectedEOF,
    66  	},
    67  	// This example comes from http://en.wikipedia.org/wiki/Graphics_Interchange_Format.
    68  	{
    69  		"gif;LSB;8",
    70  		"\x28\xff\xff\xff\x28\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
    71  		"\x00\x51\xfc\x1b\x28\x70\xa0\xc1\x83\x01\x01",
    72  		nil,
    73  	},
    74  	// This example comes from http://compgroups.net/comp.lang.ruby/Decompressing-LZW-compression-from-PDF-file
    75  	{
    76  		"pdf;MSB;8",
    77  		"-----A---B",
    78  		"\x80\x0b\x60\x50\x22\x0c\x0c\x85\x01",
    79  		nil,
    80  	},
    81  }
    82  
    83  func TestReader(t *testing.T) {
    84  	var b bytes.Buffer
    85  	for _, tt := range lzwTests {
    86  		d := strings.Split(tt.desc, ";")
    87  		var order Order
    88  		switch d[1] {
    89  		case "LSB":
    90  			order = LSB
    91  		case "MSB":
    92  			order = MSB
    93  		default:
    94  			t.Errorf("%s: bad order %q", tt.desc, d[1])
    95  		}
    96  		litWidth, _ := strconv.Atoi(d[2])
    97  		rc := NewReader(strings.NewReader(tt.compressed), order, litWidth)
    98  		defer rc.Close()
    99  		b.Reset()
   100  		n, err := io.Copy(&b, rc)
   101  		s := b.String()
   102  		if err != nil {
   103  			if err != tt.err {
   104  				t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
   105  			}
   106  			if err == io.ErrUnexpectedEOF {
   107  				// Even if the input is truncated, we should still return the
   108  				// partial decoded result.
   109  				if n == 0 || !strings.HasPrefix(tt.raw, s) {
   110  					t.Errorf("got %d bytes (%q), want a non-empty prefix of %q", n, s, tt.raw)
   111  				}
   112  			}
   113  			continue
   114  		}
   115  		if s != tt.raw {
   116  			t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
   117  		}
   118  	}
   119  }
   120  
   121  func benchmarkDecoder(b *testing.B, n int) {
   122  	b.StopTimer()
   123  	b.SetBytes(int64(n))
   124  	buf0, err := ioutil.ReadFile("../testdata/e.txt")
   125  	if err != nil {
   126  		b.Fatal(err)
   127  	}
   128  	if len(buf0) == 0 {
   129  		b.Fatalf("test file has no data")
   130  	}
   131  	compressed := new(bytes.Buffer)
   132  	w := NewWriter(compressed, LSB, 8)
   133  	for i := 0; i < n; i += len(buf0) {
   134  		if len(buf0) > n-i {
   135  			buf0 = buf0[:n-i]
   136  		}
   137  		w.Write(buf0)
   138  	}
   139  	w.Close()
   140  	buf1 := compressed.Bytes()
   141  	buf0, compressed, w = nil, nil, nil
   142  	runtime.GC()
   143  	b.StartTimer()
   144  	for i := 0; i < b.N; i++ {
   145  		io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1), LSB, 8))
   146  	}
   147  }
   148  
   149  func BenchmarkDecoder1e4(b *testing.B) {
   150  	benchmarkDecoder(b, 1e4)
   151  }
   152  
   153  func BenchmarkDecoder1e5(b *testing.B) {
   154  	benchmarkDecoder(b, 1e5)
   155  }
   156  
   157  func BenchmarkDecoder1e6(b *testing.B) {
   158  	benchmarkDecoder(b, 1e6)
   159  }