Panic if provided value is not a slice

Signed-off-by: Evgenii Stratonikov <stratonikov@runbox.com>
This commit is contained in:
Evgenii Stratonikov 2021-12-28 14:27:29 +03:00 committed by Alex Vanin
parent fad35bbd3b
commit 22b833d972
2 changed files with 4 additions and 11 deletions

4
hrw.go
View file

@ -109,7 +109,7 @@ func SortSliceByWeightIndex(slice interface{}, weights []float64, hash uint64) {
func prepareRule(slice interface{}) []uint64 {
t := reflect.TypeOf(slice)
if t.Kind() != reflect.Slice {
return nil
panic("HRW sort expects slice, got " + t.Kind().String())
}
var (
@ -188,7 +188,7 @@ func prepareRule(slice interface{}) []uint64 {
default:
if _, ok := val.Index(0).Interface().(Hasher); !ok {
return nil
panic("slice elements must implement hrw.Hasher")
}
for i := 0; i < length; i++ {

View file

@ -113,15 +113,13 @@ func TestSortSliceByValueFail(t *testing.T) {
t.Run("must be slice", func(t *testing.T) {
actual := 10
hash := Hash(testKey)
require.NotPanics(t, func() { SortSliceByValue(actual, hash) })
require.Panics(t, func() { SortSliceByValue(actual, hash) })
})
t.Run("must 'fail' for unknown type", func(t *testing.T) {
actual := []unknown{1, 2, 3, 4, 5}
expect := []unknown{1, 2, 3, 4, 5}
hash := Hash(testKey)
SortSliceByValue(actual, hash)
require.Equal(t, expect, actual)
require.Panics(t, func() { SortSliceByValue(actual, hash) })
})
}
@ -175,11 +173,6 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
expect: []uint32{5, 1, 2, 0, 3, 4},
},
{
actual: Uint32Slice{0, 1, 2, 3, 4, 5},
expect: Uint32Slice{0, 1, 2, 3, 4, 5},
},
{
actual: []int64{0, 1, 2, 3, 4, 5},
expect: []int64{5, 3, 0, 1, 4, 2},