Skip to content

Commit 453fd97

Browse files
author
Kwesi Rutledge
committed
Added Tests for MatrixSubstituteTemplate and MatrixPowerTemplate
1 parent 87250bd commit 453fd97

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

testing/symbolic/matrix_expression_test.go

Lines changed: 222 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"
@@ -191,3 +192,224 @@ func TestMatrixExpression_ConcretizeMatrixExpression5(t *testing.T) {
191192
t.Errorf("Expected a 2x2 MonomialMatrix; received %dx%d MonomialMatrix", me.Dims()[0], me.Dims()[1])
192193
}
193194
}
195+
196+
/*
197+
TestMatrixExpression_MatrixPowerTemplate1
198+
Description:
199+
200+
Tests that the matrix power template properly panics when called with a MatrixExpression that is not
201+
well-defined (in this case, a MonomialMatrix).
202+
*/
203+
func TestMatrixExpression_MatrixPowerTemplate1(t *testing.T) {
204+
// Setup
205+
m := symbolic.Monomial{
206+
Coefficient: 1.2,
207+
VariableFactors: []symbolic.Variable{symbolic.NewVariable(), symbolic.NewVariable()},
208+
Exponents: []int{1},
209+
}
210+
x := symbolic.MonomialMatrix{
211+
{m, m},
212+
{m, m},
213+
}
214+
215+
// Test
216+
defer func() {
217+
r := recover()
218+
if r == nil {
219+
t.Errorf("Expected a panic when calling MatrixPowerTemplate on a MonomialMatrix; received nil")
220+
}
221+
222+
rAsE, tf := r.(error)
223+
if !tf {
224+
t.Errorf("Expected the panic to be an error; received %T", r)
225+
}
226+
227+
if !strings.Contains(rAsE.Error(), m.Check().Error()) {
228+
t.Errorf("Expected the panic to contain the error message %v; received %v", m.Check().Error(), rAsE.Error())
229+
}
230+
}()
231+
symbolic.MatrixPowerTemplate(x, 2)
232+
}
233+
234+
/*
235+
TestMatrixExpression_MatrixPowerTemplate2
236+
Description:
237+
238+
Tests that the matrix power template properly panics when called with a MatrixExpression that is well-defined
239+
but is not square.
240+
*/
241+
func TestMatrixExpression_MatrixPowerTemplate2(t *testing.T) {
242+
// Setup
243+
m := symbolic.NewVariable().ToMonomial()
244+
x := symbolic.MonomialMatrix{
245+
{m, m},
246+
}
247+
248+
// Test
249+
defer func() {
250+
r := recover()
251+
if r == nil {
252+
t.Errorf("Expected a panic when calling MatrixPowerTemplate on a non-square MonomialMatrix; received nil")
253+
}
254+
255+
rAsE, tf := r.(error)
256+
if !tf {
257+
t.Errorf("Expected the panic to be an error; received %T", r)
258+
}
259+
260+
if !strings.Contains(rAsE.Error(), "matrix is not square") {
261+
t.Errorf("Expected the panic to contain the error message %v; received %v", "Matrix is not square", rAsE.Error())
262+
}
263+
}()
264+
symbolic.MatrixPowerTemplate(x, 2)
265+
}
266+
267+
/*
268+
TestMatrixExpression_MatrixPowerTemplate3
269+
Description:
270+
271+
Tests that the matrix power template properly panics when called with a MatrixExpression that is well-defined
272+
and is square but with a negative exponent.
273+
*/
274+
func TestMatrixExpression_MatrixPowerTemplate3(t *testing.T) {
275+
// Setup
276+
m := symbolic.NewVariable().ToMonomial()
277+
x := symbolic.MonomialMatrix{
278+
{m, m},
279+
{m, m},
280+
}
281+
282+
// Test
283+
defer func() {
284+
r := recover()
285+
if r == nil {
286+
t.Errorf("Expected a panic when calling MatrixPowerTemplate with a negative exponent; received nil")
287+
}
288+
289+
rAsE, tf := r.(error)
290+
if !tf {
291+
t.Errorf("Expected the panic to be an error; received %T", r)
292+
}
293+
294+
expectedError := smErrors.NegativeExponentError{
295+
Exponent: -2,
296+
}
297+
if !strings.Contains(rAsE.Error(), expectedError.Error()) {
298+
t.Errorf("Expected the panic to contain the error message %v; received %v", "Negative exponent", rAsE.Error())
299+
}
300+
}()
301+
symbolic.MatrixPowerTemplate(x, -2)
302+
}
303+
304+
/*
305+
TestMatrixExpression_MatrixSubstituteTemplate1
306+
Description:
307+
308+
Tests that the matrix substitute template properly panics when called with a MatrixExpression that is not
309+
well-defined (in this case, a MonomialMatrix).
310+
*/
311+
func TestMatrixExpression_MatrixSubstituteTemplate1(t *testing.T) {
312+
// Setup
313+
m := symbolic.Monomial{
314+
Coefficient: 1.2,
315+
VariableFactors: []symbolic.Variable{symbolic.NewVariable(), symbolic.NewVariable()},
316+
Exponents: []int{1},
317+
}
318+
x := symbolic.MonomialMatrix{
319+
{m, m},
320+
{m, m},
321+
}
322+
323+
// Test
324+
defer func() {
325+
r := recover()
326+
if r == nil {
327+
t.Errorf("Expected a panic when calling MatrixSubstituteTemplate on a MonomialMatrix; received nil")
328+
}
329+
330+
rAsE, tf := r.(error)
331+
if !tf {
332+
t.Errorf("Expected the panic to be an error; received %T", r)
333+
}
334+
335+
if !strings.Contains(rAsE.Error(), m.Check().Error()) {
336+
t.Errorf("Expected the panic to contain the error message %v; received %v", m.Check().Error(), rAsE.Error())
337+
}
338+
}()
339+
symbolic.MatrixSubstituteTemplate(x, symbolic.NewVariable(), symbolic.NewVariable())
340+
}
341+
342+
/*
343+
TestMatrixExpression_MatrixSubstituteTemplate2
344+
Description:
345+
346+
Tests that the matrix substitute template properly panics when called with a MatrixExpression that is well-defined
347+
and a variable vIn that is not well-defined.
348+
*/
349+
func TestMatrixExpression_MatrixSubstituteTemplate2(t *testing.T) {
350+
// Setup
351+
m := symbolic.NewVariable().ToMonomial()
352+
x := symbolic.MonomialMatrix{
353+
{m, m},
354+
{m, m},
355+
}
356+
v1 := symbolic.Variable{}
357+
358+
// Test
359+
defer func() {
360+
r := recover()
361+
if r == nil {
362+
t.Errorf("Expected a panic when calling MatrixSubstituteTemplate with an invalid variable; received nil")
363+
}
364+
365+
rAsE, tf := r.(error)
366+
if !tf {
367+
t.Errorf("Expected the panic to be an error; received %T", r)
368+
}
369+
370+
if !strings.Contains(rAsE.Error(), v1.Check().Error()) {
371+
t.Errorf("Expected the panic to contain the error message %v; received %v", "the input variable is not well-defined", rAsE.Error())
372+
}
373+
}()
374+
symbolic.MatrixSubstituteTemplate(x, v1, symbolic.NewVariable())
375+
}
376+
377+
/*
378+
TestMatrixExpression_MatrixSubstituteTemplate3
379+
Description:
380+
381+
Tests that the matrix substitute template properly panics when called with a MatrixExpression that is well-defined,
382+
a variable vIn that is well-defined, but an expression eIn that is not well-defined (in this case a monomial).
383+
*/
384+
func TestMatrixExpression_MatrixSubstituteTemplate3(t *testing.T) {
385+
// Setup
386+
m := symbolic.NewVariable().ToMonomial()
387+
x := symbolic.MonomialMatrix{
388+
{m, m},
389+
{m, m},
390+
}
391+
v1 := symbolic.NewVariable()
392+
m1 := symbolic.Monomial{
393+
Coefficient: 1.2,
394+
VariableFactors: []symbolic.Variable{symbolic.NewVariable(), symbolic.NewVariable()},
395+
Exponents: []int{1},
396+
}
397+
398+
// Test
399+
defer func() {
400+
r := recover()
401+
if r == nil {
402+
t.Errorf("Expected a panic when calling MatrixSubstituteTemplate with an invalid expression; received nil")
403+
}
404+
405+
rAsE, tf := r.(error)
406+
if !tf {
407+
t.Errorf("Expected the panic to be an error; received %T", r)
408+
}
409+
410+
if !strings.Contains(rAsE.Error(), m1.Check().Error()) {
411+
t.Errorf("Expected the panic to contain the error message %v; received %v", m1.Check().Error(), rAsE.Error())
412+
}
413+
}()
414+
symbolic.MatrixSubstituteTemplate(x, v1, m1)
415+
}

0 commit comments

Comments
 (0)