-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync_atom_test.go
More file actions
148 lines (111 loc) · 2.64 KB
/
sync_atom_test.go
File metadata and controls
148 lines (111 loc) · 2.64 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gg_test
import (
"fmt"
"testing"
"time"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
// Semi-placeholder: needs a concurrency test.
func TestAtom(t *testing.T) {
defer gtest.Catch(t)
var ref gg.Atom[string]
gtest.Zero(ref.LoadVal())
gtest.Zero(ref.LoadPtr())
gtest.Eq(
gg.Tuple2(ref.LoadedVal()),
gg.Tuple2(``, false),
)
val0 := ``
ref.StorePtr(&val0)
gtest.Eq(ref.LoadPtr(), &val0)
gtest.Eq(ref.LoadVal(), ``)
gtest.Eq(
gg.Tuple2(ref.LoadedVal()),
gg.Tuple2(``, true),
)
ref.StoreVal(val0)
gtest.NotEq(ref.LoadPtr(), &val0)
gtest.Eq(ref.LoadVal(), ``)
gtest.Eq(
gg.Tuple2(ref.LoadedVal()),
gg.Tuple2(``, true),
)
val1 := `one`
ref.StorePtr(&val1)
gtest.Eq(ref.LoadPtr(), &val1)
gtest.Eq(ref.LoadVal(), `one`)
gtest.Eq(
gg.Tuple2(ref.LoadedVal()),
gg.Tuple2(`one`, true),
)
ref.StoreVal(val1)
gtest.NotEq(ref.LoadPtr(), &val0)
gtest.NotEq(ref.LoadPtr(), &val1)
gtest.Eq(ref.LoadVal(), `one`)
gtest.Eq(
gg.Tuple2(ref.LoadedVal()),
gg.Tuple2(`one`, true),
)
val2 := `two`
gtest.False(ref.CompareAndSwapPtr(&val1, &val2))
gtest.NotEq(ref.LoadPtr(), &val0)
gtest.NotEq(ref.LoadPtr(), &val1)
gtest.NotEq(ref.LoadPtr(), &val2)
gtest.Eq(ref.LoadVal(), `one`)
ref.StorePtr(&val1)
gtest.True(ref.CompareAndSwapPtr(&val1, &val2))
gtest.Eq(ref.LoadPtr(), &val2)
gtest.Eq(ref.LoadVal(), `two`)
gtest.True(ref.CompareAndSwapPtr(&val2, &val2))
gtest.Eq(ref.LoadPtr(), &val2)
gtest.Eq(ref.LoadVal(), `two`)
gtest.Eq(val0, ``)
gtest.Eq(val1, `one`)
gtest.Eq(val2, `two`)
val2 = `three`
gtest.Eq(ref.LoadPtr(), &val2)
gtest.Eq(ref.LoadVal(), `three`)
}
func TestAtom_iface(t *testing.T) {
defer gtest.Catch(t)
var ref gg.Atom[fmt.Stringer]
gtest.Zero(ref.LoadPtr())
gtest.Zero(ref.LoadVal())
var val0 fmt.Stringer = gg.Buf(`one`)
ref.StoreVal(val0)
gtest.Is(ref.LoadVal(), val0)
gtest.Eq(ref.LoadVal().String(), `one`)
var val1 fmt.Stringer = gg.ErrStr(`two`)
ref.StoreVal(val1)
gtest.Is(ref.LoadVal(), val1)
gtest.Eq(ref.LoadVal().String(), `two`)
}
func BenchmarkAtom_StorePtr(b *testing.B) {
var ref gg.Atom[time.Time]
val := time.Now()
for ind := 0; ind < b.N; ind++ {
ref.StorePtr(&val)
}
}
func BenchmarkAtom_StoreVal(b *testing.B) {
var ref gg.Atom[time.Time]
val := time.Now()
for ind := 0; ind < b.N; ind++ {
ref.StoreVal(val)
}
}
func BenchmarkAtom_LoadPtr(b *testing.B) {
var ref gg.Atom[time.Time]
ref.StoreVal(time.Now())
for ind := 0; ind < b.N; ind++ {
gg.Nop1(ref.LoadPtr())
}
}
func BenchmarkAtom_LoadVal(b *testing.B) {
var ref gg.Atom[time.Time]
ref.StoreVal(time.Now())
for ind := 0; ind < b.N; ind++ {
gg.Nop1(ref.LoadVal())
}
}