refactor command table

This commit is contained in:
hdt3213
2021-05-24 23:17:55 +08:00
parent 68ae54f596
commit f1455534d4
24 changed files with 776 additions and 899 deletions

View File

@@ -11,29 +11,29 @@ import (
// basic add get and remove
func TestSAdd(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
size := 100
// test sadd
key := utils.RandString(10)
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
result := SAdd(testDB, utils.ToBytesList(key, member))
result := execSAdd(testDB, utils.ToBytesList(key, member))
asserts.AssertIntReply(t, result, 1)
}
// test scard
result := SCard(testDB, utils.ToBytesList(key))
result := execSCard(testDB, utils.ToBytesList(key))
asserts.AssertIntReply(t, result, size)
// test is member
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
result := SIsMember(testDB, utils.ToBytesList(key, member))
result := execSIsMember(testDB, utils.ToBytesList(key, member))
asserts.AssertIntReply(t, result, 1)
}
// test members
result = SMembers(testDB, utils.ToBytesList(key))
result = execSMembers(testDB, utils.ToBytesList(key))
multiBulk, ok := result.(*reply.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
@@ -46,25 +46,25 @@ func TestSAdd(t *testing.T) {
}
func TestSRem(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
size := 100
// mock data
key := utils.RandString(10)
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
SAdd(testDB, utils.ToBytesList(key, member))
execSAdd(testDB, utils.ToBytesList(key, member))
}
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
SRem(testDB, utils.ToBytesList(key, member))
result := SIsMember(testDB, utils.ToBytesList(key, member))
execSRem(testDB, utils.ToBytesList(key, member))
result := execSIsMember(testDB, utils.ToBytesList(key, member))
asserts.AssertIntReply(t, result, 0)
}
}
func TestSInter(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
size := 100
step := 10
@@ -75,39 +75,39 @@ func TestSInter(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
SAdd(testDB, utils.ToBytesList(key, member))
execSAdd(testDB, utils.ToBytesList(key, member))
}
start += step
}
result := SInter(testDB, utils.ToBytesList(keys...))
result := execSInter(testDB, utils.ToBytesList(keys...))
asserts.AssertMultiBulkReplySize(t, result, 70)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = SInterStore(testDB, utils.ToBytesList(keysWithDest...))
result = execSInterStore(testDB, utils.ToBytesList(keysWithDest...))
asserts.AssertIntReply(t, result, 70)
// test empty set
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
key0 := utils.RandString(10)
Del(testDB, utils.ToBytesList(key0))
testDB.Remove(key0)
key1 := utils.RandString(10)
SAdd(testDB, utils.ToBytesList(key1, "a", "b"))
execSAdd(testDB, utils.ToBytesList(key1, "a", "b"))
key2 := utils.RandString(10)
SAdd(testDB, utils.ToBytesList(key2, "1", "2"))
result = SInter(testDB, utils.ToBytesList(key0, key1, key2))
execSAdd(testDB, utils.ToBytesList(key2, "1", "2"))
result = execSInter(testDB, utils.ToBytesList(key0, key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = SInter(testDB, utils.ToBytesList(key1, key2))
result = execSInter(testDB, utils.ToBytesList(key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = SInterStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
result = execSInterStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
asserts.AssertIntReply(t, result, 0)
result = SInterStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
result = execSInterStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
asserts.AssertIntReply(t, result, 0)
}
func TestSUnion(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
size := 100
step := 10
@@ -118,22 +118,22 @@ func TestSUnion(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
SAdd(testDB, utils.ToBytesList(key, member))
execSAdd(testDB, utils.ToBytesList(key, member))
}
start += step
}
result := SUnion(testDB, utils.ToBytesList(keys...))
result := execSUnion(testDB, utils.ToBytesList(keys...))
asserts.AssertMultiBulkReplySize(t, result, 130)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = SUnionStore(testDB, utils.ToBytesList(keysWithDest...))
result = execSUnionStore(testDB, utils.ToBytesList(keysWithDest...))
asserts.AssertIntReply(t, result, 130)
}
func TestSDiff(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
size := 100
step := 20
@@ -144,52 +144,52 @@ func TestSDiff(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
SAdd(testDB, utils.ToBytesList(key, member))
execSAdd(testDB, utils.ToBytesList(key, member))
}
start += step
}
result := SDiff(testDB, utils.ToBytesList(keys...))
result := execSDiff(testDB, utils.ToBytesList(keys...))
asserts.AssertMultiBulkReplySize(t, result, step)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = SDiffStore(testDB, utils.ToBytesList(keysWithDest...))
result = execSDiffStore(testDB, utils.ToBytesList(keysWithDest...))
asserts.AssertIntReply(t, result, step)
// test empty set
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
key0 := utils.RandString(10)
Del(testDB, utils.ToBytesList(key0))
testDB.Remove(key0)
key1 := utils.RandString(10)
SAdd(testDB, utils.ToBytesList(key1, "a", "b"))
execSAdd(testDB, utils.ToBytesList(key1, "a", "b"))
key2 := utils.RandString(10)
SAdd(testDB, utils.ToBytesList(key2, "a", "b"))
result = SDiff(testDB, utils.ToBytesList(key0, key1, key2))
execSAdd(testDB, utils.ToBytesList(key2, "a", "b"))
result = execSDiff(testDB, utils.ToBytesList(key0, key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = SDiff(testDB, utils.ToBytesList(key1, key2))
result = execSDiff(testDB, utils.ToBytesList(key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = SDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
result = execSDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
asserts.AssertIntReply(t, result, 0)
result = SDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
result = execSDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
asserts.AssertIntReply(t, result, 0)
}
func TestSRandMember(t *testing.T) {
FlushAll(testDB, [][]byte{})
execFlushAll(testDB, [][]byte{})
key := utils.RandString(10)
for j := 0; j < 100; j++ {
member := strconv.Itoa(j)
SAdd(testDB, utils.ToBytesList(key, member))
execSAdd(testDB, utils.ToBytesList(key, member))
}
result := SRandMember(testDB, utils.ToBytesList(key))
result := execSRandMember(testDB, utils.ToBytesList(key))
br, ok := result.(*reply.BulkReply)
if !ok && len(br.Arg) > 0 {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
return
}
result = SRandMember(testDB, utils.ToBytesList(key, "10"))
result = execSRandMember(testDB, utils.ToBytesList(key, "10"))
asserts.AssertMultiBulkReplySize(t, result, 10)
multiBulk, ok := result.(*reply.MultiBulkReply)
if !ok {
@@ -205,12 +205,12 @@ func TestSRandMember(t *testing.T) {
return
}
result = SRandMember(testDB, utils.ToBytesList(key, "110"))
result = execSRandMember(testDB, utils.ToBytesList(key, "110"))
asserts.AssertMultiBulkReplySize(t, result, 100)
result = SRandMember(testDB, utils.ToBytesList(key, "-10"))
result = execSRandMember(testDB, utils.ToBytesList(key, "-10"))
asserts.AssertMultiBulkReplySize(t, result, 10)
result = SRandMember(testDB, utils.ToBytesList(key, "-110"))
result = execSRandMember(testDB, utils.ToBytesList(key, "-110"))
asserts.AssertMultiBulkReplySize(t, result, 110)
}