-
Notifications
You must be signed in to change notification settings - Fork 32
/
ptr_test.go
81 lines (66 loc) · 1.67 KB
/
ptr_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
package core_test
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/core"
"reflect"
"testing"
)
func ExamplePtrTo() {
res := core.PtrTo(common.Hash{})
fmt.Println(reflect.TypeOf(res))
// Output: *common.Hash
}
func ExamplePtrSlice() {
res := core.PtrSlice([]common.Hash{})
fmt.Println(reflect.TypeOf(res))
// Output: []*common.Hash
}
func TestArePointersEqual(t *testing.T) {
type TestData struct {
a interface{}
b interface{}
want bool
}
intVal1 := 1
intVal2 := 2
testData := []TestData{
{&intVal1, &intVal1, true},
{&intVal1, &intVal2, false},
{nil, nil, true},
{&intVal1, nil, false},
{nil, &intVal1, false},
{intVal1, intVal1, false}, // non-pointers
{intVal1, &intVal1, false}, // non-pointer and pointer
{&intVal1, intVal1, false}, // pointer and non-pointer
}
for _, data := range testData {
got := core.ArePointersEqual(data.a, data.b)
if got != data.want {
t.Errorf("ArePointersEqual(%v, %v) = %v; want %v", data.a, data.b, got, data.want)
}
}
}
func TestCopyPointer(t *testing.T) {
original := int64(42)
originalPtr := &original
newPtr := core.CopyPointer(originalPtr)
// nolint: staticcheck
if newPtr == nil {
t.Errorf("CopyPointer returned nil")
}
if newPtr == originalPtr {
t.Errorf("CopyPointer returned the same pointer")
}
//nolint: staticcheck
if *newPtr != *originalPtr {
t.Errorf("CopyPointer did not copy the value correctly, got %d, want %d", *newPtr, *originalPtr)
}
}
func TestCopyPointerNil(t *testing.T) {
var originalPtr *int64 = nil
newPtr := core.CopyPointer(originalPtr)
if newPtr != nil {
t.Errorf("CopyPointer did not return nil for nil input")
}
}