-
Notifications
You must be signed in to change notification settings - Fork 0
/
uintptr_test.go
113 lines (102 loc) · 1.86 KB
/
uintptr_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
// 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 (
"sync"
"testing"
)
func TestUintptr(t *testing.T) {
addr := NewUintptr(1)
if addr.Load() != 1 {
t.Error(addr.Load())
}
addr.Store(2)
if addr.Load() != 2 {
t.Error(addr.Load())
}
var delta = uintptr(2)
if addr.Add(delta) != 4 {
t.Error(addr.Load())
}
if addr.Load() != 4 {
t.Error(addr.Load())
}
var new = uintptr(5)
if addr.Swap(new) != 4 {
t.Error(addr.Load())
}
var old = new
new = 6
if !addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
if addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
}
func TestAddUintptr(t *testing.T) {
addr := NewUintptr(1)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Add(1)
}()
}
wg.Wait()
}
func TestCompareAndSwapUintptr(t *testing.T) {
addr := NewUintptr(1)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.CompareAndSwap(1, 2)
}()
}
wg.Wait()
}
func TestSwapUintptr(t *testing.T) {
addr := NewUintptr(1)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Swap(1)
}()
}
wg.Wait()
}
func BenchmarkSwapUintptr(b *testing.B) {
addr := NewUintptr(1)
for i := 0; i < b.N; i++ {
addr.Swap(1)
}
}
func BenchmarkCompareAndSwapUintptr(b *testing.B) {
addr := NewUintptr(1)
for i := 0; i < b.N; i++ {
addr.CompareAndSwap(1, 2)
}
}
func BenchmarkAddUintptr(b *testing.B) {
addr := NewUintptr(1)
for i := 0; i < b.N; i++ {
addr.Add(1)
}
}
func BenchmarkStoreUintptr(b *testing.B) {
addr := NewUintptr(1)
for i := 0; i < b.N; i++ {
addr.Store(1)
}
}
func BenchmarkLoadUintptr(b *testing.B) {
addr := NewUintptr(1)
for i := 0; i < b.N; i++ {
addr.Load()
}
}