Skip to content

Commit 73ba89b

Browse files
author
Kwesi Rutledge
committed
Added Tests for Checking Substitution Map
1 parent 53c3262 commit 73ba89b

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed

testing/symbolic/utils_test.go

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Description:
77
*/
88

99
import (
10+
"fmt"
1011
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
1112
"testing"
1213
)
@@ -208,6 +209,111 @@ func TestUtils_FindInSlice9(t *testing.T) {
208209
}
209210
}
210211

212+
/*
213+
TestUtils_FindInSlice10
214+
Description:
215+
216+
This test verifies that the FindInSlice function returns an error when the
217+
first input is a string and the second input is a slice of something else (in this case a slice of strings).
218+
*/
219+
func TestUtils_FindInSlice10(t *testing.T) {
220+
// Constants
221+
x := "x"
222+
slice := []int{2, 3, 4, 1}
223+
224+
// Test
225+
_, err := symbolic.FindInSlice(x, slice)
226+
if err == nil {
227+
t.Errorf(
228+
"Expected FindInSlice to return an error; received nil",
229+
)
230+
} else {
231+
expectedError := fmt.Errorf(
232+
"the input slice is of type %T, but the element we're searching for is of type %T",
233+
slice,
234+
x,
235+
)
236+
if err.Error() != expectedError.Error() {
237+
t.Errorf(
238+
"Expected error message to be '%v'; received %v",
239+
expectedError,
240+
err.Error(),
241+
)
242+
}
243+
}
244+
}
245+
246+
/*
247+
TestUtils_FindInSlice11
248+
Description:
249+
250+
This test verifies that the FindInSlice function returns an error when the
251+
the first input is a Variable and the second input is a slice of something else (in this case a slice of strings).
252+
*/
253+
func TestUtils_FindInSlice11(t *testing.T) {
254+
// Constants
255+
x := symbolic.NewVariable()
256+
slice := []int{2, 3, 4, 1}
257+
258+
// Test
259+
_, err := symbolic.FindInSlice(x, slice)
260+
if err == nil {
261+
t.Errorf(
262+
"Expected FindInSlice to return an error; received nil",
263+
)
264+
} else {
265+
expectedError := fmt.Errorf(
266+
"the input slice is of type %T, but the element we're searching for is of type %T",
267+
slice,
268+
x,
269+
)
270+
if err.Error() != expectedError.Error() {
271+
t.Errorf(
272+
"Expected error message to be '%v'; received %v",
273+
expectedError,
274+
err.Error(),
275+
)
276+
}
277+
}
278+
}
279+
280+
/*
281+
TestUtils_FindInSlice12
282+
Description:
283+
284+
This test verifies that the FindInSlice function returns an error when the
285+
the first input is an error (an unsupported type). The second input can be anything else.
286+
*/
287+
func TestUtils_FindInSlice12(t *testing.T) {
288+
// Constants
289+
x := fmt.Errorf("error")
290+
slice := []int{2, 3, 4, 1}
291+
292+
// Test
293+
_, err := symbolic.FindInSlice(x, slice)
294+
if err == nil {
295+
t.Errorf(
296+
"Expected FindInSlice to return an error; received nil",
297+
)
298+
} else {
299+
allowedTypes := []string{
300+
"string", "int", "uint64", "Variable",
301+
}
302+
expectedError := fmt.Errorf(
303+
"the FindInSlice() function was only defined for types %v, not type %T:",
304+
allowedTypes,
305+
x,
306+
)
307+
if err.Error() != expectedError.Error() {
308+
t.Errorf(
309+
"Expected error message to be '%v'; received %v",
310+
expectedError,
311+
err.Error(),
312+
)
313+
}
314+
}
315+
}
316+
211317
/*
212318
TestUtils_Unique1
213319
Description:
@@ -267,3 +373,161 @@ func TestUtils_Unique3(t *testing.T) {
267373
)
268374
}
269375
}
376+
377+
/*
378+
TestUtils_Unique4
379+
Description:
380+
381+
This test verifies that the unique function returns a slice of length 3
382+
when the input slice has length 3 and all elements are unique.
383+
*/
384+
func TestUtils_Unique4(t *testing.T) {
385+
// Constants
386+
slice := []uint64{13, 14, 15}
387+
388+
// Test
389+
if len(symbolic.Unique(slice)) != 3 {
390+
t.Errorf(
391+
"Expected Unique to return a slice of length 3; received %v",
392+
symbolic.Unique(slice),
393+
)
394+
}
395+
}
396+
397+
/*
398+
TestUtils_CheckSubstitutionMap1
399+
Description:
400+
401+
This test verifies that the CheckSubstitutionMap function returns an error
402+
when the input map contains a variable that is not well-defined.
403+
*/
404+
func TestUtils_CheckSubstitutionMap1(t *testing.T) {
405+
// Constants
406+
badVar := symbolic.Variable{2, -1, -2, symbolic.Binary, "Russ"}
407+
varMap := map[symbolic.Variable]symbolic.Expression{
408+
symbolic.NewVariable(): symbolic.K(3),
409+
badVar: symbolic.K(4),
410+
}
411+
412+
// Test
413+
err := symbolic.CheckSubstitutionMap(varMap)
414+
if err == nil {
415+
t.Errorf(
416+
"Expected CheckSubstitutionMap to return an error; received nil",
417+
)
418+
} else {
419+
expectedError := fmt.Errorf(
420+
"key %v in the substitution map is not a valid variable: %v",
421+
badVar,
422+
badVar.Check(),
423+
)
424+
if err.Error() != expectedError.Error() {
425+
t.Errorf(
426+
"Expected error message to be '%v'; received %v",
427+
expectedError,
428+
err,
429+
)
430+
}
431+
}
432+
}
433+
434+
/*
435+
TestUtils_CheckSubstitutionMap2
436+
Description:
437+
438+
This test verifies that the CheckSubstitutionMap function returns an error
439+
when the input map contains a value/mapped item that is not well-defined.
440+
*/
441+
func TestUtils_CheckSubstitutionMap2(t *testing.T) {
442+
// Constants
443+
goodVar := symbolic.NewVariable()
444+
badVar := symbolic.Variable{2, -1, -2, symbolic.Binary, "Russ"}
445+
varMap := map[symbolic.Variable]symbolic.Expression{
446+
symbolic.NewVariable(): symbolic.K(3),
447+
goodVar: badVar,
448+
}
449+
450+
// Test
451+
err := symbolic.CheckSubstitutionMap(varMap)
452+
if err == nil {
453+
t.Errorf(
454+
"Expected CheckSubstitutionMap to return an error; received nil",
455+
)
456+
} else {
457+
expectedError := fmt.Errorf(
458+
"value %v in the substitution map[%v] is not a valid expression: %v",
459+
badVar,
460+
goodVar,
461+
badVar.Check(),
462+
)
463+
if err.Error() != expectedError.Error() {
464+
t.Errorf(
465+
"Expected error message to be '%v'; received %v",
466+
expectedError,
467+
err,
468+
)
469+
}
470+
}
471+
}
472+
473+
/*
474+
TestUtils_CheckSubstitutionMap3
475+
Description:
476+
477+
This test verifies that the CheckSubstitutionMap function returns an error
478+
when the input map contains a value/mapped item that is not a scalar expression.
479+
*/
480+
func TestUtils_CheckSubstitutionMap3(t *testing.T) {
481+
// Constants
482+
goodVar := symbolic.NewVariable()
483+
badVar := symbolic.NewVariableMatrix(2, 2)
484+
varMap := map[symbolic.Variable]symbolic.Expression{
485+
symbolic.NewVariable(): symbolic.K(3),
486+
goodVar: badVar,
487+
}
488+
489+
// Test
490+
err := symbolic.CheckSubstitutionMap(varMap)
491+
if err == nil {
492+
t.Errorf(
493+
"Expected CheckSubstitutionMap to return an error; received nil",
494+
)
495+
} else {
496+
expectedError := fmt.Errorf(
497+
"value %v in the substitution map[%v] is not a scalar expression (received %T)",
498+
badVar,
499+
goodVar,
500+
badVar,
501+
)
502+
if err.Error() != expectedError.Error() {
503+
t.Errorf(
504+
"Expected error message to be '%v'; received %v",
505+
expectedError,
506+
err,
507+
)
508+
}
509+
}
510+
}
511+
512+
/*
513+
TestUtils_CheckSubstitutionMap4
514+
Description:
515+
516+
This test verifies that the CheckSubstitutionMap function returns no error
517+
when the input map is well-defined (has valid variable keys and its values are all valid scalar expresssions).
518+
*/
519+
func TestUtils_CheckSubstitutionMap4(t *testing.T) {
520+
// Constants
521+
varMap := map[symbolic.Variable]symbolic.Expression{
522+
symbolic.NewVariable(): symbolic.K(3),
523+
symbolic.NewVariable(): symbolic.K(4),
524+
}
525+
526+
// Test
527+
if err := symbolic.CheckSubstitutionMap(varMap); err != nil {
528+
t.Errorf(
529+
"Expected CheckSubstitutionMap to return nil; received %v",
530+
err,
531+
)
532+
}
533+
}

0 commit comments

Comments
 (0)