github.com/aloncn/graphics-go@v0.0.1/src/runtime/internal/atomic/atomic_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package atomic_test 6 7 import ( 8 "runtime" 9 "runtime/internal/atomic" 10 "testing" 11 "unsafe" 12 ) 13 14 func runParallel(N, iter int, f func()) { 15 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(int(N))) 16 done := make(chan bool) 17 for i := 0; i < N; i++ { 18 go func() { 19 for j := 0; j < iter; j++ { 20 f() 21 } 22 done <- true 23 }() 24 } 25 for i := 0; i < N; i++ { 26 <-done 27 } 28 } 29 30 func TestXadduintptr(t *testing.T) { 31 const N = 20 32 const iter = 100000 33 inc := uintptr(100) 34 total := uintptr(0) 35 runParallel(N, iter, func() { 36 atomic.Xadduintptr(&total, inc) 37 }) 38 if want := uintptr(N * iter * inc); want != total { 39 t.Fatalf("xadduintpr error, want %d, got %d", want, total) 40 } 41 total = 0 42 runParallel(N, iter, func() { 43 atomic.Xadduintptr(&total, inc) 44 atomic.Xadduintptr(&total, uintptr(-int64(inc))) 45 }) 46 if total != 0 { 47 t.Fatalf("xadduintpr total error, want %d, got %d", 0, total) 48 } 49 } 50 51 // Tests that xadduintptr correctly updates 64-bit values. The place where 52 // we actually do so is mstats.go, functions mSysStat{Inc,Dec}. 53 func TestXadduintptrOnUint64(t *testing.T) { 54 /* if runtime.BigEndian != 0 { 55 // On big endian architectures, we never use xadduintptr to update 56 // 64-bit values and hence we skip the test. (Note that functions 57 // mSysStat{Inc,Dec} in mstats.go have explicit checks for 58 // big-endianness.) 59 return 60 }*/ 61 const inc = 100 62 val := uint64(0) 63 atomic.Xadduintptr((*uintptr)(unsafe.Pointer(&val)), inc) 64 if inc != val { 65 t.Fatalf("xadduintptr should increase lower-order bits, want %d, got %d", inc, val) 66 } 67 }