-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_test.go
125 lines (95 loc) · 2.07 KB
/
sync_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
115
116
117
118
119
120
121
122
123
124
125
package gg_test
import (
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
// Placeholder, needs a concurrency test.
func TestAtom(t *testing.T) {
defer gtest.Catch(t)
var ref gg.Atom[string]
gtest.Zero(ref.Load())
gtest.Eq(
gg.Tuple2(ref.Loaded()),
gg.Tuple2(``, false),
)
ref.Store(``)
gtest.Zero(ref.Load())
gtest.Eq(
gg.Tuple2(ref.Loaded()),
gg.Tuple2(``, true),
)
ref.Store(`one`)
gtest.Eq(ref.Load(), `one`)
gtest.Eq(
gg.Tuple2(ref.Loaded()),
gg.Tuple2(`one`, true),
)
gtest.False(ref.CompareAndSwap(`three`, `two`))
gtest.Eq(ref.Load(), `one`)
gtest.True(ref.CompareAndSwap(`one`, `two`))
gtest.Eq(ref.Load(), `two`)
}
func BenchmarkAtom_Store(b *testing.B) {
var ref gg.Atom[string]
for ind := 0; ind < b.N; ind++ {
ref.Store(`str`)
}
}
func BenchmarkAtom_Load(b *testing.B) {
var ref gg.Atom[string]
ref.Store(`str`)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(ref.Load())
}
}
func TestChanInit(t *testing.T) {
defer gtest.Catch(t)
gg.ChanInit((*chan string)(nil))
var tar chan string
gtest.Eq(gg.ChanInit(&tar), tar)
gtest.NotZero(tar)
gtest.Eq(cap(tar), 0)
prev := tar
gtest.Eq(gg.ChanInit(&tar), prev)
gtest.Eq(tar, prev)
}
func TestChanInitCap(t *testing.T) {
defer gtest.Catch(t)
gg.ChanInitCap((*chan string)(nil), 1)
var tar chan string
gtest.Eq(gg.ChanInitCap(&tar, 3), tar)
gtest.NotZero(tar)
gtest.Eq(cap(tar), 3)
prev := tar
gtest.Eq(gg.ChanInitCap(&tar, 5), prev)
gtest.Eq(tar, prev)
gtest.Eq(cap(prev), 3)
gtest.Eq(cap(tar), 3)
}
func TestSendOpt(t *testing.T) {
defer gtest.Catch(t)
var tar chan string
gg.SendOpt(tar, `val`)
gg.SendOpt(tar, `val`)
gg.SendOpt(tar, `val`)
tar = make(chan string, 1)
gg.SendOpt(tar, `val`)
gg.SendOpt(tar, `val`)
gg.SendOpt(tar, `val`)
gtest.Eq(<-tar, `val`)
}
func TestSendZeroOpt(t *testing.T) {
defer gtest.Catch(t)
var tar chan string
gg.SendZeroOpt(tar)
gg.SendZeroOpt(tar)
gg.SendZeroOpt(tar)
tar = make(chan string, 1)
gg.SendZeroOpt(tar)
gg.SendZeroOpt(tar)
gg.SendZeroOpt(tar)
val, ok := <-tar
gtest.Zero(val)
gtest.True(ok)
}