2014-06-23 16:09:05 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmpBytes(a []byte, b []byte) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := range a {
|
|
|
|
if n != b[i] {
|
2014-06-26 12:27:25 +04:00
|
|
|
println("diff !")
|
|
|
|
println(i)
|
|
|
|
println(n)
|
|
|
|
println(b[i])
|
2014-06-23 16:09:05 +04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-24 08:00:41 +04:00
|
|
|
func newBytes(bitLen int32) []byte {
|
|
|
|
bytes := bitLen / 8
|
|
|
|
if bitLen%8 > 0 {
|
|
|
|
bytes++
|
|
|
|
}
|
|
|
|
|
|
|
|
return make([]byte, bytes, bytes)
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:09:05 +04:00
|
|
|
func TestBinary(t *testing.T) {
|
2014-06-26 12:27:25 +04:00
|
|
|
testSimple(t)
|
2014-06-24 08:00:41 +04:00
|
|
|
testSimpleII(t)
|
2014-06-26 12:27:25 +04:00
|
|
|
testOp(t)
|
2014-06-24 08:00:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSimple(t *testing.T) {
|
2014-06-23 16:09:05 +04:00
|
|
|
db := getTestDB()
|
|
|
|
|
|
|
|
key := []byte("test_bin")
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, 100); v != 0 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ori, _ := db.BSetBit(key, 50, 1); ori != 0 {
|
|
|
|
t.Error(ori)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, 50); v != 1 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ori, _ := db.BSetBit(key, 50, 0); ori != 1 {
|
|
|
|
t.Error(ori)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, 50); v != 0 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.BSetBit(key, 7, 1)
|
|
|
|
db.BSetBit(key, 8, 1)
|
|
|
|
db.BSetBit(key, 9, 1)
|
|
|
|
db.BSetBit(key, 10, 1)
|
|
|
|
|
|
|
|
if sum, _ := db.BCount(key, 0, -1); sum != 4 {
|
|
|
|
t.Error(sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _ := db.BGet(key)
|
|
|
|
if cmpBytes(data, []byte{0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00}) {
|
|
|
|
t.Error(data)
|
|
|
|
}
|
2014-06-24 08:00:41 +04:00
|
|
|
|
2014-06-26 12:27:25 +04:00
|
|
|
if tail, _ := db.BTail(key); tail != int32(50) {
|
2014-06-24 08:00:41 +04:00
|
|
|
t.Error(tail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSimpleII(t *testing.T) {
|
|
|
|
db := getTestDB()
|
2014-06-26 12:27:25 +04:00
|
|
|
key := []byte("test_bin_2")
|
2014-06-24 08:00:41 +04:00
|
|
|
|
|
|
|
pos := int32(1234567)
|
|
|
|
if ori, _ := db.BSetBit(key, pos, 1); ori != 0 {
|
|
|
|
t.Error(ori)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, pos); v != 1 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, pos-1); v != 0 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, _ := db.BGetBit(key, pos+1); v != 0 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
2014-06-26 12:27:25 +04:00
|
|
|
if tail, _ := db.BTail(key); tail != pos {
|
2014-06-24 08:00:41 +04:00
|
|
|
t.Error(tail)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _ := db.BGet(key)
|
|
|
|
stdData := newBytes(pos + 1)
|
|
|
|
stdData[pos/8] = uint8(1 << (uint(pos) % 8))
|
|
|
|
|
|
|
|
if cmpBytes(data, stdData) {
|
|
|
|
t.Error(len(data))
|
|
|
|
}
|
2014-06-26 12:27:25 +04:00
|
|
|
|
|
|
|
if drop, _ := db.BDelete(key); drop != 1 {
|
|
|
|
t.Error(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, _ := db.BGet(key); data != nil {
|
|
|
|
t.Error(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOp(t *testing.T) {
|
|
|
|
db := getTestDB()
|
|
|
|
|
|
|
|
dstKey := []byte("test_bin_op")
|
|
|
|
|
|
|
|
k0 := []byte("op_0")
|
|
|
|
k1 := []byte("op_10")
|
|
|
|
//k2 := []byte("op_11")
|
|
|
|
srcKeys := [][]byte{k0, k1}
|
|
|
|
|
|
|
|
/*
|
|
|
|
<seg> - <high> ... <low>
|
|
|
|
0 - [10000000] ... [00000001]
|
|
|
|
1 - nil
|
|
|
|
2 - [00000000] ... [11111111] ... [00000000]
|
|
|
|
3 - [01010101] ... [10000001] [10101010]
|
|
|
|
4 - [10000000] ... [00000000]
|
|
|
|
...
|
|
|
|
*/
|
|
|
|
// (k0 - seg:0)
|
|
|
|
db.BSetBit(k0, int32(0), 1)
|
|
|
|
db.BSetBit(k0, int32(segBitSize-1), 1)
|
|
|
|
// (k0 - seg:2)
|
|
|
|
pos := segBitSize*2 + segBitSize/2
|
|
|
|
for i := uint32(0); i < 8; i++ {
|
|
|
|
db.BSetBit(k0, int32(pos+i), 1)
|
|
|
|
}
|
|
|
|
// (k0 - seg:3)
|
|
|
|
pos = segBitSize * 3
|
|
|
|
db.BSetBit(k0, int32(pos+8), 1)
|
|
|
|
db.BSetBit(k0, int32(pos+15), 1)
|
|
|
|
for i := uint32(1); i < 8; i += 2 {
|
|
|
|
db.BSetBit(k0, int32(pos+i), 1)
|
|
|
|
}
|
|
|
|
pos = segBitSize*4 - 8
|
|
|
|
for i := uint32(0); i < 8; i += 2 {
|
|
|
|
db.BSetBit(k0, int32(pos+i), 1)
|
|
|
|
}
|
|
|
|
// (k0 - seg:3)
|
|
|
|
db.BSetBit(k0, int32(segBitSize*5-1), 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
0 - nil
|
|
|
|
1 - [00000001] ... [10000000]
|
|
|
|
2 - nil
|
|
|
|
3 - [10101010] ... [10000001] [01010101]
|
|
|
|
...
|
|
|
|
*/
|
|
|
|
// (k1 - seg:1)
|
|
|
|
db.BSetBit(k1, int32(segBitSize+7), 1)
|
|
|
|
db.BSetBit(k1, int32(segBitSize*2-8), 1)
|
|
|
|
// (k0 - seg:3)
|
|
|
|
pos = segBitSize * 3
|
|
|
|
db.BSetBit(k1, int32(pos+8), 1)
|
|
|
|
db.BSetBit(k1, int32(pos+15), 1)
|
|
|
|
for i := uint32(0); i < 8; i += 2 {
|
|
|
|
db.BSetBit(k0, int32(pos+i), 1)
|
|
|
|
}
|
|
|
|
pos = segBitSize*4 - 8
|
|
|
|
for i := uint32(1); i < 8; i += 2 {
|
|
|
|
db.BSetBit(k0, int32(pos+i), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdData []byte
|
|
|
|
var data []byte
|
|
|
|
var tmpKeys [][]byte
|
|
|
|
|
|
|
|
// op - or
|
|
|
|
db.BOperation(OPor, dstKey, srcKeys...)
|
|
|
|
|
|
|
|
stdData = make([]byte, 5*segByteSize)
|
|
|
|
stdData[0] = uint8(0x01)
|
|
|
|
stdData[segByteSize-1] = uint8(0x80)
|
|
|
|
stdData[segByteSize] = uint8(0x80)
|
|
|
|
stdData[segByteSize*2-1] = uint8(0x01)
|
|
|
|
stdData[segByteSize*2+segByteSize/2] = uint8(0xff)
|
|
|
|
stdData[segByteSize*3] = uint8(0xff)
|
|
|
|
stdData[segByteSize*3+1] = uint8(0x81)
|
|
|
|
stdData[segByteSize*4-1] = uint8(0xff)
|
|
|
|
stdData[segByteSize*5-1] = uint8(0x80)
|
|
|
|
|
|
|
|
data, _ = db.BGet(dstKey)
|
|
|
|
if cmpBytes(data, stdData) {
|
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpKeys = [][]byte{k0, dstKey, k1}
|
|
|
|
db.BOperation(OPor, dstKey, tmpKeys...)
|
|
|
|
|
|
|
|
data, _ = db.BGet(dstKey)
|
|
|
|
if cmpBytes(data, stdData) {
|
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// op - and
|
|
|
|
db.BOperation(OPand, dstKey, srcKeys...)
|
|
|
|
|
|
|
|
stdData = make([]byte, 5*segByteSize)
|
|
|
|
stdData[segByteSize*3+1] = uint8(0x81)
|
|
|
|
|
|
|
|
data, _ = db.BGet(dstKey)
|
|
|
|
if cmpBytes(data, stdData) {
|
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpKeys = [][]byte{k0, dstKey, k1}
|
|
|
|
db.BOperation(OPand, dstKey, tmpKeys...)
|
|
|
|
|
|
|
|
data, _ = db.BGet(dstKey)
|
|
|
|
if cmpBytes(data, stdData) {
|
|
|
|
t.Fatal(false)
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:09:05 +04:00
|
|
|
}
|