change v0.0.2

This commit is contained in:
Anatoly Prohacky 2023-03-04 17:30:21 +10:00
parent 894bc6617c
commit 781b198f8c
3 changed files with 42 additions and 26 deletions

View File

@ -12,20 +12,11 @@ type AclBit int64
func New(n ...NumBit) AclBit {
var i64 AclBit
for _, n0 := range n {
i64 = SetTrue(i64, n0)
i64.SetTrue(n0)
}
return i64
}
// verytify Num bit (max range 0 - 62 , summary 63 bit)
func veryNumBit(n NumBit) bool {
if n >= 0 && n < 63 {
return true
}
return false
}
// unite slice int to int64
func Unite(a ...AclBit) AclBit {
var endacl AclBit
@ -37,25 +28,21 @@ func Unite(a ...AclBit) AclBit {
}
// set acl bit in true
func SetTrue(a AclBit, n NumBit) AclBit {
func (a *AclBit) SetTrue(n NumBit) {
if veryNumBit(n) {
return a | (1 << n)
*a = *a | (1 << n)
}
return a
}
// set acl bit in false
func SetFalse(a AclBit, n NumBit) AclBit {
func (a *AclBit) SetFalse(n NumBit) {
if veryNumBit(n) {
return a &^ (1 << n)
*a = *a &^ (1 << n)
}
return a
}
// verify bit return true or false
func Verify(a AclBit, n NumBit) bool {
func (a AclBit) Verify(n NumBit) bool {
if veryNumBit(n) {
var msk AclBit = 1 << n
if (a & msk) == msk {
@ -103,3 +90,12 @@ func Int64(i any) int64 {
return i64
}
}
// verytify Num bit (max range 0 - 62 , summary 63 bit)
func veryNumBit(n NumBit) bool {
if n >= 0 && n < 63 {
return true
}
return false
}

View File

@ -11,13 +11,13 @@ func Test_Acl(t *testing.T) {
t.Run("Valid AclBits", func(t *testing.T) {
for i := 0; i < 63; i++ {
a := acl.New(acl.NumBit(i))
assert.Equal(t, true, acl.Verify(a, acl.NumBit(i)))
assert.Equal(t, true, a.Verify(acl.NumBit(i)))
}
})
t.Run("NotValid AclBits", func(t *testing.T) {
for i := -63; i == 0; i++ {
a := acl.New(acl.NumBit(i))
assert.NotEqual(t, true, acl.Verify(a, acl.NumBit(i)))
assert.NotEqual(t, true, a.Verify(acl.NumBit(i)))
}
})
}

30
main.go
View File

@ -7,14 +7,34 @@ import (
)
func main() {
a := acl.New(63, 63, 63, 3, 62, 62, 64, 64, 5654, 5435, -12234)
a := acl.New(0, 1, 2, 3, 4, 32, 60, 61, 62, 63)
fmt.Println(a)
fmt.Println(a.String())
fmt.Println(a.Int64())
b := fmt.Sprintf("%b", a)
fmt.Println(b)
fmt.Println(acl.Verify(a, 63))
fmt.Println(acl.Verify(a, 62))
fmt.Println(acl.Verify(a, -1))
fmt.Println(acl.Verify(a, 3))
fmt.Println(a.Verify(0))
a.SetFalse(0)
fmt.Println(a.Verify(0))
fmt.Println(a.Verify(1))
a.SetFalse(1)
fmt.Println(a.Verify(1))
fmt.Println(a.Verify(2))
a.SetFalse(2)
fmt.Println(a.Verify(2))
fmt.Println(a.Verify(3))
a.SetFalse(3)
fmt.Println(a.Verify(3))
fmt.Println(a.Verify(4))
a.SetFalse(4)
fmt.Println(a.Verify(4))
fmt.Println(a.Verify(5))
a.SetTrue(5)
fmt.Println(a.Verify(5))
fmt.Println(a.Verify(32))
a.SetFalse(32)
fmt.Println(a.Verify(32))
fmt.Println(a.Verify(62))
a.SetFalse(62)
fmt.Println(a.Verify(62))
}