This repository was archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint32_test.go
More file actions
94 lines (72 loc) · 1.42 KB
/
int32_test.go
File metadata and controls
94 lines (72 loc) · 1.42 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
package atomicvalue
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInt32SetGet(t *testing.T) {
assert := assert.New(t)
var a Int32
assert.Equal(0, a.Get())
a.Set(10)
assert.Equal(10, a.Get())
}
func TestInt32Swap(t *testing.T) {
assert := assert.New(t)
var a Int32
assert.Equal(0, a.Swap(0))
assert.Equal(0, a.Get())
assert.Equal(0, a.Swap(10))
assert.Equal(10, a.Get())
assert.Equal(10, a.Swap(0))
assert.Equal(0, a.Get())
}
func TestInt32CompareAndSwap(t *testing.T) {
assert := assert.New(t)
tests := []struct {
init int
old int
new int
result bool
store int
}{
{0, 9, 0, false, 0},
{0, 0, 9, true, 9},
{0, 0, 0, true, 0},
{0, 9, 9, false, 0},
{9, 9, 0, true, 0},
{9, 0, 9, false, 9},
{9, 9, 9, true, 9},
{9, 0, 0, false, 9},
}
for _, tt := range tests {
var a Int32
a.Set(tt.init)
r := a.CompareAndSwap(tt.old, tt.new)
assert.Equal(tt.result, r, "result")
assert.Equal(tt.store, a.Get(), "store")
}
}
func BenchmarkInt32Get(b *testing.B) {
var a Int32
for n := 0; n < b.N; n++ {
a.Get()
}
}
func BenchmarkInt32Set(b *testing.B) {
var a Int32
for n := 0; n < b.N; n++ {
a.Set(10)
}
}
func BenchmarkInt32CompareAndSwap_1(b *testing.B) {
var a Int32
for n := 0; n < b.N; n++ {
a.CompareAndSwap(0, 9)
}
}
func BenchmarkInt32CompareAndSwap_2(b *testing.B) {
var a Int32
for n := 0; n < b.N; n++ {
a.CompareAndSwap(9, 0)
}
}