github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/iavl/key_format_test.go (about)

     1  package iavl
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestKeyFormatBytes(t *testing.T) {
    10  	type keyPairs struct {
    11  		key      [][]byte
    12  		expected []byte
    13  	}
    14  	emptyTestVector := keyPairs{key: [][]byte{}, expected: []byte{'e'}}
    15  	threeByteTestVector := keyPairs{
    16  		key:      [][]byte{{1, 2, 3}},
    17  		expected: []byte{'e', 0, 0, 0, 0, 0, 1, 2, 3},
    18  	}
    19  	eightByteTestVector := keyPairs{
    20  		key:      [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}},
    21  		expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8},
    22  	}
    23  
    24  	tests := []struct {
    25  		name        string
    26  		kf          *KeyFormat
    27  		testVectors []keyPairs
    28  	}{{
    29  		name: "simple 3 int key format",
    30  		kf:   NewKeyFormat(byte('e'), 8, 8, 8),
    31  		testVectors: []keyPairs{
    32  			emptyTestVector,
    33  			threeByteTestVector,
    34  			eightByteTestVector,
    35  			{
    36  				key:      [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8}, {1, 1, 2, 2, 3, 3}},
    37  				expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 1, 1, 2, 2, 3, 3},
    38  			},
    39  		},
    40  	}, {
    41  		name: "zero suffix key format",
    42  		kf:   NewKeyFormat(byte('e'), 8, 0),
    43  		testVectors: []keyPairs{
    44  			emptyTestVector,
    45  			threeByteTestVector,
    46  			eightByteTestVector,
    47  			{
    48  				key:      [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8, 9}},
    49  				expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    50  			},
    51  			{
    52  				key:      [][]byte{{1, 2, 3, 4, 5, 6, 7, 8}, []byte("hellohello")},
    53  				expected: []byte{'e', 1, 2, 3, 4, 5, 6, 7, 8, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
    54  			},
    55  		},
    56  	}}
    57  	for _, tc := range tests {
    58  		kf := tc.kf
    59  		for i, v := range tc.testVectors {
    60  			assert.Equal(t, v.expected, kf.KeyBytes(v.key...), "key format %s, test case %d", tc.name, i)
    61  		}
    62  	}
    63  }
    64  
    65  func TestKeyFormat(t *testing.T) {
    66  	kf := NewKeyFormat(byte('e'), 8, 8, 8)
    67  	key := []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 1, 144}
    68  	var a, b, c int64 = 100, 200, 400
    69  	assert.Equal(t, key, kf.Key(a, b, c))
    70  
    71  	var ao, bo, co = new(int64), new(int64), new(int64)
    72  	kf.Scan(key, ao, bo, co)
    73  	assert.Equal(t, a, *ao)
    74  	assert.Equal(t, b, *bo)
    75  	assert.Equal(t, c, *co)
    76  
    77  	bs := new([]byte)
    78  	kf.Scan(key, ao, bo, bs)
    79  	assert.Equal(t, a, *ao)
    80  	assert.Equal(t, b, *bo)
    81  	assert.Equal(t, []byte{0, 0, 0, 0, 0, 0, 1, 144}, *bs)
    82  
    83  	assert.Equal(t, []byte{'e', 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 200}, kf.Key(a, b))
    84  }
    85  
    86  func TestNegativeKeys(t *testing.T) {
    87  	kf := NewKeyFormat(byte('e'), 8, 8)
    88  
    89  	var a, b int64 = -100, -200
    90  	// One's complement plus one
    91  	key := []byte{'e',
    92  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, byte(0xff + a + 1),
    93  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, byte(0xff + b + 1)}
    94  	assert.Equal(t, key, kf.Key(a, b))
    95  
    96  	var ao, bo = new(int64), new(int64)
    97  	kf.Scan(key, ao, bo)
    98  	assert.Equal(t, a, *ao)
    99  	assert.Equal(t, b, *bo)
   100  }
   101  
   102  func TestOverflow(t *testing.T) {
   103  	kf := NewKeyFormat(byte('o'), 8, 8)
   104  
   105  	var a int64 = 1 << 62
   106  	var b uint64 = 1 << 63
   107  	key := []byte{'o',
   108  		0x40, 0, 0, 0, 0, 0, 0, 0,
   109  		0x80, 0, 0, 0, 0, 0, 0, 0,
   110  	}
   111  	assert.Equal(t, key, kf.Key(a, b))
   112  
   113  	var ao, bo = new(int64), new(int64)
   114  	kf.Scan(key, ao, bo)
   115  	assert.Equal(t, a, *ao)
   116  	assert.Equal(t, int64(b), *bo)
   117  }