diff --git a/symbolic/vector_expression.go b/symbolic/vector_expression.go index ea3ac61..120e04f 100644 --- a/symbolic/vector_expression.go +++ b/symbolic/vector_expression.go @@ -344,12 +344,9 @@ Description: */ func VectorSubstituteTemplate(ve VectorExpression, vIn Variable, se ScalarExpression) VectorExpression { // Input Processing - var err error - for ii := 0; ii < ve.Len(); ii++ { - err = ve.AtVec(ii).Check() - if err != nil { - panic(fmt.Errorf("idx #%v produced error: %v", ii, err)) - } + err := ve.Check() + if err != nil { + panic(err) } err = vIn.Check() diff --git a/testing/symbolic/vector_expression_test.go b/testing/symbolic/vector_expression_test.go index bfec562..27cb931 100644 --- a/testing/symbolic/vector_expression_test.go +++ b/testing/symbolic/vector_expression_test.go @@ -2,6 +2,7 @@ package symbolic_test import ( "fmt" + "github.com/MatProGo-dev/SymbolicMath.go/smErrors" "github.com/MatProGo-dev/SymbolicMath.go/symbolic" "strings" "testing" @@ -195,3 +196,236 @@ func TestVectorExpression_ConcretizeVectorExpression7(t *testing.T) { symbolic.ConcretizeVectorExpression([]symbolic.ScalarExpression{}) t.Errorf("Problem! The function did not panic when the input slice was empty") } + +/* +TestVectorExpression_ConcretizeVectorExpression8 +Description: + + Tests that the function correctly generates a polynomial vector from a slice of ScalarExpression objects, + one is a polynomial, one is a monomial, and the other is a variable. +*/ +func TestVectorExpression_ConcretizeVectorExpression8(t *testing.T) { + // Constants + p := symbolic.NewVariable().ToPolynomial() + m := symbolic.NewVariable().ToMonomial() + v := symbolic.NewVariable() + slice := []symbolic.ScalarExpression{p, m, v} + + // Test + pv := symbolic.ConcretizeVectorExpression(slice) + + vOut, tf := pv.(symbolic.PolynomialVector) + if !tf { + t.Errorf("expected a PolynomialVector; received %T", pv) + } + + if len(vOut) != 3 { + t.Errorf("expected a PolynomialVector of length 3; received %v", len(vOut)) + } +} + +/* +TestVectorExpression_VectorSubstituteTemplate1 +Description: + + Verify that the VectorSubstituteTemplate() function panics when the input vector is + not a well-defined VectorExpression. +*/ +func TestVectorExpression_VectorSubstituteTemplate1(t *testing.T) { + // Constants + testVec := symbolic.VariableVector{ + symbolic.NewVariable(), + symbolic.Variable{}, + } + + // Test + defer func() { + r := recover() + if r == nil { + t.Errorf("expected a panic when the input vector is not a well-defined VectorExpression; received nil") + } + + rAsE, tf := r.(error) + if !tf { + t.Errorf("expected a panic of type error; received %T", r) + } + + if !strings.Contains( + rAsE.Error(), + testVec.Check().Error(), + ) { + t.Errorf("expected error message to contain %v; received %v", + "unexpected expression type in vector expression: symbolic.Variable", + rAsE.Error(), + ) + } + }() + symbolic.VectorSubstituteTemplate(testVec, symbolic.NewVariable(), symbolic.NewVariable()) + t.Errorf("Problem! The function did not panic when the input vector was not a well-defined VectorExpression") +} + +/* +TestVectorExpression_VectorSubstituteTemplate2 +Description: + + Verify that the VectorSubstituteTemplate() function panics when the input vector is + well-defined but the input target variable is not well-defined. +*/ +func TestVectorExpression_VectorSubstituteTemplate2(t *testing.T) { + // Constants + testVec := symbolic.VariableVector{ + symbolic.NewVariable(), + symbolic.NewVariable(), + } + badVar := symbolic.Variable{} + + // Test + defer func() { + r := recover() + if r == nil { + t.Errorf("expected a panic when the input target variable is not well-defined; received nil") + } + + rAsE, tf := r.(error) + if !tf { + t.Errorf("expected a panic of type error; received %T", r) + } + + if !strings.Contains( + rAsE.Error(), + badVar.Check().Error(), + ) { + t.Errorf("expected error message to contain %v; received %v", + badVar.Check().Error(), + rAsE.Error(), + ) + } + }() + symbolic.VectorSubstituteTemplate(testVec, badVar, symbolic.NewVariable()) + t.Errorf("Problem! The function did not panic when the input target variable was not well-defined") +} + +/* +TestVectorExpression_VectorSubstituteTemplate3 +Description: + + Verify that the VectorSubstituteTemplate() function panics when the input vector is + well-defined, the input target variable is well-defined, but the input scalar expression + is not well-defined. +*/ +func TestVectorExpression_VectorSubstituteTemplate3(t *testing.T) { + // Constants + testVec := symbolic.VariableVector{ + symbolic.NewVariable(), + symbolic.NewVariable(), + } + badSE := symbolic.Variable{} + + // Test + defer func() { + r := recover() + if r == nil { + t.Errorf("expected a panic when the input scalar expression is not well-defined; received nil") + } + + rAsE, tf := r.(error) + if !tf { + t.Errorf("expected a panic of type error; received %T", r) + } + + if !strings.Contains( + rAsE.Error(), + badSE.Check().Error(), + ) { + t.Errorf("expected error message to contain %v; received %v", + badSE.Check().Error(), + rAsE.Error(), + ) + } + }() + symbolic.VectorSubstituteTemplate(testVec, symbolic.NewVariable(), badSE) + t.Errorf("Problem! The function did not panic when the input scalar expression was not well-defined") +} + +/* +TestVectorExpression_VectorPowerTemplate1 +Description: + + Verify that the VectorPowerTemplate() function panics when the input vector is + not a well-defined VectorExpression. +*/ +func TestVectorExpression_VectorPowerTemplate1(t *testing.T) { + // Constants + testVec := symbolic.VariableVector{ + symbolic.NewVariable(), + symbolic.Variable{}, + } + + // Test + defer func() { + r := recover() + if r == nil { + t.Errorf("expected a panic when the input vector is not a well-defined VectorExpression; received nil") + } + + rAsE, tf := r.(error) + if !tf { + t.Errorf("expected a panic of type error; received %T", r) + } + + if !strings.Contains( + rAsE.Error(), + testVec.Check().Error(), + ) { + t.Errorf("expected error message to contain %v; received %v", + "unexpected expression type in vector expression: symbolic.Variable", + rAsE.Error(), + ) + } + }() + symbolic.VectorPowerTemplate(testVec, 2) + t.Errorf("Problem! The function did not panic when the input vector was not a well-defined VectorExpression") +} + +/* +TestVectorExpression_VectorPowerTemplate2 +Description: + + Verify that the VectorPowerTemplate() function panics when the input vector is + well-defined but the input power is less than 0. +*/ +func TestVectorExpression_VectorPowerTemplate2(t *testing.T) { + // Constants + testVec := symbolic.VariableVector{ + symbolic.NewVariable(), + symbolic.NewVariable(), + } + + // Test + defer func() { + r := recover() + if r == nil { + t.Errorf("expected a panic when the input power is less than 0; received nil") + } + + rAsE, tf := r.(error) + if !tf { + t.Errorf("expected a panic of type error; received %T", r) + } + + expectedError := smErrors.NegativeExponentError{ + -1, + } + if !strings.Contains( + rAsE.Error(), + expectedError.Error(), + ) { + t.Errorf("expected error message to contain %v; received %v", + expectedError.Error(), + rAsE.Error(), + ) + } + }() + symbolic.VectorPowerTemplate(testVec, -1) + t.Errorf("Problem! The function did not panic when the input power was less than 0") +}