-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaggedptr_test.go
More file actions
78 lines (62 loc) · 1.74 KB
/
Copy pathtaggedptr_test.go
File metadata and controls
78 lines (62 loc) · 1.74 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
package taggedptr
import (
"testing"
"unsafe"
)
type TestStruct struct {
Field int
}
func TestTag(t *testing.T) {
s := &TestStruct{42}
initialPtr := unsafe.Pointer(s)
newPtr, _ := Tag(unsafe.Pointer(s), 3)
s = (*TestStruct)(newPtr)
if unsafe.Pointer(s) != unsafe.Pointer(uintptr(initialPtr)|3) {
t.Fatal("wrong tag")
}
if _, err := Tag(unsafe.Pointer(s), 10); err == nil {
t.Fatal("expected error")
}
}
func TestGetPointer(t *testing.T) {
s := &TestStruct{42}
initialPtr := unsafe.Pointer(s)
newPtr, _ := Tag(unsafe.Pointer(s), 3)
s = (*TestStruct)(newPtr)
if ptr := GetPointer(unsafe.Pointer(s)); ptr != initialPtr {
t.Fatalf("got wrong pointer. expected: %p, got: %p", initialPtr, ptr)
}
}
func TestGetTag(t *testing.T) {
s := &TestStruct{42}
newPtr, _ := Tag(unsafe.Pointer(s), 3)
s = (*TestStruct)(newPtr)
if tag := GetTag(unsafe.Pointer(s)); tag != 3 {
t.Fatalf("got wrong pointer. expected: %d, got: %d", 3, tag)
}
}
func TestCompareAndSwap(t *testing.T) {
s := &TestStruct{42}
casPtr := unsafe.Pointer(s)
initialPtr := unsafe.Pointer(s)
if _ = CompareAndSwap(&casPtr, casPtr, casPtr, 0, 1); casPtr != unsafe.Pointer(uintptr(initialPtr)|1) {
t.Fatal("wrong cas")
}
}
func TestGet(t *testing.T) {
s := &TestStruct{42}
initialPtr := unsafe.Pointer(s)
newPtr, _ := Tag(unsafe.Pointer(s), 3)
s = (*TestStruct)(newPtr)
if ptr, tag := Get(unsafe.Pointer(s)); ptr != initialPtr || tag != 3 {
t.Fatalf("wrong get. expected: %p %d, got: %p %d", initialPtr, 3, ptr, tag)
}
}
func TestAttempTag(t *testing.T) {
s := &TestStruct{42}
casPtr := unsafe.Pointer(s)
initialPtr := unsafe.Pointer(s)
if _ = AttemptTag(&casPtr, casPtr, 1); casPtr != unsafe.Pointer(uintptr(initialPtr)|1) {
t.Fatal("wrong tag attempt")
}
}