-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr_test.go
96 lines (81 loc) · 2.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package g
import "testing"
// TestPtr ensures the Ptr function works correctly for all intended use cases.
func TestPtr(t *testing.T) {
equalIntSlices := func(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// Test integer pointer creation from literal.
if got := *Ptr(42); got != 42 {
t.Errorf("Ptr(42) = %v, want 42", got)
}
// Test creation of pointer to default value.
if got := *Ptr[int](); got != 0 {
t.Errorf("Ptr[int]() = %v, want 0", got)
}
// Test float pointer creation from literal.
if got := *Ptr(42.5); got != 42.5 {
t.Errorf("Ptr(42.5) = %v, want 42.5", got)
}
// Test string pointer creation from literal.
if got := *Ptr("hello"); got != "hello" {
t.Errorf("Ptr(\"hello\") = %v, want \"hello\"", got)
}
// Test boolean pointer creation from literal.
if got := *Ptr(true); !got {
t.Errorf("Ptr(true) = %v, want true", got)
}
// Test creating a pointer from a complex type (slice).
slice := []int{1, 2, 3}
if got := *Ptr(slice); !equalIntSlices(got, slice) {
t.Errorf("Ptr([]int{1, 2, 3}) = %v, want %v", got, slice)
}
}
// TestPtrIf ensures the PtrIf function works correctly
// for all intended use cases.
func TestPtrIf(t *testing.T) {
equalIntSlices := func(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// Test integer pointer creation from literal.
if got := PtrIf(true, 42); *got != 42 {
t.Errorf("PtrIf(true, 42) = %v, want 42", *got)
}
// Test creation of pointer to default value.
if got := PtrIf(false, 42); got != nil {
t.Errorf("PtrIf(false, 42) = %v, want nil", got)
}
// Test float pointer creation from literal.
if got := PtrIf(true, 42.5); *got != 42.5 {
t.Errorf("PtrIf(true, 42.5) = %v, want 42.5", *got)
}
// Test string pointer creation from literal.
if got := PtrIf(true, "hello"); *got != "hello" {
t.Errorf("PtrIf(true, \"hello\") = %v, want \"hello\"", *got)
}
// Test boolean pointer creation from literal.
if got := PtrIf(true, true); *got != true {
t.Errorf("PtrIf(true, true) = %v, want true", *got)
}
// Test creating a pointer from a complex type (slice).
slice := []int{1, 2, 3}
if got := PtrIf(true, slice); !equalIntSlices(*got, slice) {
t.Errorf("PtrIf(true, []int{1, 2, 3}) = %v, want %v", *got, slice)
}
}