diff --git a/controller/atomic.go b/controller/atomic.go index 2ee4b1d1..882bb253 100644 --- a/controller/atomic.go +++ b/controller/atomic.go @@ -10,7 +10,7 @@ type aint struct{ v uintptr } func (a *aint) add(d int) int { 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))) } diff --git a/controller/atomic_test.go b/controller/atomic_test.go new file mode 100644 index 00000000..b26152f6 --- /dev/null +++ b/controller/atomic_test.go @@ -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()) + } +}