github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/cache/binary_write_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"io"
     7  	"math/big"
     8  	"reflect"
     9  	"strconv"
    10  	"testing"
    11  )
    12  
    13  type testMarshaler struct {
    14  	value string
    15  }
    16  
    17  func (t *testMarshaler) MarshalCache(writer io.Writer) error {
    18  	v, err := strconv.Atoi(t.value)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	return binary.Write(writer, binary.LittleEndian, int32(v))
    23  }
    24  
    25  func TestWriteValue(t *testing.T) {
    26  	var data []byte
    27  	buf := bytes.NewBuffer(data)
    28  
    29  	expectedUint64 := uint64(123)
    30  	if err := WriteValue(buf, expectedUint64); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	var uint64Value uint64
    34  	if err := binary.Read(buf, binary.LittleEndian, &uint64Value); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	if uint64Value != expectedUint64 {
    39  		t.Fatal("uint64 wrong value:", uint64Value)
    40  	}
    41  
    42  	buf.Reset()
    43  	expectedStr := "hello"
    44  	expectedStrLen := uint64(len(expectedStr))
    45  	if err := WriteValue(buf, expectedStr); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	var stringValue string
    49  	var stringLen uint64
    50  	var strBytes []byte
    51  	if err := binary.Read(buf, binary.LittleEndian, &stringLen); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	strBytes = make([]byte, stringLen)
    55  	if err := binary.Read(buf, binary.LittleEndian, strBytes); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	stringValue = string(strBytes)
    59  	if stringLen != expectedStrLen {
    60  		t.Fatal("string wrong length", stringLen)
    61  	}
    62  	if stringValue != expectedStr {
    63  		t.Fatal("string wrong value:", stringValue)
    64  	}
    65  
    66  	buf.Reset()
    67  	expectedStrSlice := []string{"hello", "world"}
    68  	if err := WriteValue(buf, expectedStrSlice); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	var sliceSize uint64
    72  	if err := binary.Read(buf, binary.LittleEndian, &sliceSize); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	stringSliceValue := make([]string, 0, sliceSize)
    76  	for range expectedStrSlice {
    77  		var strLen uint64
    78  		if err := binary.Read(buf, binary.LittleEndian, &strLen); err != nil {
    79  			t.Fatal(err)
    80  		}
    81  		value := make([]byte, 5)
    82  		if err := binary.Read(buf, binary.LittleEndian, value); err != nil {
    83  			t.Fatal(err)
    84  		}
    85  		stringSliceValue = append(stringSliceValue, string(value))
    86  	}
    87  	if !reflect.DeepEqual(stringSliceValue, expectedStrSlice) {
    88  		t.Fatal("string slice wrong value", stringSliceValue)
    89  	}
    90  
    91  	buf.Reset()
    92  	marshalValue := new(testMarshaler)
    93  	marshalValue.value = "42"
    94  	expectedMarshalValue := int32(42)
    95  	if err := WriteValue(buf, marshalValue); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	var marshalResult int32
    99  	if err := binary.Read(buf, binary.LittleEndian, &marshalResult); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if marshalResult != expectedMarshalValue {
   103  		t.Fatal("unmarshal wrong value", marshalResult)
   104  	}
   105  
   106  	buf.Reset()
   107  	expectedMarshalerSlice := []Marshaler{
   108  		&testMarshaler{"10"},
   109  		&testMarshaler{"21"},
   110  	}
   111  	if err := WriteValue(buf, expectedMarshalerSlice); err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	if err := binary.Read(buf, binary.LittleEndian, &sliceSize); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	marshalerSliceValue := make([]int32, 0, sliceSize)
   118  	for range expectedMarshalerSlice {
   119  		var value int32
   120  		if err := binary.Read(buf, binary.LittleEndian, &value); err != nil {
   121  			t.Fatal(err)
   122  		}
   123  		marshalerSliceValue = append(marshalerSliceValue, value)
   124  	}
   125  	if len(marshalerSliceValue) != len(expectedMarshalerSlice) {
   126  		t.Fatal("marshaler slice wrong len")
   127  	}
   128  	if !reflect.DeepEqual(marshalerSliceValue, []int32{10, 21}) {
   129  		t.Fatal("string slice wrong value", marshalerSliceValue)
   130  	}
   131  }
   132  
   133  func TestWriteBigInt(t *testing.T) {
   134  	buf := new(bytes.Buffer)
   135  	bigint := big.NewInt(0)
   136  	_, ok := bigint.SetString("57006123709077586392", 10)
   137  	if !ok {
   138  		t.Fatal("cannot set test value")
   139  	}
   140  	if err := writeBigInt(buf, bigint); err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	var size uint64
   145  	if err := read(buf, &size); err != nil {
   146  		t.Fatal(err)
   147  	}
   148  	// we expect size to be len(bigint.Bytes()) + 1, because GobEncode adds 1 byte
   149  	// for storing the sign
   150  	if byteLen := len(bigint.Bytes()) + 1; int(size) != byteLen {
   151  		t.Fatal("size is", size, "expected", byteLen)
   152  	}
   153  
   154  	data := make([]byte, size)
   155  	if err := read(buf, data); err != nil {
   156  		t.Fatal(err)
   157  	}
   158  
   159  	result := big.NewInt(0)
   160  	if err := result.GobDecode(data); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	if result.Cmp(bigint) != 0 {
   164  		t.Fatal("values are not same")
   165  	}
   166  }