github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/types/id_test.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestID(t *testing.T) { 11 id := ID(123456789) 12 data, err := json.Marshal(id) 13 require.Nil(t, err, "marshal must success") 14 newId := new(ID) 15 err = json.Unmarshal(data, newId) 16 require.Nil(t, err, "unmarshal must success") 17 require.Equal(t, ID(123456789), *newId, "data must be 123456789") 18 } 19 20 func TestIDMarshal(t *testing.T) { 21 id := ID(1234567890987654321) 22 rawID, err := json.Marshal(id) 23 require.Nil(t, err, "marshal must success") 24 t.Logf("raw id is %s", rawID) 25 } 26 27 func BenchmarkIDMarshal(b *testing.B) { 28 id := ID(1234567890987654321) 29 rawID := []byte("V55K6jgLnPngp") 30 b.Run("marshal", func(b *testing.B) { 31 b.ResetTimer() 32 b.ReportAllocs() 33 for i := 0; i < b.N; i++ { 34 json.Marshal(id) 35 } 36 }) 37 b.Run("unmarshal", func(b *testing.B) { 38 idPtr := new(ID) 39 b.ResetTimer() 40 b.ReportAllocs() 41 for i := 0; i < b.N; i++ { 42 json.Unmarshal(rawID, idPtr) 43 } 44 }) 45 }