Add tests for Toggle()

This commit is contained in:
Tevin Zhang 2020-06-30 10:09:54 +08:00
parent 9e538c6d38
commit 9c19061983
No known key found for this signature in database
GPG Key ID: EE7DA2A50F0960FB
1 changed files with 52 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package abool package abool
import ( import (
"math"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -66,6 +67,57 @@ func TestBool(t *testing.T) {
} }
} }
func TestToogleMultipleTimes(t *testing.T) {
t.Parallel()
v := New()
pre := NewBool(!v.IsSet())
for i := 0; i < 100; i++ {
v.SetTo(false)
for j := 0; j < i; j++ {
pre = v.Toggle()
}
expected := i%2 != 0
if v.IsSet() != expected {
t.Fatalf("AtomicBool.Toogle() doesn't work after %d calls, expected: %v, got %v", i, expected, v.IsSet())
}
if pre.IsSet() == v.IsSet() {
t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre.IsSet())
}
}
}
func TestToogleAfterOverflow(t *testing.T) {
t.Parallel()
var value int32 = math.MaxInt32
v := (*AtomicBool)(&value)
valueBeforeToggle := *(*int32)(v)
// test first toggle after overflow
v.Toggle()
expected := math.MaxInt32%2 == 0
if v.IsSet() != expected {
t.Fatalf("AtomicBool.Toogle() doesn't work after overflow, expected: %v, got %v", expected, v.IsSet())
}
// make sure overflow happened
var valueAfterToggle int32 = *(*int32)(v)
if valueAfterToggle >= valueBeforeToggle {
t.Fatalf("Overflow does not happen as expected, before %d, after: %d", valueBeforeToggle, valueAfterToggle)
}
// test second toggle after overflow
v.Toggle()
expected = !expected
if v.IsSet() != expected {
t.Fatalf("AtomicBool.Toogle() doesn't work after the second call after overflow, expected: %v, got %v", expected, v.IsSet())
}
}
func TestRace(t *testing.T) { func TestRace(t *testing.T) {
repeat := 10000 repeat := 10000
var wg sync.WaitGroup var wg sync.WaitGroup