deflate_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Copyright (c) 2015 Klaus Post
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package flate
  6. import (
  7. "bytes"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "reflect"
  12. "strings"
  13. "sync"
  14. "testing"
  15. )
  16. type deflateTest struct {
  17. in []byte
  18. level int
  19. out []byte
  20. }
  21. type deflateInflateTest struct {
  22. in []byte
  23. }
  24. type reverseBitsTest struct {
  25. in uint16
  26. bitCount uint8
  27. out uint16
  28. }
  29. var deflateTests = []*deflateTest{
  30. {[]byte{}, 0, []byte{0x3, 0x0}},
  31. {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}},
  32. {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}},
  33. {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}},
  34. {[]byte{0x11}, 0, []byte{0x0, 0x1, 0x0, 0xfe, 0xff, 0x11, 0x3, 0x0}},
  35. {[]byte{0x11, 0x12}, 0, []byte{0x0, 0x2, 0x0, 0xfd, 0xff, 0x11, 0x12, 0x3, 0x0}},
  36. {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 0,
  37. []byte{0x0, 0x8, 0x0, 0xf7, 0xff, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x3, 0x0},
  38. },
  39. {[]byte{}, 1, []byte{0x3, 0x0}},
  40. {[]byte{0x11}, BestCompression, []byte{0x12, 0x4, 0xc, 0x0}},
  41. {[]byte{0x11, 0x12}, BestCompression, []byte{0x12, 0x14, 0x2, 0xc, 0x0}},
  42. {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, BestCompression, []byte{0x12, 0x84, 0x2, 0xc0, 0x0}},
  43. {[]byte{}, 9, []byte{0x3, 0x0}},
  44. {[]byte{0x11}, 9, []byte{0x12, 0x4, 0xc, 0x0}},
  45. {[]byte{0x11, 0x12}, 9, []byte{0x12, 0x14, 0x2, 0xc, 0x0}},
  46. {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 9, []byte{0x12, 0x84, 0x2, 0xc0, 0x0}},
  47. }
  48. var deflateInflateTests = []*deflateInflateTest{
  49. {[]byte{}},
  50. {[]byte{0x11}},
  51. {[]byte{0x11, 0x12}},
  52. {[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
  53. {[]byte{0x11, 0x10, 0x13, 0x41, 0x21, 0x21, 0x41, 0x13, 0x87, 0x78, 0x13}},
  54. {largeDataChunk()},
  55. }
  56. var reverseBitsTests = []*reverseBitsTest{
  57. {1, 1, 1},
  58. {1, 2, 2},
  59. {1, 3, 4},
  60. {1, 4, 8},
  61. {1, 5, 16},
  62. {17, 5, 17},
  63. {257, 9, 257},
  64. {29, 5, 23},
  65. }
  66. func largeDataChunk() []byte {
  67. result := make([]byte, 100000)
  68. for i := range result {
  69. result[i] = byte(i * i & 0xFF)
  70. }
  71. return result
  72. }
  73. func TestBulkHash4(t *testing.T) {
  74. for _, x := range deflateTests {
  75. y := x.out
  76. if len(y) >= minMatchLength {
  77. y = append(y, y...)
  78. for j := 4; j < len(y); j++ {
  79. y := y[:j]
  80. dst := make([]uint32, len(y)-minMatchLength+1)
  81. for i := range dst {
  82. dst[i] = uint32(i + 100)
  83. }
  84. bulkHash4(y, dst)
  85. for i, val := range dst {
  86. got := val
  87. expect := hash4(y[i:])
  88. if got != expect && got == uint32(i)+100 {
  89. t.Errorf("Len:%d Index:%d, expected 0x%08x but not modified", len(y), i, expect)
  90. } else if got != expect {
  91. t.Errorf("Len:%d Index:%d, got 0x%08x expected:0x%08x", len(y), i, got, expect)
  92. } else {
  93. //t.Logf("Len:%d Index:%d OK (0x%08x)", len(y), i, got)
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. func TestDeflate(t *testing.T) {
  101. for i, h := range deflateTests {
  102. var buf bytes.Buffer
  103. w, err := NewWriter(&buf, h.level)
  104. if err != nil {
  105. t.Errorf("NewWriter: %v", err)
  106. continue
  107. }
  108. w.Write(h.in)
  109. w.Close()
  110. if !bytes.Equal(buf.Bytes(), h.out) {
  111. t.Errorf("%d: Deflate(%d, %x) = \n%#v, want \n%#v", i, h.level, h.in, buf.Bytes(), h.out)
  112. }
  113. }
  114. }
  115. // A sparseReader returns a stream consisting of 0s followed by 1<<16 1s.
  116. // This tests missing hash references in a very large input.
  117. type sparseReader struct {
  118. l int64
  119. cur int64
  120. }
  121. func (r *sparseReader) Read(b []byte) (n int, err error) {
  122. if r.cur >= r.l {
  123. return 0, io.EOF
  124. }
  125. n = len(b)
  126. cur := r.cur + int64(n)
  127. if cur > r.l {
  128. n -= int(cur - r.l)
  129. cur = r.l
  130. }
  131. for i := range b[0:n] {
  132. if r.cur+int64(i) >= r.l-1<<16 {
  133. b[i] = 1
  134. } else {
  135. b[i] = 0
  136. }
  137. }
  138. r.cur = cur
  139. return
  140. }
  141. func TestVeryLongSparseChunk(t *testing.T) {
  142. if testing.Short() {
  143. t.Skip("skipping sparse chunk during short test")
  144. }
  145. w, err := NewWriter(ioutil.Discard, 1)
  146. if err != nil {
  147. t.Errorf("NewWriter: %v", err)
  148. return
  149. }
  150. if _, err = io.Copy(w, &sparseReader{l: 23e8}); err != nil {
  151. t.Errorf("Compress failed: %v", err)
  152. return
  153. }
  154. }
  155. type syncBuffer struct {
  156. buf bytes.Buffer
  157. mu sync.RWMutex
  158. closed bool
  159. ready chan bool
  160. }
  161. func newSyncBuffer() *syncBuffer {
  162. return &syncBuffer{ready: make(chan bool, 1)}
  163. }
  164. func (b *syncBuffer) Read(p []byte) (n int, err error) {
  165. for {
  166. b.mu.RLock()
  167. n, err = b.buf.Read(p)
  168. b.mu.RUnlock()
  169. if n > 0 || b.closed {
  170. return
  171. }
  172. <-b.ready
  173. }
  174. }
  175. func (b *syncBuffer) signal() {
  176. select {
  177. case b.ready <- true:
  178. default:
  179. }
  180. }
  181. func (b *syncBuffer) Write(p []byte) (n int, err error) {
  182. n, err = b.buf.Write(p)
  183. b.signal()
  184. return
  185. }
  186. func (b *syncBuffer) WriteMode() {
  187. b.mu.Lock()
  188. }
  189. func (b *syncBuffer) ReadMode() {
  190. b.mu.Unlock()
  191. b.signal()
  192. }
  193. func (b *syncBuffer) Close() error {
  194. b.closed = true
  195. b.signal()
  196. return nil
  197. }
  198. func testSync(t *testing.T, level int, input []byte, name string) {
  199. if len(input) == 0 {
  200. return
  201. }
  202. t.Logf("--testSync %d, %d, %s", level, len(input), name)
  203. buf := newSyncBuffer()
  204. buf1 := new(bytes.Buffer)
  205. buf.WriteMode()
  206. w, err := NewWriter(io.MultiWriter(buf, buf1), level)
  207. if err != nil {
  208. t.Errorf("NewWriter: %v", err)
  209. return
  210. }
  211. r := NewReader(buf)
  212. // Write half the input and read back.
  213. for i := 0; i < 2; i++ {
  214. var lo, hi int
  215. if i == 0 {
  216. lo, hi = 0, (len(input)+1)/2
  217. } else {
  218. lo, hi = (len(input)+1)/2, len(input)
  219. }
  220. t.Logf("#%d: write %d-%d", i, lo, hi)
  221. if _, err := w.Write(input[lo:hi]); err != nil {
  222. t.Errorf("testSync: write: %v", err)
  223. return
  224. }
  225. if i == 0 {
  226. if err := w.Flush(); err != nil {
  227. t.Errorf("testSync: flush: %v", err)
  228. return
  229. }
  230. } else {
  231. if err := w.Close(); err != nil {
  232. t.Errorf("testSync: close: %v", err)
  233. }
  234. }
  235. buf.ReadMode()
  236. out := make([]byte, hi-lo+1)
  237. m, err := io.ReadAtLeast(r, out, hi-lo)
  238. t.Logf("#%d: read %d", i, m)
  239. if m != hi-lo || err != nil {
  240. t.Errorf("testSync/%d (%d, %d, %s): read %d: %d, %v (%d left)", i, level, len(input), name, hi-lo, m, err, buf.buf.Len())
  241. return
  242. }
  243. if !bytes.Equal(input[lo:hi], out[:hi-lo]) {
  244. t.Errorf("testSync/%d: read wrong bytes: %x vs %x", i, input[lo:hi], out[:hi-lo])
  245. return
  246. }
  247. // This test originally checked that after reading
  248. // the first half of the input, there was nothing left
  249. // in the read buffer (buf.buf.Len() != 0) but that is
  250. // not necessarily the case: the write Flush may emit
  251. // some extra framing bits that are not necessary
  252. // to process to obtain the first half of the uncompressed
  253. // data. The test ran correctly most of the time, because
  254. // the background goroutine had usually read even
  255. // those extra bits by now, but it's not a useful thing to
  256. // check.
  257. buf.WriteMode()
  258. }
  259. buf.ReadMode()
  260. out := make([]byte, 10)
  261. if n, err := r.Read(out); n > 0 || err != io.EOF {
  262. t.Errorf("testSync (%d, %d, %s): final Read: %d, %v (hex: %x)", level, len(input), name, n, err, out[0:n])
  263. }
  264. if buf.buf.Len() != 0 {
  265. t.Errorf("testSync (%d, %d, %s): extra data at end", level, len(input), name)
  266. }
  267. r.Close()
  268. // stream should work for ordinary reader too
  269. r = NewReader(buf1)
  270. out, err = ioutil.ReadAll(r)
  271. if err != nil {
  272. t.Errorf("testSync: read: %s", err)
  273. return
  274. }
  275. r.Close()
  276. if !bytes.Equal(input, out) {
  277. t.Errorf("testSync: decompress(compress(data)) != data: level=%d input=%s", level, name)
  278. }
  279. }
  280. func testToFromWithLevelAndLimit(t *testing.T, level int, input []byte, name string, limit int) {
  281. var buffer bytes.Buffer
  282. w, err := NewWriter(&buffer, level)
  283. if err != nil {
  284. t.Errorf("NewWriter: %v", err)
  285. return
  286. }
  287. w.Write(input)
  288. w.Close()
  289. if limit > 0 && buffer.Len() > limit {
  290. t.Errorf("level: %d, len(compress(data)) = %d > limit = %d", level, buffer.Len(), limit)
  291. return
  292. }
  293. if limit > 0 {
  294. t.Logf("level: %d - Size:%.2f%%, %d b\n", level, float64(buffer.Len()*100)/float64(limit), buffer.Len())
  295. }
  296. r := NewReader(&buffer)
  297. out, err := ioutil.ReadAll(r)
  298. if err != nil {
  299. t.Errorf("read: %s", err)
  300. return
  301. }
  302. r.Close()
  303. if !bytes.Equal(input, out) {
  304. t.Errorf("decompress(compress(data)) != data: level=%d input=%s", level, name)
  305. return
  306. }
  307. testSync(t, level, input, name)
  308. }
  309. func testToFromWithLimit(t *testing.T, input []byte, name string, limit [11]int) {
  310. for i := 0; i < 10; i++ {
  311. testToFromWithLevelAndLimit(t, i, input, name, limit[i])
  312. }
  313. testToFromWithLevelAndLimit(t, -2, input, name, limit[10])
  314. }
  315. func TestDeflateInflate(t *testing.T) {
  316. for i, h := range deflateInflateTests {
  317. testToFromWithLimit(t, h.in, fmt.Sprintf("#%d", i), [11]int{})
  318. }
  319. }
  320. func TestReverseBits(t *testing.T) {
  321. for _, h := range reverseBitsTests {
  322. if v := reverseBits(h.in, h.bitCount); v != h.out {
  323. t.Errorf("reverseBits(%v,%v) = %v, want %v",
  324. h.in, h.bitCount, v, h.out)
  325. }
  326. }
  327. }
  328. type deflateInflateStringTest struct {
  329. filename string
  330. label string
  331. limit [11]int // Number 11 is ConstantCompression
  332. }
  333. var deflateInflateStringTests = []deflateInflateStringTest{
  334. {
  335. "../testdata/e.txt",
  336. "2.718281828...",
  337. [...]int{100018, 67900, 50960, 51150, 50930, 50790, 50790, 50790, 50790, 50790, 43683 + 100},
  338. },
  339. {
  340. "../testdata/Mark.Twain-Tom.Sawyer.txt",
  341. "Mark.Twain-Tom.Sawyer",
  342. [...]int{387999, 185000, 182361, 179974, 174124, 168819, 162936, 160506, 160295, 160295, 233460 + 100},
  343. },
  344. }
  345. func TestDeflateInflateString(t *testing.T) {
  346. for _, test := range deflateInflateStringTests {
  347. gold, err := ioutil.ReadFile(test.filename)
  348. if err != nil {
  349. t.Error(err)
  350. }
  351. // Remove returns that may be present on Windows
  352. neutral := strings.Map(func(r rune) rune {
  353. if r != '\r' {
  354. return r
  355. }
  356. return -1
  357. }, string(gold))
  358. testToFromWithLimit(t, []byte(neutral), test.label, test.limit)
  359. if testing.Short() {
  360. break
  361. }
  362. }
  363. }
  364. func TestReaderDict(t *testing.T) {
  365. const (
  366. dict = "hello world"
  367. text = "hello again world"
  368. )
  369. var b bytes.Buffer
  370. w, err := NewWriter(&b, 5)
  371. if err != nil {
  372. t.Fatalf("NewWriter: %v", err)
  373. }
  374. w.Write([]byte(dict))
  375. w.Flush()
  376. b.Reset()
  377. w.Write([]byte(text))
  378. w.Close()
  379. r := NewReaderDict(&b, []byte(dict))
  380. data, err := ioutil.ReadAll(r)
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. if string(data) != "hello again world" {
  385. t.Fatalf("read returned %q want %q", string(data), text)
  386. }
  387. }
  388. func TestWriterDict(t *testing.T) {
  389. const (
  390. dict = "hello world Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  391. text = "hello world Lorem ipsum dolor sit amet"
  392. )
  393. // This test is sensitive to algorithm changes that skip
  394. // data in favour of speed. Higher levels are less prone to this
  395. // so we test level 4-9.
  396. for l := 4; l < 9; l++ {
  397. var b bytes.Buffer
  398. w, err := NewWriter(&b, l)
  399. if err != nil {
  400. t.Fatalf("level %d, NewWriter: %v", l, err)
  401. }
  402. w.Write([]byte(dict))
  403. w.Flush()
  404. b.Reset()
  405. w.Write([]byte(text))
  406. w.Close()
  407. var b1 bytes.Buffer
  408. w, _ = NewWriterDict(&b1, l, []byte(dict))
  409. w.Write([]byte(text))
  410. w.Close()
  411. if !bytes.Equal(b1.Bytes(), b.Bytes()) {
  412. t.Errorf("level %d, writer wrote\n%v\n want\n%v", l, b1.Bytes(), b.Bytes())
  413. }
  414. }
  415. }
  416. // See http://code.google.com/p/go/issues/detail?id=2508
  417. func TestRegression2508(t *testing.T) {
  418. if testing.Short() {
  419. t.Logf("test disabled with -short")
  420. return
  421. }
  422. w, err := NewWriter(ioutil.Discard, 1)
  423. if err != nil {
  424. t.Fatalf("NewWriter: %v", err)
  425. }
  426. buf := make([]byte, 1024)
  427. for i := 0; i < 131072; i++ {
  428. if _, err := w.Write(buf); err != nil {
  429. t.Fatalf("writer failed: %v", err)
  430. }
  431. }
  432. w.Close()
  433. }
  434. func TestWriterReset(t *testing.T) {
  435. for level := -2; level <= 9; level++ {
  436. if level == -1 {
  437. level++
  438. }
  439. if testing.Short() && level > 1 {
  440. break
  441. }
  442. w, err := NewWriter(ioutil.Discard, level)
  443. if err != nil {
  444. t.Fatalf("NewWriter: %v", err)
  445. }
  446. buf := []byte("hello world")
  447. for i := 0; i < 1024; i++ {
  448. w.Write(buf)
  449. }
  450. w.Reset(ioutil.Discard)
  451. wref, err := NewWriter(ioutil.Discard, level)
  452. if err != nil {
  453. t.Fatalf("NewWriter: %v", err)
  454. }
  455. // DeepEqual doesn't compare functions.
  456. w.d.fill, wref.d.fill = nil, nil
  457. w.d.step, wref.d.step = nil, nil
  458. w.d.state, wref.d.state = nil, nil
  459. w.d.fast, wref.d.fast = nil, nil
  460. // hashMatch is always overwritten when used.
  461. if w.d.tokens.n != 0 {
  462. t.Errorf("level %d Writer not reset after Reset. %d tokens were present", level, w.d.tokens.n)
  463. }
  464. // As long as the length is 0, we don't care about the content.
  465. w.d.tokens = wref.d.tokens
  466. // We don't care if there are values in the window, as long as it is at d.index is 0
  467. w.d.window = wref.d.window
  468. if !reflect.DeepEqual(w, wref) {
  469. t.Errorf("level %d Writer not reset after Reset", level)
  470. }
  471. }
  472. for i := HuffmanOnly; i <= BestCompression; i++ {
  473. testResetOutput(t, fmt.Sprint("level-", i), func(w io.Writer) (*Writer, error) { return NewWriter(w, i) })
  474. }
  475. dict := []byte(strings.Repeat("we are the world - how are you?", 3))
  476. for i := HuffmanOnly; i <= BestCompression; i++ {
  477. testResetOutput(t, fmt.Sprint("dict-level-", i), func(w io.Writer) (*Writer, error) { return NewWriterDict(w, i, dict) })
  478. }
  479. for i := HuffmanOnly; i <= BestCompression; i++ {
  480. testResetOutput(t, fmt.Sprint("dict-reset-level-", i), func(w io.Writer) (*Writer, error) {
  481. w2, err := NewWriter(nil, i)
  482. if err != nil {
  483. return w2, err
  484. }
  485. w2.ResetDict(w, dict)
  486. return w2, nil
  487. })
  488. }
  489. }
  490. func testResetOutput(t *testing.T, name string, newWriter func(w io.Writer) (*Writer, error)) {
  491. t.Run(name, func(t *testing.T) {
  492. buf := new(bytes.Buffer)
  493. w, err := newWriter(buf)
  494. if err != nil {
  495. t.Fatalf("NewWriter: %v", err)
  496. }
  497. b := []byte("hello world - how are you doing?")
  498. for i := 0; i < 1024; i++ {
  499. w.Write(b)
  500. }
  501. w.Close()
  502. out1 := buf.Bytes()
  503. buf2 := new(bytes.Buffer)
  504. w.Reset(buf2)
  505. for i := 0; i < 1024; i++ {
  506. w.Write(b)
  507. }
  508. w.Close()
  509. out2 := buf2.Bytes()
  510. if len(out1) != len(out2) {
  511. t.Errorf("got %d, expected %d bytes", len(out2), len(out1))
  512. }
  513. if bytes.Compare(out1, out2) != 0 {
  514. mm := 0
  515. for i, b := range out1[:len(out2)] {
  516. if b != out2[i] {
  517. t.Errorf("mismatch index %d: %02x, expected %02x", i, out2[i], b)
  518. }
  519. mm++
  520. if mm == 10 {
  521. t.Fatal("Stopping")
  522. }
  523. }
  524. }
  525. t.Logf("got %d bytes", len(out1))
  526. })
  527. }
  528. // TestBestSpeed tests that round-tripping through deflate and then inflate
  529. // recovers the original input. The Write sizes are near the thresholds in the
  530. // compressor.encSpeed method (0, 16, 128), as well as near maxStoreBlockSize
  531. // (65535).
  532. func TestBestSpeed(t *testing.T) {
  533. abc := make([]byte, 128)
  534. for i := range abc {
  535. abc[i] = byte(i)
  536. }
  537. abcabc := bytes.Repeat(abc, 131072/len(abc))
  538. var want []byte
  539. testCases := [][]int{
  540. {65536, 0},
  541. {65536, 1},
  542. {65536, 1, 256},
  543. {65536, 1, 65536},
  544. {65536, 14},
  545. {65536, 15},
  546. {65536, 16},
  547. {65536, 16, 256},
  548. {65536, 16, 65536},
  549. {65536, 127},
  550. {65536, 128},
  551. {65536, 128, 256},
  552. {65536, 128, 65536},
  553. {65536, 129},
  554. {65536, 65536, 256},
  555. {65536, 65536, 65536},
  556. }
  557. for i, tc := range testCases {
  558. for _, firstN := range []int{1, 65534, 65535, 65536, 65537, 131072} {
  559. tc[0] = firstN
  560. outer:
  561. for _, flush := range []bool{false, true} {
  562. buf := new(bytes.Buffer)
  563. want = want[:0]
  564. w, err := NewWriter(buf, BestSpeed)
  565. if err != nil {
  566. t.Errorf("i=%d, firstN=%d, flush=%t: NewWriter: %v", i, firstN, flush, err)
  567. continue
  568. }
  569. for _, n := range tc {
  570. want = append(want, abcabc[:n]...)
  571. if _, err := w.Write(abcabc[:n]); err != nil {
  572. t.Errorf("i=%d, firstN=%d, flush=%t: Write: %v", i, firstN, flush, err)
  573. continue outer
  574. }
  575. if !flush {
  576. continue
  577. }
  578. if err := w.Flush(); err != nil {
  579. t.Errorf("i=%d, firstN=%d, flush=%t: Flush: %v", i, firstN, flush, err)
  580. continue outer
  581. }
  582. }
  583. if err := w.Close(); err != nil {
  584. t.Errorf("i=%d, firstN=%d, flush=%t: Close: %v", i, firstN, flush, err)
  585. continue
  586. }
  587. r := NewReader(buf)
  588. got, err := ioutil.ReadAll(r)
  589. if err != nil {
  590. t.Errorf("i=%d, firstN=%d, flush=%t: ReadAll: %v", i, firstN, flush, err)
  591. continue
  592. }
  593. r.Close()
  594. if !bytes.Equal(got, want) {
  595. t.Errorf("i=%d, firstN=%d, flush=%t: corruption during deflate-then-inflate", i, firstN, flush)
  596. continue
  597. }
  598. }
  599. }
  600. }
  601. }