goProject/.svn/pristine/e5/e5f2da69627ec9f1cca305600e859c660e059155.svn-base
2025-01-06 16:21:36 +08:00

77 lines
1.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package zlibUtil
import (
"compress/zlib"
"strings"
"testing"
)
var (
InitString = `{"Code":4,"Message":"IPNotAuthorized","Data":null}`
CompressBytes []byte
)
func TestCompress(t *testing.T) {
data := ([]byte)(InitString)
result, _ := Compress(data, zlib.DefaultCompression)
// if isEqual(result, CompressBytes) == false {
// t.Errorf("压缩失败,期待%v实际%v", InitBytes, result)
// }
CompressBytes = result
}
func TestDecompress(t *testing.T) {
data, _ := Decompress(CompressBytes)
result := string(data)
if result != InitString {
t.Errorf("解压缩失败,期待%s实际%s", InitString, result)
}
}
func isEqual(a, b []byte) bool {
if a == nil && b == nil {
return true
} else if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
//BenchmarkCompress-12 10989 107509 ns/op
func BenchmarkCompress(b *testing.B) {
toCompress := []byte(strings.Repeat(InitString, 100))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_,err :=Compress(toCompress, zlib.DefaultCompression)
if err!=nil{
b.Log(err)
}
}
b.StopTimer()
}
//BenchmarkDeCompress-12 99153 10802 ns/op
func BenchmarkDeCompress(b *testing.B) {
toCompress := []byte(strings.Repeat(InitString, 100))
compressedData,_ := Compress(toCompress, zlib.DefaultCompression)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_,err := Decompress(compressedData)
if err!=nil{
b.Log(err)
}
}
b.StopTimer()
}