Debian Codegraph @

matrix_test.go

raw
1 /**
2 * Unit tests for Matrix
3 *
4 * Copyright 2015, Klaus Post
5 * Copyright 2015, Backblaze, Inc. All rights reserved.
6 */
7
8 package reedsolomon
9
10 import (
11 "testing"
12 )
13
14 // TestNewMatrix - Tests validate the result for invalid input and the allocations made by newMatrix method.
15 func TestNewMatrix(t *testing.T) {
16 testCases := []struct {
17 rows int
18 columns int
19
20 // flag to indicate whether the test should pass.
21 shouldPass bool
22 expectedResult matrix
23 expectedErr error
24 }{
25 // Test case - 1.
26 // Test case with a negative row size.
27 {-1, 10, false, nil, errInvalidRowSize},
28 // Test case - 2.
29 // Test case with a negative column size.
30 {10, -1, false, nil, errInvalidColSize},
31 // Test case - 3.
32 // Test case with negative value for both row and column size.
33 {-1, -1, false, nil, errInvalidRowSize},
34 // Test case - 4.
35 // Test case with 0 value for row size.
36 {0, 10, false, nil, errInvalidRowSize},
37 // Test case - 5.
38 // Test case with 0 value for column size.
39 {-1, 0, false, nil, errInvalidRowSize},
40 // Test case - 6.
41 // Test case with 0 value for both row and column size.
42 {0, 0, false, nil, errInvalidRowSize},
43 }
44 for i, testCase := range testCases {
45 actualResult, actualErr := newMatrix(testCase.rows, testCase.columns)
46 if actualErr != nil && testCase.shouldPass {
47 t.Errorf("Test %d: Expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
48 }
49 if actualErr == nil && !testCase.shouldPass {
50 t.Errorf("Test %d: Expected to fail with <ERROR> \"%s\", but passed instead.", i+1, testCase.expectedErr)
51 }
52 // Failed as expected, but does it fail for the expected reason.
53 if actualErr != nil && !testCase.shouldPass {
54 if testCase.expectedErr != actualErr {
55 t.Errorf("Test %d: Expected to fail with error \"%s\", but instead failed with error \"%s\" instead.", i+1, testCase.expectedErr, actualErr)
56 }
57 }
58 // Test passes as expected, but the output values
59 // are verified for correctness here.
60 if actualErr == nil && testCase.shouldPass {
61 if testCase.rows != len(actualResult) {
62 // End the tests here if the the size doesn't match number of rows.
63 t.Fatalf("Test %d: Expected the size of the row of the new matrix to be `%d`, but instead found `%d`", i+1, testCase.rows, len(actualResult))
64 }
65 // Iterating over each row and validating the size of the column.
66 for j, row := range actualResult {
67 // If the row check passes, verify the size of each columns.
68 if testCase.columns != len(row) {
69 t.Errorf("Test %d: Row %d: Expected the size of the column of the new matrix to be `%d`, but instead found `%d`", i+1, j+1, testCase.columns, len(row))
70 }
71 }
72 }
73 }
74 }
75
76 // TestMatrixIdentity - validates the method for returning identity matrix of given size.
77 func TestMatrixIdentity(t *testing.T) {
78 m, err := identityMatrix(3)
79 if err != nil {
80 t.Fatal(err)
81 }
82 str := m.String()
83 expect := "[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
84 if str != expect {
85 t.Fatal(str, "!=", expect)
86 }
87 }
88
89 // Tests validate the output of matrix multiplication method.
90 func TestMatrixMultiply(t *testing.T) {
91 m1, err := newMatrixData(
92 [][]byte{
93 {1, 2},
94 {3, 4},
95 })
96 if err != nil {
97 t.Fatal(err)
98 }
99
100 m2, err := newMatrixData(
101 [][]byte{
102 {5, 6},
103 {7, 8},
104 })
105 if err != nil {
106 t.Fatal(err)
107 }
108 actual, err := m1.Multiply(m2)
109 if err != nil {
110 t.Fatal(err)
111 }
112 str := actual.String()
113 expect := "[[11, 22], [19, 42]]"
114 if str != expect {
115 t.Fatal(str, "!=", expect)
116 }
117 }
118
119 // Tests validate the output of the method with computes inverse of matrix.
120 func TestMatrixInverse(t *testing.T) {
121 testCases := []struct {
122 matrixData [][]byte
123 // expected inverse matrix.
124 expectedResult string
125 // flag indicating whether the test should pass.
126 shouldPass bool
127 expectedErr error
128 }{
129 // Test case - 1.
130 // Test case validating inverse of the input Matrix.
131 {
132 // input data to construct the matrix.
133 [][]byte{
134 {56, 23, 98},
135 {3, 100, 200},
136 {45, 201, 123},
137 },
138 // expected Inverse matrix.
139 "[[175, 133, 33], [130, 13, 245], [112, 35, 126]]",
140 // test is expected to pass.
141 true,
142 nil,
143 },
144 // Test case - 2.
145 // Test case validating inverse of the input Matrix.
146 {
147 // input data to construct the matrix.
148 [][]byte{
149 {1, 0, 0, 0, 0},
150 {0, 1, 0, 0, 0},
151 {0, 0, 0, 1, 0},
152 {0, 0, 0, 0, 1},
153 {7, 7, 6, 6, 1},
154 },
155 // expectedInverse matrix.
156 "[[1, 0, 0, 0, 0]," +
157 " [0, 1, 0, 0, 0]," +
158 " [123, 123, 1, 122, 122]," +
159 " [0, 0, 1, 0, 0]," +
160 " [0, 0, 0, 1, 0]]",
161 // test is expected to pass.
162 true,
163 nil,
164 },
165 // Test case with a non-square matrix.
166 // expected to fail with errNotSquare.
167 {
168 [][]byte{
169 {56, 23},
170 {3, 100},
171 {45, 201},
172 },
173 "",
174 false,
175 errNotSquare,
176 },
177 // Test case with singular matrix.
178 // expected to fail with error errSingular.
179 {
180
181 [][]byte{
182 {4, 2},
183 {12, 6},
184 },
185 "",
186 false,
187 errSingular,
188 },
189 }
190
191 for i, testCase := range testCases {
192 m, err := newMatrixData(testCase.matrixData)
193 if err != nil {
194 t.Fatalf("Test %d: Failed initializing new Matrix : %s", i+1, err)
195 }
196 actualResult, actualErr := m.Invert()
197 if actualErr != nil && testCase.shouldPass {
198 t.Errorf("Test %d: Expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
199 }
200 if actualErr == nil && !testCase.shouldPass {
201 t.Errorf("Test %d: Expected to fail with <ERROR> \"%s\", but passed instead.", i+1, testCase.expectedErr)
202 }
203 // Failed as expected, but does it fail for the expected reason.
204 if actualErr != nil && !testCase.shouldPass {
205 if testCase.expectedErr != actualErr {
206 t.Errorf("Test %d: Expected to fail with error \"%s\", but instead failed with error \"%s\" instead.", i+1, testCase.expectedErr, actualErr)
207 }
208 }
209 // Test passes as expected, but the output values
210 // are verified for correctness here.
211 if actualErr == nil && testCase.shouldPass {
212 if testCase.expectedResult != actualResult.String() {
213 t.Errorf("Test %d: The inverse matrix doesn't match the expected result", i+1)
214 }
215 }
216 }
217 }