github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/level_0/int_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/v2pro/plz/msgfmt/jsonfmt"
     8  	"github.com/v2pro/plz/reflect2"
     9  )
    10  
    11  func Test_int8(t *testing.T) {
    12  	should := require.New(t)
    13  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(int8(1)))
    14  	should.Equal("-1", string(encoder.Encode(nil,nil, reflect2.PtrOf(int8(-1)))))
    15  }
    16  
    17  func Test_uint8(t *testing.T) {
    18  	should := require.New(t)
    19  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(uint8(1)))
    20  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(uint8(222)))))
    21  }
    22  
    23  func Test_int16(t *testing.T) {
    24  	should := require.New(t)
    25  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(int16(1)))
    26  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(int16(222)))))
    27  }
    28  
    29  func Test_uint16(t *testing.T) {
    30  	should := require.New(t)
    31  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(uint16(1)))
    32  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(uint16(222)))))
    33  }
    34  
    35  func Test_int32(t *testing.T) {
    36  	should := require.New(t)
    37  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(int32(1)))
    38  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(int32(222)))))
    39  }
    40  
    41  func Test_uint32(t *testing.T) {
    42  	should := require.New(t)
    43  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(uint32(1)))
    44  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(uint32(222)))))
    45  }
    46  
    47  func Test_int64(t *testing.T) {
    48  	should := require.New(t)
    49  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(int64(1)))
    50  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(int64(222)))))
    51  }
    52  
    53  func Test_uint64(t *testing.T) {
    54  	should := require.New(t)
    55  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(uint64(1)))
    56  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(uint64(222)))))
    57  }
    58  
    59  func Test_int(t *testing.T) {
    60  	should := require.New(t)
    61  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(1))
    62  	should.Equal("1", string(encoder.Encode(nil,nil, reflect2.PtrOf(1))))
    63  	should.Equal("998123123", string(encoder.Encode(nil,nil, reflect2.PtrOf(998123123))))
    64  	should.Equal("-998123123", string(encoder.Encode(nil,nil, reflect2.PtrOf(-998123123))))
    65  }
    66  
    67  func Test_uint(t *testing.T) {
    68  	should := require.New(t)
    69  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(uint(1)))
    70  	should.Equal("222", string(encoder.Encode(nil,nil, reflect2.PtrOf(uint(222)))))
    71  }
    72  
    73  func Benchmark_int(b *testing.B) {
    74  	encoder := jsonfmt.EncoderOf(reflect2.TypeOf(1))
    75  	values := []int{998123123, 123123435, 1230}
    76  	ptrs := make([]unsafe.Pointer, len(values))
    77  	for i := 0; i < len(values); i++ {
    78  		ptrs[i] = unsafe.Pointer(&values[i])
    79  	}
    80  	var buf []byte
    81  	b.ResetTimer()
    82  	b.ReportAllocs()
    83  	for i := 0; i < b.N; i++ {
    84  		buf = buf[:0]
    85  		buf = encoder.Encode(nil,buf, ptrs[i%3])
    86  	}
    87  }