@@ -87,6 +87,35 @@ func TestCompressDecompress(t *testing.T) {
87
87
}
88
88
}
89
89
90
+ func TestCompressDecompressInto (t * testing.T ) {
91
+ payload := []byte ("Hello World!" )
92
+ compressed , err := Compress (make ([]byte , CompressBound (len (payload ))), payload )
93
+ if err != nil {
94
+ t .Fatalf ("Error while compressing: %v" , err )
95
+ }
96
+ t .Logf ("Compressed: %v" , compressed )
97
+
98
+ // We know the size of the payload; construct a buffer that perfectly fits
99
+ // the payload and use DecompressInto.
100
+ decompressed := make ([]byte , len (payload ))
101
+ if n , err := DecompressInto (decompressed , compressed ); err != nil {
102
+ t .Fatalf ("error while decompressing into buffer of size %d: %v" ,
103
+ len (decompressed ), err )
104
+ } else if n != len (decompressed ) {
105
+ t .Errorf ("DecompressedInto = (%d, nil), want (%d, nil)" , n , len (decompressed ))
106
+ }
107
+ if ! bytes .Equal (payload , decompressed ) {
108
+ t .Fatalf ("DecompressInto(_, Compress(_, %q)) yielded %q, want %q" , payload , decompressed , payload )
109
+ }
110
+
111
+ // Ensure that decompressing into a buffer too small errors appropriately.
112
+ smallBuffer := make ([]byte , len (payload )- 1 )
113
+ if _ , err := DecompressInto (smallBuffer , compressed ); ! IsDstSizeTooSmallError (err ) {
114
+ t .Fatalf ("DecompressInto(<%d-sized buffer>, Compress(_, %q)) = %v, want 'Destination buffer is too small'" ,
115
+ len (smallBuffer ), payload , err )
116
+ }
117
+ }
118
+
90
119
func TestCompressLevel (t * testing.T ) {
91
120
inputs := [][]byte {
92
121
nil , {}, {0 }, []byte ("Hello World!" ),
0 commit comments