Skip to content

Commit

Permalink
Simplified Some of VectorExpression functions and added tests for err…
Browse files Browse the repository at this point in the history
…or handling in VectorExpression templates
  • Loading branch information
Kwesi Rutledge committed Jun 9, 2024
1 parent 453fd97 commit 51ed887
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 6 deletions.
9 changes: 3 additions & 6 deletions symbolic/vector_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
234 changes: 234 additions & 0 deletions testing/symbolic/vector_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

0 comments on commit 51ed887

Please sign in to comment.