github.com/zhongdalu/gf@v1.0.0/g/encoding/gcompress/gcompress_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gcompress_test 8 9 import ( 10 "testing" 11 12 "github.com/zhongdalu/gf/g/encoding/gcompress" 13 "github.com/zhongdalu/gf/g/test/gtest" 14 ) 15 16 func TestZlib(t *testing.T) { 17 gtest.Case(t, func() { 18 src := "hello, world\n" 19 dst := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147} 20 data, _ := gcompress.Zlib([]byte(src)) 21 gtest.Assert(data, dst) 22 23 data, _ = gcompress.UnZlib(dst) 24 gtest.Assert(data, []byte(src)) 25 26 data, _ = gcompress.Zlib(nil) 27 gtest.Assert(data, nil) 28 data, _ = gcompress.UnZlib(nil) 29 gtest.Assert(data, nil) 30 31 data, _ = gcompress.UnZlib(dst[1:]) 32 gtest.Assert(data, nil) 33 }) 34 35 } 36 37 func TestGzip(t *testing.T) { 38 src := "Hello World!!" 39 40 gzip := []byte{ 41 0x1f, 0x8b, 0x08, 0x00, 0x00, 42 0x00, 0x00, 0x00, 0x00, 0xff, 43 0xf2, 0x48, 0xcd, 0xc9, 0xc9, 44 0x57, 0x08, 0xcf, 0x2f, 0xca, 45 0x49, 0x51, 0x54, 0x04, 0x04, 46 0x00, 0x00, 0xff, 0xff, 0x9d, 47 0x24, 0xa8, 0xd1, 0x0d, 0x00, 48 0x00, 0x00, 49 } 50 51 arr := []byte(src) 52 data, _ := gcompress.Gzip(arr) 53 gtest.Assert(data, gzip) 54 55 data, _ = gcompress.UnGzip(gzip) 56 gtest.Assert(data, arr) 57 58 data, _ = gcompress.UnGzip(gzip[1:]) 59 gtest.Assert(data, nil) 60 }