diff --git a/pointer/pointer.go b/pointer/pointer.go index e745a81b..638e27d4 100644 --- a/pointer/pointer.go +++ b/pointer/pointer.go @@ -103,6 +103,62 @@ func Int32Equal(a, b *int32) bool { return *a == *b } +// Uint returns a pointer to an uint +func Uint(i uint) *uint { + return &i +} + +// UintPtr is a function variable referring to Uint. +// Deprecated: Use Uint instead. +var UintPtr = Uint // for back-compat + +// UintDeref dereferences the uint ptr and returns it if not nil, or else +// returns def. +func UintDeref(ptr *uint, def uint) uint { + if ptr != nil { + return *ptr + } + return def +} + +// UintPtrDerefOr is a function variable referring to UintDeref. +// Deprecated: Use UintDeref instead. +var UintPtrDerefOr = UintDeref // for back-compat + +// Uint32 returns a pointer to an uint32. +func Uint32(i uint32) *uint32 { + return &i +} + +// Uint32Ptr is a function variable referring to Uint32. +// Deprecated: Use Uint32 instead. +var Uint32Ptr = Uint32 // for back-compat + +// Uint32Deref dereferences the uint32 ptr and returns it if not nil, or else +// returns def. +func Uint32Deref(ptr *uint32, def uint32) uint32 { + if ptr != nil { + return *ptr + } + return def +} + +// Uint32PtrDerefOr is a function variable referring to Uint32Deref. +// Deprecated: Use Uint32Deref instead. +var Uint32PtrDerefOr = Uint32Deref // for back-compat + +// Uint32Equal returns true if both arguments are nil or both arguments +// dereference to the same value. +func Uint32Equal(a, b *uint32) bool { + if (a == nil) != (b == nil) { + return false + } + if a == nil { + return true + } + return *a == *b +} + // Int64 returns a pointer to an int64. func Int64(i int64) *int64 { return &i @@ -137,6 +193,40 @@ func Int64Equal(a, b *int64) bool { return *a == *b } +// Uint64 returns a pointer to an uint64. +func Uint64(i uint64) *uint64 { + return &i +} + +// Uint64Ptr is a function variable referring to Uint64. +// Deprecated: Use Uint64 instead. +var Uint64Ptr = Uint64 // for back-compat + +// Uint64Deref dereferences the uint64 ptr and returns it if not nil, or else +// returns def. +func Uint64Deref(ptr *uint64, def uint64) uint64 { + if ptr != nil { + return *ptr + } + return def +} + +// Uint64PtrDerefOr is a function variable referring to Uint64Deref. +// Deprecated: Use Uint64Deref instead. +var Uint64PtrDerefOr = Uint64Deref // for back-compat + +// Uint64Equal returns true if both arguments are nil or both arguments +// dereference to the same value. +func Uint64Equal(a, b *uint64) bool { + if (a == nil) != (b == nil) { + return false + } + if a == nil { + return true + } + return *a == *b +} + // Bool returns a pointer to a bool. func Bool(b bool) *bool { return &b diff --git a/pointer/pointer_test.go b/pointer/pointer_test.go index cd4480a8..a41286f0 100644 --- a/pointer/pointer_test.go +++ b/pointer/pointer_test.go @@ -144,6 +144,80 @@ func TestInt32Equal(t *testing.T) { } } +func TestUint(t *testing.T) { + val := uint(0) + ptr := Uint(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } + + val = uint(1) + ptr = Uint(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } +} + +func TestUintDeref(t *testing.T) { + var val, def uint = 1, 0 + + out := UintDeref(&val, def) + if out != val { + t.Errorf("expected %d, got %d", val, out) + } + + out = UintDeref(nil, def) + if out != def { + t.Errorf("expected %d, got %d", def, out) + } +} + +func TestUint32(t *testing.T) { + val := uint32(0) + ptr := Uint32(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } + + val = uint32(1) + ptr = Uint32(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } +} + +func TestUint32Deref(t *testing.T) { + var val, def uint32 = 1, 0 + + out := Uint32Deref(&val, def) + if out != val { + t.Errorf("expected %d, got %d", val, out) + } + + out = Uint32Deref(nil, def) + if out != def { + t.Errorf("expected %d, got %d", def, out) + } +} + +func TestUint32Equal(t *testing.T) { + if !Uint32Equal(nil, nil) { + t.Errorf("expected true (nil == nil)") + } + if !Uint32Equal(Uint32(123), Uint32(123)) { + t.Errorf("expected true (val == val)") + } + if Uint32Equal(nil, Uint32(123)) { + t.Errorf("expected false (nil != val)") + } + if Uint32Equal(Uint32(123), nil) { + t.Errorf("expected false (val != nil)") + } + if Uint32Equal(Uint32(123), Uint32(456)) { + t.Errorf("expected false (val != val)") + } +} + func TestInt64(t *testing.T) { val := int64(0) ptr := Int64(val) @@ -190,6 +264,52 @@ func TestInt64Equal(t *testing.T) { } } +func TestUint64(t *testing.T) { + val := uint64(0) + ptr := Uint64(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } + + val = uint64(1) + ptr = Uint64(val) + if *ptr != val { + t.Errorf("expected %d, got %d", val, *ptr) + } +} + +func TestUint64Deref(t *testing.T) { + var val, def uint64 = 1, 0 + + out := Uint64Deref(&val, def) + if out != val { + t.Errorf("expected %d, got %d", val, out) + } + + out = Uint64Deref(nil, def) + if out != def { + t.Errorf("expected %d, got %d", def, out) + } +} + +func TestUint64Equal(t *testing.T) { + if !Uint64Equal(nil, nil) { + t.Errorf("expected true (nil == nil)") + } + if !Uint64Equal(Uint64(123), Uint64(123)) { + t.Errorf("expected true (val == val)") + } + if Uint64Equal(nil, Uint64(123)) { + t.Errorf("expected false (nil != val)") + } + if Uint64Equal(Uint64(123), nil) { + t.Errorf("expected false (val != nil)") + } + if Uint64Equal(Uint64(123), Uint64(456)) { + t.Errorf("expected false (val != val)") + } +} + func TestBool(t *testing.T) { val := false ptr := Bool(val)