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