MCPcopy
hub / github.com/TheAlgorithms/Go / TestMultiplyMatrix

Function TestMultiplyMatrix

math/matrix/multiply_test.go:9–50  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7)
8
9func TestMultiplyMatrix(t *testing.T) {
10 // Test case with compatible NULL matrices
11 t.Run("NULL Matrices", func(t *testing.T) {
12 mat1 := matrix.New(0, 0, 0)
13 mat2 := matrix.New(0, 0, 0)
14
15 expected := matrix.New(0, 0, 0)
16 result, err := mat1.Multiply(mat2)
17 if err != nil {
18 t.Errorf("Expected no error, got %v", err)
19 } else if !result.CheckEqual(expected) {
20 t.Errorf("Result matrix does not match the expected result.")
21 }
22
23 })
24 // Test case with compatible matrices
25 t.Run("Compatible Matrices", func(t *testing.T) {
26 mat1 := [][]int{{1, 2, 3}, {4, 5, 6}}
27 mat2 := [][]int{{7, 8}, {9, 10}, {11, 12}}
28 m1, err := matrix.NewFromElements(mat1)
29 if err != nil {
30 t.Fatalf("Failed to copy matrix: %v", err)
31 }
32 m2, err := matrix.NewFromElements(mat2)
33 if err != nil {
34 t.Fatalf("Failed to copy matrix: %v", err)
35 }
36 exp := [][]int{{58, 64}, {139, 154}}
37 expected, err := matrix.NewFromElements(exp)
38 if err != nil {
39 t.Fatalf("Failed to copy matrix: %v", err)
40 }
41 result, err := m1.Multiply(m2)
42 if err != nil {
43 t.Errorf("Expected no error, got %v", err)
44 } else if !result.CheckEqual(expected) {
45 t.Errorf("Result matrix does not match the expected result.")
46 }
47
48 })
49
50}
51
52func TestMultiplyIncompatibleMatrix(t *testing.T) {
53 // Test case with incompatible matrices

Callers

nothing calls this directly

Calls 4

NewFunction · 0.92
NewFromElementsFunction · 0.92
MultiplyMethod · 0.80
CheckEqualMethod · 0.80

Tested by

no test coverage detected