mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-24 18:21:38 +00:00
Merge pull request #85 from tendermint/85-indexing-2
IntInSlice and StringInSlice functions
This commit is contained in:
@ -53,3 +53,13 @@ func PutInt64BE(dest []byte, i int64) {
|
|||||||
func GetInt64BE(src []byte) int64 {
|
func GetInt64BE(src []byte) int64 {
|
||||||
return int64(binary.BigEndian.Uint64(src))
|
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
|
||||||
|
}
|
||||||
|
14
common/int_test.go
Normal file
14
common/int_test.go
Normal file
@ -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
|
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
|
||||||
|
}
|
||||||
|
14
common/string_test.go
Normal file
14
common/string_test.go
Normal file
@ -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{}))
|
||||||
|
}
|
Reference in New Issue
Block a user