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.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

View File

@ -38,6 +38,11 @@ func (ab *AtomicBool) IsSet() bool {
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.
func (ab *AtomicBool) SetTo(yes bool) {
if yes {

View File

@ -25,6 +25,10 @@ func TestBool(t *testing.T) {
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()
if !v.IsSet() {
t.Fatal("AtomicBool.Set() failed")
@ -164,12 +168,18 @@ func TestRace(t *testing.T) {
}
func ExampleAtomicBool() {
cond := New() // default to false
cond.Set() // set to true
cond.IsSet() // returns true
cond.UnSet() // set to false
cond.SetTo(true) // set to whatever you want
cond.Toggle() // toggles the boolean value
cond := New() // default to false
any := true
old := any
new := !any
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