github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue100_test.go (about)

     1  /*
     2   * Copyright 2021 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20  	"fmt"
    21  	. "github.com/goshafaq/sonic"
    22  	"reflect"
    23  	_ "sync"
    24  	"testing"
    25  	"unsafe"
    26  
    27  	stdjson "encoding/json"
    28  )
    29  
    30  func TestLargeMapValue(t *testing.T) {
    31  	var jsonStr = `{
    32  		"1": {},
    33  		"2": {},
    34  		"3": {},
    35  		"4": {},
    36  		"5": {},
    37  		"6": {},
    38  		"7": {},
    39  		"8": {},
    40  		"9": {}
    41  	}`
    42  	type Case struct {
    43  		std   interface{}
    44  		sonic interface{}
    45  	}
    46  	cases := []Case{
    47  		{&map[string]TestIssue100_LargeMapValue{}, &map[string]TestIssue100_LargeMapValue{}},
    48  		{&map[int32]TestIssue100_LargeMapValue{}, &map[int32]TestIssue100_LargeMapValue{}},
    49  		{&map[int64]TestIssue100_LargeMapValue{}, &map[int64]TestIssue100_LargeMapValue{}},
    50  		{&map[uint32]TestIssue100_LargeMapValue{}, &map[uint32]TestIssue100_LargeMapValue{}},
    51  		{&map[uint64]TestIssue100_LargeMapValue{}, &map[uint64]TestIssue100_LargeMapValue{}},
    52  		{&map[TestIssue100_textMarshalKey]TestIssue100_LargeMapValue{}, &map[TestIssue100_textMarshalKey]TestIssue100_LargeMapValue{}},
    53  		{&map[TestIssue100_textMarshalKeyPtr]TestIssue100_LargeMapValue{}, &map[TestIssue100_textMarshalKeyPtr]TestIssue100_LargeMapValue{}},
    54  	}
    55  	for i, c := range cases {
    56  		var stdw, sonicw = c.std, c.sonic
    57  		if err := stdjson.Unmarshal([]byte(jsonStr), stdw); err != nil {
    58  			t.Fatal(i, err)
    59  		}
    60  		fmt.Printf("[%d]struct size: %d\tmap length: %d\n", i, unsafe.Sizeof(TestIssue100_LargeMapValue{}), reflect.ValueOf(stdw).Elem().Len())
    61  		if err := Unmarshal([]byte(jsonStr), sonicw); err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		if !reflect.DeepEqual(stdw, sonicw) {
    65  			fmt.Printf("have:\n\t%#v\nwant:\n\t%#v\n", sonicw, stdw)
    66  			t.Fatal(i)
    67  		}
    68  	}
    69  }
    70  
    71  type TestIssue100_textMarshalKey string
    72  
    73  func (self TestIssue100_textMarshalKey) UnmarshalText(text []byte) error {
    74  	_ = TestIssue100_textMarshalKey(text)
    75  	return nil
    76  }
    77  
    78  type TestIssue100_textMarshalKeyPtr string
    79  
    80  func (self *TestIssue100_textMarshalKeyPtr) UnmarshalText(text []byte) error {
    81  	*self = TestIssue100_textMarshalKeyPtr(text)
    82  	return nil
    83  }
    84  
    85  type TestIssue100_LargeMapValue struct {
    86  	Id [129]byte
    87  }