github.com/devwanda/aphelion-staking@v0.33.9/libs/cmap/cmap_test.go (about) 1 package cmap 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( 39 t, 40 len(keys), 41 len(cmap.Keys()), 42 "[]keys and []Keys() should not be equal, they are copies, one item was removed", 43 ) 44 } 45 46 func TestContains(t *testing.T) { 47 cmap := NewCMap() 48 49 cmap.Set("key1", "value1") 50 51 // Test for known values 52 assert.True(t, cmap.Has("key1")) 53 assert.Equal(t, "value1", cmap.Get("key1")) 54 55 // Test for unknown values 56 assert.False(t, cmap.Has("key2")) 57 assert.Nil(t, cmap.Get("key2")) 58 } 59 60 func BenchmarkCMapHas(b *testing.B) { 61 m := NewCMap() 62 for i := 0; i < 1000; i++ { 63 m.Set(fmt.Sprint(i), i) 64 } 65 b.ResetTimer() 66 for i := 0; i < b.N; i++ { 67 m.Has(fmt.Sprint(i)) 68 } 69 } 70