Files
goext/langext/pointer_test.go
T
Mikescher 02d6894ec6
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
[🤖] Add Unit-Tests
2026-04-27 16:31:29 +02:00

144 lines
2.7 KiB
Go

package langext
import (
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"testing"
)
func TestPtr(t *testing.T) {
p := Ptr(42)
if p == nil {
t.Fatalf("expected non-nil")
}
tst.AssertEqual(t, *p, 42)
}
func TestPtrString(t *testing.T) {
p := Ptr("hi")
tst.AssertEqual(t, *p, "hi")
}
func TestPTrue(t *testing.T) {
if PTrue == nil || *PTrue != true {
t.Errorf("PTrue should point to true")
}
}
func TestPFalse(t *testing.T) {
if PFalse == nil || *PFalse != false {
t.Errorf("PFalse should point to false")
}
}
func TestDblPtr(t *testing.T) {
pp := DblPtr(7)
if pp == nil || *pp == nil {
t.Fatalf("expected non-nil double pointer")
}
tst.AssertEqual(t, **pp, 7)
}
func TestDblPtrIfNotNilWithValue(t *testing.T) {
v := 5
pp := DblPtrIfNotNil(&v)
if pp == nil {
t.Fatalf("expected non-nil double pointer")
}
tst.AssertEqual(t, **pp, 5)
}
func TestDblPtrIfNotNilNil(t *testing.T) {
pp := DblPtrIfNotNil[int](nil)
if pp != nil {
t.Errorf("expected nil for nil input")
}
}
func TestDblPtrNil(t *testing.T) {
pp := DblPtrNil[int]()
if pp == nil {
t.Fatalf("expected non-nil outer pointer")
}
if *pp != nil {
t.Errorf("expected inner pointer to be nil")
}
}
func TestArrPtr(t *testing.T) {
p := ArrPtr(1, 2, 3)
if p == nil {
t.Fatalf("expected non-nil pointer")
}
tst.AssertArrayEqual(t, *p, []int{1, 2, 3})
}
func TestPtrInt32(t *testing.T) {
p := PtrInt32(7)
tst.AssertEqual(t, *p, int32(7))
}
func TestPtrInt64(t *testing.T) {
p := PtrInt64(7)
tst.AssertEqual(t, *p, int64(7))
}
func TestPtrFloat32(t *testing.T) {
p := PtrFloat32(1.5)
tst.AssertEqual(t, *p, float32(1.5))
}
func TestPtrFloat64(t *testing.T) {
p := PtrFloat64(2.5)
tst.AssertEqual(t, *p, 2.5)
}
func TestIsNilTrue(t *testing.T) {
tst.AssertEqual(t, IsNil(nil), true)
var p *int
tst.AssertEqual(t, IsNil(p), true)
var m map[string]int
tst.AssertEqual(t, IsNil(m), true)
var s []int
tst.AssertEqual(t, IsNil(s), true)
var c chan int
tst.AssertEqual(t, IsNil(c), true)
var f func()
tst.AssertEqual(t, IsNil(f), true)
}
func TestIsNilFalse(t *testing.T) {
v := 5
tst.AssertEqual(t, IsNil(&v), false)
tst.AssertEqual(t, IsNil(5), false)
tst.AssertEqual(t, IsNil("hi"), false)
tst.AssertEqual(t, IsNil(map[string]int{}), false)
tst.AssertEqual(t, IsNil([]int{}), false)
}
func TestPtrEqualsBothNil(t *testing.T) {
tst.AssertEqual(t, PtrEquals[int](nil, nil), true)
}
func TestPtrEqualsBothEqual(t *testing.T) {
a := 5
b := 5
tst.AssertEqual(t, PtrEquals(&a, &b), true)
}
func TestPtrEqualsBothDifferent(t *testing.T) {
a := 5
b := 6
tst.AssertEqual(t, PtrEquals(&a, &b), false)
}
func TestPtrEqualsOneNil(t *testing.T) {
a := 5
tst.AssertEqual(t, PtrEquals(&a, nil), false)
tst.AssertEqual(t, PtrEquals[int](nil, &a), false)
}