-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes_test.go
More file actions
127 lines (114 loc) · 2.21 KB
/
bytes_test.go
File metadata and controls
127 lines (114 loc) · 2.21 KB
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
// 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 TestBytesEqual(t *testing.T) {
var a = []byte{1, 2, 3}
var b = []byte{1, 2, 3}
if !bytesEqual(a, b) {
t.Error("fail")
}
}
func TestBytes(t *testing.T) {
var val = []byte{1, 2, 3}
addr := NewBytes(val)
if !bytesEqual(addr.Load(), val) {
t.Error(addr.Load())
}
addr.Store(val[:2])
if !bytesEqual(addr.Load(), val[:2]) {
t.Error(addr.Load())
}
var delta = val[2:]
if !bytesEqual(addr.Add(delta), val) {
t.Error(addr.Load())
}
if !bytesEqual(addr.Load(), val) {
t.Error(addr.Load())
}
var new = []byte{4, 5, 6}
if !bytesEqual(addr.Swap(new), val) {
t.Error(addr.Load())
}
var old = new
new = []byte{7, 8, 9}
if !addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
if addr.CompareAndSwap(old, new) {
t.Error(addr.Load())
}
addr = &Bytes{}
if addr.Load() != nil || !bytesEqual(addr.Load(), []byte{}) {
t.Error(addr.Load())
}
}
func TestAddBytes(t *testing.T) {
addr := NewBytes(nil)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Add(nil)
}()
}
wg.Wait()
}
func TestCompareAndSwapBytes(t *testing.T) {
addr := NewBytes(nil)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.CompareAndSwap(nil, nil)
}()
}
wg.Wait()
}
func TestSwapBytes(t *testing.T) {
addr := NewBytes(nil)
var wg sync.WaitGroup
for i := 0; i < 8192; i++ {
wg.Add(1)
go func() {
defer wg.Done()
addr.Swap(nil)
}()
}
wg.Wait()
}
func BenchmarkSwapBytes(b *testing.B) {
addr := NewBytes(nil)
for i := 0; i < b.N; i++ {
addr.Swap(nil)
}
}
func BenchmarkCompareAndSwapBytes(b *testing.B) {
addr := NewBytes(nil)
for i := 0; i < b.N; i++ {
addr.CompareAndSwap(nil, nil)
}
}
func BenchmarkAddBytes(b *testing.B) {
addr := NewBytes(nil)
for i := 0; i < b.N; i++ {
addr.Add(nil)
}
}
func BenchmarkStoreBytes(b *testing.B) {
addr := NewBytes(nil)
for i := 0; i < b.N; i++ {
addr.Store(nil)
}
}
func BenchmarkLoadBytes(b *testing.B) {
addr := NewBytes(nil)
for i := 0; i < b.N; i++ {
addr.Load()
}
}