-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
114 lines (102 loc) · 1.89 KB
/
bool_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
// 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 TestBool(t *testing.T) {
addr := NewBool(false)
if addr.Load() != false {
t.Error(addr.Load())
}
addr.Store(true)
if addr.Load() != true {
t.Error(addr.Load())
}
var delta = true
if addr.Add(delta) != true {
t.Error(addr.Load())
}
if addr.Load() != true {
t.Error(addr.Load())
}
var new = false
if addr.Swap(new) != true {
t.Error(addr.Load())
}
var old = new
new = true
if !addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
if addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
}
func TestAddBool(t *testing.T) {
addr := NewBool(false)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Add(false)
}()
}
wg.Wait()
}
func TestCompareAndSwapBool(t *testing.T) {
addr := NewBool(false)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.CompareAndSwap(false, true)
}()
}
wg.Wait()
}
func TestSwapBool(t *testing.T) {
addr := NewBool(false)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Swap(false)
}()
}
wg.Wait()
}
func BenchmarkSwapBool(b *testing.B) {
addr := NewBool(false)
for i := 0; i < b.N; i++ {
addr.Swap(false)
}
}
func BenchmarkCompareAndSwapBool(b *testing.B) {
addr := NewBool(false)
for i := 0; i < b.N; i++ {
addr.CompareAndSwap(false, true)
}
}
func BenchmarkAddBool(b *testing.B) {
addr := NewBool(false)
for i := 0; i < b.N; i++ {
addr.Add(false)
}
}
func BenchmarkStoreBool(b *testing.B) {
addr := NewBool(false)
for i := 0; i < b.N; i++ {
addr.Store(false)
}
}
func BenchmarkLoadBool(b *testing.B) {
addr := NewBool(false)
for i := 0; i < b.N; i++ {
addr.Load()
}
}