-
Notifications
You must be signed in to change notification settings - Fork 11
/
value_test.go
101 lines (76 loc) · 2.35 KB
/
value_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
package async
import (
"testing"
"github.com/reugn/async/internal/assert"
"github.com/reugn/async/internal/util"
)
func TestValueCompareAndSwap(t *testing.T) {
var value Value
swapped := value.CompareAndSwap(1, 2)
assert.Equal(t, swapped, false)
assert.IsNil(t, value.Load())
swapped = value.CompareAndSwap(1, nil)
assert.Equal(t, swapped, false)
assert.IsNil(t, value.Load())
swapped = value.CompareAndSwap(nil, 1)
assert.Equal(t, swapped, false)
assert.IsNil(t, value.Load())
value.Store(1)
swapped = value.CompareAndSwap("a", nil)
assert.Equal(t, swapped, false)
assert.Equal(t, value.Load(), 1)
swapped = value.CompareAndSwap(nil, nil)
assert.Equal(t, swapped, false)
assert.Equal(t, value.Load(), 1)
swapped = value.CompareAndSwap("a", 2)
assert.Equal(t, swapped, false)
assert.Equal(t, value.Load(), 1)
swapped = value.CompareAndSwap(-1, 2)
assert.Equal(t, swapped, false)
assert.Equal(t, value.Load(), 1)
swapped = value.CompareAndSwap(1, 2)
assert.Equal(t, swapped, true)
assert.Equal(t, value.Load(), 2)
swapped = value.CompareAndSwap(2, "a")
assert.Equal(t, swapped, true)
assert.Equal(t, value.Load(), "a")
stringPointer := util.Ptr("b")
swapped = value.CompareAndSwap("a", stringPointer)
assert.Equal(t, swapped, true)
assert.Same(t, value.Load().(*string), stringPointer)
swapped = value.CompareAndSwap(util.Ptr("b"), "c")
assert.Equal(t, swapped, false)
assert.Same(t, value.Load().(*string), stringPointer)
swapped = value.CompareAndSwap(stringPointer, "c")
assert.Equal(t, swapped, true)
assert.Equal(t, value.Load(), "c")
}
func TestValueLoad(t *testing.T) {
var value Value
assert.IsNil(t, value.Load())
value.Store(1)
assert.Equal(t, value.Load(), 1)
}
func TestValueStore(t *testing.T) {
var value Value
value.Store(1)
assert.Equal(t, value.Load(), 1)
assert.Panics(t, func() { value.Store(nil) })
value.Store("a")
assert.Equal(t, value.Load(), "a")
stringPointer := util.Ptr("b")
value.Store(stringPointer)
assert.Same(t, value.Load().(*string), stringPointer)
}
func TestValueSwap(t *testing.T) {
var value Value
old := value.Swap(1)
assert.IsNil(t, old)
assert.Panics(t, func() { _ = value.Swap(nil) })
old = value.Swap("a")
assert.Equal(t, old, 1)
stringPointer := util.Ptr("b")
old = value.Swap(stringPointer)
assert.Equal(t, old, "a")
assert.Same(t, value.Load().(*string), stringPointer)
}