-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_test.go
139 lines (131 loc) · 2.55 KB
/
atomic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package atomic
import (
"testing"
"unsafe"
)
func TestAtomicInt32(t *testing.T) {
var v int32
StoreInt32(&v, 1)
if LoadInt32(&v) != 1 {
t.Log(LoadInt32(&v))
}
old := SwapInt32(&v, 2)
if old != 1 {
t.Log(LoadInt32(&v))
}
if AddInt32(&v, 1) != 3 {
t.Log(LoadInt32(&v))
}
if !CompareAndSwapInt32(&v, 3, 4) {
t.Log(LoadInt32(&v))
}
if CompareAndSwapInt32(&v, 3, 4) {
t.Log(LoadInt32(&v))
}
}
func TestAtomicInt64(t *testing.T) {
var v int64
StoreInt64(&v, 1)
if LoadInt64(&v) != 1 {
t.Log(LoadInt64(&v))
}
old := SwapInt64(&v, 2)
if old != 1 {
t.Log(LoadInt64(&v))
}
if AddInt64(&v, 1) != 3 {
t.Log(LoadInt64(&v))
}
if !CompareAndSwapInt64(&v, 3, 4) {
t.Log(LoadInt64(&v))
}
if CompareAndSwapInt64(&v, 3, 4) {
t.Log(LoadInt64(&v))
}
}
func TestAtomicUint32(t *testing.T) {
var v uint32
StoreUint32(&v, 1)
if LoadUint32(&v) != 1 {
t.Log(LoadUint32(&v))
}
old := SwapUint32(&v, 2)
if old != 1 {
t.Log(LoadUint32(&v))
}
if AddUint32(&v, 1) != 3 {
t.Log(LoadUint32(&v))
}
if !CompareAndSwapUint32(&v, 3, 4) {
t.Log(LoadUint32(&v))
}
if CompareAndSwapUint32(&v, 3, 4) {
t.Log(LoadUint32(&v))
}
}
func TestAtomicUint64(t *testing.T) {
var v uint64
StoreUint64(&v, 1)
if LoadUint64(&v) != 1 {
t.Log(LoadUint64(&v))
}
old := SwapUint64(&v, 2)
if old != 1 {
t.Log(LoadUint64(&v))
}
if AddUint64(&v, 1) != 3 {
t.Log(LoadUint64(&v))
}
if !CompareAndSwapUint64(&v, 3, 4) {
t.Log(LoadUint64(&v))
}
if CompareAndSwapUint64(&v, 3, 4) {
t.Log(LoadUint64(&v))
}
}
func TestAtomicUintptr(t *testing.T) {
var v uintptr
StoreUintptr(&v, 1)
if LoadUintptr(&v) != 1 {
t.Log(LoadUintptr(&v))
}
old := SwapUintptr(&v, 2)
if old != 1 {
t.Log(LoadUintptr(&v))
}
if AddUintptr(&v, 1) != 3 {
t.Log(LoadUintptr(&v))
}
if !CompareAndSwapUintptr(&v, 3, 4) {
t.Log(LoadUintptr(&v))
}
if CompareAndSwapUintptr(&v, 3, 4) {
t.Log(LoadUintptr(&v))
}
}
func TestAtomicPointer(t *testing.T) {
var v string
var vp = unsafe.Pointer(&v)
var v1 = "Hello World"
var vp1 = unsafe.Pointer(&v1)
StorePointer(&vp, vp1)
if LoadPointer(&vp) != vp1 {
t.Log(LoadPointer(&vp))
}
var v2 = "Foo"
var vp2 = unsafe.Pointer(&v2)
old := SwapPointer(&vp, vp2)
if old != vp1 {
t.Log(LoadPointer(&vp))
}
var v3 = "Bar"
var vp3 = unsafe.Pointer(&v3)
if !CompareAndSwapPointer(&vp, vp2, vp3) {
t.Log(LoadPointer(&vp))
}
if CompareAndSwapPointer(&vp, vp2, vp3) {
t.Log(LoadPointer(&vp))
}
}