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

     1  package misc_tests
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"testing"
     8  
     9  	"github.com/bingoohuang/gg/pkg/jsoni"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Test_empty_array(t *testing.T) {
    14  	should := require.New(t)
    15  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[]`)
    16  	cont := iter.ReadArray()
    17  	should.False(cont)
    18  	iter = jsoni.ParseString(jsoni.ConfigDefault, `[]`)
    19  	iter.ReadArrayCB(func(iter *jsoni.Iterator) bool {
    20  		should.FailNow("should not call")
    21  		return true
    22  	})
    23  }
    24  
    25  func Test_one_element(t *testing.T) {
    26  	should := require.New(t)
    27  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[1]`)
    28  	should.True(iter.ReadArray())
    29  	should.Equal(1, iter.ReadInt())
    30  	should.False(iter.ReadArray())
    31  	iter = jsoni.ParseString(jsoni.ConfigDefault, `[1]`)
    32  	iter.ReadArrayCB(func(iter *jsoni.Iterator) bool {
    33  		should.Equal(1, iter.ReadInt())
    34  		return true
    35  	})
    36  }
    37  
    38  func Test_two_elements(t *testing.T) {
    39  	should := require.New(t)
    40  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[1,2]`)
    41  	should.True(iter.ReadArray())
    42  	should.Equal(int64(1), iter.ReadInt64())
    43  	should.True(iter.ReadArray())
    44  	should.Equal(int64(2), iter.ReadInt64())
    45  	should.False(iter.ReadArray())
    46  	iter = jsoni.ParseString(jsoni.ConfigDefault, `[1,2]`)
    47  	should.Equal([]interface{}{float64(1), float64(2)}, iter.Read(context.Background()))
    48  }
    49  
    50  func Test_whitespace_in_head(t *testing.T) {
    51  	iter := jsoni.ParseString(jsoni.ConfigDefault, ` [1]`)
    52  	cont := iter.ReadArray()
    53  	if cont != true {
    54  		t.FailNow()
    55  	}
    56  	if iter.ReadUint64() != 1 {
    57  		t.FailNow()
    58  	}
    59  }
    60  
    61  func Test_whitespace_after_array_start(t *testing.T) {
    62  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[ 1]`)
    63  	cont := iter.ReadArray()
    64  	if cont != true {
    65  		t.FailNow()
    66  	}
    67  	if iter.ReadUint64() != 1 {
    68  		t.FailNow()
    69  	}
    70  }
    71  
    72  func Test_whitespace_before_array_end(t *testing.T) {
    73  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[1 ]`)
    74  	cont := iter.ReadArray()
    75  	if cont != true {
    76  		t.FailNow()
    77  	}
    78  	if iter.ReadUint64() != 1 {
    79  		t.FailNow()
    80  	}
    81  	cont = iter.ReadArray()
    82  	if cont != false {
    83  		t.FailNow()
    84  	}
    85  }
    86  
    87  func Test_whitespace_before_comma(t *testing.T) {
    88  	iter := jsoni.ParseString(jsoni.ConfigDefault, `[1 ,2]`)
    89  	cont := iter.ReadArray()
    90  	if cont != true {
    91  		t.FailNow()
    92  	}
    93  	if iter.ReadUint64() != 1 {
    94  		t.FailNow()
    95  	}
    96  	cont = iter.ReadArray()
    97  	if cont != true {
    98  		t.FailNow()
    99  	}
   100  	if iter.ReadUint64() != 2 {
   101  		t.FailNow()
   102  	}
   103  	cont = iter.ReadArray()
   104  	if cont != false {
   105  		t.FailNow()
   106  	}
   107  }
   108  
   109  func Test_write_array(t *testing.T) {
   110  	should := require.New(t)
   111  	buf := &bytes.Buffer{}
   112  	stream := jsoni.NewStream(jsoni.Config{IndentionStep: 2}.Froze(), buf, 4096)
   113  	stream.WriteArrayStart()
   114  	stream.WriteInt(1)
   115  	stream.WriteMore()
   116  	stream.WriteInt(2)
   117  	stream.WriteArrayEnd()
   118  	stream.Flush()
   119  	should.Nil(stream.Error)
   120  	should.Equal("[\n  1,\n  2\n]", buf.String())
   121  }
   122  
   123  func Test_write_val_array(t *testing.T) {
   124  	should := require.New(t)
   125  	val := []int{1, 2, 3}
   126  	str, err := jsoni.MarshalToString(&val)
   127  	should.Nil(err)
   128  	should.Equal("[1,2,3]", str)
   129  }
   130  
   131  func Test_write_val_empty_array(t *testing.T) {
   132  	should := require.New(t)
   133  	val := []int{}
   134  	str, err := jsoni.MarshalToString(val)
   135  	should.Nil(err)
   136  	should.Equal("[]", str)
   137  }
   138  
   139  func Test_write_array_of_interface_in_struct(t *testing.T) {
   140  	should := require.New(t)
   141  	type TestObject struct {
   142  		Field  []interface{}
   143  		Field2 string
   144  	}
   145  	val := TestObject{[]interface{}{1, 2}, ""}
   146  	str, err := jsoni.MarshalToString(val)
   147  	should.Nil(err)
   148  	should.Contains(str, `"Field":[1,2]`)
   149  	should.Contains(str, `"Field2":""`)
   150  }
   151  
   152  func Test_encode_byte_array(t *testing.T) {
   153  	should := require.New(t)
   154  	bytes, err := json.Marshal([]byte{1, 2, 3})
   155  	should.Nil(err)
   156  	should.Equal(`"AQID"`, string(bytes))
   157  	bytes, err = jsoni.Marshal([]byte{1, 2, 3})
   158  	should.Nil(err)
   159  	should.Equal(`"AQID"`, string(bytes))
   160  }
   161  
   162  func Test_encode_empty_byte_array(t *testing.T) {
   163  	should := require.New(t)
   164  	bytes, err := json.Marshal([]byte{})
   165  	should.Nil(err)
   166  	should.Equal(`""`, string(bytes))
   167  	bytes, err = jsoni.Marshal([]byte{})
   168  	should.Nil(err)
   169  	should.Equal(`""`, string(bytes))
   170  }
   171  
   172  func Test_encode_nil_byte_array(t *testing.T) {
   173  	should := require.New(t)
   174  	var nilSlice []byte
   175  	bytes, err := json.Marshal(nilSlice)
   176  	should.Nil(err)
   177  	should.Equal(`null`, string(bytes))
   178  	bytes, err = jsoni.Marshal(nilSlice)
   179  	should.Nil(err)
   180  	should.Equal(`null`, string(bytes))
   181  }
   182  
   183  func Test_decode_byte_array_from_base64(t *testing.T) {
   184  	should := require.New(t)
   185  	data := []byte{}
   186  	err := json.Unmarshal([]byte(`"AQID"`), &data)
   187  	should.Nil(err)
   188  	should.Equal([]byte{1, 2, 3}, data)
   189  	err = jsoni.Unmarshal([]byte(`"AQID"`), &data)
   190  	should.Nil(err)
   191  	should.Equal([]byte{1, 2, 3}, data)
   192  }
   193  
   194  func Test_decode_byte_array_from_base64_with_newlines(t *testing.T) {
   195  	should := require.New(t)
   196  	data := []byte{}
   197  	err := json.Unmarshal([]byte(`"A\rQ\nID"`), &data)
   198  	should.Nil(err)
   199  	should.Equal([]byte{1, 2, 3}, data)
   200  	err = jsoni.Unmarshal([]byte(`"A\rQ\nID"`), &data)
   201  	should.Nil(err)
   202  	should.Equal([]byte{1, 2, 3}, data)
   203  }
   204  
   205  func Test_decode_byte_array_from_array(t *testing.T) {
   206  	should := require.New(t)
   207  	data := []byte{}
   208  	err := json.Unmarshal([]byte(`[1,2,3]`), &data)
   209  	should.Nil(err)
   210  	should.Equal([]byte{1, 2, 3}, data)
   211  	err = jsoni.Unmarshal([]byte(`[1,2,3]`), &data)
   212  	should.Nil(err)
   213  	should.Equal([]byte{1, 2, 3}, data)
   214  }
   215  
   216  func Test_decode_slice(t *testing.T) {
   217  	should := require.New(t)
   218  	slice := make([]string, 0, 5)
   219  	jsoni.UnmarshalFromString(`["hello", "world"]`, &slice)
   220  	should.Equal([]string{"hello", "world"}, slice)
   221  }
   222  
   223  func Test_decode_large_slice(t *testing.T) {
   224  	should := require.New(t)
   225  	slice := make([]int, 0, 1)
   226  	jsoni.UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
   227  	should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
   228  }
   229  
   230  func Benchmark_jsoniter_array(b *testing.B) {
   231  	b.ReportAllocs()
   232  	input := []byte(`[1,2,3,4,5,6,7,8,9]`)
   233  	iter := jsoni.ParseBytes(jsoni.ConfigDefault, input)
   234  	b.ResetTimer()
   235  	for n := 0; n < b.N; n++ {
   236  		iter.ResetBytes(input)
   237  		for iter.ReadArray() {
   238  			iter.ReadUint64()
   239  		}
   240  	}
   241  }
   242  
   243  func Benchmark_json_array(b *testing.B) {
   244  	for n := 0; n < b.N; n++ {
   245  		result := []interface{}{}
   246  		json.Unmarshal([]byte(`[1,2,3]`), &result)
   247  	}
   248  }