Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(459)

Delta Between Two Patch Sets: src/pkg/compress/zlib/writer_test.go

Issue 13171046: code review 13171046: compress/zlib: add Reset method to Writer. (Closed)
Left Patch Set: diff -r bc785da349e2 https://go.googlecode.com/hg/ Created 10 years, 7 months ago
Right Patch Set: diff -r f31ef66588fc https://go.googlecode.com/hg/ Created 10 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/compress/zlib/writer.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package zlib 5 package zlib
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "io" 10 "io"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 return 82 return
83 } 83 }
84 for i := 0; i < len(b0); i++ { 84 for i := 0; i < len(b0); i++ {
85 if b0[i] != b1[i] { 85 if b0[i] != b1[i] {
86 t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i]) 86 t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i])
87 return 87 return
88 } 88 }
89 } 89 }
90 } 90 }
91 91
92 func testFileLevelReset(t *testing.T, fn string, level int) { 92 func testFileLevelDictReset(t *testing.T, fn string, level int, dict []byte) {
93 var b0 []byte 93 var b0 []byte
94 var err error 94 var err error
95 if fn != "" { 95 if fn != "" {
96 b0, err = ioutil.ReadFile(fn) 96 b0, err = ioutil.ReadFile(fn)
97 if err != nil { 97 if err != nil {
98 t.Errorf("%s (level=%d): %v", fn, level, err) 98 t.Errorf("%s (level=%d): %v", fn, level, err)
99 return 99 return
100 } 100 }
101 } 101 }
102 102
103 // Compress once. 103 // Compress once.
104 buf := new(bytes.Buffer) 104 buf := new(bytes.Buffer)
105 » zlibw, err := NewWriterLevel(buf, level) 105 » var zlibw *Writer
106 » if dict == nil {
107 » » zlibw, err = NewWriterLevel(buf, level)
108 » } else {
109 » » zlibw, err = NewWriterLevelDict(buf, level, dict)
110 » }
106 if err == nil { 111 if err == nil {
107 _, err = zlibw.Write(b0) 112 _, err = zlibw.Write(b0)
108 } 113 }
109 if err == nil { 114 if err == nil {
110 err = zlibw.Close() 115 err = zlibw.Close()
111 } 116 }
112 if err != nil { 117 if err != nil {
113 t.Errorf("%s (level=%d): %v", fn, level, err) 118 t.Errorf("%s (level=%d): %v", fn, level, err)
114 return 119 return
115 } 120 }
116 out := buf.String() 121 out := buf.String()
117 122
118 // Reset and comprses again. 123 // Reset and comprses again.
119 » buf.Reset() 124 » buf2 := new(bytes.Buffer)
120 » zlibw.Reset(buf) 125 » zlibw.Reset(buf2)
121 _, err = zlibw.Write(b0) 126 _, err = zlibw.Write(b0)
122 if err == nil { 127 if err == nil {
123 err = zlibw.Close() 128 err = zlibw.Close()
124 } 129 }
125 if err != nil { 130 if err != nil {
126 t.Errorf("%s (level=%d): %v", fn, level, err) 131 t.Errorf("%s (level=%d): %v", fn, level, err)
127 return 132 return
128 } 133 }
129 » out2 := buf.String() 134 » out2 := buf2.String()
130 135
131 if out2 != out { 136 if out2 != out {
132 t.Errorf("%s (level=%d): different output after reset (got %d by tes, expected %d", 137 t.Errorf("%s (level=%d): different output after reset (got %d by tes, expected %d",
133 fn, level, len(out2), len(out)) 138 fn, level, len(out2), len(out))
134 } 139 }
135 } 140 }
136 141
137 func TestWriter(t *testing.T) { 142 func TestWriter(t *testing.T) {
138 for i, s := range data { 143 for i, s := range data {
139 b := []byte(s) 144 b := []byte(s)
(...skipping 21 matching lines...) Expand all
161 for _, fn := range filenames { 166 for _, fn := range filenames {
162 testFileLevelDict(t, fn, DefaultCompression, dictionary) 167 testFileLevelDict(t, fn, DefaultCompression, dictionary)
163 testFileLevelDict(t, fn, NoCompression, dictionary) 168 testFileLevelDict(t, fn, NoCompression, dictionary)
164 for level := BestSpeed; level <= BestCompression; level++ { 169 for level := BestSpeed; level <= BestCompression; level++ {
165 testFileLevelDict(t, fn, level, dictionary) 170 testFileLevelDict(t, fn, level, dictionary)
166 } 171 }
167 } 172 }
168 } 173 }
169 174
170 func TestWriterReset(t *testing.T) { 175 func TestWriterReset(t *testing.T) {
176 const dictionary = "0123456789."
171 for _, fn := range filenames { 177 for _, fn := range filenames {
172 » » testFileLevelReset(t, fn, DefaultCompression) 178 » » testFileLevelDictReset(t, fn, NoCompression, nil)
173 » » testFileLevelReset(t, fn, NoCompression) 179 » » testFileLevelDictReset(t, fn, DefaultCompression, nil)
174 » » for level := BestSpeed; level <= BestCompression; level++ { 180 » » testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
175 » » » if !testing.Short() { 181 » » testFileLevelDictReset(t, fn, DefaultCompression, []byte(diction ary))
176 » » » » testFileLevelReset(t, fn, level) 182 » » if !testing.Short() {
183 » » » for level := BestSpeed; level <= BestCompression; level+ + {
184 » » » » testFileLevelDictReset(t, fn, level, nil)
177 } 185 }
178 } 186 }
179 } 187 }
180 } 188 }
181 189
182 func TestWriterDictIsUsed(t *testing.T) { 190 func TestWriterDictIsUsed(t *testing.T) {
183 var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") 191 var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
184 var buf bytes.Buffer 192 var buf bytes.Buffer
185 compressor, err := NewWriterLevelDict(&buf, BestCompression, input) 193 compressor, err := NewWriterLevelDict(&buf, BestCompression, input)
186 if err != nil { 194 if err != nil {
187 t.Errorf("error in NewWriterLevelDict: %s", err) 195 t.Errorf("error in NewWriterLevelDict: %s", err)
188 return 196 return
189 } 197 }
190 compressor.Write(input) 198 compressor.Write(input)
191 compressor.Close() 199 compressor.Close()
192 const expectedMaxSize = 25 200 const expectedMaxSize = 25
193 output := buf.Bytes() 201 output := buf.Bytes()
194 if len(output) > expectedMaxSize { 202 if len(output) > expectedMaxSize {
195 t.Errorf("result too large (got %d, want <= %d bytes). Is the di ctionary being used?", len(output), expectedMaxSize) 203 t.Errorf("result too large (got %d, want <= %d bytes). Is the di ctionary being used?", len(output), expectedMaxSize)
196 } 204 }
197 } 205 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b