File: pointer_helpers_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (116 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download
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
package linodego

import (
	"testing"
)

// TestPointer tests the Pointer helper function with various types
func TestPointer(t *testing.T) {
	// Test nil pointer for int (should be nil if explicitly set)
	nilIntPtr := Pointer[*int](nil)
	if nilIntPtr == nil || *nilIntPtr != nil {
		t.Errorf("Expected nil pointer, got %v", nilIntPtr)
	}

	// Test with an integer
	intValue := 11
	intPtr := Pointer(intValue)
	if *intPtr != intValue {
		t.Errorf("Expected %d, got %d", intValue, *intPtr)
	}

	// Test with a float
	floatValue := 1.23
	floatPtr := Pointer(floatValue)
	if *floatPtr != floatValue {
		t.Errorf("Expected %f, got %f", floatValue, *floatPtr)
	}

	// Test with a string
	stringValue := "hello world"
	stringPtr := Pointer(stringValue)
	if *stringPtr != stringValue {
		t.Errorf("Expected %s, got %s", stringValue, *stringPtr)
	}

	// Test with a boolean
	boolValue := true
	boolPtr := Pointer(boolValue)
	if *boolPtr != boolValue {
		t.Errorf("Expected %t, got %t", boolValue, *boolPtr)
	}

	// Test with a struct
	type myStruct struct {
		Field1 int
		Field2 string
	}
	structValue := myStruct{Field1: 1, Field2: "test"}
	structPtr := Pointer(structValue)
	if structPtr.Field1 != structValue.Field1 || structPtr.Field2 != structValue.Field2 {
		t.Errorf("Expected %+v, got %+v", structValue, *structPtr)
	}
}

// TestDoublePointer tests the DoublePointer helper function with various types
func TestDoublePointer(t *testing.T) {
	// Test with an integer
	intValue := 42
	intDoublePtr := DoublePointer(intValue)
	if **intDoublePtr != intValue {
		t.Errorf("Expected %d, got %d", intValue, **intDoublePtr)
	}

	// Test with a string
	strValue := "double"
	strDoublePtr := DoublePointer(strValue)
	if **strDoublePtr != strValue {
		t.Errorf("Expected %s, got %s", strValue, **strDoublePtr)
	}

	// Test with a boolean
	boolValue := false
	boolDoublePtr := DoublePointer(boolValue)
	if **boolDoublePtr != boolValue {
		t.Errorf("Expected %t, got %t", boolValue, **boolDoublePtr)
	}

	// Test with a struct
	type myStruct struct {
		Field int
	}
	structValue := myStruct{Field: 7}
	structDoublePtr := DoublePointer(structValue)
	if (**structDoublePtr).Field != structValue.Field {
		t.Errorf("Expected %+v, got %+v", structValue, **structDoublePtr)
	}
}

func TestDoublePointerNull(t *testing.T) {
	// Test with an integer
	intDoublePtr := DoublePointerNull[int]()
	if intDoublePtr == nil || *intDoublePtr != nil {
		t.Errorf("Expected nil pointer, got %v", intDoublePtr)
	}

	// Test with a string
	stringDoublePtr := DoublePointerNull[string]()
	if stringDoublePtr == nil || *stringDoublePtr != nil {
		t.Errorf("Expected nil pointer, got %v", stringDoublePtr)
	}

	// Test with a boolean
	boolDoublePtr := DoublePointerNull[bool]()
	if boolDoublePtr == nil || *boolDoublePtr != nil {
		t.Errorf("Expected nil pointer, got %v", boolDoublePtr)
	}

	// Test with a struct
	type myStruct struct {
		Field int
	}
	structDoublePtr := DoublePointerNull[myStruct]()
	if structDoublePtr == nil || *structDoublePtr != nil {
		t.Errorf("Expected nil pointer, got %v", structDoublePtr)
	}
}