github.com/hdt3213/godis@v1.2.9/database/string_test.go (about)

     1  package database
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hdt3213/godis/lib/utils"
     6  	"github.com/hdt3213/godis/redis/protocol"
     7  	"github.com/hdt3213/godis/redis/protocol/asserts"
     8  	"strconv"
     9  	"testing"
    10  )
    11  
    12  var testDB = makeTestDB()
    13  var testServer = NewStandaloneServer()
    14  
    15  func TestSet2(t *testing.T) {
    16  	key := utils.RandString(10)
    17  	value := utils.RandString(10)
    18  	for i := 0; i < 1000; i++ {
    19  		testDB.Exec(nil, utils.ToCmdLine("SET", key, value))
    20  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
    21  		expected := protocol.MakeBulkReply([]byte(value))
    22  		if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
    23  			t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
    24  		}
    25  	}
    26  }
    27  
    28  func TestSetEmpty(t *testing.T) {
    29  	key := utils.RandString(10)
    30  	testDB.Exec(nil, utils.ToCmdLine("SET", key, ""))
    31  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
    32  	bulkReply, ok := actual.(*protocol.BulkReply)
    33  	if !ok {
    34  		t.Errorf("expected bulk protocol, actually %s", actual.ToBytes())
    35  		return
    36  	}
    37  	if !(bulkReply.Arg != nil && len(bulkReply.Arg) == 0) {
    38  		t.Error("illegal empty string")
    39  	}
    40  }
    41  
    42  func TestSet(t *testing.T) {
    43  	testDB.Flush()
    44  	key := utils.RandString(10)
    45  	value := utils.RandString(10)
    46  
    47  	// normal set
    48  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value))
    49  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
    50  	expected := protocol.MakeBulkReply([]byte(value))
    51  	if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
    52  		t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
    53  	}
    54  
    55  	// set nx
    56  	actual = testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "NX"))
    57  	if _, ok := actual.(*protocol.NullBulkReply); !ok {
    58  		t.Error("expected true actual false")
    59  	}
    60  
    61  	testDB.Flush()
    62  	key = utils.RandString(10)
    63  	value = utils.RandString(10)
    64  	actual = testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "NX"))
    65  	actual = testDB.Exec(nil, utils.ToCmdLine("GET", key))
    66  	expected = protocol.MakeBulkReply([]byte(value))
    67  	if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
    68  		t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
    69  	}
    70  
    71  	// set xx
    72  	testDB.Flush()
    73  	key = utils.RandString(10)
    74  	value = utils.RandString(10)
    75  	actual = testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "XX"))
    76  	if _, ok := actual.(*protocol.NullBulkReply); !ok {
    77  		t.Error("expected true actually false ")
    78  	}
    79  
    80  	execSet(testDB, utils.ToCmdLine(key, value))
    81  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value))
    82  	actual = testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "XX"))
    83  	actual = testDB.Exec(nil, utils.ToCmdLine("GET", key))
    84  	asserts.AssertBulkReply(t, actual, value)
    85  
    86  	// set ex
    87  	testDB.Remove(key)
    88  	ttl := "1000"
    89  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "EX", ttl))
    90  	actual = testDB.Exec(nil, utils.ToCmdLine("GET", key))
    91  	asserts.AssertBulkReply(t, actual, value)
    92  	actual = execTTL(testDB, utils.ToCmdLine(key))
    93  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
    94  	intResult, ok := actual.(*protocol.IntReply)
    95  	if !ok {
    96  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
    97  		return
    98  	}
    99  	if intResult.Code <= 0 || intResult.Code > 1000 {
   100  		t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code))
   101  		return
   102  	}
   103  
   104  	// set px
   105  	testDB.Remove(key)
   106  	ttlPx := "1000000"
   107  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "PX", ttlPx))
   108  	actual = testDB.Exec(nil, utils.ToCmdLine("GET", key))
   109  	asserts.AssertBulkReply(t, actual, value)
   110  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
   111  	intResult, ok = actual.(*protocol.IntReply)
   112  	if !ok {
   113  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   114  		return
   115  	}
   116  	if intResult.Code <= 0 || intResult.Code > 1000 {
   117  		t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code))
   118  		return
   119  	}
   120  }
   121  
   122  func TestSetNX(t *testing.T) {
   123  	testDB.Flush()
   124  	key := utils.RandString(10)
   125  	value := utils.RandString(10)
   126  	testDB.Exec(nil, utils.ToCmdLine("SETNX", key, value))
   127  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   128  	expected := protocol.MakeBulkReply([]byte(value))
   129  	if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
   130  		t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   131  	}
   132  
   133  	actual = testDB.Exec(nil, utils.ToCmdLine("SETNX", key, value))
   134  	expected2 := protocol.MakeIntReply(int64(0))
   135  	if !utils.BytesEquals(actual.ToBytes(), expected2.ToBytes()) {
   136  		t.Error("expected: " + string(expected2.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   137  	}
   138  }
   139  
   140  func TestSetEX(t *testing.T) {
   141  	testDB.Flush()
   142  	key := utils.RandString(10)
   143  	value := utils.RandString(10)
   144  	ttl := "1000"
   145  
   146  	testDB.Exec(nil, utils.ToCmdLine("SETEX", key, ttl, value))
   147  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   148  	asserts.AssertBulkReply(t, actual, value)
   149  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
   150  	intResult, ok := actual.(*protocol.IntReply)
   151  	if !ok {
   152  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   153  		return
   154  	}
   155  	if intResult.Code <= 0 || intResult.Code > 1000 {
   156  		t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code))
   157  		return
   158  	}
   159  }
   160  
   161  func TestPSetEX(t *testing.T) {
   162  	testDB.Flush()
   163  	key := utils.RandString(10)
   164  	value := utils.RandString(10)
   165  	ttl := "1000000"
   166  
   167  	testDB.Exec(nil, utils.ToCmdLine("PSetEx", key, ttl, value))
   168  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   169  	asserts.AssertBulkReply(t, actual, value)
   170  	actual = testDB.Exec(nil, utils.ToCmdLine("PTTL", key))
   171  	intResult, ok := actual.(*protocol.IntReply)
   172  	if !ok {
   173  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   174  		return
   175  	}
   176  	if intResult.Code <= 0 || intResult.Code > 1000000 {
   177  		t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code))
   178  		return
   179  	}
   180  }
   181  
   182  func TestMSet(t *testing.T) {
   183  	testDB.Flush()
   184  	size := 10
   185  	keys := make([]string, size)
   186  	values := make([][]byte, size)
   187  	var args []string
   188  	for i := 0; i < size; i++ {
   189  		keys[i] = utils.RandString(10)
   190  		value := utils.RandString(10)
   191  		values[i] = []byte(value)
   192  		args = append(args, keys[i], value)
   193  	}
   194  	testDB.Exec(nil, utils.ToCmdLine2("MSET", args...))
   195  	actual := testDB.Exec(nil, utils.ToCmdLine2("MGET", keys...))
   196  	expected := protocol.MakeMultiBulkReply(values)
   197  	if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
   198  		t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   199  	}
   200  
   201  	// test mget with wrong type
   202  	key1 := utils.RandString(10)
   203  	testDB.Exec(nil, utils.ToCmdLine2("SET", key1, key1))
   204  	key2 := utils.RandString(10)
   205  	testDB.Exec(nil, utils.ToCmdLine2("LPush", key2, key2))
   206  	actual = testDB.Exec(nil, utils.ToCmdLine2("MGET", key1, key2))
   207  	arr := actual.(*protocol.MultiBulkReply)
   208  	if string(arr.Args[0]) != key1 {
   209  		t.Error("expected: " + key1 + ", actual: " + string(arr.Args[1]))
   210  	}
   211  	if len(arr.Args[1]) > 0 {
   212  		t.Error("expect null, actual: " + string(arr.Args[0]))
   213  	}
   214  }
   215  
   216  func TestIncr(t *testing.T) {
   217  	testDB.Flush()
   218  	size := 10
   219  	key := utils.RandString(10)
   220  	for i := 0; i < size; i++ {
   221  		testDB.Exec(nil, utils.ToCmdLine("INCR", key))
   222  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   223  		expected := protocol.MakeBulkReply([]byte(strconv.FormatInt(int64(i+1), 10)))
   224  		if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
   225  			t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   226  		}
   227  	}
   228  	for i := 0; i < size; i++ {
   229  		testDB.Exec(nil, utils.ToCmdLine("INCRBY", key, "-1"))
   230  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   231  		expected := protocol.MakeBulkReply([]byte(strconv.FormatInt(int64(size-i-1), 10)))
   232  		if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
   233  			t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   234  		}
   235  	}
   236  
   237  	testDB.Flush()
   238  	key = utils.RandString(10)
   239  	for i := 0; i < size; i++ {
   240  		testDB.Exec(nil, utils.ToCmdLine("INCRBY", key, "1"))
   241  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   242  		expected := protocol.MakeBulkReply([]byte(strconv.FormatInt(int64(i+1), 10)))
   243  		if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
   244  			t.Error("expected: " + string(expected.ToBytes()) + ", actual: " + string(actual.ToBytes()))
   245  		}
   246  	}
   247  	testDB.Remove(key)
   248  	for i := 0; i < size; i++ {
   249  		testDB.Exec(nil, utils.ToCmdLine("INCRBYFLOAT", key, "-1.0"))
   250  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   251  		expected := -i - 1
   252  		bulk, ok := actual.(*protocol.BulkReply)
   253  		if !ok {
   254  			t.Error(fmt.Sprintf("expected bulk protocol, actually %s", actual.ToBytes()))
   255  			return
   256  		}
   257  		val, err := strconv.ParseFloat(string(bulk.Arg), 10)
   258  		if err != nil {
   259  			t.Error(err)
   260  			return
   261  		}
   262  		if int(val) != expected {
   263  			t.Errorf("expect %d, actual: %d", expected, int(val))
   264  			return
   265  		}
   266  	}
   267  }
   268  
   269  func TestDecr(t *testing.T) {
   270  	testDB.Flush()
   271  	size := 10
   272  	key := utils.RandString(10)
   273  	for i := 0; i < size; i++ {
   274  		testDB.Exec(nil, utils.ToCmdLine("DECR", key))
   275  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   276  		asserts.AssertBulkReply(t, actual, strconv.Itoa(-i-1))
   277  	}
   278  	testDB.Remove(key)
   279  	for i := 0; i < size; i++ {
   280  		testDB.Exec(nil, utils.ToCmdLine("DECRBY", key, "1"))
   281  		actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
   282  		expected := -i - 1
   283  		bulk, ok := actual.(*protocol.BulkReply)
   284  		if !ok {
   285  			t.Error(fmt.Sprintf("expected bulk protocol, actually %s", actual.ToBytes()))
   286  			return
   287  		}
   288  		val, err := strconv.ParseFloat(string(bulk.Arg), 10)
   289  		if err != nil {
   290  			t.Error(err)
   291  			return
   292  		}
   293  		if int(val) != expected {
   294  			t.Errorf("expect %d, actual: %d", expected, int(val))
   295  			return
   296  		}
   297  	}
   298  }
   299  
   300  func TestGetEX(t *testing.T) {
   301  	testDB.Flush()
   302  	key := utils.RandString(10)
   303  	value := utils.RandString(10)
   304  	ttl := "1000"
   305  
   306  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value))
   307  
   308  	// Normal Get
   309  	actual := testDB.Exec(nil, utils.ToCmdLine("GETEX", key))
   310  	asserts.AssertBulkReply(t, actual, value)
   311  
   312  	// Test GetEX Key EX Seconds
   313  	actual = testDB.Exec(nil, utils.ToCmdLine("GETEX", key, "EX", ttl))
   314  	asserts.AssertBulkReply(t, actual, value)
   315  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
   316  	intResult, ok := actual.(*protocol.IntReply)
   317  	if !ok {
   318  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   319  		return
   320  	}
   321  	if intResult.Code <= 0 || intResult.Code > 1000 {
   322  		t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code))
   323  		return
   324  	}
   325  
   326  	// Test GetEX Key Persist
   327  	actual = testDB.Exec(nil, utils.ToCmdLine("GETEX", key, "PERSIST"))
   328  	asserts.AssertBulkReply(t, actual, value)
   329  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
   330  	intResult, ok = actual.(*protocol.IntReply)
   331  	if !ok {
   332  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   333  		return
   334  	}
   335  	if intResult.Code != -1 {
   336  		t.Error(fmt.Sprintf("expected int equals -1, actually %d", intResult.Code))
   337  		return
   338  	}
   339  
   340  	// Test GetEX Key NX Milliseconds
   341  	ttl = "1000000"
   342  	actual = testDB.Exec(nil, utils.ToCmdLine("GETEX", key, "PX", ttl))
   343  	asserts.AssertBulkReply(t, actual, value)
   344  	actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
   345  	intResult, ok = actual.(*protocol.IntReply)
   346  	if !ok {
   347  		t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes()))
   348  		return
   349  	}
   350  	if intResult.Code <= 0 || intResult.Code > 1000000 {
   351  		t.Error(fmt.Sprintf("expected int between [0, 1000000], actually %d", intResult.Code))
   352  		return
   353  	}
   354  }
   355  
   356  func TestGetSet(t *testing.T) {
   357  	testDB.Flush()
   358  	key := utils.RandString(10)
   359  	value := utils.RandString(10)
   360  
   361  	actual := testDB.Exec(nil, utils.ToCmdLine("GETSET", key, value))
   362  	_, ok := actual.(*protocol.NullBulkReply)
   363  	if !ok {
   364  		t.Errorf("expect null bulk protocol, get: %s", string(actual.ToBytes()))
   365  		return
   366  	}
   367  
   368  	value2 := utils.RandString(10)
   369  	actual = testDB.Exec(nil, utils.ToCmdLine("GETSET", key, value2))
   370  	asserts.AssertBulkReply(t, actual, value)
   371  	actual = testDB.Exec(nil, utils.ToCmdLine("GET", key))
   372  	asserts.AssertBulkReply(t, actual, value2)
   373  
   374  	// Test GetDel
   375  	actual = testDB.Exec(nil, utils.ToCmdLine("GETDEL", key))
   376  	asserts.AssertBulkReply(t, actual, value2)
   377  
   378  	actual = testDB.Exec(nil, utils.ToCmdLine("GETDEL", key))
   379  	_, ok = actual.(*protocol.NullBulkReply)
   380  	if !ok {
   381  		t.Errorf("expect null bulk protocol, get: %s", string(actual.ToBytes()))
   382  		return
   383  	}
   384  }
   385  
   386  func TestMSetNX(t *testing.T) {
   387  	testDB.Flush()
   388  	size := 10
   389  	args := make([]string, 0, size*2)
   390  	for i := 0; i < size; i++ {
   391  		str := utils.RandString(10)
   392  		args = append(args, str, str)
   393  	}
   394  	result := testDB.Exec(nil, utils.ToCmdLine2("MSETNX", args...))
   395  	asserts.AssertIntReply(t, result, 1)
   396  
   397  	result = testDB.Exec(nil, utils.ToCmdLine2("MSETNX", args[0:4]...))
   398  	asserts.AssertIntReply(t, result, 0)
   399  }
   400  
   401  func TestStrLen(t *testing.T) {
   402  	testDB.Flush()
   403  	key := utils.RandString(10)
   404  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   405  
   406  	actual := testDB.Exec(nil, utils.ToCmdLine("StrLen", key))
   407  	size, ok := actual.(*protocol.IntReply)
   408  	if !ok {
   409  		t.Errorf("expect int bulk protocol, get: %s", string(actual.ToBytes()))
   410  		return
   411  	}
   412  	asserts.AssertIntReply(t, size, 10)
   413  }
   414  
   415  func TestStrLen_KeyNotExist(t *testing.T) {
   416  	testDB.Flush()
   417  	key := utils.RandString(10)
   418  
   419  	actual := testDB.Exec(nil, utils.ToCmdLine("StrLen", key))
   420  	result, ok := actual.(*protocol.IntReply)
   421  	if !ok {
   422  		t.Errorf("expect null bulk protocol, get: %s", string(actual.ToBytes()))
   423  		return
   424  	}
   425  
   426  	asserts.AssertIntReply(t, result, 0)
   427  }
   428  
   429  func TestAppend_KeyExist(t *testing.T) {
   430  	testDB.Flush()
   431  	key := utils.RandString(10)
   432  	key2 := utils.RandString(10)
   433  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   434  
   435  	actual := testDB.Exec(nil, utils.ToCmdLine("Append", key, key2))
   436  	val, ok := actual.(*protocol.IntReply)
   437  	if !ok {
   438  		t.Errorf("expect nil bulk protocol, get: %s", string(actual.ToBytes()))
   439  		return
   440  	}
   441  	asserts.AssertIntReply(t, val, len(key)*2)
   442  }
   443  
   444  func TestAppend_KeyNotExist(t *testing.T) {
   445  	testDB.Flush()
   446  	key := utils.RandString(10)
   447  
   448  	actual := testDB.Exec(nil, utils.ToCmdLine("Append", key, key))
   449  	val, ok := actual.(*protocol.IntReply)
   450  	if !ok {
   451  		t.Errorf("expect nil bulk protocol, get: %s", string(actual.ToBytes()))
   452  		return
   453  	}
   454  	asserts.AssertIntReply(t, val, len(key))
   455  }
   456  
   457  func TestSetRange_StringExist(t *testing.T) {
   458  	testDB.Flush()
   459  	key := utils.RandString(10)
   460  	key2 := utils.RandString(3)
   461  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   462  
   463  	actual := testDB.Exec(nil, utils.ToCmdLine("SetRange", key, fmt.Sprint(0), key2))
   464  	val, ok := actual.(*protocol.IntReply)
   465  	if !ok {
   466  		t.Errorf("expect int bulk protocol, get: %s", string(actual.ToBytes()))
   467  		return
   468  	}
   469  
   470  	result := len(key2 + key[3:])
   471  	asserts.AssertIntReply(t, val, result)
   472  }
   473  
   474  func TestSetRange_StringExist_OffsetOutOfLen(t *testing.T) {
   475  	testDB.Flush()
   476  	key := utils.RandString(10)
   477  	key2 := utils.RandString(3)
   478  	emptyByteLen := 5
   479  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   480  
   481  	actual := testDB.Exec(nil, utils.ToCmdLine("SetRange", key, fmt.Sprint(len(key)+emptyByteLen), key2))
   482  	val, ok := actual.(*protocol.IntReply)
   483  	if !ok {
   484  		t.Errorf("expect int bulk protocol, get: %s", string(actual.ToBytes()))
   485  		return
   486  	}
   487  
   488  	result := len(key + string(make([]byte, emptyByteLen)) + key2)
   489  	asserts.AssertIntReply(t, val, result)
   490  }
   491  
   492  func TestSetRange_StringNotExist(t *testing.T) {
   493  	testDB.Flush()
   494  	key := utils.RandString(10)
   495  
   496  	actual := testDB.Exec(nil, utils.ToCmdLine("SetRange", key, fmt.Sprint(0), key))
   497  	val, ok := actual.(*protocol.IntReply)
   498  	if !ok {
   499  		t.Errorf("expect int bulk protocol, get: %s", string(actual.ToBytes()))
   500  		return
   501  	}
   502  	asserts.AssertIntReply(t, val, len(key))
   503  }
   504  
   505  func TestGetRange_StringExist(t *testing.T) {
   506  	testDB.Flush()
   507  	key := utils.RandString(10)
   508  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   509  
   510  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(len(key))))
   511  	val, ok := actual.(*protocol.BulkReply)
   512  	if !ok {
   513  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   514  		return
   515  	}
   516  
   517  	asserts.AssertBulkReply(t, val, key)
   518  }
   519  
   520  func TestGetRange_RangeLargeThenDataLen(t *testing.T) {
   521  	testDB.Flush()
   522  	key := utils.RandString(10)
   523  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   524  
   525  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(len(key)+2)))
   526  	val, ok := actual.(*protocol.BulkReply)
   527  	if !ok {
   528  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   529  		return
   530  	}
   531  
   532  	asserts.AssertBulkReply(t, val, key)
   533  }
   534  
   535  func TestGetRange_StringNotExist(t *testing.T) {
   536  	testDB.Flush()
   537  	key := utils.RandString(10)
   538  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(len(key))))
   539  	val, ok := actual.(*protocol.NullBulkReply)
   540  	if !ok {
   541  		t.Errorf("expect nil bulk protocol, get: %s", string(actual.ToBytes()))
   542  		return
   543  	}
   544  
   545  	asserts.AssertNullBulk(t, val)
   546  }
   547  
   548  func TestGetRange_StringExist_GetPartial(t *testing.T) {
   549  	testDB.Flush()
   550  	key := utils.RandString(10)
   551  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   552  
   553  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(len(key)/2)))
   554  	val, ok := actual.(*protocol.BulkReply)
   555  	if !ok {
   556  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   557  		return
   558  	}
   559  
   560  	asserts.AssertBulkReply(t, val, key[:(len(key)/2)+1])
   561  }
   562  
   563  func TestGetRange_StringExist_EndIdxOutOfRange(t *testing.T) {
   564  	testDB.Flush()
   565  	key := utils.RandString(10)
   566  	emptyByteLen := 2
   567  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   568  
   569  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(len(key)+emptyByteLen)))
   570  	val, ok := actual.(*protocol.BulkReply)
   571  	if !ok {
   572  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   573  		return
   574  	}
   575  
   576  	asserts.AssertBulkReply(t, val, key)
   577  }
   578  
   579  func TestGetRange_StringExist_StartIdxEndIdxAreSame(t *testing.T) {
   580  	testDB.Flush()
   581  	key := utils.RandString(10)
   582  	emptyByteLen := 2
   583  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   584  
   585  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(len(key)+emptyByteLen), fmt.Sprint(len(key)+emptyByteLen)))
   586  	val, ok := actual.(*protocol.NullBulkReply)
   587  	if !ok {
   588  		t.Errorf("expect nil bulk protocol, get: %s", string(actual.ToBytes()))
   589  		return
   590  	}
   591  
   592  	asserts.AssertNullBulk(t, val)
   593  }
   594  
   595  func TestGetRange_StringExist_StartIdxGreaterThanEndIdx(t *testing.T) {
   596  	testDB.Flush()
   597  	key := utils.RandString(10)
   598  
   599  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(len(key)+1), fmt.Sprint(len(key))))
   600  	val, ok := actual.(*protocol.NullBulkReply)
   601  	if !ok {
   602  		t.Errorf("expect nil bulk protocol, get: %s", string(actual.ToBytes()))
   603  		return
   604  	}
   605  
   606  	asserts.AssertNullBulk(t, val)
   607  }
   608  
   609  func TestGetRange_StringExist_StartIdxEndIdxAreNegative(t *testing.T) {
   610  	testDB.Flush()
   611  	key := utils.RandString(10)
   612  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   613  
   614  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(-1*len(key)), fmt.Sprint(-1)))
   615  	val, ok := actual.(*protocol.BulkReply)
   616  	if !ok {
   617  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   618  		return
   619  	}
   620  
   621  	asserts.AssertBulkReply(t, val, key)
   622  }
   623  
   624  func TestGetRange_StringExist_StartIdxNegative(t *testing.T) {
   625  	testDB.Flush()
   626  	key := utils.RandString(10)
   627  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   628  
   629  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(-1*len(key)), fmt.Sprint(len(key)/2)))
   630  	val, ok := actual.(*protocol.BulkReply)
   631  	if !ok {
   632  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   633  		return
   634  	}
   635  
   636  	asserts.AssertBulkReply(t, val, key[0:(len(key)/2)+1])
   637  }
   638  
   639  func TestGetRange_StringExist_EndIdxNegative(t *testing.T) {
   640  	testDB.Flush()
   641  	key := utils.RandString(10)
   642  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   643  
   644  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(-len(key)/2)))
   645  	val, ok := actual.(*protocol.BulkReply)
   646  	if !ok {
   647  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   648  		return
   649  	}
   650  
   651  	asserts.AssertBulkReply(t, val, key[0:(len(key)/2)+1])
   652  }
   653  
   654  func TestGetRange_StringExist_StartIsOutOfRange(t *testing.T) {
   655  	testDB.Flush()
   656  	key := utils.RandString(10)
   657  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   658  
   659  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(-len(key)-3), fmt.Sprint(len(key))))
   660  	val, ok := actual.(*protocol.NullBulkReply)
   661  	if !ok {
   662  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   663  		return
   664  	}
   665  
   666  	asserts.AssertNullBulk(t, val)
   667  }
   668  
   669  func TestGetRange_StringExist_EndIdxIsOutOfRange(t *testing.T) {
   670  	testDB.Flush()
   671  	key := utils.RandString(10)
   672  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   673  
   674  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), fmt.Sprint(-len(key)-3)))
   675  	val, ok := actual.(*protocol.NullBulkReply)
   676  	if !ok {
   677  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   678  		return
   679  	}
   680  
   681  	asserts.AssertNullBulk(t, val)
   682  }
   683  
   684  func TestGetRange_StringExist_StartIdxGreaterThanDataLen(t *testing.T) {
   685  	testDB.Flush()
   686  	key := utils.RandString(10)
   687  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   688  
   689  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(len(key)+1), fmt.Sprint(0)))
   690  	val, ok := actual.(*protocol.NullBulkReply)
   691  	if !ok {
   692  		t.Errorf("expect bulk protocol, get: %s", string(actual.ToBytes()))
   693  		return
   694  	}
   695  
   696  	asserts.AssertNullBulk(t, val)
   697  }
   698  
   699  func TestGetRange_StringExist_StartIdxIncorrectFormat(t *testing.T) {
   700  	testDB.Flush()
   701  	key := utils.RandString(10)
   702  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   703  	incorrectValue := "incorrect"
   704  
   705  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, incorrectValue, fmt.Sprint(0)))
   706  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   707  }
   708  
   709  func TestGetRange_StringExist_EndIdxIncorrectFormat(t *testing.T) {
   710  	testDB.Flush()
   711  	key := utils.RandString(10)
   712  	testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   713  	incorrectValue := "incorrect"
   714  
   715  	actual := testDB.Exec(nil, utils.ToCmdLine("GetRange", key, fmt.Sprint(0), incorrectValue))
   716  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   717  }
   718  
   719  func TestSetBit(t *testing.T) {
   720  	testDB.Flush()
   721  	key := utils.RandString(10)
   722  	actual := testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "15", "1"))
   723  	asserts.AssertIntReply(t, actual, 0)
   724  	actual = testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "15", "0"))
   725  	asserts.AssertIntReply(t, actual, 1)
   726  	testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "13", "1"))
   727  	actual = testDB.Exec(nil, utils.ToCmdLine("GetBit", key, "13"))
   728  	asserts.AssertIntReply(t, actual, 1)
   729  	actual = testDB.Exec(nil, utils.ToCmdLine("GetBit", key+"1", "13")) // test not exist key
   730  	asserts.AssertIntReply(t, actual, 0)
   731  
   732  	actual = testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "13", "a"))
   733  	asserts.AssertErrReply(t, actual, "ERR bit is not an integer or out of range")
   734  	actual = testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "a", "1"))
   735  	asserts.AssertErrReply(t, actual, "ERR bit offset is not an integer or out of range")
   736  	actual = testDB.Exec(nil, utils.ToCmdLine("GetBit", key, "a"))
   737  	asserts.AssertErrReply(t, actual, "ERR bit offset is not an integer or out of range")
   738  
   739  	key2 := utils.RandString(11)
   740  	testDB.Exec(nil, utils.ToCmdLine("rpush", key2, "1"))
   741  	actual = testDB.Exec(nil, utils.ToCmdLine("SetBit", key2, "15", "0"))
   742  	asserts.AssertErrReply(t, actual, "WRONGTYPE Operation against a key holding the wrong kind of value")
   743  	actual = testDB.Exec(nil, utils.ToCmdLine("GetBit", key2, "15"))
   744  	asserts.AssertErrReply(t, actual, "WRONGTYPE Operation against a key holding the wrong kind of value")
   745  }
   746  
   747  func TestBitCount(t *testing.T) {
   748  	testDB.Flush()
   749  	key := utils.RandString(10)
   750  	testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "15", "1"))
   751  	testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "13", "1"))
   752  	actual := testDB.Exec(nil, utils.ToCmdLine("BitCount", key))
   753  	asserts.AssertIntReply(t, actual, 2)
   754  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "14", "15", "BIT"))
   755  	asserts.AssertIntReply(t, actual, 1)
   756  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "16", "20", "BIT"))
   757  	asserts.AssertIntReply(t, actual, 0)
   758  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "1", "1", "BYTE"))
   759  	asserts.AssertIntReply(t, actual, 2)
   760  
   761  	key2 := utils.RandString(11)
   762  	testDB.Exec(nil, utils.ToCmdLine("rpush", key2, "1"))
   763  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key2))
   764  	asserts.AssertErrReply(t, actual, "WRONGTYPE Operation against a key holding the wrong kind of value")
   765  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key+"a"))
   766  	asserts.AssertIntReply(t, actual, 0)
   767  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "14", "15", "B"))
   768  	asserts.AssertErrReply(t, actual, "ERR syntax error")
   769  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "14", "A"))
   770  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   771  	actual = testDB.Exec(nil, utils.ToCmdLine("BitCount", key, "A", "-1"))
   772  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   773  }
   774  
   775  func TestBitPos(t *testing.T) {
   776  	testDB.Flush()
   777  	key := utils.RandString(10)
   778  	testDB.Exec(nil, utils.ToCmdLine("SetBit", key, "15", "1"))
   779  	actual := testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "0"))
   780  	asserts.AssertIntReply(t, actual, 0)
   781  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1"))
   782  	asserts.AssertIntReply(t, actual, 15)
   783  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1", "0", "-1", "BIT"))
   784  	asserts.AssertIntReply(t, actual, 15)
   785  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1", "1", "1", "BYTE"))
   786  	asserts.AssertIntReply(t, actual, 15)
   787  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "0", "1", "1", "BYTE"))
   788  	asserts.AssertIntReply(t, actual, 8)
   789  
   790  	key2 := utils.RandString(12)
   791  	testDB.Exec(nil, utils.ToCmdLine("rpush", key2, "1"))
   792  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key2, "1"))
   793  	asserts.AssertErrReply(t, actual, "WRONGTYPE Operation against a key holding the wrong kind of value")
   794  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key+"a", "1"))
   795  	asserts.AssertIntReply(t, actual, -1)
   796  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1", "1", "15", "B"))
   797  	asserts.AssertErrReply(t, actual, "ERR syntax error")
   798  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1", "14", "A"))
   799  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   800  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "1", "a", "14"))
   801  	asserts.AssertErrReply(t, actual, "ERR value is not an integer or out of range")
   802  	actual = testDB.Exec(nil, utils.ToCmdLine("BitPos", key, "-1"))
   803  	asserts.AssertErrReply(t, actual, "ERR bit is not an integer or out of range")
   804  }
   805  
   806  func TestRandomkey(t *testing.T) {
   807  	testDB.Flush()
   808  	for i := 0; i < 10; i++ {
   809  		key := utils.RandString(10)
   810  		testDB.Exec(nil, utils.ToCmdLine2("SET", key, key))
   811  	}
   812  	actual := testDB.Exec(nil, utils.ToCmdLine("Randomkey"))
   813  	asserts.AssertNotError(t, actual)
   814  }