feat: add SPOP command

This commit is contained in:
Singularity0909
2022-05-11 14:11:07 +08:00
committed by finley
parent c1d800e110
commit 7059a123d1
6 changed files with 80 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts"
"math/rand"
"strconv"
"testing"
)
@@ -63,6 +64,40 @@ func TestSRem(t *testing.T) {
}
}
func TestSPop(t *testing.T) {
testDB.Flush()
size := 100
// mock data
key := utils.RandString(10)
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
result := testDB.Exec(nil, utils.ToCmdLine("spop", key))
asserts.AssertMultiBulkReplySize(t, result, 1)
currentSize := size - 1
for currentSize > 0 {
count := rand.Intn(currentSize) + 1
resultSpop := testDB.Exec(nil, utils.ToCmdLine("spop", key, strconv.FormatInt(int64(count), 10)))
multiBulk, ok := resultSpop.(*protocol.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", resultSpop.ToBytes()))
return
}
removedSize := len(multiBulk.Args)
for _, arg := range multiBulk.Args {
resultSIsMember := testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, string(arg)))
asserts.AssertIntReply(t, resultSIsMember, 0)
}
currentSize -= removedSize
resultSCard := testDB.Exec(nil, utils.ToCmdLine("SCard", key))
asserts.AssertIntReply(t, resultSCard, currentSize)
}
}
func TestSInter(t *testing.T) {
testDB.Flush()
size := 100