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