mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 08:46:56 +08:00
feat: add SPOP command
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user