1
2
3
4
5 package atomic_test
6
7 import (
8 "internal/runtime/atomic"
9 "testing"
10 )
11
12 func TestXchg8(t *testing.T) {
13 var a [16]uint8
14 for i := range a {
15 next := uint8(i + 50)
16 a[i] = next
17 }
18 b := a
19
20
21
22 for i := range a {
23 next := uint8(i + 100)
24 pa := atomic.Xchg8(&a[i], next)
25 pb := b[i]
26 b[i] = next
27 if pa != pb {
28 t.Errorf("atomic.Xchg8(a[%d]); %d != %d", i, pa, pb)
29 }
30 if a != b {
31 t.Errorf("after atomic.Xchg8(a[%d]); %d != %d", i, a, b)
32 }
33 if t.Failed() {
34 break
35 }
36 }
37 }
38
39 func BenchmarkXchg8(b *testing.B) {
40 var x [512]uint8
41 sink = &x
42 for i := 0; i < b.N; i++ {
43 atomic.Xchg8(&x[255], uint8(i))
44 }
45 }
46
47 func BenchmarkXchg8Parallel(b *testing.B) {
48 var x [512]uint8
49 sink = &x
50 b.RunParallel(func(pb *testing.PB) {
51 i := uint8(0)
52 for pb.Next() {
53 atomic.Xchg8(&x[255], i)
54 i++
55 }
56 })
57 }
58
View as plain text