goProject/trunk/goutil/zlibUtil/zlib_test.go
皮蛋13361098506 1b77f62820 初始化项目
2025-01-06 16:01:02 +08:00

77 lines
1.5 KiB
Go
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()
}