forked from mirror/abool
implments Toggle method more explicit and simple.
This commit is contained in:
parent
774dbeaec2
commit
9e538c6d38
|
@ -18,7 +18,7 @@ cond.IsSet() // Returns true
|
||||||
cond.UnSet() // Set to false
|
cond.UnSet() // Set to false
|
||||||
cond.SetTo(true) // Set to whatever you want
|
cond.SetTo(true) // Set to whatever you want
|
||||||
cond.SetToIf(false, true) // Set to true if it is false, returns false(not set)
|
cond.SetToIf(false, true) // Set to true if it is false, returns false(not set)
|
||||||
cond.Toggle() bool // Toggle the boolean value atomically and returns the previous value.
|
cond.Toggle() *AtomicBool // Negates boolean atomically and returns a new AtomicBool object which holds previous boolean value.
|
||||||
|
|
||||||
|
|
||||||
// embedding
|
// embedding
|
||||||
|
|
6
bool.go
6
bool.go
|
@ -49,9 +49,9 @@ func (ab *AtomicBool) SetTo(yes bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle negates boolean atomically and returns the previous value.
|
// Toggle negates boolean atomically and returns a new AtomicBool object which holds previous boolean value.
|
||||||
func (ab *AtomicBool) Toggle() bool {
|
func (ab *AtomicBool) Toggle() *AtomicBool {
|
||||||
return (atomic.AddInt32((*int32)(ab), 1)-1)&1 == 1
|
return NewBool(atomic.AddInt32((*int32)(ab), 1)&1 == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetToIf sets the Boolean to new only if the Boolean matches the old
|
// SetToIf sets the Boolean to new only if the Boolean matches the old
|
||||||
|
|
13
bool_test.go
13
bool_test.go
|
@ -50,11 +50,20 @@ func TestBool(t *testing.T) {
|
||||||
t.Fatal("AtomicBool.SetTo(false, true) failed")
|
t.Fatal("AtomicBool.SetTo(false, true) failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = v.Toggle() // expected false
|
v = New()
|
||||||
if v.IsSet() {
|
if v.IsSet() {
|
||||||
t.Fatal("AtomicBool.Toggle() failed")
|
t.Fatal("Empty value of AtomicBool should be false")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = v.Toggle()
|
||||||
|
if !v.IsSet() {
|
||||||
|
t.Fatal("AtomicBool.Toggle() to true failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
prev := v.Toggle()
|
||||||
|
if v.IsSet() == prev.IsSet() {
|
||||||
|
t.Fatal("AtomicBool.Toggle() to false failed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRace(t *testing.T) {
|
func TestRace(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue