Merge pull request #85 from tendermint/85-indexing-2
IntInSlice and StringInSlice functions
This commit is contained in:
commit
850fd24ee9
|
@ -53,3 +53,13 @@ func PutInt64BE(dest []byte, i int64) {
|
|||
func GetInt64BE(src []byte) int64 {
|
||||
return int64(binary.BigEndian.Uint64(src))
|
||||
}
|
||||
|
||||
// IntInSlice returns true if a is found in the list.
|
||||
func IntInSlice(a int, list []int) bool {
|
||||
for _, b := range list {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIntInSlice(t *testing.T) {
|
||||
assert.True(t, IntInSlice(1, []int{1, 2, 3}))
|
||||
assert.False(t, IntInSlice(4, []int{1, 2, 3}))
|
||||
assert.True(t, IntInSlice(0, []int{0}))
|
||||
assert.False(t, IntInSlice(0, []int{}))
|
||||
}
|
|
@ -43,3 +43,13 @@ func StripHex(s string) string {
|
|||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// StringInSlice returns true if a is found the list.
|
||||
func StringInSlice(a string, list []string) bool {
|
||||
for _, b := range list {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStringInSlice(t *testing.T) {
|
||||
assert.True(t, StringInSlice("a", []string{"a", "b", "c"}))
|
||||
assert.False(t, StringInSlice("d", []string{"a", "b", "c"}))
|
||||
assert.True(t, StringInSlice("", []string{""}))
|
||||
assert.False(t, StringInSlice("", []string{}))
|
||||
}
|
Loading…
Reference in New Issue