github.com/GuanceCloud/cliutils@v1.1.21/diskcache/pos_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package diskcache 7 8 import ( 9 "fmt" 10 T "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestDump(t *T.T) { 16 t.Run(`dump-undump`, func(t *T.T) { 17 p := &pos{ 18 Seek: 1024 * 1024 * 1024, 19 Name: []byte(fmt.Sprintf("data.%032d", 1234)), 20 } 21 22 data, err := p.MarshalBinary() 23 assert.NoError(t, err) 24 25 t.Logf("get dump: %x", data) 26 27 var p2 pos 28 29 assert.NoError(t, p2.UnmarshalBinary(data)) 30 31 assert.Equal(t, int64(1024*1024*1024), p2.Seek) 32 assert.Equal(t, []byte(fmt.Sprintf("data.%032d", 1234)), p2.Name) 33 34 t.Logf("pos: %s", p) 35 }) 36 37 t.Run(`seek--1`, func(t *T.T) { 38 p := &pos{ 39 Seek: -1, 40 Name: []byte(fmt.Sprintf("data.%032d", 1234)), 41 } 42 43 data, err := p.MarshalBinary() 44 assert.NoError(t, err) 45 46 t.Logf("get dump: %x", data) 47 48 var p2 pos 49 assert.NoError(t, p2.UnmarshalBinary(data)) 50 assert.Equal(t, int64(-1), p2.Seek) 51 assert.Equal(t, []byte(fmt.Sprintf("data.%032d", 1234)), p2.Name) 52 53 t.Logf("pos: %s", p) 54 }) 55 56 t.Run(`seek-0`, func(t *T.T) { 57 p := &pos{ 58 Seek: 0, 59 Name: []byte(fmt.Sprintf("data.%032d", 1234)), 60 } 61 62 data, err := p.MarshalBinary() 63 assert.NoError(t, err) 64 65 t.Logf("get dump: %x", data) 66 67 var p2 pos 68 assert.NoError(t, p2.UnmarshalBinary(data)) 69 70 assert.Equal(t, int64(0), p2.Seek) 71 assert.Equal(t, []byte(fmt.Sprintf("data.%032d", 1234)), p2.Name) 72 73 t.Logf("pos: %s", p) 74 }) 75 } 76 77 func BenchmarkPosDump(b *T.B) { 78 p := pos{ 79 Seek: 1024 * 1024 * 1024, 80 Name: []byte(fmt.Sprintf("data.%032d", 1234)), 81 } 82 83 b.Run("binary", func(b *T.B) { 84 for i := 0; i < b.N; i++ { 85 p.MarshalBinary() 86 } 87 }) 88 89 b.Run("json", func(b *T.B) { 90 for i := 0; i < b.N; i++ { 91 p.dumpJSON() 92 } 93 }) 94 }