github.com/safing/portbase@v0.19.5/utils/slices_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	stringTestSlice  = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
    10  	stringTestSlice2 = []string{"a", "x", "x", "x", "x", "x", "x", "x", "x", "j"}
    11  	stringTestSlice3 = []string{"a", "x"}
    12  	byteTestSlice    = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    13  )
    14  
    15  func TestStringInSlice(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	if !StringInSlice(stringTestSlice, "a") {
    19  		t.Fatal("string reported not in slice (1), but it is")
    20  	}
    21  	if !StringInSlice(stringTestSlice, "d") {
    22  		t.Fatal("string reported not in slice (2), but it is")
    23  	}
    24  	if !StringInSlice(stringTestSlice, "j") {
    25  		t.Fatal("string reported not in slice (3), but it is")
    26  	}
    27  
    28  	if StringInSlice(stringTestSlice, "0") {
    29  		t.Fatal("string reported in slice (1), but is not")
    30  	}
    31  	if StringInSlice(stringTestSlice, "x") {
    32  		t.Fatal("string reported in slice (2), but is not")
    33  	}
    34  	if StringInSlice(stringTestSlice, "k") {
    35  		t.Fatal("string reported in slice (3), but is not")
    36  	}
    37  }
    38  
    39  func TestRemoveFromStringSlice(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	test1 := DuplicateStrings(stringTestSlice)
    43  	test1 = RemoveFromStringSlice(test1, "b")
    44  	if StringInSlice(test1, "b") {
    45  		t.Fatal("string reported in slice, but was removed")
    46  	}
    47  	if len(test1) != len(stringTestSlice)-1 {
    48  		t.Fatalf("new string slice length not as expected: is %d, should be %d\nnew slice is %v", len(test1), len(stringTestSlice)-1, test1)
    49  	}
    50  	RemoveFromStringSlice(test1, "b")
    51  }
    52  
    53  func TestDuplicateStrings(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	a := DuplicateStrings(stringTestSlice)
    57  	if !StringSliceEqual(a, stringTestSlice) {
    58  		t.Fatal("copied string slice is not equal")
    59  	}
    60  	a[0] = "x"
    61  	if StringSliceEqual(a, stringTestSlice) {
    62  		t.Fatal("copied string slice is not a real copy")
    63  	}
    64  }
    65  
    66  func TestStringSliceEqual(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	if !StringSliceEqual(stringTestSlice, stringTestSlice) {
    70  		t.Fatal("strings are equal, but are reported as not")
    71  	}
    72  	if StringSliceEqual(stringTestSlice, stringTestSlice2) {
    73  		t.Fatal("strings are not equal (1), but are reported as equal")
    74  	}
    75  	if StringSliceEqual(stringTestSlice, stringTestSlice3) {
    76  		t.Fatal("strings are not equal (1), but are reported as equal")
    77  	}
    78  }
    79  
    80  func TestDuplicateBytes(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	a := DuplicateBytes(byteTestSlice)
    84  	if !bytes.Equal(a, byteTestSlice) {
    85  		t.Fatal("copied bytes slice is not equal")
    86  	}
    87  	a[0] = 0xff
    88  	if bytes.Equal(a, byteTestSlice) {
    89  		t.Fatal("copied bytes slice is not a real copy")
    90  	}
    91  }