From dc611282841dd084eef1a2ccd6838c66392a5d97 Mon Sep 17 00:00:00 2001 From: Kwesi Rutledge Date: Sat, 17 Jun 2023 14:41:54 -0400 Subject: [PATCH] Added Tests for FindInSlice --- testing/util_test.go | 202 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/testing/util_test.go b/testing/util_test.go index 7a1fbb5..721196b 100644 --- a/testing/util_test.go +++ b/testing/util_test.go @@ -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, + ) + } +}