github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/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 if err != nil { 102 if err != tt.err { 103 t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err) 104 } 105 continue 106 } 107 s := b.String() 108 if s != tt.raw { 109 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw) 110 } 111 } 112 } 113 114 func benchmarkDecoder(b *testing.B, n int) { 115 b.StopTimer() 116 b.SetBytes(int64(n)) 117 buf0, err := ioutil.ReadFile("../testdata/e.txt") 118 if err != nil { 119 b.Fatal(err) 120 } 121 if len(buf0) == 0 { 122 b.Fatalf("test file has no data") 123 } 124 compressed := new(bytes.Buffer) 125 w := NewWriter(compressed, LSB, 8) 126 for i := 0; i < n; i += len(buf0) { 127 if len(buf0) > n-i { 128 buf0 = buf0[:n-i] 129 } 130 w.Write(buf0) 131 } 132 w.Close() 133 buf1 := compressed.Bytes() 134 buf0, compressed, w = nil, nil, nil 135 runtime.GC() 136 b.StartTimer() 137 for i := 0; i < b.N; i++ { 138 io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1), LSB, 8)) 139 } 140 } 141 142 func BenchmarkDecoder1e4(b *testing.B) { 143 benchmarkDecoder(b, 1e4) 144 } 145 146 func BenchmarkDecoder1e5(b *testing.B) { 147 benchmarkDecoder(b, 1e5) 148 } 149 150 func BenchmarkDecoder1e6(b *testing.B) { 151 benchmarkDecoder(b, 1e6) 152 }