github.com/fananchong/cstruct-go@v0.0.0-20220616060855-b65d9a2f2e17/tests/slice_struct_test.go (about)

     1  package mytest
     2  
     3  import (
     4  	"testing"
     5  
     6  	cstruct "github.com/fananchong/cstruct-go"
     7  )
     8  
     9  type mystruct12 struct {
    10  	F1 int8
    11  }
    12  
    13  type mystruct10 struct {
    14  	F2 []mystruct12
    15  	F1 int32
    16  	F3 []mystruct12
    17  }
    18  
    19  type mystruct9 struct {
    20  	F90 []mystruct10
    21  }
    22  
    23  func Test_LE9(t *testing.T) {
    24  	b1 := []mystruct12{mystruct12{1}, mystruct12{2}, mystruct12{3}, mystruct12{4}}
    25  	b2 := []mystruct12{}
    26  	b3 := []mystruct12{mystruct12{1}, mystruct12{2}}
    27  	{
    28  		a := &mystruct9{}
    29  		a.F90 = []mystruct10{mystruct10{nil, 1, nil}, mystruct10{nil, 2, nil}, mystruct10{b3, 3, b1}}
    30  		test9(t, a)
    31  	}
    32  	{
    33  		a := &mystruct9{}
    34  		a.F90 = []mystruct10{mystruct10{b1, 1, b3}}
    35  		test9(t, a)
    36  	}
    37  	{
    38  		a := &mystruct9{}
    39  		a.F90 = []mystruct10{}
    40  		test9(t, a)
    41  	}
    42  	{
    43  		a := &mystruct9{}
    44  		a.F90 = []mystruct10{mystruct10{b1, 1, b2}}
    45  		test9(t, a)
    46  
    47  	}
    48  	{
    49  		a := &mystruct9{}
    50  		a.F90 = []mystruct10{mystruct10{b2, 1, b1}}
    51  		test9(t, a)
    52  	}
    53  	{
    54  		a := &mystruct9{}
    55  		a.F90 = []mystruct10{mystruct10{b1, 1, b2}, mystruct10{b2, 1, b1}, mystruct10{b2, 1, b1}, mystruct10{b2, 1, b1}}
    56  		test9(t, a)
    57  
    58  	}
    59  }
    60  
    61  func test9(t *testing.T, a *mystruct9) {
    62  	buf_l, _ := cstruct.Marshal(a)
    63  	b := &mystruct9{}
    64  	t.Log(buf_l)
    65  	if err := cstruct.Unmarshal(buf_l, b); err != nil {
    66  		t.Log(err)
    67  		t.Error("出错啦!")
    68  		return
    69  	}
    70  	t.Log(b)
    71  }