Skip to content

Commit 51ed887

Browse files
author
Kwesi Rutledge
committed
Simplified Some of VectorExpression functions and added tests for error handling in VectorExpression templates
1 parent 453fd97 commit 51ed887

File tree

2 files changed

+237
-6
lines changed

2 files changed

+237
-6
lines changed

symbolic/vector_expression.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,9 @@ Description:
344344
*/
345345
func VectorSubstituteTemplate(ve VectorExpression, vIn Variable, se ScalarExpression) VectorExpression {
346346
// Input Processing
347-
var err error
348-
for ii := 0; ii < ve.Len(); ii++ {
349-
err = ve.AtVec(ii).Check()
350-
if err != nil {
351-
panic(fmt.Errorf("idx #%v produced error: %v", ii, err))
352-
}
347+
err := ve.Check()
348+
if err != nil {
349+
panic(err)
353350
}
354351

355352
err = vIn.Check()

testing/symbolic/vector_expression_test.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package symbolic_test
22

33
import (
44
"fmt"
5+
"github.com/MatProGo-dev/SymbolicMath.go/smErrors"
56
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
67
"strings"
78
"testing"
@@ -195,3 +196,236 @@ func TestVectorExpression_ConcretizeVectorExpression7(t *testing.T) {
195196
symbolic.ConcretizeVectorExpression([]symbolic.ScalarExpression{})
196197
t.Errorf("Problem! The function did not panic when the input slice was empty")
197198
}
199+
200+
/*
201+
TestVectorExpression_ConcretizeVectorExpression8
202+
Description:
203+
204+
Tests that the function correctly generates a polynomial vector from a slice of ScalarExpression objects,
205+
one is a polynomial, one is a monomial, and the other is a variable.
206+
*/
207+
func TestVectorExpression_ConcretizeVectorExpression8(t *testing.T) {
208+
// Constants
209+
p := symbolic.NewVariable().ToPolynomial()
210+
m := symbolic.NewVariable().ToMonomial()
211+
v := symbolic.NewVariable()
212+
slice := []symbolic.ScalarExpression{p, m, v}
213+
214+
// Test
215+
pv := symbolic.ConcretizeVectorExpression(slice)
216+
217+
vOut, tf := pv.(symbolic.PolynomialVector)
218+
if !tf {
219+
t.Errorf("expected a PolynomialVector; received %T", pv)
220+
}
221+
222+
if len(vOut) != 3 {
223+
t.Errorf("expected a PolynomialVector of length 3; received %v", len(vOut))
224+
}
225+
}
226+
227+
/*
228+
TestVectorExpression_VectorSubstituteTemplate1
229+
Description:
230+
231+
Verify that the VectorSubstituteTemplate() function panics when the input vector is
232+
not a well-defined VectorExpression.
233+
*/
234+
func TestVectorExpression_VectorSubstituteTemplate1(t *testing.T) {
235+
// Constants
236+
testVec := symbolic.VariableVector{
237+
symbolic.NewVariable(),
238+
symbolic.Variable{},
239+
}
240+
241+
// Test
242+
defer func() {
243+
r := recover()
244+
if r == nil {
245+
t.Errorf("expected a panic when the input vector is not a well-defined VectorExpression; received nil")
246+
}
247+
248+
rAsE, tf := r.(error)
249+
if !tf {
250+
t.Errorf("expected a panic of type error; received %T", r)
251+
}
252+
253+
if !strings.Contains(
254+
rAsE.Error(),
255+
testVec.Check().Error(),
256+
) {
257+
t.Errorf("expected error message to contain %v; received %v",
258+
"unexpected expression type in vector expression: symbolic.Variable",
259+
rAsE.Error(),
260+
)
261+
}
262+
}()
263+
symbolic.VectorSubstituteTemplate(testVec, symbolic.NewVariable(), symbolic.NewVariable())
264+
t.Errorf("Problem! The function did not panic when the input vector was not a well-defined VectorExpression")
265+
}
266+
267+
/*
268+
TestVectorExpression_VectorSubstituteTemplate2
269+
Description:
270+
271+
Verify that the VectorSubstituteTemplate() function panics when the input vector is
272+
well-defined but the input target variable is not well-defined.
273+
*/
274+
func TestVectorExpression_VectorSubstituteTemplate2(t *testing.T) {
275+
// Constants
276+
testVec := symbolic.VariableVector{
277+
symbolic.NewVariable(),
278+
symbolic.NewVariable(),
279+
}
280+
badVar := symbolic.Variable{}
281+
282+
// Test
283+
defer func() {
284+
r := recover()
285+
if r == nil {
286+
t.Errorf("expected a panic when the input target variable is not well-defined; received nil")
287+
}
288+
289+
rAsE, tf := r.(error)
290+
if !tf {
291+
t.Errorf("expected a panic of type error; received %T", r)
292+
}
293+
294+
if !strings.Contains(
295+
rAsE.Error(),
296+
badVar.Check().Error(),
297+
) {
298+
t.Errorf("expected error message to contain %v; received %v",
299+
badVar.Check().Error(),
300+
rAsE.Error(),
301+
)
302+
}
303+
}()
304+
symbolic.VectorSubstituteTemplate(testVec, badVar, symbolic.NewVariable())
305+
t.Errorf("Problem! The function did not panic when the input target variable was not well-defined")
306+
}
307+
308+
/*
309+
TestVectorExpression_VectorSubstituteTemplate3
310+
Description:
311+
312+
Verify that the VectorSubstituteTemplate() function panics when the input vector is
313+
well-defined, the input target variable is well-defined, but the input scalar expression
314+
is not well-defined.
315+
*/
316+
func TestVectorExpression_VectorSubstituteTemplate3(t *testing.T) {
317+
// Constants
318+
testVec := symbolic.VariableVector{
319+
symbolic.NewVariable(),
320+
symbolic.NewVariable(),
321+
}
322+
badSE := symbolic.Variable{}
323+
324+
// Test
325+
defer func() {
326+
r := recover()
327+
if r == nil {
328+
t.Errorf("expected a panic when the input scalar expression is not well-defined; received nil")
329+
}
330+
331+
rAsE, tf := r.(error)
332+
if !tf {
333+
t.Errorf("expected a panic of type error; received %T", r)
334+
}
335+
336+
if !strings.Contains(
337+
rAsE.Error(),
338+
badSE.Check().Error(),
339+
) {
340+
t.Errorf("expected error message to contain %v; received %v",
341+
badSE.Check().Error(),
342+
rAsE.Error(),
343+
)
344+
}
345+
}()
346+
symbolic.VectorSubstituteTemplate(testVec, symbolic.NewVariable(), badSE)
347+
t.Errorf("Problem! The function did not panic when the input scalar expression was not well-defined")
348+
}
349+
350+
/*
351+
TestVectorExpression_VectorPowerTemplate1
352+
Description:
353+
354+
Verify that the VectorPowerTemplate() function panics when the input vector is
355+
not a well-defined VectorExpression.
356+
*/
357+
func TestVectorExpression_VectorPowerTemplate1(t *testing.T) {
358+
// Constants
359+
testVec := symbolic.VariableVector{
360+
symbolic.NewVariable(),
361+
symbolic.Variable{},
362+
}
363+
364+
// Test
365+
defer func() {
366+
r := recover()
367+
if r == nil {
368+
t.Errorf("expected a panic when the input vector is not a well-defined VectorExpression; received nil")
369+
}
370+
371+
rAsE, tf := r.(error)
372+
if !tf {
373+
t.Errorf("expected a panic of type error; received %T", r)
374+
}
375+
376+
if !strings.Contains(
377+
rAsE.Error(),
378+
testVec.Check().Error(),
379+
) {
380+
t.Errorf("expected error message to contain %v; received %v",
381+
"unexpected expression type in vector expression: symbolic.Variable",
382+
rAsE.Error(),
383+
)
384+
}
385+
}()
386+
symbolic.VectorPowerTemplate(testVec, 2)
387+
t.Errorf("Problem! The function did not panic when the input vector was not a well-defined VectorExpression")
388+
}
389+
390+
/*
391+
TestVectorExpression_VectorPowerTemplate2
392+
Description:
393+
394+
Verify that the VectorPowerTemplate() function panics when the input vector is
395+
well-defined but the input power is less than 0.
396+
*/
397+
func TestVectorExpression_VectorPowerTemplate2(t *testing.T) {
398+
// Constants
399+
testVec := symbolic.VariableVector{
400+
symbolic.NewVariable(),
401+
symbolic.NewVariable(),
402+
}
403+
404+
// Test
405+
defer func() {
406+
r := recover()
407+
if r == nil {
408+
t.Errorf("expected a panic when the input power is less than 0; received nil")
409+
}
410+
411+
rAsE, tf := r.(error)
412+
if !tf {
413+
t.Errorf("expected a panic of type error; received %T", r)
414+
}
415+
416+
expectedError := smErrors.NegativeExponentError{
417+
-1,
418+
}
419+
if !strings.Contains(
420+
rAsE.Error(),
421+
expectedError.Error(),
422+
) {
423+
t.Errorf("expected error message to contain %v; received %v",
424+
expectedError.Error(),
425+
rAsE.Error(),
426+
)
427+
}
428+
}()
429+
symbolic.VectorPowerTemplate(testVec, -1)
430+
t.Errorf("Problem! The function did not panic when the input power was less than 0")
431+
}

0 commit comments

Comments
 (0)