mirror of
https://github.com/asticode/go-astikit.git
synced 2026-04-22 14:57:10 +08:00
Added bit flags
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package astikit
|
||||
|
||||
type BitFlags uint64
|
||||
|
||||
func (fs BitFlags) Add(f uint64) uint64 { return uint64(fs) | f }
|
||||
|
||||
func (fs BitFlags) Del(f uint64) uint64 { return uint64(fs) &^ f }
|
||||
|
||||
func (fs BitFlags) Has(f uint64) bool { return uint64(fs)&f > 0 }
|
||||
@@ -0,0 +1,23 @@
|
||||
package astikit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBitFlags(t *testing.T) {
|
||||
f := BitFlags(2 | 4)
|
||||
r := f.Add(1)
|
||||
if e, g := uint64(7), r; e != g {
|
||||
t.Errorf("expected %d, got %d", e, g)
|
||||
}
|
||||
r = f.Del(2)
|
||||
if e, g := uint64(4), r; e != g {
|
||||
t.Errorf("expected %d, got %d", e, g)
|
||||
}
|
||||
if e, g := false, f.Has(1); e != g {
|
||||
t.Errorf("expected %v, got %v", e, g)
|
||||
}
|
||||
if e, g := true, f.Has(4); e != g {
|
||||
t.Errorf("expected %v, got %v", e, g)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user