-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_of_test.go
51 lines (45 loc) · 1.21 KB
/
value_of_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
package gokit
import (
"testing"
)
func TestValueOf(t *testing.T) {
type TestData struct {
Value int
Expected int
}
// Create a new ValueOf instance
vo := NewValueOf[int]()
// Test Load() method
loadData := TestData{
Value: 42,
Expected: 42,
}
vo.Store(loadData.Value)
if val := vo.Load(); val != loadData.Expected {
t.Errorf("Load() failed, expected %d but got %d", loadData.Expected, val)
}
// Test Swap() method
swapData := TestData{
Value: 77,
Expected: 77,
}
oldVal := vo.Swap(swapData.Value)
if oldVal != loadData.Expected {
t.Errorf("Swap() failed, expected old value %d but got %d", loadData.Expected, oldVal)
}
if val := vo.Load(); val != swapData.Expected {
t.Errorf("Swap() failed, expected new value %d but got %d", swapData.Expected, val)
}
// Test CompareAndSwap() method
compareAndSwapData := TestData{
Value: 55,
Expected: 55,
}
swapped := vo.CompareAndSwap(swapData.Expected, compareAndSwapData.Value)
if !swapped {
t.Errorf("CompareAndSwap() failed, expected swap to be successful")
}
if val := vo.Load(); val != compareAndSwapData.Expected {
t.Errorf("CompareAndSwap() failed, expected new value %d but got %d", compareAndSwapData.Expected, val)
}
}