aint tests

This commit is contained in:
Josh Baker 2018-03-06 22:07:38 -07:00
parent dc30f35646
commit c8353a18b2
2 changed files with 20 additions and 1 deletions

View File

@ -10,7 +10,7 @@ type aint struct{ v uintptr }
func (a *aint) add(d int) int { func (a *aint) add(d int) int {
if d < 0 { if d < 0 {
return int(atomic.AddUintptr(&a.v, ^uintptr(d-1))) return int(atomic.AddUintptr(&a.v, ^uintptr((d*-1)-1)))
} }
return int(atomic.AddUintptr(&a.v, uintptr(d))) return int(atomic.AddUintptr(&a.v, uintptr(d)))
} }

19
controller/atomic_test.go Normal file
View File

@ -0,0 +1,19 @@
package controller
import "testing"
func TestAtomicInt(t *testing.T) {
var x aint
x.set(10)
if x.get() != 10 {
t.Fatalf("expected %v, got %v", 10, x.get())
}
x.add(-9)
if x.get() != 1 {
t.Fatalf("expected %v, got %v", 1, x.get())
}
x.add(-1)
if x.get() != 0 {
t.Fatalf("expected %v, got %v", 0, x.get())
}
}