Skip to content

Commit

Permalink
Added Tests for FindInSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
kwesiRutledge committed Jun 17, 2023
1 parent daabf5d commit dc61128
Showing 1 changed file with 202 additions and 0 deletions.
202 changes: 202 additions & 0 deletions testing/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,205 @@ func TestUtil_Identity2(t *testing.T) {
}

}

/*
TestUtil_FindInSlice1
Description:
Tests the find in slice function for strings!
(string in slice)
*/
func TestUtil_FindInSlice1(t *testing.T) {
// Constant
slice0 := []string{"Why", "test", "this", "?"}

// Find!
foundIndex, err := optim.FindInSlice("?", slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != 3 {
t.Errorf(
"Expected index of string to be 3; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice2
Description:
Tests the find in slice function for strings!
(string NOT in slice)
*/
func TestUtil_FindInSlice2(t *testing.T) {
// Constant
slice0 := []string{"Why", "test", "this", "?"}

// Find!
foundIndex, err := optim.FindInSlice("llama", slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != -1 {
t.Errorf(
"Expected index of string to be -1; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice3
Description:
Tests the find in slice function for ints!
(int in slice)
*/
func TestUtil_FindInSlice3(t *testing.T) {
// Constant
slice0 := []int{1, 3, 7, 11}

// Find!
foundIndex, err := optim.FindInSlice(1, slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != 0 {
t.Errorf(
"Expected index of string to be 0; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice4
Description:
Tests the find in slice function for ints!
(int NOT in slice)
*/
func TestUtil_FindInSlice4(t *testing.T) {
// Constant
slice0 := []int{1, 3, 7, 11}

// Find!
foundIndex, err := optim.FindInSlice(21, slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != -1 {
t.Errorf(
"Expected index of string to be -1; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice5
Description:
Tests the find in slice function for uint64!
(uint64 in slice)
*/
func TestUtil_FindInSlice5(t *testing.T) {
// Constant
slice0 := []uint64{1, 3, 7, 11}
var x uint64 = 1

// Find!
foundIndex, err := optim.FindInSlice(x, slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != 0 {
t.Errorf(
"Expected index of string to be 0; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice6
Description:
Tests the find in slice function for uint64!
(uint64 NOT in slice)
*/
func TestUtil_FindInSlice6(t *testing.T) {
// Constant
slice0 := []uint64{1, 3, 7, 11}
var x uint64 = 21

// Find!
foundIndex, err := optim.FindInSlice(x, slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != -1 {
t.Errorf(
"Expected index of string to be -1; received %v.", foundIndex,
)
}
}

/*
TestUtil_FindInSlice7
Description:
Tests the find in slice function for strings!
(Variable in slice)
*/
func TestUtil_FindInSlice7(t *testing.T) {
// Constant
m := optim.NewModel("test-util-findinslice7")
vv1 := m.AddVariableVector(100)
slice0 := vv1.Elements

// Find!
indexUnderTest := 2
foundIndex, err := optim.FindInSlice(vv1.Elements[indexUnderTest], slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != indexUnderTest {
t.Errorf(
"Expected index of string to be %v; received %v.",
indexUnderTest, foundIndex,
)
}
}

/*
TestUtil_FindInSlice4
Description:
Tests the find in slice function for strings!
(int NOT in slice)
*/
func TestUtil_FindInSlice8(t *testing.T) {
// Constant
m := optim.NewModel("test-util-findinslice7")
vv1 := m.AddVariableVector(100)
slice0 := vv1.Elements

// Find!
newVar := m.AddVariable()
foundIndex, err := optim.FindInSlice(newVar, slice0)
if err != nil {
t.Errorf("Received an error while trying to find valid string: %v", err)
}

if foundIndex != -1 {
t.Errorf(
"Expected index of string to be %v; received %v.",
-1, foundIndex,
)
}
}

0 comments on commit dc61128

Please sign in to comment.