github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/cosmos/libs/common/cmap_test.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestIterateKeysWithValues(t *testing.T) {
    12  	cmap := NewCMap()
    13  
    14  	for i := 1; i <= 10; i++ {
    15  		cmap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
    16  	}
    17  
    18  	// Testing size
    19  	assert.Equal(t, 10, cmap.Size())
    20  	assert.Equal(t, 10, len(cmap.Keys()))
    21  	assert.Equal(t, 10, len(cmap.Values()))
    22  
    23  	// Iterating Keys, checking for matching Value
    24  	for _, key := range cmap.Keys() {
    25  		val := strings.Replace(key, "key", "value", -1)
    26  		assert.Equal(t, val, cmap.Get(key))
    27  	}
    28  
    29  	// Test if all keys are within []Keys()
    30  	keys := cmap.Keys()
    31  	for i := 1; i <= 10; i++ {
    32  		assert.Contains(t, keys, fmt.Sprintf("key%d", i), "cmap.Keys() should contain key")
    33  	}
    34  
    35  	// Delete 1 Key
    36  	cmap.Delete("key1")
    37  
    38  	assert.NotEqual(t, len(keys), len(cmap.Keys()), "[]keys and []Keys() should not be equal, they are copies, one item was removed")
    39  }
    40  
    41  func TestContains(t *testing.T) {
    42  	cmap := NewCMap()
    43  
    44  	cmap.Set("key1", "value1")
    45  
    46  	// Test for known values
    47  	assert.True(t, cmap.Has("key1"))
    48  	assert.Equal(t, "value1", cmap.Get("key1"))
    49  
    50  	// Test for unknown values
    51  	assert.False(t, cmap.Has("key2"))
    52  	assert.Nil(t, cmap.Get("key2"))
    53  }
    54  
    55  func BenchmarkCMapHas(b *testing.B) {
    56  	m := NewCMap()
    57  	for i := 0; i < 1000; i++ {
    58  		m.Set(string(i), i)
    59  	}
    60  	b.ResetTimer()
    61  	for i := 0; i < b.N; i++ {
    62  		m.Has(string(i))
    63  	}
    64  }