github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/serializer/serializer_test.go (about)

     1  // Copyright 2017 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package serializer
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestSerializer(t *testing.T) {
    14  	x := &X{
    15  		Y: Y{1},
    16  		P: &Y{2},
    17  		A: []Y{{3}, {4}},
    18  		B: true,
    19  		S: "a\x09b",
    20  		T: T1,
    21  		I: []interface{}{
    22  			nil,
    23  			Y{V: 42},
    24  			new(Y),
    25  			(*Y)(nil),
    26  			0,
    27  			42,
    28  			T(0),
    29  			T(42),
    30  			U(96),
    31  			false,
    32  			B(false),
    33  			"",
    34  			"foo",
    35  			S(""),
    36  			S("foo"),
    37  		},
    38  		F: nil,
    39  	}
    40  	want := `&X{Y{1},&Y{2},[]Y{
    41  {3},
    42  {4},
    43  },true,"a\tb",1,[]{
    44  nil,
    45  Y{},
    46  &Y{},
    47  nil,
    48  0,
    49  42,
    50  T(0),
    51  T(42),
    52  U(96),
    53  false,
    54  B(false),
    55  "",
    56  "foo",
    57  S(""),
    58  S("foo"),
    59  },nil}`
    60  	buf := new(bytes.Buffer)
    61  	Write(buf, x)
    62  	if diff := cmp.Diff(want, buf.String()); diff != "" {
    63  		t.Fatal(diff)
    64  	}
    65  }
    66  
    67  type X struct {
    68  	Y Y
    69  	P *Y
    70  	A []Y
    71  	B bool
    72  	S string
    73  	T T
    74  	I []interface{}
    75  	F func()
    76  }
    77  
    78  type Y struct {
    79  	V int
    80  }
    81  
    82  type (
    83  	S string
    84  	B bool
    85  	T int
    86  	U uint16
    87  )
    88  
    89  const (
    90  	_ T = iota
    91  	T1
    92  )