Skip to content

Commit ac36532

Browse files
author
Kwesi Rutledge
committed
Adding tests for VectorConstraint.SubstituteAccordingTo
1 parent b673b93 commit ac36532

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

testing/symbolic/vector_constraint_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,116 @@ func TestVectorConstraint_Substitute2(t *testing.T) {
927927
}
928928

929929
}
930+
931+
/*
932+
TestVectorConstraint_SubstituteAccordingTo1
933+
Description:
934+
935+
This function tests that the SubstituteAccordingTo method properly panics
936+
if the input scalar expression is not well defined.
937+
(In this case, the left hand side is a monomial that is not well-defined.)
938+
*/
939+
func TestVectorConstraint_SubstituteAccordingTo1(t *testing.T) {
940+
// Setup
941+
N := 7
942+
x := symbolic.NewVariable()
943+
left := symbolic.VecDenseToKVector(symbolic.OnesVector(N)).ToMonomialVector()
944+
left[6] = symbolic.Monomial{
945+
Coefficient: 1,
946+
Exponents: []int{1, 0},
947+
VariableFactors: []symbolic.Variable{x},
948+
}
949+
right := symbolic.NewVariableVector(N)
950+
951+
// Create the vector constraint
952+
vc := symbolic.VectorConstraint{left, right, symbolic.SenseLessThanEqual}
953+
954+
// Create the function for handling the panic
955+
defer func() {
956+
r := recover()
957+
if r == nil {
958+
t.Errorf(
959+
"Expected vc.SubstituteAccordingTo() to panic; received nil",
960+
)
961+
}
962+
963+
rAsError := r.(error)
964+
expectedError := left[6].Check()
965+
if !strings.Contains(rAsError.Error(), expectedError.Error()) {
966+
t.Errorf(
967+
"Expected vc.SubstituteAccordingTo() to panic with error \"%v\"; received \"%v\"",
968+
expectedError.Error(),
969+
rAsError.Error(),
970+
)
971+
}
972+
}()
973+
974+
// Test
975+
subMap := map[symbolic.Variable]symbolic.Expression{
976+
x: x.Power(3),
977+
}
978+
vc.SubstituteAccordingTo(subMap)
979+
980+
// Raise an error if the test did not panic
981+
t.Errorf(
982+
"Expected vc.SubstituteAccordingTo() to panic; received nil",
983+
)
984+
985+
}
986+
987+
/*
988+
TestVectorConstraint_SubstituteAccordingTo2
989+
Description:
990+
991+
This function tests that the SubstituteAccordingTo method properly returns
992+
a new vector constraint when the input scalar expressions are well-defined.
993+
In this case, we will start with a vector of monomials on the left and
994+
a vector of constants on the right.
995+
We will substitute a variable on the left hand side with a polynomial
996+
and expect the result to be a vector of polynomials on the left hand side
997+
and a vector of constants on the right hand side.
998+
*/
999+
func TestVectorConstraint_SubstituteAccordingTo2(t *testing.T) {
1000+
// Setup
1001+
N := 7
1002+
x := symbolic.NewVariableVector(N)
1003+
left := x.ToMonomialVector()
1004+
right := symbolic.VecDenseToKVector(symbolic.OnesVector(N))
1005+
1006+
// Create the vector constraint
1007+
vc := symbolic.VectorConstraint{left, right, symbolic.SenseLessThanEqual}
1008+
1009+
// Create the new expression to replace one of the variables with:
1010+
newExpr := x[0].Plus(x[1]).(symbolic.ScalarExpression)
1011+
1012+
// Substitute the variable
1013+
subMap := map[symbolic.Variable]symbolic.Expression{
1014+
x[0]: newExpr,
1015+
}
1016+
newVC := vc.SubstituteAccordingTo(subMap).(symbolic.VectorConstraint)
1017+
1018+
// Check the left hand side
1019+
// it should now be a vector of polynomials
1020+
if _, ok := newVC.LeftHandSide.(symbolic.PolynomialVector); !ok {
1021+
t.Errorf(
1022+
"Expected vc.SubstituteAccordingTo() to return a vector constraint with a polynomial vector on the left hand side; received %v",
1023+
newVC.LeftHandSide,
1024+
)
1025+
}
1026+
1027+
// Check the right hand side
1028+
if _, ok := newVC.RightHandSide.(symbolic.KVector); !ok {
1029+
t.Errorf(
1030+
"Expected vc.SubstituteAccordingTo() to return a vector constraint with a constant vector on the right hand side; received %v",
1031+
newVC.RightHandSide,
1032+
)
1033+
}
1034+
1035+
// Check the sense
1036+
if newVC.Sense != vc.Sense {
1037+
t.Errorf(
1038+
"Expected vc.SubstituteAccordingTo() to return a vector constraint with the same sense; received %v",
1039+
newVC.Sense,
1040+
)
1041+
}
1042+
}

0 commit comments

Comments
 (0)