@@ -516,54 +516,65 @@ func TestWriterReset(t *testing.T) {
516516 t .Errorf ("level %d Writer not reset after Reset" , level )
517517 }
518518 }
519- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriter (w , NoCompression ) })
520- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriter (w , DefaultCompression ) })
521- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriter (w , BestCompression ) })
522- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriter (w , ConstantCompression ) })
523- dict := []byte ("we are the world" )
524- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriterDict (w , NoCompression , dict ) })
525- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriterDict (w , DefaultCompression , dict ) })
526- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriterDict (w , BestCompression , dict ) })
527- testResetOutput (t , func (w io.Writer ) (* Writer , error ) { return NewWriterDict (w , ConstantCompression , dict ) })
528- }
529-
530- func testResetOutput (t * testing.T , newWriter func (w io.Writer ) (* Writer , error )) {
531- buf := new (bytes.Buffer )
532- w , err := newWriter (buf )
533- if err != nil {
534- t .Fatalf ("NewWriter: %v" , err )
535- }
536- b := []byte ("hello world" )
537- for i := 0 ; i < 1024 ; i ++ {
538- w .Write (b )
539- }
540- w .Close ()
541- out1 := buf .Bytes ()
542519
543- buf2 := new (bytes.Buffer )
544- w .Reset (buf2 )
545- for i := 0 ; i < 1024 ; i ++ {
546- w .Write (b )
520+ for i := HuffmanOnly ; i <= BestCompression ; i ++ {
521+ testResetOutput (t , fmt .Sprint ("level-" , i ), func (w io.Writer ) (* Writer , error ) { return NewWriter (w , i ) })
547522 }
548- w .Close ()
549- out2 := buf2 .Bytes ()
550-
551- if len (out1 ) != len (out2 ) {
552- t .Errorf ("got %d, expected %d bytes" , len (out2 ), len (out1 ))
523+ dict := []byte (strings .Repeat ("we are the world - how are you?" , 3 ))
524+ for i := HuffmanOnly ; i <= BestCompression ; i ++ {
525+ testResetOutput (t , fmt .Sprint ("dict-level-" , i ), func (w io.Writer ) (* Writer , error ) { return NewWriterDict (w , i , dict ) })
553526 }
554- if bytes . Compare ( out1 , out2 ) != 0 {
555- mm := 0
556- for i , b := range out1 [: len ( out2 )] {
557- if b != out2 [ i ] {
558- t . Errorf ( "mismatch index %d: %02x, expected %02x" , i , out2 [ i ], b )
527+ for i := HuffmanOnly ; i <= BestCompression ; i ++ {
528+ testResetOutput ( t , fmt . Sprint ( "dict-reset-level-" , i ), func ( w io. Writer ) ( * Writer , error ) {
529+ w2 , err := NewWriter ( nil , i )
530+ if err != nil {
531+ return w2 , err
559532 }
560- mm ++
561- if mm == 10 {
562- t .Fatal ("Stopping" )
533+ w2 .ResetDict (w , dict )
534+ return w2 , nil
535+ })
536+ }
537+ }
538+
539+ func testResetOutput (t * testing.T , name string , newWriter func (w io.Writer ) (* Writer , error )) {
540+ t .Run (name , func (t * testing.T ) {
541+ buf := new (bytes.Buffer )
542+ w , err := newWriter (buf )
543+ if err != nil {
544+ t .Fatalf ("NewWriter: %v" , err )
545+ }
546+ b := []byte ("hello world - how are you doing?" )
547+ for i := 0 ; i < 1024 ; i ++ {
548+ w .Write (b )
549+ }
550+ w .Close ()
551+ out1 := buf .Bytes ()
552+
553+ buf2 := new (bytes.Buffer )
554+ w .Reset (buf2 )
555+ for i := 0 ; i < 1024 ; i ++ {
556+ w .Write (b )
557+ }
558+ w .Close ()
559+ out2 := buf2 .Bytes ()
560+
561+ if len (out1 ) != len (out2 ) {
562+ t .Errorf ("got %d, expected %d bytes" , len (out2 ), len (out1 ))
563+ }
564+ if bytes .Compare (out1 , out2 ) != 0 {
565+ mm := 0
566+ for i , b := range out1 [:len (out2 )] {
567+ if b != out2 [i ] {
568+ t .Errorf ("mismatch index %d: %02x, expected %02x" , i , out2 [i ], b )
569+ }
570+ mm ++
571+ if mm == 10 {
572+ t .Fatal ("Stopping" )
573+ }
563574 }
564575 }
565- }
566- t . Logf ( "got %d bytes" , len ( out1 ) )
576+ t . Logf ( "got %d bytes" , len ( out1 ))
577+ } )
567578}
568579
569580// TestBestSpeed tests that round-tripping through deflate and then inflate
0 commit comments