-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathatomic_test.go
More file actions
102 lines (86 loc) · 2.34 KB
/
atomic_test.go
File metadata and controls
102 lines (86 loc) · 2.34 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
package rate
import (
"math"
"testing"
)
// TestAtomicSliceUint64Basic verifies basic operations for atomicSliceUint64.
func TestAtomicSliceUint64Basic(t *testing.T) {
// Create a slice of length 3
slice := newAtomicSliceUint64(3)
// Initial values should be zero
if got := slice.Get(0); got != 0 {
t.Errorf("Initial value: got %d, want 0", got)
}
// Set and Get
slice.Set(1, 42)
if got := slice.Get(1); got != 42 {
t.Errorf("After Set: got %d, want 42", got)
}
// CompareAndSet success
ok := slice.CompareAndSwap(1, 42, 100)
if !ok {
t.Error("CompareAndSwap should succeed")
}
if got := slice.Get(1); got != 100 {
t.Errorf("After CompareAndSwap: got %d, want 100", got)
}
// CompareAndSet failure
ok = slice.CompareAndSwap(1, 42, 200)
if ok {
t.Error("CompareAndSwap should fail")
}
if got := slice.Get(1); got != 100 {
t.Errorf("After failed CompareAndSwap: got %d, should still be 100", got)
}
// Check Len
if got := slice.Len(); got != 3 {
t.Errorf("Len: got %d, want 3", got)
}
}
// TestAtomicSliceFloat64Basic verifies basic operations for atomicSliceFloat64.
func TestAtomicSliceFloat64Basic(t *testing.T) {
// Create a slice of length 3
slice := newAtomicSliceFloat64(3)
// Initial values should be zero
if got := slice.Get(0); got != 0.0 {
t.Errorf("Initial value: got %f, want 0.0", got)
}
// Set and Get
slice.Set(1, 42.5)
if got := slice.Get(1); got != 42.5 {
t.Errorf("After Set: got %f, want 42.5", got)
}
// CompareAndSet success
ok := slice.CompareAndSwap(1, 42.5, 100.5)
if !ok {
t.Error("CompareAndSwap should succeed")
}
if got := slice.Get(1); got != 100.5 {
t.Errorf("After CompareAndSwap: got %f, want 100.5", got)
}
// CompareAndSet failure
ok = slice.CompareAndSwap(1, 42.5, 200.5)
if ok {
t.Error("CompareAndSwap should fail")
}
if got := slice.Get(1); got != 100.5 {
t.Errorf("After failed CompareAndSwap: got %f, should still be 100.5", got)
}
// Check Len
if got := slice.Len(); got != 3 {
t.Errorf("Len: got %d, want 3", got)
}
// Test with special float values
slice.Set(0, math.NaN())
if !math.IsNaN(slice.Get(0)) {
t.Error("Expected NaN value")
}
slice.Set(1, math.Inf(1))
if !math.IsInf(slice.Get(1), 1) {
t.Error("Expected positive infinity")
}
slice.Set(2, math.Inf(-1))
if !math.IsInf(slice.Get(2), -1) {
t.Error("Expected negative infinity")
}
}