generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ex0rcist/iter9
Инкремент №9
- Loading branch information
Showing
50 changed files
with
2,941 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package compression | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
) | ||
|
||
func Pack(data []byte) (*bytes.Buffer, error) { | ||
bb := new(bytes.Buffer) | ||
|
||
encoder, err := gzip.NewWriterLevel(bb, gzip.BestSpeed) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed init compress writer: %v", err) | ||
} | ||
|
||
if _, err = encoder.Write(data); err != nil { | ||
return nil, fmt.Errorf("failed write data to compress temporary buffer: %v", err) | ||
} | ||
|
||
if err = encoder.Close(); err != nil { | ||
return nil, fmt.Errorf("failed compress data: %v", err) | ||
} | ||
|
||
return bb, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package compression | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestPack_Success(t *testing.T) { | ||
data := []byte("test data") | ||
buffer, err := Pack(data) | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
|
||
reader, err := gzip.NewReader(buffer) | ||
if err != nil { | ||
t.Fatalf("expected no error creating gzip reader, got %v", err) | ||
} | ||
defer reader.Close() | ||
|
||
unpackedData, err := io.ReadAll(reader) | ||
if err != nil { | ||
t.Fatalf("expected no error reading from gzip reader, got %v", err) | ||
} | ||
|
||
if !bytes.Equal(data, unpackedData) { | ||
t.Fatalf("expected %s, got %s", data, unpackedData) | ||
} | ||
} |
Oops, something went wrong.