Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking nil values in the equal function #2769

Merged
merged 2 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions types/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,25 @@ func equal(a, b Val) bool {
}
switch a.Tid {
case DateTimeID:
return a.Value.(time.Time).Equal((b.Value.(time.Time)))
aVal, aOk := a.Value.(time.Time)
bVal, bOk := b.Value.(time.Time)
return aOk && bOk && aVal.Equal(bVal)
case IntID:
return (a.Value.(int64)) == (b.Value.(int64))
aVal, aOk := a.Value.(int64)
bVal, bOk := b.Value.(int64)
return aOk && bOk && aVal == bVal
case FloatID:
return (a.Value.(float64)) == (b.Value.(float64))
aVal, aOk := a.Value.(float64)
bVal, bOk := b.Value.(float64)
return aOk && bOk && aVal == bVal
case StringID, DefaultID:
return (a.Value.(string)) == (b.Value.(string))
aVal, aOk := a.Value.(string)
bVal, bOk := b.Value.(string)
return aOk && bOk && aVal == bVal
case BoolID:
return a.Value.(bool) == (b.Value.(bool))
aVal, aOk := a.Value.(bool)
bVal, bOk := b.Value.(bool)
return aOk && bOk && aVal == bVal
}
return false
}
26 changes: 26 additions & 0 deletions types/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ func TestSortIntAndFloat(t *testing.T) {

}

func TestEqual(t *testing.T) {
require.True(t, equal(Val{Tid: IntID, Value: int64(3)}, Val{Tid: IntID, Value: int64(3)}),
"equal should return true for two equal values")

require.False(t, equal(Val{Tid: IntID, Value: int64(3)}, Val{Tid: IntID, Value: int64(4)}),
"equal should return false for two different values")

// not equal when the types are different
require.False(t, equal(Val{Tid: IntID, Value: int64(3)}, Val{Tid: FloatID, Value: float64(3.0)}),
"equal should return false for two values with different types")

// not equal when either parameter has the Value field being nil
require.False(t, equal(Val{Tid: IntID, Value: int64(3)}, Val{Tid: IntID}),
"equal should return false when either parameter cannot have its value converted")
require.False(t, equal(Val{Tid: IntID}, Val{Tid: IntID, Value: int64(3)}),
"equal should return false when either parameter cannot have its value converted")
require.False(t, equal(Val{Tid: IntID}, Val{Tid: IntID}), "equal should return false when either parameter cannot have its value converted")

// not equal when there is a type mismatch between value and tid for either parameter
require.False(t, equal(Val{Tid: IntID, Value: float64(3.0)}, Val{Tid: FloatID, Value: float64(3.0)}),
"equal should return false when either parameter's value has a type mismatch with its Tid")
require.False(t, equal(Val{Tid: FloatID, Value: float64(3.0)}, Val{Tid: IntID, Value: float64(3.0)}),
"equal should return false when either parameter's value has a type mismatch with its Tid")

}

func findIndex(t *testing.T, uids []uint64, uid uint64) int {
for i := range uids {
if uids[i] == uid {
Expand Down