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.
26 var lzwTests
= []lzwTest
{
41 "TOBEORNOTTOBEORTOBEORNOT",
42 "\x54\x4f\x42\x45\x4f\x52\x4e\x4f\x54\x82\x84\x86\x8b\x85\x87\x89\x81",
47 "TOBEORNOTTOBEORTOBEORNOT",
48 "\x54\x9e\x08\x29\xf2\x44\x8a\x93\x27\x54\x04\x12\x34\xb8\xb0\xe0\xc1\x84\x01\x01",
53 "TOBEORNOTTOBEORTOBEORNOT",
54 "\x54\x4f\x42\x45\x4f\x52\x4e\x4f\x54\x82\x84\x86\x8b\x85\x87\x89\x81",
59 "TOBEORNOTTOBEORTOBEORNOT",
60 "\x2a\x13\xc8\x44\x52\x79\x48\x9c\x4f\x2a\x40\xa0\x90\x68\x5c\x16\x0f\x09\x80\x80",
64 "tobe-truncated;LSB;8",
65 "TOBEORNOTTOBEORTOBEORNOT",
66 "\x54\x9e\x08\x29\xf2\x44\x8a\x93\x27\x54\x04",
69 // This example comes from http://en.wikipedia.org/wiki/Graphics_Interchange_Format.
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",
76 // This example comes from http://compgroups.net/comp.lang.ruby/Decompressing-LZW-compression-from-PDF-file
80 "\x80\x0b\x60\x50\x22\x0c\x0c\x85\x01",
85 func TestReader(t
*testing
.T
) {
87 for _
, tt
:= range lzwTests
{
88 d
:= strings
.Split(tt
.desc
, ";")
96 t
.Errorf("%s: bad order %q", tt
.desc
, d
[1])
98 litWidth
, _
:= strconv
.Atoi(d
[2])
99 rc
:= NewReader(strings
.NewReader(tt
.compressed
), order
, litWidth
)
102 n
, err
:= io
.Copy(&b
, rc
)
106 t
.Errorf("%s: io.Copy: %v want %v", tt
.desc
, err
, tt
.err
)
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
)
118 t
.Errorf("%s: got %d-byte %q want %d-byte %q", tt
.desc
, n
, s
, len(tt
.raw
), tt
.raw
)
123 func BenchmarkDecoder(b
*testing
.B
) {
124 buf
, err
:= ioutil
.ReadFile("../testdata/e.txt")
129 b
.Fatalf("test file has no data")
132 for e
:= 4; e
<= 6; e
++ {
133 n
:= int(math
.Pow10(e
))
134 b
.Run(fmt
.Sprint("1e", e
), func(b
*testing
.B
) {
138 compressed
:= new(bytes
.Buffer
)
139 w
:= NewWriter(compressed
, LSB
, 8)
140 for i
:= 0; i
< n
; i
+= len(buf0
) {
147 buf1
:= compressed
.Bytes()
148 buf0
, compressed
, w
= nil, nil, nil
151 for i
:= 0; i
< b
.N
; i
++ {
152 io
.Copy(ioutil
.Discard
, NewReader(bytes
.NewReader(buf1
), LSB
, 8))