github.com/Finschia/finschia-sdk@v0.48.1/store/internal/maps/maps_test.go (about)

     1  package maps
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestEmptyKeyMerkleMap(t *testing.T) {
    12  	db := newMerkleMap()
    13  	require.Panics(t, func() { db.set("", []byte("value")) }, "setting an empty key should panic")
    14  }
    15  
    16  func TestMerkleMap(t *testing.T) {
    17  	tests := []struct {
    18  		keys   []string
    19  		values []string // each string gets converted to []byte in test
    20  		want   string
    21  	}{
    22  		{[]string{}, []string{}, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
    23  		{[]string{"key1"}, []string{"value1"}, "a44d3cc7daba1a4600b00a2434b30f8b970652169810d6dfa9fb1793a2189324"},
    24  		{[]string{"key1"}, []string{"value2"}, "0638e99b3445caec9d95c05e1a3fc1487b4ddec6a952ff337080360b0dcc078c"},
    25  		// swap order with 2 keys
    26  		{
    27  			[]string{"key1", "key2"},
    28  			[]string{"value1", "value2"},
    29  			"8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
    30  		},
    31  		{
    32  			[]string{"key2", "key1"},
    33  			[]string{"value2", "value1"},
    34  			"8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
    35  		},
    36  		// swap order with 3 keys
    37  		{
    38  			[]string{"key1", "key2", "key3"},
    39  			[]string{"value1", "value2", "value3"},
    40  			"1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
    41  		},
    42  		{
    43  			[]string{"key1", "key3", "key2"},
    44  			[]string{"value1", "value3", "value2"},
    45  			"1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
    46  		},
    47  	}
    48  	for i, tc := range tests {
    49  		db := newMerkleMap()
    50  		for i := 0; i < len(tc.keys); i++ {
    51  			db.set(tc.keys[i], []byte(tc.values[i]))
    52  		}
    53  
    54  		got := db.hash()
    55  		assert.Equal(t, tc.want, fmt.Sprintf("%x", got), "Hash didn't match on tc %d", i)
    56  	}
    57  }
    58  
    59  func TestEmptyKeySimpleMap(t *testing.T) {
    60  	db := newSimpleMap()
    61  	require.Panics(t, func() { db.Set("", []byte("value")) }, "setting an empty key should panic")
    62  }
    63  
    64  func TestSimpleMap(t *testing.T) {
    65  	tests := []struct {
    66  		keys   []string
    67  		values []string // each string gets converted to []byte in test
    68  		want   string
    69  	}{
    70  		{[]string{}, []string{}, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
    71  		{[]string{"key1"}, []string{"value1"}, "a44d3cc7daba1a4600b00a2434b30f8b970652169810d6dfa9fb1793a2189324"},
    72  		{[]string{"key1"}, []string{"value2"}, "0638e99b3445caec9d95c05e1a3fc1487b4ddec6a952ff337080360b0dcc078c"},
    73  		// swap order with 2 keys
    74  		{
    75  			[]string{"key1", "key2"},
    76  			[]string{"value1", "value2"},
    77  			"8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
    78  		},
    79  		{
    80  			[]string{"key2", "key1"},
    81  			[]string{"value2", "value1"},
    82  			"8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
    83  		},
    84  		// swap order with 3 keys
    85  		{
    86  			[]string{"key1", "key2", "key3"},
    87  			[]string{"value1", "value2", "value3"},
    88  			"1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
    89  		},
    90  		{
    91  			[]string{"key1", "key3", "key2"},
    92  			[]string{"value1", "value3", "value2"},
    93  			"1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
    94  		},
    95  	}
    96  	for i, tc := range tests {
    97  		db := newSimpleMap()
    98  		for i := 0; i < len(tc.keys); i++ {
    99  			db.Set(tc.keys[i], []byte(tc.values[i]))
   100  		}
   101  		got := db.Hash()
   102  		assert.Equal(t, tc.want, fmt.Sprintf("%x", got), "Hash didn't match on tc %d", i)
   103  	}
   104  }