mirror of https://github.com/tidwall/tile38.git
aint tests
This commit is contained in:
parent
dc30f35646
commit
c8353a18b2
|
@ -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)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue