Skip to content

Commit 9dc9f8d

Browse files
author
Kwesi Rutledge
committed
Added more tests for coverage of new functions
1 parent aacd0d8 commit 9dc9f8d

File tree

2 files changed

+182
-2
lines changed

2 files changed

+182
-2
lines changed

symbolic/scalar_constraint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ func (sc ScalarConstraint) ImpliesThisIsAlsoSatisfied(other Constraint) bool {
350350

351351
// Simplify both constraints
352352
sc = sc.Simplify()
353-
other = other.AsSimplifiedConstraint().(ScalarConstraint)
354353

355354
switch otherC := other.(type) {
356355
case ScalarConstraint:
357-
// Continue
356+
otherC = otherC.Simplify()
357+
358358
// Naive implication check:
359359
// 1. Both constraints contain only 1 variable AND it is the same variable. Then, simply check the bounds.
360360
containsOneVar := len(sc.Variables()) == 1 && len(otherC.Variables()) == 1

testing/symbolic/matrix_constraint_test.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,183 @@ func TestMatrixConstraint_AsSimplifiedConstraint1(t *testing.T) {
693693
)
694694

695695
}
696+
697+
/*
698+
TestMatrixConstraint_Variables1
699+
Description:
700+
701+
Verifies that the Variables() method works as expected.
702+
We verify that the method properly returns 9 unique variables when
703+
we create a constraint between a 3x3 matrix of variables and a
704+
3x3 matrix of constants.
705+
*/
706+
func TestMatrixConstraint_Variables1(t *testing.T) {
707+
// Constants
708+
vm1 := symbolic.NewVariableMatrix(3, 3)
709+
km2 := symbolic.DenseToKMatrix(symbolic.OnesMatrix(3, 3))
710+
711+
mConstr := symbolic.MatrixConstraint{vm1, km2, symbolic.SenseLessThanEqual}
712+
713+
// Test
714+
vars := mConstr.Variables()
715+
716+
if len(vars) != 9 {
717+
t.Errorf(
718+
"Expected len(vars) to be 9; received %v",
719+
len(vars),
720+
)
721+
}
722+
// Check that each variable is unique
723+
varsMap := make(map[symbolic.Variable]bool)
724+
for _, v := range vars {
725+
if _, ok := varsMap[v]; ok {
726+
t.Errorf(
727+
"Expected all variables to be unique; received duplicate variable %v",
728+
v,
729+
)
730+
}
731+
varsMap[v] = true
732+
}
733+
}
734+
735+
/*
736+
TestMatrixConstraint_ImpliesThisIsAlsoSatisfied1
737+
Description:
738+
739+
Verifies that the ImpliesThisIsAlsoSatisfied method works as expected
740+
when the input constraint is a scalar constraint that matches one of
741+
the constraints in the original matrix constraint (i.e., the scalar constraint
742+
is just the (0,1)-th constraint in the original matrix constraint).
743+
*/
744+
func TestMatrixConstraint_ImpliesThisIsAlsoSatisfied1(t *testing.T) {
745+
// Constants
746+
vm1 := symbolic.NewVariableMatrix(3, 3)
747+
km2 := symbolic.DenseToKMatrix(symbolic.OnesMatrix(3, 3))
748+
749+
mConstr := symbolic.MatrixConstraint{vm1, km2, symbolic.SenseLessThanEqual}
750+
751+
// Extract the (0,1)-th constraint from mConstr
752+
scalarConstraint := mConstr.At(0, 1)
753+
754+
// Test
755+
if !mConstr.ImpliesThisIsAlsoSatisfied(scalarConstraint) {
756+
t.Errorf(
757+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(scalarConstraint) to be true; received false",
758+
)
759+
}
760+
}
761+
762+
/*
763+
TestMatrixConstraint_ImpliesThisIsAlsoSatisfied2
764+
Description:
765+
766+
Verifies that the ImpliesThisIsAlsoSatisfied method correctly panics
767+
when the input matrix constraint is malformed.
768+
*/
769+
func TestMatrixConstraint_ImpliesThisIsAlsoSatisfied2(t *testing.T) {
770+
// Constants
771+
vm1 := symbolic.NewVariableMatrix(3, 2)
772+
km2 := symbolic.DenseToKMatrix(symbolic.OnesMatrix(3, 3))
773+
774+
// Create matrix constraint
775+
mConstr := symbolic.MatrixConstraint{vm1, km2, symbolic.SenseLessThanEqual}
776+
777+
// Create normal matrix constraint to use as input
778+
left := symbolic.DenseToKMatrix(symbolic.Identity(3))
779+
right := symbolic.DenseToKMatrix(symbolic.ZerosMatrix(3, 3))
780+
normalMC := symbolic.MatrixConstraint{left, right, symbolic.SenseLessThanEqual}
781+
782+
expectedError := mConstr.Check()
783+
784+
// Test
785+
defer func() {
786+
r := recover()
787+
if r == nil {
788+
t.Errorf(
789+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(normalMC) to panic; did not panic",
790+
)
791+
}
792+
793+
// Check that the error is the expected error
794+
err, ok := r.(error)
795+
if !ok {
796+
t.Errorf(
797+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(normalMC) to panic with type error; received %T",
798+
r,
799+
)
800+
}
801+
if err.Error() != expectedError.Error() {
802+
t.Errorf(
803+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(normalMC) to panic with error \"%v\"; received \"%v\"",
804+
expectedError,
805+
err,
806+
)
807+
}
808+
809+
}()
810+
811+
// Call ImpliesThisIsAlsoSatisfied
812+
mConstr.ImpliesThisIsAlsoSatisfied(normalMC)
813+
814+
t.Errorf(
815+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(normalMC) to panic; did not panic",
816+
)
817+
}
818+
819+
/*
820+
TestConstraint_ImpliesThisIsAlsoSatisfied3
821+
Description:
822+
823+
Verifies that the ImpliesThisIsAlsoSatisfied method correctly panics
824+
when the receiver is well-defined but the input constraint is not well-formed.
825+
*/
826+
func TestConstraint_ImpliesThisIsAlsoSatisfied3(t *testing.T) {
827+
// Constants
828+
vm1 := symbolic.NewVariableMatrix(3, 3)
829+
km2 := symbolic.DenseToKMatrix(symbolic.OnesMatrix(3, 3))
830+
831+
// Create matrix constraint
832+
mConstr := symbolic.MatrixConstraint{vm1, km2, symbolic.SenseLessThanEqual}
833+
834+
// Create malformed scalar constraint to use as input
835+
scLeft := symbolic.Monomial{
836+
Exponents: []int{1},
837+
}
838+
scRight := symbolic.K(5)
839+
sc := symbolic.ScalarConstraint{scLeft, scRight, symbolic.SenseEqual}
840+
expectedError := sc.Check()
841+
842+
// Test
843+
defer func() {
844+
r := recover()
845+
if r == nil {
846+
t.Errorf(
847+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(sc) to panic; did not panic",
848+
)
849+
}
850+
851+
// Check that the error is the expected error
852+
err, ok := r.(error)
853+
if !ok {
854+
t.Errorf(
855+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(sc) to panic with type error; received %T",
856+
r,
857+
)
858+
}
859+
if err.Error() != expectedError.Error() {
860+
t.Errorf(
861+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(sc) to panic with error \"%v\"; received \"%v\"",
862+
expectedError,
863+
err,
864+
)
865+
}
866+
867+
}()
868+
869+
// Call ImpliesThisIsAlsoSatisfied
870+
mConstr.ImpliesThisIsAlsoSatisfied(sc)
871+
872+
t.Errorf(
873+
"Expected mConstr.ImpliesThisIsAlsoSatisfied(sc) to panic; did not panic",
874+
)
875+
}

0 commit comments

Comments
 (0)