Skip to content

Commit f7b49d3

Browse files
committed
Add support to compare uintptr
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent 0ab3ce1 commit f7b49d3

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

assert/assertion_compare.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var (
2828
uint32Type = reflect.TypeOf(uint32(1))
2929
uint64Type = reflect.TypeOf(uint64(1))
3030

31+
uintptrType = reflect.TypeOf(uintptr(1))
32+
3133
float32Type = reflect.TypeOf(float32(1))
3234
float64Type = reflect.TypeOf(float64(1))
3335

@@ -345,6 +347,26 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
345347

346348
return CompareType(bytes.Compare(bytesObj1, bytesObj2)), true
347349
}
350+
case reflect.Uintptr:
351+
{
352+
uintptrObj1, ok := obj1.(uintptr)
353+
if !ok {
354+
uintptrObj1 = obj1Value.Convert(uintptrType).Interface().(uintptr)
355+
}
356+
uintptrObj2, ok := obj2.(uintptr)
357+
if !ok {
358+
uintptrObj2 = obj2Value.Convert(uintptrType).Interface().(uintptr)
359+
}
360+
if uintptrObj1 > uintptrObj2 {
361+
return compareGreater, true
362+
}
363+
if uintptrObj1 == uintptrObj2 {
364+
return compareEqual, true
365+
}
366+
if uintptrObj1 < uintptrObj2 {
367+
return compareLess, true
368+
}
369+
}
348370
}
349371

350372
return compareEqual, false

assert/assertion_compare_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestCompare(t *testing.T) {
2222
type customFloat32 float32
2323
type customFloat64 float64
2424
type customString string
25+
type customUintptr uintptr
2526
for _, currCase := range []struct {
2627
less interface{}
2728
greater interface{}
@@ -52,6 +53,8 @@ func TestCompare(t *testing.T) {
5253
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
5354
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
5455
{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
56+
{less: uintptr(1), greater: uintptr(2), cType: "uintptr"},
57+
{less: customUintptr(1), greater: customUintptr(2), cType: "uint64"},
5558
} {
5659
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
5760
if !isComparable {
@@ -148,6 +151,7 @@ func TestGreater(t *testing.T) {
148151
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`},
149152
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`},
150153
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`},
154+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than "2"`},
151155
} {
152156
out := &outputT{buf: bytes.NewBuffer(nil)}
153157
False(t, Greater(out, currCase.less, currCase.greater))
@@ -189,6 +193,7 @@ func TestGreaterOrEqual(t *testing.T) {
189193
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`},
190194
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
191195
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
196+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than or equal to "2"`},
192197
} {
193198
out := &outputT{buf: bytes.NewBuffer(nil)}
194199
False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
@@ -230,6 +235,7 @@ func TestLess(t *testing.T) {
230235
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`},
231236
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`},
232237
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`},
238+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than "1"`},
233239
} {
234240
out := &outputT{buf: bytes.NewBuffer(nil)}
235241
False(t, Less(out, currCase.greater, currCase.less))
@@ -271,6 +277,7 @@ func TestLessOrEqual(t *testing.T) {
271277
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`},
272278
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
273279
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
280+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than or equal to "1"`},
274281
} {
275282
out := &outputT{buf: bytes.NewBuffer(nil)}
276283
False(t, LessOrEqual(out, currCase.greater, currCase.less))

0 commit comments

Comments
 (0)