-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatomic-max_test.go
141 lines (121 loc) · 2.73 KB
/
atomic-max_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
© 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"testing"
)
func TestAtomicMax(t *testing.T) {
var threshold1, value1, value2 = 1, 1, 2
var value, zeroValue int
var hasValue, isNewMax, isPanic bool
var err error
var a AtomicMax[int]
_ = 1
// a new should have no value
a = AtomicMax[int]{}
value, hasValue = a.Max()
if value != zeroValue {
t.Errorf("new value %d exp %d", value, zeroValue)
}
if hasValue {
t.Error("new hasValue true")
}
// Value below threshold should not be a max
a = *NewAtomicMax(threshold1)
isNewMax = a.Value(zeroValue)
if isNewMax {
t.Error("below isNewMax")
}
value, hasValue = a.Max()
if value != zeroValue {
t.Errorf("below value %d exp %d", value, zeroValue)
}
if hasValue {
t.Error("below hasValue true")
}
// zero should be max
a = AtomicMax[int]{}
isNewMax = a.Value(zeroValue)
if !isNewMax {
t.Error("zero isNewMax false")
}
value, hasValue = a.Max()
if value != zeroValue {
t.Errorf("zero value %d exp %d", value, zeroValue)
}
if !hasValue {
t.Error("zero hasValue false")
}
// zero-zero should not be max
a = AtomicMax[int]{}
isNewMax = a.Value(zeroValue)
_ = isNewMax
isNewMax = a.Value(zeroValue)
if isNewMax {
t.Error("zero isNewMax false")
}
// equal to threshold should be max
a = *NewAtomicMax(threshold1)
isNewMax = a.Value(value1)
if !isNewMax {
t.Error("equal isNewMax false")
}
value, hasValue = a.Max()
if value != value1 {
t.Errorf("equal value %d exp %d", value, value1)
}
if !hasValue {
t.Error("equal hasValue false")
}
// smaller value should not be max
a = AtomicMax[int]{}
isNewMax = a.Value(value2)
_ = isNewMax
isNewMax = a.Value(value1)
if isNewMax {
t.Error("smaller isNewMax")
}
value, hasValue = a.Max()
if value != value2 {
t.Errorf("smaller value %d exp %d", value, value2)
}
if !hasValue {
t.Error("smaller hasValue false")
}
// max1 should work
a = AtomicMax[int]{}
isNewMax = a.Value(value1)
_ = isNewMax
value = a.Max1()
if value != value1 {
t.Errorf("Max1 value %d exp %d", value, value1)
}
// negative threshold should panic
isPanic, err = invokeNewAtomicMax()
if !isPanic {
t.Error("negative threshold no panic")
}
if err == nil {
t.Error("negative threshold no error")
}
// negative value should panic
isPanic, err = invokeAtomicMaxValue()
if !isPanic {
t.Error("negative value no panic")
}
if err == nil {
t.Error("negative value no error")
}
}
func invokeNewAtomicMax() (isPanic bool, err error) {
defer PanicToErr(&err, &isPanic)
NewAtomicMax(-1)
return
}
func invokeAtomicMaxValue() (isPanic bool, err error) {
defer PanicToErr(&err, &isPanic)
NewAtomicMax(0).Value(-1)
return
}