Skip to content

Commit

Permalink
refactor: 重命名seek到头部和尾部的方法以避免理解错误
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Oct 24, 2023
1 parent 5570a3e commit e6a2ab4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions bit_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func (x *BitBuffer) SetBytes(bytes []byte) *BitBuffer {
return x
}

// SeekFirst 移动操作指针
func (x *BitBuffer) SeekFirst() *BitBuffer {
// SeekHead 移动操作指针到头部
func (x *BitBuffer) SeekHead() *BitBuffer {
return x.Seek(0)
}

// SeekLast 移动指针到最后一个位置
func (x *BitBuffer) SeekLast() *BitBuffer {
// SeekTail 移动指针到尾部
func (x *BitBuffer) SeekTail() *BitBuffer {
return x.Seek(len(x.bytes) * 8)
}

// Seek 移动指针
// Seek 移动指针到指定的position
func (x *BitBuffer) Seek(offset int) *BitBuffer {

// 保证空间是足够的
Expand Down Expand Up @@ -114,8 +114,8 @@ func (x *BitBuffer) ReadBit() int {
return int(value)
}

// IsAtLast 是否已经存在最后一个位置了
func (x *BitBuffer) IsAtLast() bool {
// IsTail 是否已经存在尾部了
func (x *BitBuffer) IsTail() bool {
return x.GetSeek() == x.Capacity()
}

Expand Down
28 changes: 14 additions & 14 deletions bit_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,47 @@ func TestBitBuffer_Seek(t *testing.T) {
assert.Equal(t, "111111110", buffer.Seek(9).ToBinaryString())
assert.Equal(t, "111111110000000", buffer.Seek(15).ToBinaryString())
assert.Equal(t, "1111111100000000", buffer.Seek(16).ToBinaryString())
assert.Equal(t, "101111110000000000000000", buffer.Seek(1).WriteBit(0).SeekLast().ToBinaryString())
assert.Equal(t, "101111110000000000000000", buffer.Seek(1).WriteBit(0).SeekTail().ToBinaryString())
}

func TestBitBuffer_SeekFirst(t *testing.T) {
func TestBitBuffer_SeekHead(t *testing.T) {
buffer := New().WriteByte(0xFF)
assert.Equal(t, "11111111", buffer.ToBinaryString())
assert.Equal(t, "", buffer.SeekFirst().ToBinaryString())
assert.Equal(t, "", buffer.SeekHead().ToBinaryString())
assert.Equal(t, "1", buffer.WriteBit(0x1).ToBinaryString())
}

func TestBitBuffer_SeekLast(t *testing.T) {
func TestBitBuffer_SeekTail(t *testing.T) {
buffer := New().WriteByte(0xFF)
assert.Equal(t, "11111111", buffer.ToBinaryString())
assert.Equal(t, "11111111", buffer.SeekLast().ToBinaryString())
assert.Equal(t, "11111111", buffer.SeekTail().ToBinaryString())
assert.Equal(t, "111111111", buffer.WriteBit(0x1).ToBinaryString())
}

func TestBitBuffer_ReadBit(t *testing.T) {
buffer := New().WriteByte(0xF0).SeekFirst()
buffer := New().WriteByte(0xF0).SeekHead()

assert.Equal(t, 1, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 1, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 1, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 1, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 0, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 0, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 0, buffer.ReadBit())
assert.False(t, buffer.IsAtLast())
assert.False(t, buffer.IsTail())

assert.Equal(t, 0, buffer.ReadBit())
assert.True(t, buffer.IsAtLast())
assert.True(t, buffer.IsTail())
}

0 comments on commit e6a2ab4

Please sign in to comment.