github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/value_tests/int_test.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/bingoohuang/gg/pkg/jsoni"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func init() {
    15  	unmarshalCases = append(unmarshalCases, unmarshalCase{
    16  		ptr: (*struct {
    17  			F1  int8
    18  			F2  int16
    19  			F3  int32
    20  			F4  int64
    21  			F5  int
    22  			F6  uint8
    23  			F7  uint16
    24  			F8  uint32
    25  			F9  uint64
    26  			F10 uint
    27  			F11 float32
    28  			F12 float64
    29  			F13 uintptr
    30  		})(nil),
    31  		input: `{
    32  			"f1":null,
    33  			"f2":null,
    34  			"f3":null,
    35  			"f4":null,
    36  			"f5":null,
    37  			"f6":null,
    38  			"f7":null,
    39  			"f8":null,
    40  			"f9":null,
    41  			"f10":null,
    42  			"f11":null,
    43  			"f12":null,
    44  			"f13":null
    45  		}`,
    46  	})
    47  }
    48  
    49  func Test_int8(t *testing.T) {
    50  	inputs := []string{`127`, `-128`}
    51  	for _, input := range inputs {
    52  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    53  			should := require.New(t)
    54  			iter := jsoni.ParseString(jsoni.ConfigDefault, input)
    55  			expected, err := strconv.ParseInt(input, 10, 8)
    56  			should.Nil(err)
    57  			should.Equal(int8(expected), iter.ReadInt8())
    58  		})
    59  	}
    60  }
    61  
    62  func Test_read_int16(t *testing.T) {
    63  	inputs := []string{`32767`, `-32768`}
    64  	for _, input := range inputs {
    65  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    66  			should := require.New(t)
    67  			iter := jsoni.ParseString(jsoni.ConfigDefault, input)
    68  			expected, err := strconv.ParseInt(input, 10, 16)
    69  			should.Nil(err)
    70  			should.Equal(int16(expected), iter.ReadInt16())
    71  		})
    72  	}
    73  }
    74  
    75  func Test_read_int32(t *testing.T) {
    76  	inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`, `-2147483648`}
    77  	for _, input := range inputs {
    78  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    79  			should := require.New(t)
    80  			iter := jsoni.ParseString(jsoni.ConfigDefault, input)
    81  			expected, err := strconv.ParseInt(input, 10, 32)
    82  			should.Nil(err)
    83  			should.Equal(int32(expected), iter.ReadInt32())
    84  		})
    85  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    86  			should := require.New(t)
    87  			iter := jsoni.Parse(jsoni.ConfigDefault, bytes.NewBufferString(input), 2)
    88  			expected, err := strconv.ParseInt(input, 10, 32)
    89  			should.Nil(err)
    90  			should.Equal(int32(expected), iter.ReadInt32())
    91  		})
    92  	}
    93  }
    94  
    95  func Test_read_int_overflow(t *testing.T) {
    96  	should := require.New(t)
    97  	inputArr := []string{"123451", "-123451"}
    98  	for _, s := range inputArr {
    99  		iter := jsoni.ParseString(jsoni.ConfigDefault, s)
   100  		iter.ReadInt8()
   101  		should.NotNil(iter.Error)
   102  
   103  		iterU := jsoni.ParseString(jsoni.ConfigDefault, s)
   104  		iterU.ReadUint8()
   105  		should.NotNil(iterU.Error)
   106  
   107  	}
   108  
   109  	inputArr = []string{"12345678912", "-12345678912"}
   110  	for _, s := range inputArr {
   111  		iter := jsoni.ParseString(jsoni.ConfigDefault, s)
   112  		iter.ReadInt16()
   113  		should.NotNil(iter.Error)
   114  
   115  		iterUint := jsoni.ParseString(jsoni.ConfigDefault, s)
   116  		iterUint.ReadUint16()
   117  		should.NotNil(iterUint.Error)
   118  	}
   119  
   120  	inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
   121  	for _, s := range inputArr {
   122  		iter := jsoni.ParseString(jsoni.ConfigDefault, s)
   123  		iter.ReadInt32()
   124  		should.NotNil(iter.Error)
   125  
   126  		iterUint := jsoni.ParseString(jsoni.ConfigDefault, s)
   127  		iterUint.ReadUint32()
   128  		should.NotNil(iterUint.Error)
   129  	}
   130  
   131  	inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
   132  	for _, s := range inputArr {
   133  		iter := jsoni.ParseString(jsoni.ConfigDefault, s)
   134  		iter.ReadInt64()
   135  		should.NotNil(iter.Error)
   136  
   137  		iterUint := jsoni.ParseString(jsoni.ConfigDefault, s)
   138  		iterUint.ReadUint64()
   139  		should.NotNil(iterUint.Error)
   140  	}
   141  }
   142  
   143  func Test_read_int64(t *testing.T) {
   144  	inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
   145  	for _, input := range inputs {
   146  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
   147  			should := require.New(t)
   148  			iter := jsoni.ParseString(jsoni.ConfigDefault, input)
   149  			expected, err := strconv.ParseInt(input, 10, 64)
   150  			should.Nil(err)
   151  			should.Equal(expected, iter.ReadInt64())
   152  		})
   153  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
   154  			should := require.New(t)
   155  			iter := jsoni.Parse(jsoni.ConfigDefault, bytes.NewBufferString(input), 2)
   156  			expected, err := strconv.ParseInt(input, 10, 64)
   157  			should.Nil(err)
   158  			should.Equal(expected, iter.ReadInt64())
   159  		})
   160  	}
   161  }
   162  
   163  func Test_write_uint8(t *testing.T) {
   164  	ctx := context.Background()
   165  	vals := []uint8{0, 1, 11, 111, 255}
   166  	for _, val := range vals {
   167  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   168  			should := require.New(t)
   169  			buf := &bytes.Buffer{}
   170  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   171  			stream.WriteUint8(val)
   172  			stream.Flush()
   173  			should.Nil(stream.Error)
   174  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   175  		})
   176  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   177  			should := require.New(t)
   178  			buf := &bytes.Buffer{}
   179  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   180  			stream.WriteVal(ctx, val)
   181  			stream.Flush()
   182  			should.Nil(stream.Error)
   183  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   184  		})
   185  	}
   186  	should := require.New(t)
   187  	buf := &bytes.Buffer{}
   188  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 3)
   189  	stream.WriteRaw("a")
   190  	stream.WriteUint8(100) // should clear buffer
   191  	stream.Flush()
   192  	should.Nil(stream.Error)
   193  	should.Equal("a100", buf.String())
   194  }
   195  
   196  func Test_write_int8(t *testing.T) {
   197  	ctx := context.Background()
   198  	vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
   199  	for _, val := range vals {
   200  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   201  			should := require.New(t)
   202  			buf := &bytes.Buffer{}
   203  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   204  			stream.WriteInt8(val)
   205  			stream.Flush()
   206  			should.Nil(stream.Error)
   207  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   208  		})
   209  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   210  			should := require.New(t)
   211  			buf := &bytes.Buffer{}
   212  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   213  			stream.WriteVal(ctx, val)
   214  			stream.Flush()
   215  			should.Nil(stream.Error)
   216  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   217  		})
   218  	}
   219  	should := require.New(t)
   220  	buf := &bytes.Buffer{}
   221  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4)
   222  	stream.WriteRaw("a")
   223  	stream.WriteInt8(-100) // should clear buffer
   224  	stream.Flush()
   225  	should.Nil(stream.Error)
   226  	should.Equal("a-100", buf.String())
   227  }
   228  
   229  func Test_write_uint16(t *testing.T) {
   230  	vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
   231  	ctx := context.Background()
   232  	for _, val := range vals {
   233  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   234  			should := require.New(t)
   235  			buf := &bytes.Buffer{}
   236  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   237  			stream.WriteUint16(val)
   238  			stream.Flush()
   239  			should.Nil(stream.Error)
   240  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   241  		})
   242  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   243  			should := require.New(t)
   244  			buf := &bytes.Buffer{}
   245  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   246  			stream.WriteVal(ctx, val)
   247  			stream.Flush()
   248  			should.Nil(stream.Error)
   249  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   250  		})
   251  	}
   252  	should := require.New(t)
   253  	buf := &bytes.Buffer{}
   254  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 5)
   255  	stream.WriteRaw("a")
   256  	stream.WriteUint16(10000) // should clear buffer
   257  	stream.Flush()
   258  	should.Nil(stream.Error)
   259  	should.Equal("a10000", buf.String())
   260  }
   261  
   262  func Test_write_int16(t *testing.T) {
   263  	ctx := context.Background()
   264  	vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
   265  	for _, val := range vals {
   266  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   267  			should := require.New(t)
   268  			buf := &bytes.Buffer{}
   269  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   270  			stream.WriteInt16(val)
   271  			stream.Flush()
   272  			should.Nil(stream.Error)
   273  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   274  		})
   275  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   276  			should := require.New(t)
   277  			buf := &bytes.Buffer{}
   278  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   279  			stream.WriteVal(ctx, val)
   280  			stream.Flush()
   281  			should.Nil(stream.Error)
   282  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   283  		})
   284  	}
   285  	should := require.New(t)
   286  	buf := &bytes.Buffer{}
   287  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 6)
   288  	stream.WriteRaw("a")
   289  	stream.WriteInt16(-10000) // should clear buffer
   290  	stream.Flush()
   291  	should.Nil(stream.Error)
   292  	should.Equal("a-10000", buf.String())
   293  }
   294  
   295  func Test_write_uint32(t *testing.T) {
   296  	vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
   297  	ctx := context.Background()
   298  	for _, val := range vals {
   299  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   300  			should := require.New(t)
   301  			buf := &bytes.Buffer{}
   302  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   303  			stream.WriteUint32(val)
   304  			stream.Flush()
   305  			should.Nil(stream.Error)
   306  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   307  		})
   308  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   309  			should := require.New(t)
   310  			buf := &bytes.Buffer{}
   311  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   312  			stream.WriteVal(ctx, val)
   313  			stream.Flush()
   314  			should.Nil(stream.Error)
   315  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   316  		})
   317  	}
   318  	should := require.New(t)
   319  	buf := &bytes.Buffer{}
   320  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 10)
   321  	stream.WriteRaw("a")
   322  	stream.WriteUint32(0xffffffff) // should clear buffer
   323  	stream.Flush()
   324  	should.Nil(stream.Error)
   325  	should.Equal("a4294967295", buf.String())
   326  }
   327  
   328  func Test_write_int32(t *testing.T) {
   329  	vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
   330  	ctx := context.Background()
   331  	for _, val := range vals {
   332  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   333  			should := require.New(t)
   334  			buf := &bytes.Buffer{}
   335  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   336  			stream.WriteInt32(val)
   337  			stream.Flush()
   338  			should.Nil(stream.Error)
   339  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   340  		})
   341  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   342  			should := require.New(t)
   343  			buf := &bytes.Buffer{}
   344  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   345  			stream.WriteVal(ctx, val)
   346  			stream.Flush()
   347  			should.Nil(stream.Error)
   348  			should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
   349  		})
   350  	}
   351  	should := require.New(t)
   352  	buf := &bytes.Buffer{}
   353  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 11)
   354  	stream.WriteRaw("a")
   355  	stream.WriteInt32(-0x7fffffff) // should clear buffer
   356  	stream.Flush()
   357  	should.Nil(stream.Error)
   358  	should.Equal("a-2147483647", buf.String())
   359  }
   360  
   361  func Test_write_uint64(t *testing.T) {
   362  	vals := []uint64{
   363  		0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
   364  		0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
   365  		0xfffffffffffffff, 0xffffffffffffffff,
   366  	}
   367  	ctx := context.Background()
   368  	for _, val := range vals {
   369  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   370  			should := require.New(t)
   371  			buf := &bytes.Buffer{}
   372  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   373  			stream.WriteUint64(val)
   374  			stream.Flush()
   375  			should.Nil(stream.Error)
   376  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   377  		})
   378  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   379  			should := require.New(t)
   380  			buf := &bytes.Buffer{}
   381  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   382  			stream.WriteVal(ctx, val)
   383  			stream.Flush()
   384  			should.Nil(stream.Error)
   385  			should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
   386  		})
   387  	}
   388  	should := require.New(t)
   389  	buf := &bytes.Buffer{}
   390  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 10)
   391  	stream.WriteRaw("a")
   392  	stream.WriteUint64(0xffffffff) // should clear buffer
   393  	stream.Flush()
   394  	should.Nil(stream.Error)
   395  	should.Equal("a4294967295", buf.String())
   396  }
   397  
   398  func Test_write_int64(t *testing.T) {
   399  	vals := []int64{
   400  		0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
   401  		0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
   402  		0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000,
   403  	}
   404  	ctx := context.Background()
   405  	for _, val := range vals {
   406  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   407  			should := require.New(t)
   408  			buf := &bytes.Buffer{}
   409  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   410  			stream.WriteInt64(val)
   411  			stream.Flush()
   412  			should.Nil(stream.Error)
   413  			should.Equal(strconv.FormatInt(val, 10), buf.String())
   414  		})
   415  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   416  			should := require.New(t)
   417  			buf := &bytes.Buffer{}
   418  			stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
   419  			stream.WriteVal(ctx, val)
   420  			stream.Flush()
   421  			should.Nil(stream.Error)
   422  			should.Equal(strconv.FormatInt(val, 10), buf.String())
   423  		})
   424  	}
   425  	should := require.New(t)
   426  	buf := &bytes.Buffer{}
   427  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 10)
   428  	stream.WriteRaw("a")
   429  	stream.WriteInt64(0xffffffff) // should clear buffer
   430  	stream.Flush()
   431  	should.Nil(stream.Error)
   432  	should.Equal("a4294967295", buf.String())
   433  }