forked from p93542168/wheat-cache
!87 test(structure-val): add perf test
Merge pull request !87 from bandl/auto-5261189-master-1636014762802
This commit is contained in:
commit
4c9ab82123
|
@ -147,26 +147,28 @@ func (v *Value) ChangeValueLength(f func()) UpdateLength {
|
||||||
|
|
||||||
func (v *Value) SetByte(offset int, val bool) {
|
func (v *Value) SetByte(offset int, val bool) {
|
||||||
v.onType = DynamicNull // 位图使用无类型
|
v.onType = DynamicNull // 位图使用无类型
|
||||||
b := byte(0)
|
|
||||||
if val {
|
|
||||||
b = byte(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.length >= offset {
|
// 扩容
|
||||||
v.val[offset] = b
|
if len(v.val) <= offset/8 {
|
||||||
|
newByte := make([]byte, (offset/8)+1)
|
||||||
|
copy(newByte, v.val[:len(v.val)])
|
||||||
|
v.val = newByte
|
||||||
|
v.length = len(v.val)
|
||||||
|
}
|
||||||
|
if val {
|
||||||
|
// true 位
|
||||||
|
v.val[offset/8] |= (0b1 << (offset % 8))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newByte := make([]byte, offset+1)
|
// false 位
|
||||||
newByte[offset] = b
|
v.val[offset/8] ^= (0b1 << (offset % 8))
|
||||||
copy(newByte, v.val[:v.length])
|
|
||||||
v.val = newByte
|
|
||||||
v.length = len(newByte)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Value) GetByte(offset int) (bool, error) {
|
func (v *Value) GetByte(offset int) (bool, error) {
|
||||||
if v.length >= offset {
|
if len(v.val) >= offset/8 {
|
||||||
return v.val[offset] == byte(1), nil
|
// 采用 & 来运算 是否为 true
|
||||||
|
return v.val[offset/8]&(0b1<<(offset%8)) != 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, errorx.New("the maximum length is exceeded")
|
return false, errorx.New("the maximum length is exceeded")
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package structure
|
package structure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -126,4 +127,20 @@ func TestValue_SetByte(t *testing.T) {
|
||||||
v, err = value.GetByte(10001)
|
v, err = value.GetByte(10001)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, v, true)
|
require.Equal(t, v, true)
|
||||||
|
require.Equal(t, value.GetSize(), (10001/8)+1+16)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValue_SetByteWei(t *testing.T) {
|
||||||
|
k := make([]byte, 100)
|
||||||
|
offset := 700
|
||||||
|
k[offset/8] = 0b00000001
|
||||||
|
k[offset/8] |= 0b1 << (offset % 8)
|
||||||
|
|
||||||
|
fmt.Printf("%b\n", k[offset/8])
|
||||||
|
|
||||||
|
fmt.Printf("%v", (k[offset/8]&(0b1<<(offset%8))) != 0)
|
||||||
|
|
||||||
|
k[offset/8] ^= 0b1 << (offset % 8)
|
||||||
|
|
||||||
|
fmt.Printf("%v", (k[offset/8]&(0b1<<(offset%8))) != 0)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue