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) {
|
||||
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
|
||||
}
|
||||
|
||||
newByte := make([]byte, offset+1)
|
||||
newByte[offset] = b
|
||||
copy(newByte, v.val[:v.length])
|
||||
v.val = newByte
|
||||
v.length = len(newByte)
|
||||
// false 位
|
||||
v.val[offset/8] ^= (0b1 << (offset % 8))
|
||||
}
|
||||
|
||||
func (v *Value) GetByte(offset int) (bool, error) {
|
||||
if v.length >= offset {
|
||||
return v.val[offset] == byte(1), nil
|
||||
if len(v.val) >= offset/8 {
|
||||
// 采用 & 来运算 是否为 true
|
||||
return v.val[offset/8]&(0b1<<(offset%8)) != 0, nil
|
||||
}
|
||||
|
||||
return false, errorx.New("the maximum length is exceeded")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package structure
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
|
@ -126,4 +127,20 @@ func TestValue_SetByte(t *testing.T) {
|
|||
v, err = value.GetByte(10001)
|
||||
require.NoError(t, err)
|
||||
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