Merge pull request #4607 from jimashi/develop
add template functions eq,lt to support uint and int compare.
This commit is contained in:
commit
fea7c914cc
@ -1,5 +1,6 @@
|
||||
# developing
|
||||
|
||||
- Add template functions eq,lt to support uint and int compare. [4607](https://github.com/beego/beego/pull/4607)
|
||||
- Migrate tests to GitHub Actions. [4663](https://github.com/beego/beego/issues/4663)
|
||||
- Add http client and option func. [4455](https://github.com/beego/beego/issues/4455)
|
||||
- Add: Convenient way to generate mock object [4620](https://github.com/beego/beego/issues/4620)
|
||||
|
||||
@ -606,10 +606,23 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
truth := false
|
||||
if k1 != k2 {
|
||||
// Special case: Can compare integer values regardless of type's sign.
|
||||
switch {
|
||||
case k1 == intKind && k2 == uintKind:
|
||||
truth = v1.Int() >= 0 && uint64(v1.Int()) == v2.Uint()
|
||||
case k1 == uintKind && k2 == intKind:
|
||||
truth = v2.Int() >= 0 && v1.Uint() == uint64(v2.Int())
|
||||
default:
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
if truth {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
switch k1 {
|
||||
case boolKind:
|
||||
truth = v1.Bool() == v2.Bool()
|
||||
@ -652,10 +665,18 @@ func lt(arg1, arg2 interface{}) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
truth := false
|
||||
if k1 != k2 {
|
||||
// Special case: Can compare integer values regardless of type's sign.
|
||||
switch {
|
||||
case k1 == intKind && k2 == uintKind:
|
||||
truth = v1.Int() < 0 || uint64(v1.Int()) < v2.Uint()
|
||||
case k1 == uintKind && k2 == intKind:
|
||||
truth = v2.Int() >= 0 && v1.Uint() < uint64(v2.Int())
|
||||
default:
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
} else {
|
||||
switch k1 {
|
||||
case boolKind, complexKind:
|
||||
return false, errBadComparisonType
|
||||
@ -668,7 +689,8 @@ func lt(arg1, arg2 interface{}) (bool, error) {
|
||||
case uintKind:
|
||||
truth = v1.Uint() < v2.Uint()
|
||||
default:
|
||||
panic("invalid kind")
|
||||
return false, errBadComparisonType
|
||||
}
|
||||
}
|
||||
return truth, nil
|
||||
}
|
||||
|
||||
@ -377,3 +377,206 @@ func TestMapGet(t *testing.T) {
|
||||
t.Errorf("Error happens %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_eq(t *testing.T) {
|
||||
tests := []struct {
|
||||
a interface{}
|
||||
b interface{}
|
||||
result bool
|
||||
}{
|
||||
{uint8(1), int(1), true},
|
||||
{uint8(3), int(1), false},
|
||||
{uint16(1), int(1), true},
|
||||
{uint16(3), int(1), false},
|
||||
{uint32(1), int(1), true},
|
||||
{uint32(3), int(1), false},
|
||||
{uint(1), int(1), true},
|
||||
{uint(3), int(1), false},
|
||||
{uint64(1), int(1), true},
|
||||
{uint64(3), int(1), false},
|
||||
{int8(-1), uint(1), false},
|
||||
{int16(-2), uint(1), false},
|
||||
{int32(-3), uint(1), false},
|
||||
{int64(-4), uint(1), false},
|
||||
{int8(1), uint(1), true},
|
||||
{int16(1), uint(1), true},
|
||||
{int32(1), uint(1), true},
|
||||
{int64(1), uint(1), true},
|
||||
{int8(-1), uint8(1), false},
|
||||
{int16(-2), uint8(1), false},
|
||||
{int32(-3), uint8(1), false},
|
||||
{int64(-4), uint8(1), false},
|
||||
{int8(1), uint8(1), true},
|
||||
{int16(1), uint8(1), true},
|
||||
{int32(1), uint8(1), true},
|
||||
{int64(1), uint8(1), true},
|
||||
{int8(-1), uint16(1), false},
|
||||
{int16(-2), uint16(1), false},
|
||||
{int32(-3), uint16(1), false},
|
||||
{int64(-4), uint16(1), false},
|
||||
{int8(1), uint16(1), true},
|
||||
{int16(1), uint16(1), true},
|
||||
{int32(1), uint16(1), true},
|
||||
{int64(1), uint16(1), true},
|
||||
{int8(-1), uint32(1), false},
|
||||
{int16(-2), uint32(1), false},
|
||||
{int32(-3), uint32(1), false},
|
||||
{int64(-4), uint32(1), false},
|
||||
{int8(1), uint32(1), true},
|
||||
{int16(1), uint32(1), true},
|
||||
{int32(1), uint32(1), true},
|
||||
{int64(1), uint32(1), true},
|
||||
{int8(-1), uint64(1), false},
|
||||
{int16(-2), uint64(1), false},
|
||||
{int32(-3), uint64(1), false},
|
||||
{int64(-4), uint64(1), false},
|
||||
{int8(1), uint64(1), true},
|
||||
{int16(1), uint64(1), true},
|
||||
{int32(1), uint64(1), true},
|
||||
{int64(1), uint64(1), true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if res, err := eq(test.a, test.b); err != nil {
|
||||
if res != test.result {
|
||||
t.Errorf("a:%v(%T) equals b:%v(%T) should be %v", test.a, test.a, test.b, test.b, test.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_lt(t *testing.T) {
|
||||
tests := []struct {
|
||||
a interface{}
|
||||
b interface{}
|
||||
result bool
|
||||
}{
|
||||
{uint8(1), int(3), true},
|
||||
{uint8(1), int(1), false},
|
||||
{uint8(3), int(1), false},
|
||||
{uint16(1), int(3), true},
|
||||
{uint16(1), int(1), false},
|
||||
{uint16(3), int(1), false},
|
||||
{uint32(1), int(3), true},
|
||||
{uint32(1), int(1), false},
|
||||
{uint32(3), int(1), false},
|
||||
{uint(1), int(3), true},
|
||||
{uint(1), int(1), false},
|
||||
{uint(3), int(1), false},
|
||||
{uint64(1), int(3), true},
|
||||
{uint64(1), int(1), false},
|
||||
{uint64(3), int(1), false},
|
||||
{int(-1), int(1), true},
|
||||
{int(1), int(1), false},
|
||||
{int(1), int(3), true},
|
||||
{int(3), int(1), false},
|
||||
{int8(-1), uint(1), true},
|
||||
{int8(1), uint(1), false},
|
||||
{int8(1), uint(3), true},
|
||||
{int8(3), uint(1), false},
|
||||
{int16(-1), uint(1), true},
|
||||
{int16(1), uint(1), false},
|
||||
{int16(1), uint(3), true},
|
||||
{int16(3), uint(1), false},
|
||||
{int32(-1), uint(1), true},
|
||||
{int32(1), uint(1), false},
|
||||
{int32(1), uint(3), true},
|
||||
{int32(3), uint(1), false},
|
||||
{int64(-1), uint(1), true},
|
||||
{int64(1), uint(1), false},
|
||||
{int64(1), uint(3), true},
|
||||
{int64(3), uint(1), false},
|
||||
{int(-1), uint(1), true},
|
||||
{int(1), uint(1), false},
|
||||
{int(1), uint(3), true},
|
||||
{int(3), uint(1), false},
|
||||
{int8(-1), uint8(1), true},
|
||||
{int8(1), uint8(1), false},
|
||||
{int8(1), uint8(3), true},
|
||||
{int8(3), uint8(1), false},
|
||||
{int16(-1), uint8(1), true},
|
||||
{int16(1), uint8(1), false},
|
||||
{int16(1), uint8(3), true},
|
||||
{int16(3), uint8(1), false},
|
||||
{int32(-1), uint8(1), true},
|
||||
{int32(1), uint8(1), false},
|
||||
{int32(1), uint8(3), true},
|
||||
{int32(3), uint8(1), false},
|
||||
{int64(-1), uint8(1), true},
|
||||
{int64(1), uint8(1), false},
|
||||
{int64(1), uint8(3), true},
|
||||
{int64(3), uint8(1), false},
|
||||
{int(-1), uint8(1), true},
|
||||
{int(1), uint8(1), false},
|
||||
{int(1), uint8(3), true},
|
||||
{int(3), uint8(1), false},
|
||||
{int8(-1), uint16(1), true},
|
||||
{int8(1), uint16(1), false},
|
||||
{int8(1), uint16(3), true},
|
||||
{int8(3), uint16(1), false},
|
||||
{int16(-1), uint16(1), true},
|
||||
{int16(1), uint16(1), false},
|
||||
{int16(1), uint16(3), true},
|
||||
{int16(3), uint16(1), false},
|
||||
{int32(-1), uint16(1), true},
|
||||
{int32(1), uint16(1), false},
|
||||
{int32(1), uint16(3), true},
|
||||
{int32(3), uint16(1), false},
|
||||
{int64(-1), uint16(1), true},
|
||||
{int64(1), uint16(1), false},
|
||||
{int64(1), uint16(3), true},
|
||||
{int64(3), uint16(1), false},
|
||||
{int(-1), uint16(1), true},
|
||||
{int(1), uint16(1), false},
|
||||
{int(1), uint16(3), true},
|
||||
{int(3), uint16(1), false},
|
||||
{int8(-1), uint32(1), true},
|
||||
{int8(1), uint32(1), false},
|
||||
{int8(1), uint32(3), true},
|
||||
{int8(3), uint32(1), false},
|
||||
{int16(-1), uint32(1), true},
|
||||
{int16(1), uint32(1), false},
|
||||
{int16(1), uint32(3), true},
|
||||
{int16(3), uint32(1), false},
|
||||
{int32(-1), uint32(1), true},
|
||||
{int32(1), uint32(1), false},
|
||||
{int32(1), uint32(3), true},
|
||||
{int32(3), uint32(1), false},
|
||||
{int64(-1), uint32(1), true},
|
||||
{int64(1), uint32(1), false},
|
||||
{int64(1), uint32(3), true},
|
||||
{int64(3), uint32(1), false},
|
||||
{int(-1), uint32(1), true},
|
||||
{int(1), uint32(1), false},
|
||||
{int(1), uint32(3), true},
|
||||
{int(3), uint32(1), false},
|
||||
{int8(-1), uint64(1), true},
|
||||
{int8(1), uint64(1), false},
|
||||
{int8(1), uint64(3), true},
|
||||
{int8(3), uint64(1), false},
|
||||
{int16(-1), uint64(1), true},
|
||||
{int16(1), uint64(1), false},
|
||||
{int16(1), uint64(3), true},
|
||||
{int16(3), uint64(1), false},
|
||||
{int32(-1), uint64(1), true},
|
||||
{int32(1), uint64(1), false},
|
||||
{int32(1), uint64(3), true},
|
||||
{int32(3), uint64(1), false},
|
||||
{int64(-1), uint64(1), true},
|
||||
{int64(1), uint64(1), false},
|
||||
{int64(1), uint64(3), true},
|
||||
{int64(3), uint64(1), false},
|
||||
{int(-1), uint64(1), true},
|
||||
{int(1), uint64(1), false},
|
||||
{int(1), uint64(3), true},
|
||||
{int(3), uint64(1), false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if res, err := lt(test.a, test.b); err != nil {
|
||||
if res != test.result {
|
||||
t.Errorf("a:%v(%T) lt b:%v(%T) should be %v", test.a, test.a, test.b, test.b, test.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user