Add IsNotSet()

This commit is contained in:
Tevin Zhang 2020-07-16 14:46:58 +08:00
parent 3a33536c80
commit 8ae5c93531
No known key found for this signature in database
GPG Key ID: EE7DA2A50F0960FB
3 changed files with 22 additions and 6 deletions

View File

@ -17,6 +17,7 @@ cond := abool.New() // default to false
cond.Set() // Sets to true cond.Set() // Sets to true
cond.IsSet() // Returns true cond.IsSet() // Returns true
cond.UnSet() // Sets to false cond.UnSet() // Sets to false
cond.IsNotSet() // Returns true
cond.SetTo(any) // Sets to whatever you want cond.SetTo(any) // Sets to whatever you want
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
cond.Toggle() // Inverts the boolean then returns the value before inverting cond.Toggle() // Inverts the boolean then returns the value before inverting

View File

@ -38,6 +38,11 @@ func (ab *AtomicBool) IsSet() bool {
return atomic.LoadInt32((*int32)(ab))&1 == 1 return atomic.LoadInt32((*int32)(ab))&1 == 1
} }
// IsNotSet returns whether the Boolean is false.
func (ab *AtomicBool) IsNotSet() bool {
return !ab.IsSet()
}
// SetTo sets the boolean with given Boolean. // SetTo sets the boolean with given Boolean.
func (ab *AtomicBool) SetTo(yes bool) { func (ab *AtomicBool) SetTo(yes bool) {
if yes { if yes {

View File

@ -25,6 +25,10 @@ func TestBool(t *testing.T) {
t.Fatal("Empty value of AtomicBool should be false") t.Fatal("Empty value of AtomicBool should be false")
} }
if v.IsSet() == v.IsNotSet() {
t.Fatal("AtomicBool.IsNotSet() should be the opposite of IsSet()")
}
v.Set() v.Set()
if !v.IsSet() { if !v.IsSet() {
t.Fatal("AtomicBool.Set() failed") t.Fatal("AtomicBool.Set() failed")
@ -165,11 +169,17 @@ func TestRace(t *testing.T) {
func ExampleAtomicBool() { func ExampleAtomicBool() {
cond := New() // default to false cond := New() // default to false
cond.Set() // set to true any := true
cond.IsSet() // returns true old := any
cond.UnSet() // set to false new := !any
cond.SetTo(true) // set to whatever you want
cond.Toggle() // toggles the boolean value cond.Set() // Sets to true
cond.IsSet() // Returns true
cond.UnSet() // Sets to false
cond.IsNotSet() // Returns true
cond.SetTo(any) // Sets to whatever you want
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
cond.Toggle() // Inverts the boolean then returns the value before inverting
} }
// Benchmark Read // Benchmark Read