Skip to content

Commit 85db6dc

Browse files
author
Kwesi Rutledge
committed
Added tests for MatrixConstraint.Substitute and MatrixConstraint.SubstituteAccordingTo
1 parent c37016c commit 85db6dc

File tree

1 file changed

+210
-1
lines changed

1 file changed

+210
-1
lines changed

testing/symbolic/matrix_constraint_test.go

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ Description:
88

99
import (
1010
"fmt"
11+
"testing"
12+
1113
"github.com/MatProGo-dev/SymbolicMath.go/smErrors"
1214
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
13-
"testing"
1415
)
1516

1617
/*
@@ -432,3 +433,211 @@ func TestMatrixConstraint_At3(t *testing.T) {
432433

433434
mc.At(0, 0)
434435
}
436+
437+
/*
438+
TestMatrixConstraint_Substitute1
439+
Description:
440+
441+
Tests that the Substitute() method properly panics
442+
when the left hand side is not a well-formed matrix expression.
443+
(In this case, the left hand side is a monomial matrix that is not well-formed)
444+
*/
445+
func TestMatrixConstraint_Substitute1(t *testing.T) {
446+
// Constants
447+
left := symbolic.MonomialMatrix{}
448+
right := symbolic.DenseToKMatrix(symbolic.ZerosMatrix(3, 4))
449+
mc := symbolic.MatrixConstraint{left, right, symbolic.SenseLessThanEqual}
450+
451+
expectedError := left.Check()
452+
453+
// Test
454+
defer func() {
455+
r := recover()
456+
if r == nil {
457+
t.Errorf(
458+
"Expected mc.Substitute() to panic; did not panic",
459+
)
460+
}
461+
462+
// Check that the error is the expected error
463+
err, ok := r.(error)
464+
if !ok {
465+
t.Errorf(
466+
"Expected mc.Substitute() to panic with type error; received %T",
467+
r,
468+
)
469+
}
470+
471+
if err.Error() != expectedError.Error() {
472+
t.Errorf(
473+
"Expected mc.Substitute() to panic with error \"%v\"; received \"%v\"",
474+
expectedError,
475+
err,
476+
)
477+
}
478+
479+
}()
480+
481+
mc.Substitute(symbolic.NewVariable(), symbolic.NewVariable())
482+
483+
t.Errorf(
484+
"Expected mc.Substitute() to panic; did not panic",
485+
)
486+
}
487+
488+
/*
489+
TestMatrixConstraint_Substitute2
490+
Description:
491+
492+
Tests that the Substitute() method properly returns a
493+
well-formed matrix constraint when the left and right hand sides
494+
are well-formed and the sense is well-formed.
495+
The left hand side is a monomial matrix and the right hand side
496+
is a constant matrix.
497+
After substitution, the left hand side should be a polynomial matrix
498+
and the right hand side should be a constant matrix.
499+
*/
500+
func TestMatrixConstraint_Substitute2(t *testing.T) {
501+
// Constants
502+
v1 := symbolic.NewVariable()
503+
v2 := symbolic.NewVariable()
504+
left := symbolic.MonomialMatrix{
505+
{v1.ToMonomial(), v1.ToMonomial(), v1.ToMonomial(), v1.ToMonomial()},
506+
}
507+
right := symbolic.DenseToKMatrix(symbolic.ZerosMatrix(1, 4))
508+
mc := symbolic.MatrixConstraint{left, right, symbolic.SenseLessThanEqual}
509+
510+
// Test
511+
mcSubstituted := mc.Substitute(v1, v2.Plus(v2).(symbolic.ScalarExpression))
512+
513+
// Verify that mcSubstituted is a MatrixConstraint type
514+
mcSubstitutedAsMC, ok := mcSubstituted.(symbolic.MatrixConstraint)
515+
if !ok {
516+
t.Errorf(
517+
"Expected mcSubstituted to be of type MatrixConstraint; received %T",
518+
mcSubstituted,
519+
)
520+
}
521+
522+
// Verify that the left hand side is a polynomial matrix
523+
if _, ok := mcSubstitutedAsMC.LeftHandSide.(symbolic.PolynomialMatrix); !ok {
524+
t.Errorf(
525+
"Expected mcSubstituted.LeftHandSide to be of type PolynomialMatrix; received %T",
526+
mcSubstitutedAsMC.LeftHandSide,
527+
)
528+
}
529+
530+
// Verify that the right hand side is a constant matrix
531+
if _, ok := mcSubstitutedAsMC.RightHandSide.(symbolic.KMatrix); !ok {
532+
t.Errorf(
533+
"Expected mcSubstituted.RightHandSide to be of type KMatrix; received %T",
534+
mcSubstitutedAsMC.RightHandSide,
535+
)
536+
}
537+
}
538+
539+
/*
540+
TestMatrixConstraint_SubstituteAccordingTo1
541+
Description:
542+
543+
Tests that the SubstituteAccordingTo() method properly panics
544+
when the left hand side is not a well-formed matrix expression.
545+
(In this case, the left hand side is a monomial matrix that is not well-formed)
546+
*/
547+
func TestMatrixConstraint_SubstituteAccordingTo1(t *testing.T) {
548+
// Constants
549+
left := symbolic.MonomialMatrix{}
550+
right := symbolic.DenseToKMatrix(symbolic.ZerosMatrix(3, 4))
551+
mc := symbolic.MatrixConstraint{left, right, symbolic.SenseLessThanEqual}
552+
553+
expectedError := left.Check()
554+
555+
// Test
556+
defer func() {
557+
r := recover()
558+
if r == nil {
559+
t.Errorf(
560+
"Expected mc.SubstituteAccordingTo() to panic; did not panic",
561+
)
562+
}
563+
564+
// Check that the error is the expected error
565+
err, ok := r.(error)
566+
if !ok {
567+
t.Errorf(
568+
"Expected mc.SubstituteAccordingTo() to panic with type error; received %T",
569+
r,
570+
)
571+
}
572+
573+
if err.Error() != expectedError.Error() {
574+
t.Errorf(
575+
"Expected mc.SubstituteAccordingTo() to panic with error \"%v\"; received \"%v\"",
576+
expectedError,
577+
err,
578+
)
579+
}
580+
581+
}()
582+
583+
mc.SubstituteAccordingTo(map[symbolic.Variable]symbolic.Expression{})
584+
585+
t.Errorf(
586+
"Expected mc.SubstituteAccordingTo() to panic; did not panic",
587+
)
588+
}
589+
590+
/*
591+
TestMatrixConstraint_SubstituteAccordingTo2
592+
Description:
593+
594+
Tests that the SubstituteAccordingTo() method properly returns a
595+
well-formed matrix constraint when the left and right hand sides
596+
are well-formed and the sense is well-formed.
597+
The left hand side is a monomial matrix and the right hand side
598+
is a constant matrix.
599+
After substitution, the left hand side should be a polynomial matrix
600+
and the right hand side should be a constant matrix.
601+
*/
602+
func TestMatrixConstraint_SubstituteAccordingTo2(t *testing.T) {
603+
// Constants
604+
v1 := symbolic.NewVariable()
605+
v2 := symbolic.NewVariable()
606+
left := symbolic.MonomialMatrix{
607+
{v1.ToMonomial(), v1.ToMonomial(), v1.ToMonomial(), v1.ToMonomial()},
608+
}
609+
right := symbolic.DenseToKMatrix(symbolic.ZerosMatrix(1, 4))
610+
mc := symbolic.MatrixConstraint{left, right, symbolic.SenseLessThanEqual}
611+
612+
// Test
613+
mcSubstituted := mc.SubstituteAccordingTo(
614+
map[symbolic.Variable]symbolic.Expression{
615+
v1: v2.Plus(v2).(symbolic.ScalarExpression),
616+
},
617+
)
618+
619+
// Verify that mcSubstituted is a MatrixConstraint type
620+
mcSubstitutedAsMC, ok := mcSubstituted.(symbolic.MatrixConstraint)
621+
if !ok {
622+
t.Errorf(
623+
"Expected mcSubstituted to be of type MatrixConstraint; received %T",
624+
mcSubstituted,
625+
)
626+
}
627+
628+
// Verify that the left hand side is a polynomial matrix
629+
if _, ok := mcSubstitutedAsMC.LeftHandSide.(symbolic.PolynomialMatrix); !ok {
630+
t.Errorf(
631+
"Expected mcSubstituted.LeftHandSide to be of type PolynomialMatrix; received %T",
632+
mcSubstitutedAsMC.LeftHandSide,
633+
)
634+
}
635+
636+
// Verify that the right hand side is a constant matrix
637+
if _, ok := mcSubstitutedAsMC.RightHandSide.(symbolic.KMatrix); !ok {
638+
t.Errorf(
639+
"Expected mcSubstituted.RightHandSide to be of type KMatrix; received %T",
640+
mcSubstitutedAsMC.RightHandSide,
641+
)
642+
}
643+
}

0 commit comments

Comments
 (0)