Skip to content

Commit e045612

Browse files
Merge pull request #1339 from bogdandrutu/uintptr
Add support to compare uintptr
2 parents 5b6926d + 21ba5d2 commit e045612

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

assert/assertion_compare.go

+22
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

+12-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
func TestCompare(t *testing.T) {
13+
type customString string
1314
type customInt int
1415
type customInt8 int8
1516
type customInt16 int16
@@ -22,7 +23,7 @@ func TestCompare(t *testing.T) {
2223
type customUInt64 uint64
2324
type customFloat32 float32
2425
type customFloat64 float64
25-
type customString string
26+
type customUintptr uintptr
2627
type customTime time.Time
2728
type customBytes []byte
2829
for _, currCase := range []struct {
@@ -55,6 +56,8 @@ func TestCompare(t *testing.T) {
5556
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
5657
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
5758
{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
59+
{less: uintptr(1), greater: uintptr(2), cType: "uintptr"},
60+
{less: customUintptr(1), greater: customUintptr(2), cType: "uint64"},
5861
{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
5962
{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
6063
{less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"},
@@ -155,8 +158,9 @@ func TestGreater(t *testing.T) {
155158
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`},
156159
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`},
157160
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`},
158-
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`},
161+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than "2"`},
159162
{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than "0001-01-01 01:00:00 +0000 UTC"`},
163+
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`},
160164
} {
161165
out := &outputT{buf: bytes.NewBuffer(nil)}
162166
False(t, Greater(out, currCase.less, currCase.greater))
@@ -198,8 +202,9 @@ func TestGreaterOrEqual(t *testing.T) {
198202
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`},
199203
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
200204
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
201-
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`},
205+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than or equal to "2"`},
202206
{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than or equal to "0001-01-01 01:00:00 +0000 UTC"`},
207+
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`},
203208
} {
204209
out := &outputT{buf: bytes.NewBuffer(nil)}
205210
False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
@@ -241,8 +246,9 @@ func TestLess(t *testing.T) {
241246
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`},
242247
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`},
243248
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`},
244-
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`},
249+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than "1"`},
245250
{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than "0001-01-01 00:00:00 +0000 UTC"`},
251+
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`},
246252
} {
247253
out := &outputT{buf: bytes.NewBuffer(nil)}
248254
False(t, Less(out, currCase.greater, currCase.less))
@@ -284,8 +290,9 @@ func TestLessOrEqual(t *testing.T) {
284290
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`},
285291
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
286292
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
287-
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`},
293+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than or equal to "1"`},
288294
{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than or equal to "0001-01-01 00:00:00 +0000 UTC"`},
295+
{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`},
289296
} {
290297
out := &outputT{buf: bytes.NewBuffer(nil)}
291298
False(t, LessOrEqual(out, currCase.greater, currCase.less))

0 commit comments

Comments
 (0)