github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/ds/zset/zset_test.go (about) 1 package zset 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func InitZSet() *SortedSet { 9 zSet := New() 10 zSet.ZAdd("myzset", 19, "ced") 11 zSet.ZAdd("myzset", 12, "acd") 12 zSet.ZAdd("myzset", 17, "bcd") 13 zSet.ZAdd("myzset", 32, "acc") 14 zSet.ZAdd("myzset", 17, "mcd") 15 zSet.ZAdd("myzset", 21, "ccd") 16 zSet.ZAdd("myzset", 17, "ecd") 17 18 return zSet 19 } 20 21 func TestSortedSet_ZAdd(t *testing.T) { 22 zSet := InitZSet() 23 zSet.ZAdd("myzset", 39, "mmd") 24 25 c1 := zSet.ZCard("myzset") 26 assert.Equal(t, 8, c1) 27 } 28 29 func TestSortedSet_ZScore(t *testing.T) { 30 zSet := InitZSet() 31 ok, s1 := zSet.ZScore("myzset", "acd") 32 assert.Equal(t, true, ok) 33 assert.Equal(t, float64(12), s1) 34 35 ok, s2 := zSet.ZScore("myzset", "aaa") 36 assert.Equal(t, false, ok) 37 assert.Equal(t, float64(0), s2) 38 } 39 40 func TestSortedSet_ZRank(t *testing.T) { 41 key := "myzset" 42 zset := InitZSet() 43 r1 := zset.ZRank(key, "acd") 44 assert.Equal(t, int64(0), r1) 45 46 r2 := zset.ZRank(key, "bcd") 47 assert.Equal(t, int64(1), r2) 48 49 r3 := zset.ZRank(key, "not exist") 50 assert.Equal(t, int64(-1), r3) 51 } 52 53 func TestSortedSet_ZRevRank(t *testing.T) { 54 key := "myzset" 55 zset := InitZSet() 56 r1 := zset.ZRevRank(key, "acd") 57 assert.Equal(t, int64(6), r1) 58 59 r2 := zset.ZRevRank(key, "bcd") 60 assert.Equal(t, int64(5), r2) 61 62 r3 := zset.ZRevRank(key, "not exist") 63 assert.Equal(t, int64(-1), r3) 64 } 65 66 func TestSortedSet_ZIncrBy(t *testing.T) { 67 zset := InitZSet() 68 key := "myzset" 69 70 incr1 := zset.ZIncrBy(key, 300, "acd") 71 assert.Equal(t, float64(312), incr1) 72 73 incr2 := zset.ZIncrBy(key, 100, "acc") 74 assert.Equal(t, float64(132), incr2) 75 } 76 77 func TestSortedSet_ZRange(t *testing.T) { 78 zSet := InitZSet() 79 key := "myzset" 80 81 ran := zSet.ZRange(key, 0, -1) 82 assert.Equal(t, 7, len(ran)) 83 84 for _, v := range ran { 85 assert.NotNil(t, v) 86 } 87 } 88 89 func TestSortedSet_ZRangeWithScores(t *testing.T) { 90 zSet := InitZSet() 91 key := "myzset" 92 93 values := zSet.ZRangeWithScores(key, 0, -1) 94 assert.NotNil(t, values) 95 96 for _, v := range values { 97 assert.NotNil(t, v) 98 } 99 } 100 101 func TestSortedSet_ZRevRange(t *testing.T) { 102 zSet := InitZSet() 103 key := "myzset" 104 105 values := zSet.ZRevRange(key, 0, -1) 106 assert.NotNil(t, values) 107 108 for _, v := range values { 109 assert.NotNil(t, v) 110 } 111 } 112 113 func TestSortedSet_ZRevRangeWithScores(t *testing.T) { 114 zSet := InitZSet() 115 key := "myzset" 116 117 values := zSet.ZRevRangeWithScores(key, 0, -1) 118 assert.NotNil(t, values) 119 120 for _, v := range values { 121 assert.NotNil(t, v) 122 } 123 } 124 125 func TestSortedSet_ZRem(t *testing.T) { 126 zset := InitZSet() 127 key := "myzset" 128 129 ok1 := zset.ZRem(key, "acd") 130 assert.Equal(t, true, ok1) 131 132 ok2 := zset.ZRem(key, "aaaaaaa") 133 assert.Equal(t, false, ok2) 134 } 135 136 func TestSortedSet_ZGetByRank(t *testing.T) { 137 zset := InitZSet() 138 key := "myzset" 139 140 getRank := func(rank int) { 141 val := zset.ZGetByRank(key, rank) 142 if val != nil { 143 for _, v := range val { 144 assert.NotNil(t, v) 145 } 146 } 147 } 148 getRank(0) 149 getRank(4) 150 getRank(6) 151 }