Skip to content

Commit

Permalink
add SetBit()
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Dec 31, 2024
1 parent 7f479c1 commit c6c0e85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/trie/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,18 @@ func (b *BitArray) SetUint64(length uint8, data uint64) *BitArray {
return b
}

// Sets the bit array to a single bit.
func (b *BitArray) SetBit(bit bool) *BitArray {
b.len = 1
if bit {
b.words[0] = 1
} else {
b.words[0] = 0
}
b.truncateToLength()
return b
}

// Returns the length of the encoded bit array in bytes.
func (b *BitArray) EncodedLen() uint {
return b.byteCount() + 1
Expand Down
34 changes: 34 additions & 0 deletions core/trie/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1713,3 +1713,37 @@ func TestSetFeltValidation(t *testing.T) {
})
}
}

func TestSetBit(t *testing.T) {
tests := []struct {
name string
bit bool
want BitArray
}{
{
name: "set bit false",
bit: false,
want: BitArray{
len: 1,
words: [4]uint64{0, 0, 0, 0},
},
},
{
name: "set bit true",
bit: true,
want: BitArray{
len: 1,
words: [4]uint64{1, 0, 0, 0},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := new(BitArray).SetBit(tt.bit)
if !got.Equal(&tt.want) {
t.Errorf("SetBit(%v) = %v, want %v", tt.bit, got, tt.want)
}
})
}
}

0 comments on commit c6c0e85

Please sign in to comment.