mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Improve docs and tests of utils package
This commit is contained in:
parent
627939f7c6
commit
c9f41a65af
2 changed files with 14 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
func StringInSlice(s string, a []string) bool {
|
// StringInSlice returns whether the given string is in the string slice.
|
||||||
|
func StringInSlice(a []string, s string) bool {
|
||||||
for _, entry := range a {
|
for _, entry := range a {
|
||||||
if entry == s {
|
if entry == s {
|
||||||
return true
|
return true
|
||||||
|
@ -9,6 +10,7 @@ func StringInSlice(s string, a []string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveFromStringSlice removes the given string from the slice and returns a new slice.
|
||||||
func RemoveFromStringSlice(a []string, s string) []string {
|
func RemoveFromStringSlice(a []string, s string) []string {
|
||||||
for key, entry := range a {
|
for key, entry := range a {
|
||||||
if entry == s {
|
if entry == s {
|
||||||
|
@ -19,12 +21,14 @@ func RemoveFromStringSlice(a []string, s string) []string {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DuplicateStrings returns a new copy of the given string slice.
|
||||||
func DuplicateStrings(a []string) []string {
|
func DuplicateStrings(a []string) []string {
|
||||||
b := make([]string, len(a))
|
b := make([]string, len(a))
|
||||||
copy(b, a)
|
copy(b, a)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSliceEqual returns whether the given string slices are equal.
|
||||||
func StringSliceEqual(a []string, b []string) bool {
|
func StringSliceEqual(a []string, b []string) bool {
|
||||||
if len(a) != len(b) {
|
if len(a) != len(b) {
|
||||||
return false
|
return false
|
||||||
|
@ -37,6 +41,7 @@ func StringSliceEqual(a []string, b []string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DuplicateBytes returns a new copy of the given byte slice.
|
||||||
func DuplicateBytes(a []byte) []byte {
|
func DuplicateBytes(a []byte) []byte {
|
||||||
b := make([]byte, len(a))
|
b := make([]byte, len(a))
|
||||||
copy(b, a)
|
copy(b, a)
|
||||||
|
|
|
@ -13,23 +13,23 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStringInSlice(t *testing.T) {
|
func TestStringInSlice(t *testing.T) {
|
||||||
if !StringInSlice("a", stringTestSlice) {
|
if !StringInSlice(stringTestSlice, "a") {
|
||||||
t.Fatal("string reported not in slice (1), but it is")
|
t.Fatal("string reported not in slice (1), but it is")
|
||||||
}
|
}
|
||||||
if !StringInSlice("d", stringTestSlice) {
|
if !StringInSlice(stringTestSlice, "d") {
|
||||||
t.Fatal("string reported not in slice (2), but it is")
|
t.Fatal("string reported not in slice (2), but it is")
|
||||||
}
|
}
|
||||||
if !StringInSlice("j", stringTestSlice) {
|
if !StringInSlice(stringTestSlice, "j") {
|
||||||
t.Fatal("string reported not in slice (3), but it is")
|
t.Fatal("string reported not in slice (3), but it is")
|
||||||
}
|
}
|
||||||
|
|
||||||
if StringInSlice("0", stringTestSlice) {
|
if StringInSlice(stringTestSlice, "0") {
|
||||||
t.Fatal("string reported in slice (1), but is not")
|
t.Fatal("string reported in slice (1), but is not")
|
||||||
}
|
}
|
||||||
if StringInSlice("x", stringTestSlice) {
|
if StringInSlice(stringTestSlice, "x") {
|
||||||
t.Fatal("string reported in slice (2), but is not")
|
t.Fatal("string reported in slice (2), but is not")
|
||||||
}
|
}
|
||||||
if StringInSlice("k", stringTestSlice) {
|
if StringInSlice(stringTestSlice, "k") {
|
||||||
t.Fatal("string reported in slice (3), but is not")
|
t.Fatal("string reported in slice (3), but is not")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,12 +37,13 @@ func TestStringInSlice(t *testing.T) {
|
||||||
func TestRemoveFromStringSlice(t *testing.T) {
|
func TestRemoveFromStringSlice(t *testing.T) {
|
||||||
test1 := DuplicateStrings(stringTestSlice)
|
test1 := DuplicateStrings(stringTestSlice)
|
||||||
test1 = RemoveFromStringSlice(test1, "b")
|
test1 = RemoveFromStringSlice(test1, "b")
|
||||||
if StringInSlice("b", test1) {
|
if StringInSlice(test1, "b") {
|
||||||
t.Fatal("string reported in slice, but was removed")
|
t.Fatal("string reported in slice, but was removed")
|
||||||
}
|
}
|
||||||
if len(test1) != len(stringTestSlice)-1 {
|
if len(test1) != len(stringTestSlice)-1 {
|
||||||
t.Fatalf("new string slice length not as expected: is %d, should be %d\nnew slice is %v", len(test1), len(stringTestSlice)-1, test1)
|
t.Fatalf("new string slice length not as expected: is %d, should be %d\nnew slice is %v", len(test1), len(stringTestSlice)-1, test1)
|
||||||
}
|
}
|
||||||
|
RemoveFromStringSlice(test1, "b")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDuplicateStrings(t *testing.T) {
|
func TestDuplicateStrings(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue