unit tests of cmd_bit

This commit is contained in:
wenyekui 2014-07-24 17:19:44 +08:00
parent 58cdace7df
commit c0f3c54f3b
2 changed files with 178 additions and 5 deletions

View File

@ -106,12 +106,12 @@ func bcountCommand(db *ledis.DB, args ...string) (interface{}, error) {
var start, end int64 = 0, -1
if argCnt > 1 {
if start, err = strconv.ParseInt(args[1], 10, 32); err != nil {
return nil, err
return nil, ErrValue
}
}
if argCnt > 2 {
if end, err = strconv.ParseInt(args[1], 10, 32); err != nil {
return nil, err
if end, err = strconv.ParseInt(args[2], 10, 32); err != nil {
return nil, ErrValue
}
}
key := []byte(args[0])
@ -148,7 +148,7 @@ func boptCommand(db *ledis.DB, args ...string) (interface{}, error) {
case "not":
op = ledis.OPnot
default:
return nil, fmt.Errorf("ERR invalid argument '%s' for 'bopt' command", opDesc)
return nil, fmt.Errorf("invalid argument '%s' for 'bopt' command", opDesc)
}
if blen, err := db.BOperation(op, dstKey, srcKeys...); err != nil {
return nil, err

View File

@ -1,8 +1,9 @@
package http
import (
// "github.com/siddontang/ledisdb/ledis"
"fmt"
"testing"
"time"
)
func TestBgetCommand(t *testing.T) {
@ -98,4 +99,176 @@ func TestBCountCommand(t *testing.T) {
if err == nil || err.Error() != "wrong number of arguments for 'bcount' command" {
t.Fatalf("invalid err %v", err)
}
db.BSetBit([]byte("test_bcount"), 1, 1)
db.BSetBit([]byte("test_bcount"), 3, 1)
cnt, err := bcountCommand(db, "test_bcount", "0", "3")
if err != nil {
t.Fatal(err.Error())
}
if cnt.(int32) != 2 {
t.Fatal("invalid value", cnt)
}
cnt, err = bcountCommand(db, "test_bcount", "2")
if err != nil {
t.Fatal(err.Error())
}
if cnt.(int32) != 1 {
t.Fatal("invalid value", cnt)
}
cnt, err = bcountCommand(db, "test_bcount")
if err != nil {
t.Fatal(err.Error())
}
if cnt.(int32) != 2 {
t.Fatal("invalid value", cnt)
}
}
func TestBOptCommand(t *testing.T) {
db := getTestDB()
_, err := boptCommand(db, "test_bopt")
if err == nil || err.Error() != "wrong number of arguments for 'bopt' command" {
t.Fatalf("invalid err %v", err)
}
db.BSetBit([]byte("test_bopt_and_1"), 1, 1)
db.BSetBit([]byte("test_bopt_and_2"), 1, 1)
_, err = boptCommand(db, "and", "test_bopt_and_3", "test_bopt_and_1", "test_bopt_and_2")
if err != nil {
t.Fatal(err.Error())
}
r, _ := db.BGet([]byte("test_bopt_and_3"))
if len(r) != 1 || r[0] != 2 {
t.Fatalf("invalid result %v", r)
}
db.BSetBit([]byte("test_bopt_or_1"), 0, 1)
db.BSetBit([]byte("test_bopt_or_1"), 1, 1)
db.BSetBit([]byte("test_bopt_or_2"), 0, 1)
db.BSetBit([]byte("test_bopt_or_2"), 2, 1)
_, err = boptCommand(db, "or", "test_bopt_or_3", "test_bopt_or_1", "test_bopt_or_2")
if err != nil {
t.Fatal(err.Error())
}
r, _ = db.BGet([]byte("test_bopt_or_3"))
if len(r) != 1 || r[0] != 7 {
t.Fatalf("invalid result %v", r)
}
db.BSetBit([]byte("test_bopt_xor_1"), 0, 1)
db.BSetBit([]byte("test_bopt_xor_1"), 1, 1)
db.BSetBit([]byte("test_bopt_xor_2"), 0, 1)
db.BSetBit([]byte("test_bopt_xor_2"), 2, 1)
_, err = boptCommand(db, "xor", "test_bopt_xor_3", "test_bopt_xor_1", "test_bopt_xor_2")
if err != nil {
t.Fatal(err.Error())
}
r, _ = db.BGet([]byte("test_bopt_xor_3"))
if len(r) != 1 || r[0] != 6 {
t.Fatalf("invalid result %v", r)
}
db.BSetBit([]byte("test_bopt_not_1"), 0, 1)
db.BSetBit([]byte("test_bopt_not_1"), 1, 0)
_, err = boptCommand(db, "not", "test_bopt_not_2", "test_bopt_not_1")
if err != nil {
t.Fatal(err.Error())
}
r, _ = db.BGet([]byte("test_bopt_not_2"))
if len(r) != 1 || r[0] != 2 {
t.Fatalf("invalid result %v", r)
}
_, err = boptCommand(db, "invalid_opt", "abc")
if err == nil || err.Error() != "invalid argument 'invalid_opt' for 'bopt' command" {
t.Fatal("invalid err ", err.Error())
}
}
func TestBExpireCommand(t *testing.T) {
db := getTestDB()
_, err := bexpireCommand(db, "test_bexpire", "a", "b")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "bexpire") {
t.Fatalf("invalid err %v", err)
}
db.BSetBit([]byte("test_bexpire"), 1, 1)
bexpireCommand(db, "test_bexpire", "1000")
n, err := db.BTTL([]byte("test_bexpire"))
if err != nil {
t.Fatal(err.Error())
}
if n == -1 {
t.Fatal("wrong result ", n)
}
}
func TestBExpireAtCommand(t *testing.T) {
db := getTestDB()
_, err := bexpireatCommand(db, "test_bexpireat", "a", "b")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "bexpireat") {
t.Fatalf("invalid err %v", err)
}
db.BSetBit([]byte("test_bexpireat"), 1, 1)
expireAt := fmt.Sprintf("%d", time.Now().Unix()+100)
if _, err = bexpireatCommand(db, "test_bexpireat", expireAt); err != nil {
t.Fatal(err.Error())
}
n, err := db.BTTL([]byte("test_bexpireat"))
if err != nil {
t.Fatal(err.Error())
}
if n == -1 {
t.Fatal("wrong result ", n)
}
}
func TestBTTLCommand(t *testing.T) {
db := getTestDB()
_, err := bttlCommand(db, "test_bttl", "a", "b")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "bttl") {
t.Fatalf("invalid err %v", err)
}
v, err := bttlCommand(db, "test_bttl")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != -1 {
t.Fatal("invalid result ", v)
}
}
func TestBPersistCommand(t *testing.T) {
db := getTestDB()
_, err := bpersistCommand(db, "test_bpersist", "a", "b")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "bpersist") {
t.Fatalf("invalid err %v", err)
}
v, err := bpersistCommand(db, "test_bpersist")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 0 {
t.Fatal("invalid result ", v)
}
}