Skip to content

Commit b673b93

Browse files
author
Kwesi Rutledge
committed
Add in tests for VectorConstraint.Substitute()
1 parent 1821442 commit b673b93

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

testing/symbolic/vector_constraint_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,111 @@ func TestVectorConstraint_LinearEqualityConstraintRepresentation5(t *testing.T)
819819

820820
vc.LinearEqualityConstraintRepresentation()
821821
}
822+
823+
/*
824+
TestVectorConstraint_Substitute1
825+
Description:
826+
827+
This function tests that the Substitute method properly panics
828+
if the input scalar expression is not well defined.
829+
(In this case, the left hand side is a monomial that is not well-defined.)
830+
*/
831+
func TestVectorConstraint_Substitute1(t *testing.T) {
832+
// Setup
833+
N := 7
834+
x := symbolic.NewVariable()
835+
left := symbolic.VecDenseToKVector(symbolic.OnesVector(N)).ToMonomialVector()
836+
left[6] = symbolic.Monomial{
837+
Coefficient: 1,
838+
Exponents: []int{1, 0},
839+
VariableFactors: []symbolic.Variable{x},
840+
}
841+
right := symbolic.NewVariableVector(N)
842+
843+
// Create the vector constraint
844+
vc := symbolic.VectorConstraint{left, right, symbolic.SenseLessThanEqual}
845+
846+
// Create the function for handling the panic
847+
defer func() {
848+
r := recover()
849+
if r == nil {
850+
t.Errorf(
851+
"Expected vc.Substitute() to panic; received nil",
852+
)
853+
}
854+
855+
rAsError := r.(error)
856+
expectedError := left[6].Check()
857+
if !strings.Contains(rAsError.Error(), expectedError.Error()) {
858+
t.Errorf(
859+
"Expected vc.Substitute() to panic with error \"%v\"; received \"%v\"",
860+
expectedError.Error(),
861+
rAsError.Error(),
862+
)
863+
}
864+
}()
865+
866+
// Test
867+
vc.Substitute(x, x.Power(3).(symbolic.ScalarExpression))
868+
869+
// Raise an error if the test did not panic
870+
t.Errorf(
871+
"Expected vc.Substitute() to panic; received nil",
872+
)
873+
874+
}
875+
876+
/*
877+
TestVectorConstraint_Substitute2
878+
Description:
879+
880+
This function tests that the Substitute method properly returns
881+
a new vector constraint when the input scalar expressions are well-defined.
882+
In this case, we will start with a vector of monomials on the left and
883+
a vector of constants on the right.
884+
We will substitute a variable on the left hand side with a polynomial
885+
and expect the result to be a vector of polynomials on the left hand side
886+
and a vector of constants on the right hand side.
887+
*/
888+
func TestVectorConstraint_Substitute2(t *testing.T) {
889+
// Setup
890+
N := 7
891+
x := symbolic.NewVariableVector(N)
892+
left := x.ToMonomialVector()
893+
right := symbolic.VecDenseToKVector(symbolic.OnesVector(N))
894+
895+
// Create the vector constraint
896+
vc := symbolic.VectorConstraint{left, right, symbolic.SenseLessThanEqual}
897+
898+
// Create the new expression to replace one of the variables with:
899+
newExpr := x[0].Plus(x[1]).(symbolic.ScalarExpression)
900+
901+
// Substitute the variable
902+
newVC := vc.Substitute(x[0], newExpr).(symbolic.VectorConstraint)
903+
904+
// Check the left hand side
905+
// it should now be a vector of polynomials
906+
if _, ok := newVC.LeftHandSide.(symbolic.PolynomialVector); !ok {
907+
t.Errorf(
908+
"Expected vc.Substitute() to return a vector constraint with a polynomial vector on the left hand side; received %v",
909+
newVC.LeftHandSide,
910+
)
911+
}
912+
913+
// Check the right hand side
914+
if _, ok := newVC.RightHandSide.(symbolic.KVector); !ok {
915+
t.Errorf(
916+
"Expected vc.Substitute() to return a vector constraint with a constant vector on the right hand side; received %v",
917+
newVC.RightHandSide,
918+
)
919+
}
920+
921+
// Check the sense
922+
if newVC.Sense != vc.Sense {
923+
t.Errorf(
924+
"Expected vc.Substitute() to return a vector constraint with the same sense; received %v",
925+
newVC.Sense,
926+
)
927+
}
928+
929+
}

0 commit comments

Comments
 (0)