github.com/prysmaticlabs/prysm@v1.4.4/shared/sszutil/deep_equal_test.go (about)

     1  package sszutil_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/sszutil"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    10  )
    11  
    12  func TestDeepEqualBasicTypes(t *testing.T) {
    13  	assert.Equal(t, true, sszutil.DeepEqual(true, true))
    14  	assert.Equal(t, false, sszutil.DeepEqual(true, false))
    15  
    16  	assert.Equal(t, true, sszutil.DeepEqual(byte(222), byte(222)))
    17  	assert.Equal(t, false, sszutil.DeepEqual(byte(222), byte(111)))
    18  
    19  	assert.Equal(t, true, sszutil.DeepEqual(uint64(1234567890), uint64(1234567890)))
    20  	assert.Equal(t, false, sszutil.DeepEqual(uint64(1234567890), uint64(987653210)))
    21  
    22  	assert.Equal(t, true, sszutil.DeepEqual("hello", "hello"))
    23  	assert.Equal(t, false, sszutil.DeepEqual("hello", "world"))
    24  
    25  	assert.Equal(t, true, sszutil.DeepEqual([3]byte{1, 2, 3}, [3]byte{1, 2, 3}))
    26  	assert.Equal(t, false, sszutil.DeepEqual([3]byte{1, 2, 3}, [3]byte{1, 2, 4}))
    27  
    28  	var nilSlice1, nilSlice2 []byte
    29  	assert.Equal(t, true, sszutil.DeepEqual(nilSlice1, nilSlice2))
    30  	assert.Equal(t, true, sszutil.DeepEqual(nilSlice1, []byte{}))
    31  	assert.Equal(t, true, sszutil.DeepEqual([]byte{1, 2, 3}, []byte{1, 2, 3}))
    32  	assert.Equal(t, false, sszutil.DeepEqual([]byte{1, 2, 3}, []byte{1, 2, 4}))
    33  }
    34  
    35  func TestDeepEqualStructs(t *testing.T) {
    36  	type Store struct {
    37  		V1 uint64
    38  		V2 []byte
    39  	}
    40  	store1 := Store{uint64(1234), nil}
    41  	store2 := Store{uint64(1234), []byte{}}
    42  	store3 := Store{uint64(4321), []byte{}}
    43  	assert.Equal(t, true, sszutil.DeepEqual(store1, store2))
    44  	assert.Equal(t, false, sszutil.DeepEqual(store1, store3))
    45  }
    46  
    47  func TestDeepEqualStructs_Unexported(t *testing.T) {
    48  	type Store struct {
    49  		V1           uint64
    50  		V2           []byte
    51  		dontIgnoreMe string
    52  	}
    53  	store1 := Store{uint64(1234), nil, "hi there"}
    54  	store2 := Store{uint64(1234), []byte{}, "hi there"}
    55  	store3 := Store{uint64(4321), []byte{}, "wow"}
    56  	store4 := Store{uint64(4321), []byte{}, "bow wow"}
    57  	assert.Equal(t, true, sszutil.DeepEqual(store1, store2))
    58  	assert.Equal(t, false, sszutil.DeepEqual(store1, store3))
    59  	assert.Equal(t, false, sszutil.DeepEqual(store3, store4))
    60  }
    61  
    62  func TestDeepEqualProto(t *testing.T) {
    63  	var fork1, fork2 *pb.Fork
    64  	assert.Equal(t, true, sszutil.DeepEqual(fork1, fork2))
    65  
    66  	fork1 = &pb.Fork{
    67  		PreviousVersion: []byte{123},
    68  		CurrentVersion:  []byte{124},
    69  		Epoch:           1234567890,
    70  	}
    71  	fork2 = &pb.Fork{
    72  		PreviousVersion: []byte{123},
    73  		CurrentVersion:  []byte{125},
    74  		Epoch:           1234567890,
    75  	}
    76  	assert.Equal(t, true, sszutil.DeepEqual(fork1, fork1))
    77  	assert.Equal(t, false, sszutil.DeepEqual(fork1, fork2))
    78  
    79  	checkpoint1 := &ethpb.Checkpoint{
    80  		Epoch: 1234567890,
    81  		Root:  []byte{},
    82  	}
    83  	checkpoint2 := &ethpb.Checkpoint{
    84  		Epoch: 1234567890,
    85  		Root:  nil,
    86  	}
    87  	assert.Equal(t, true, sszutil.DeepEqual(checkpoint1, checkpoint2))
    88  }
    89  
    90  func Test_IsProto(t *testing.T) {
    91  	tests := []struct {
    92  		name string
    93  		item interface{}
    94  		want bool
    95  	}{
    96  		{
    97  			name: "uint64",
    98  			item: 0,
    99  			want: false,
   100  		},
   101  		{
   102  			name: "string",
   103  			item: "foobar cheese",
   104  			want: false,
   105  		},
   106  		{
   107  			name: "uint64 array",
   108  			item: []uint64{1, 2, 3, 4, 5, 6},
   109  			want: false,
   110  		},
   111  		{
   112  			name: "Attestation",
   113  			item: &ethpb.Attestation{},
   114  			want: true,
   115  		},
   116  		{
   117  			name: "Array of attestations",
   118  			item: []*ethpb.Attestation{},
   119  			want: true,
   120  		},
   121  		{
   122  			name: "Map of attestations",
   123  			item: make(map[uint64]*ethpb.Attestation),
   124  			want: true,
   125  		},
   126  	}
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129  			if got := sszutil.IsProto(tt.item); got != tt.want {
   130  				t.Errorf("isProtoSlice() = %v, want %v", got, tt.want)
   131  			}
   132  		})
   133  	}
   134  }