forked from mirror/abool
Change return type of Toggle to simple bool
This commit is contained in:
parent
b830aebdb7
commit
53396ec6fe
6
bool.go
6
bool.go
|
@ -49,9 +49,9 @@ func (ab *AtomicBool) SetTo(yes bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle negates boolean atomically and returns a new AtomicBool object which holds previous boolean value.
|
// Toggle inverts the boolean then returns the value before inverting.
|
||||||
func (ab *AtomicBool) Toggle() *AtomicBool {
|
func (ab *AtomicBool) Toggle() bool {
|
||||||
return NewBool(atomic.AddInt32((*int32)(ab), 1)&1 == 0)
|
return 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
|
||||||
|
|
|
@ -64,7 +64,7 @@ func TestBool(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
prev := v.Toggle()
|
prev := v.Toggle()
|
||||||
if v.IsSet() == prev.IsSet() {
|
if v.IsSet() == prev {
|
||||||
t.Fatal("AtomicBool.Toggle() to false failed")
|
t.Fatal("AtomicBool.Toggle() to false failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ func TestToogleMultipleTimes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
v := New()
|
v := New()
|
||||||
pre := NewBool(!v.IsSet())
|
pre := !v.IsSet()
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
v.SetTo(false)
|
v.SetTo(false)
|
||||||
for j := 0; j < i; j++ {
|
for j := 0; j < i; j++ {
|
||||||
|
@ -85,8 +85,8 @@ func TestToogleMultipleTimes(t *testing.T) {
|
||||||
t.Fatalf("AtomicBool.Toogle() doesn't work after %d calls, expected: %v, got %v", i, expected, v.IsSet())
|
t.Fatalf("AtomicBool.Toogle() doesn't work after %d calls, expected: %v, got %v", i, expected, v.IsSet())
|
||||||
}
|
}
|
||||||
|
|
||||||
if pre.IsSet() == v.IsSet() {
|
if pre == v.IsSet() {
|
||||||
t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre.IsSet())
|
t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue