Merge pull request #1265 from weicut/master

This commit is contained in:
John Guo
2021-06-02 13:11:59 +08:00
committed by GitHub
2 changed files with 35 additions and 2 deletions

View File

@ -77,12 +77,28 @@ func ComparatorUint64(a, b interface{}) int {
// ComparatorFloat32 provides a basic comparison on float32.
func ComparatorFloat32(a, b interface{}) int {
return int(gconv.Float32(a) - gconv.Float32(b))
aFloat := gconv.Float64(a)
bFloat := gconv.Float64(b)
if aFloat == bFloat {
return 0
}
if aFloat > bFloat {
return 1
}
return -1
}
// ComparatorFloat64 provides a basic comparison on float64.
func ComparatorFloat64(a, b interface{}) int {
return int(gconv.Float64(a) - gconv.Float64(b))
aFloat := gconv.Float64(a)
bFloat := gconv.Float64(b)
if aFloat == bFloat {
return 0
}
if aFloat > bFloat {
return 1
}
return -1
}
// ComparatorByte provides a basic comparison on byte.

View File

@ -160,3 +160,20 @@ func Test_ComparatorTime(t *testing.T) {
t.Assert(l, -1)
})
}
func Test_ComparatorFloat32OfFixed(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gutil.ComparatorFloat32(0.1, 0.1), 0)
t.Assert(gutil.ComparatorFloat32(1.1, 2.1), -1)
t.Assert(gutil.ComparatorFloat32(2.1, 1.1), 1)
})
}
func Test_ComparatorFloat64OfFixed(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gutil.ComparatorFloat64(0.1, 0.1), 0)
t.Assert(gutil.ComparatorFloat64(1.1, 2.1), -1)
t.Assert(gutil.ComparatorFloat64(2.1, 1.1), 1)
})
}