github.com/ethersphere/bee/v2@v2.2.0/pkg/sharky/shard_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package sharky_test
     5  
     6  import (
     7  	"fmt"
     8  	"math"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/sharky"
    12  )
    13  
    14  func TestLocationSerialization(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	for _, tc := range []*sharky.Location{
    18  		{
    19  			Shard:  1,
    20  			Slot:   100,
    21  			Length: 4096,
    22  		},
    23  		{
    24  			Shard:  0,
    25  			Slot:   0,
    26  			Length: 0,
    27  		},
    28  		{
    29  			Shard:  math.MaxUint8,
    30  			Slot:   math.MaxUint32,
    31  			Length: math.MaxUint16,
    32  		},
    33  	} {
    34  		tc := tc
    35  		t.Run(fmt.Sprintf("%d_%d_%d", tc.Shard, tc.Slot, tc.Length), func(t *testing.T) {
    36  			t.Parallel()
    37  
    38  			buf, err := tc.MarshalBinary()
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			if len(buf) != sharky.LocationSize {
    44  				t.Fatal("unexpected length of buffer")
    45  			}
    46  
    47  			l2 := &sharky.Location{}
    48  
    49  			err = l2.UnmarshalBinary(buf)
    50  			if err != nil {
    51  				t.Fatal(err)
    52  			}
    53  
    54  			if l2.Shard != tc.Shard || l2.Slot != tc.Slot || l2.Length != tc.Length {
    55  				t.Fatalf("read incorrect values from buf exp: %v found %v", tc, l2)
    56  			}
    57  		})
    58  	}
    59  }