1 #include <gtest/gtest.h>
2 #include "matrixtest.h"
3 #include <iostream>
4 #include <iomanip>
5
6
7
8 class MatrixTest : public ::testing::Test
9 {
10 protected:
11 virtual void SetUp ();
12 virtual void TearDown ();
13 };
14
15
16
SetUp()17 void MatrixTest::SetUp ()
18 {
19 }
20
21
22
TearDown()23 void MatrixTest::TearDown ()
24 {
25 }
26
27
28
TEST_F(MatrixTest,resize_From1x1To2x2WithDefaultValueDefaulted_Success)29 TEST_F (MatrixTest, resize_From1x1To2x2WithDefaultValueDefaulted_Success)
30 {
31 // Arrange.
32 Matrix<double> etalon_matrix{
33 {7.0, 0.0},
34 {0.0, 0.0}
35 };
36 Matrix<double> test_matrix{
37 {7.0}
38 };
39
40 // Act.
41 test_matrix.resize (2, 2);
42
43 // Assert.
44 EXPECT_EQ (etalon_matrix, test_matrix);
45 }
46
47
48
TEST_F(MatrixTest,resize_From1x1To5x5WithDefaultValueDefaulted_Success)49 TEST_F (MatrixTest, resize_From1x1To5x5WithDefaultValueDefaulted_Success)
50 {
51 // Arrange.
52 Matrix<double> etalon_matrix{
53 {7.0, 0.0, 0.0, 0.0, 0.0},
54 {0.0, 0.0, 0.0, 0.0, 0.0},
55 {0.0, 0.0, 0.0, 0.0, 0.0},
56 {0.0, 0.0, 0.0, 0.0, 0.0},
57 {0.0, 0.0, 0.0, 0.0, 0.0}
58 };
59 Matrix<double> test_matrix{
60 {7.0}
61 };
62
63 // Act.
64 test_matrix.resize (5, 5);
65
66 // Assert.
67 EXPECT_EQ (etalon_matrix, test_matrix);
68 }
69
70
71
TEST_F(MatrixTest,resize_From5x5To3x3WithDefaultValueDefaulted_Success)72 TEST_F (MatrixTest, resize_From5x5To3x3WithDefaultValueDefaulted_Success)
73 {
74 // Arrange.
75 Matrix<double> etalon_matrix{
76 {0.0, 0.1, 0.2},
77 {1.0, 1.1, 1.2},
78 {2.0, 2.1, 2.2}
79 };
80 Matrix<double> test_matrix{
81 {0.0, 0.1, 0.2, 0.3, 0.4},
82 {1.0, 1.1, 1.2, 1.3, 1.4},
83 {2.0, 2.1, 2.2, 2.3, 2.4},
84 {3.0, 3.1, 3.2, 3.3, 3.4},
85 {4.0, 4.1, 4.2, 4.3, 4.4}
86 };
87
88 // Act.
89 test_matrix.resize (3, 3);
90
91 // Assert.
92 EXPECT_EQ (etalon_matrix, test_matrix);
93 }
94
95
96
TEST_F(MatrixTest,resize_From2x2To4x4WithDefaultValueExplicit_Success)97 TEST_F (MatrixTest, resize_From2x2To4x4WithDefaultValueExplicit_Success)
98 {
99 // Arrange.
100 Matrix<double> etalon_matrix{
101 {0.0, 0.1, 9.9, 9.9},
102 {1.0, 1.1, 9.9, 9.9},
103 {9.9, 9.9, 9.9, 9.9},
104 {9.9, 9.9, 9.9, 9.9}
105 };
106 Matrix<double> test_matrix{
107 {0.0, 0.1},
108 {1.0, 1.1}
109 };
110
111 // Act.
112 test_matrix.resize (4, 4, 9.9);
113
114 // Assert.
115 EXPECT_EQ (etalon_matrix, test_matrix);
116 }
117
118
119
TEST_F(MatrixTest,clear_1x1_Success)120 TEST_F (MatrixTest, clear_1x1_Success)
121 {
122 // Arrange.
123 Matrix<double> etalon_matrix{
124 {0.0}
125 };
126 Matrix<double> test_matrix{
127 {7.0}
128 };
129
130 // Act.
131 test_matrix.clear ();
132
133 // Assert.
134 EXPECT_EQ (etalon_matrix, test_matrix);
135 }
136
137
138
TEST_F(MatrixTest,clear_4x4_Success)139 TEST_F (MatrixTest, clear_4x4_Success)
140 {
141 // Arrange.
142 Matrix<double> etalon_matrix{
143 {0.0, 0.0, 0.0, 0.0},
144 {0.0, 0.0, 0.0, 0.0},
145 {0.0, 0.0, 0.0, 0.0},
146 {0.0, 0.0, 0.0, 0.0}
147 };
148 Matrix<double> test_matrix{
149 {1.1, 1.2, 1.3, 1.4},
150 {2.1, 2.2, 2.3, 2.4},
151 {3.1, 3.2, 3.3, 3.4},
152 {4.1, 4.2, 4.3, 4.4}
153 };
154
155 // Act.
156 test_matrix.clear ();
157
158 // Assert.
159 EXPECT_EQ (etalon_matrix, test_matrix);
160 }
161
162
163
TEST_F(MatrixTest,operatorAssignment_0x0_Success)164 TEST_F (MatrixTest, operatorAssignment_0x0_Success)
165 {
166 // Arrange.
167 Matrix<double> etalon_matrix {std::initializer_list <std::initializer_list <double> > () };
168 Matrix<double> test_matrix{
169 {0.0, 0.1, 0.2},
170 {3.0, 3.1, 3.2},
171 {4.0, 4.1, 4.2}
172 };
173
174 // Act.
175 test_matrix = etalon_matrix;
176
177 // Assert.
178 EXPECT_EQ (etalon_matrix, test_matrix);
179 }
180
181
182
TEST_F(MatrixTest,operatorAssignment_3x3_Success)183 TEST_F (MatrixTest, operatorAssignment_3x3_Success)
184 {
185 // Arrange.
186 Matrix<double> etalon_matrix{
187 {0.0, 0.1, 0.2},
188 {1.0, 1.1, 1.2},
189 {2.0, 2.1, 2.2}
190 };
191 Matrix<double> test_matrix;
192
193 // Act.
194 test_matrix = etalon_matrix;
195
196 // Assert.
197 EXPECT_EQ (etalon_matrix, test_matrix);
198 }
199
200
201
TEST_F(MatrixTest,operatorSubscript_Success)202 TEST_F (MatrixTest, operatorSubscript_Success)
203 {
204 // Arrange.
205 Matrix<double> test_matrix{
206 {0.0, 0.1, 0.2},
207 {1.0, 1.1, 1.2},
208 {2.0, 2.1, 2.2}
209 };
210
211 // Act, Assert.
212 EXPECT_FLOAT_EQ (0.0, test_matrix (0, 0) );
213 EXPECT_FLOAT_EQ (0.1, test_matrix (0, 1) );
214 EXPECT_FLOAT_EQ (0.2, test_matrix (0, 2) );
215 EXPECT_FLOAT_EQ (1.0, test_matrix (1, 0) );
216 EXPECT_FLOAT_EQ (1.1, test_matrix (1, 1) );
217 EXPECT_FLOAT_EQ (1.2, test_matrix (1, 2) );
218 EXPECT_FLOAT_EQ (2.0, test_matrix (2, 0) );
219 EXPECT_FLOAT_EQ (2.1, test_matrix (2, 1) );
220 EXPECT_FLOAT_EQ (2.2, test_matrix (2, 2) );
221 }
222
223
224
TEST_F(MatrixTest,max_1x1_Success)225 TEST_F (MatrixTest, max_1x1_Success)
226 {
227 // Arrange.
228 Matrix<double> test_matrix{
229 {0.0}
230 };
231 constexpr double etalon_result {0.0};
232
233 // Act.
234 const double test_result = test_matrix.max ();
235
236 // Assert.
237 EXPECT_FLOAT_EQ (etalon_result, test_result);
238 }
239
240
241
TEST_F(MatrixTest,max_2x2MinusInfinity_Success)242 TEST_F (MatrixTest, max_2x2MinusInfinity_Success)
243 {
244 // Arrange.
245 constexpr auto minusInfinity = - std::numeric_limits <double>::infinity ();
246 Matrix<double> test_matrix{
247 {minusInfinity, minusInfinity},
248 {minusInfinity, minusInfinity}
249 };
250 constexpr double etalon_result {minusInfinity};
251
252 // Act.
253 const double test_result = test_matrix.max ();
254
255 // Assert.
256 EXPECT_FLOAT_EQ (etalon_result, test_result);
257 }
258
259
260
TEST_F(MatrixTest,max_2x2Negative_Success)261 TEST_F (MatrixTest, max_2x2Negative_Success)
262 {
263 // Arrange.
264 constexpr auto minusInfinity = - std::numeric_limits <double>::infinity ();
265 constexpr auto negativeValue = std::numeric_limits <double>::min ();
266 Matrix<double> test_matrix{
267 {negativeValue, minusInfinity},
268 {minusInfinity, negativeValue}
269 };
270 constexpr double etalon_result {negativeValue};
271
272 // Act.
273 const double test_result = test_matrix.max ();
274
275 // Assert.
276 EXPECT_FLOAT_EQ (etalon_result, test_result);
277 }
278
279
280
TEST_F(MatrixTest,max_2x2Zero_Success)281 TEST_F (MatrixTest, max_2x2Zero_Success)
282 {
283 // Arrange.
284 constexpr auto minusInfinity = - std::numeric_limits <double>::infinity ();
285 constexpr auto negativeValue = std::numeric_limits <double>::min ();
286 Matrix<double> test_matrix{
287 { 0.0, minusInfinity},
288 {minusInfinity, negativeValue}
289 };
290 constexpr double etalon_result {0.0};
291
292 // Act.
293 const double test_result = test_matrix.max ();
294
295 // Assert.
296 EXPECT_FLOAT_EQ (etalon_result, test_result);
297 }
298
299
300
TEST_F(MatrixTest,max_2x2Positive_Success)301 TEST_F (MatrixTest, max_2x2Positive_Success)
302 {
303 // Arrange.
304 constexpr auto minusInfinity = - std::numeric_limits <double>::infinity ();
305 constexpr auto negativeValue = std::numeric_limits <double>::min ();
306 constexpr auto positiveValue = std::numeric_limits <double>::max ();
307 Matrix<double> test_matrix{
308 {negativeValue, minusInfinity},
309 {positiveValue, 0.0}
310 };
311 constexpr double etalon_result {positiveValue};
312
313 // Act.
314 const double test_result = test_matrix.max ();
315
316 // Assert.
317 EXPECT_FLOAT_EQ (etalon_result, test_result);
318 }
319
320
321
TEST_F(MatrixTest,max_2x2PlusInfinity_Success)322 TEST_F (MatrixTest, max_2x2PlusInfinity_Success)
323 {
324 // Arrange.
325 constexpr auto minusInfinity = - std::numeric_limits <double>::infinity ();
326 constexpr auto negativeValue = std::numeric_limits <double>::min ();
327 constexpr auto positiveValue = std::numeric_limits <double>::max ();
328 constexpr auto plusInfinity = std::numeric_limits <double>::infinity ();
329 Matrix<double> test_matrix{
330 {minusInfinity, positiveValue},
331 {negativeValue, plusInfinity}
332 };
333 constexpr double etalon_result {plusInfinity};
334
335 // Act.
336 const double test_result = test_matrix.max ();
337
338 // Assert.
339 EXPECT_FLOAT_EQ (etalon_result, test_result);
340 }
341
342
343
TEST_F(MatrixTest,minsize_RowsCountIsMin_Success)344 TEST_F (MatrixTest, minsize_RowsCountIsMin_Success)
345 {
346 // Arrange.
347 Matrix<double> test_matrix (1u, 2u);
348 constexpr unsigned int etalon_result {1u};
349
350 // Act.
351 const unsigned int test_result = test_matrix.minsize ();
352
353 // Assert.
354 EXPECT_EQ (etalon_result, test_result);
355 }
356
357
358
TEST_F(MatrixTest,minsize_ColumnsCountIsMin_Success)359 TEST_F (MatrixTest, minsize_ColumnsCountIsMin_Success)
360 {
361 // Arrange.
362 Matrix<double> test_matrix (2u, 1u);
363 constexpr unsigned int etalon_result {1u};
364
365 // Act.
366 const unsigned int test_result = test_matrix.minsize ();
367
368 // Assert.
369 EXPECT_EQ (etalon_result, test_result);
370 }
371
372
373
TEST_F(MatrixTest,minsize_RowsCountAndColumnsCountAreEqual_Success)374 TEST_F (MatrixTest, minsize_RowsCountAndColumnsCountAreEqual_Success)
375 {
376 // Arrange.
377 Matrix<double> test_matrix (3u, 3u);
378 constexpr unsigned int etalon_result {3u};
379
380 // Act.
381 const unsigned int test_result = test_matrix.minsize ();
382
383 // Assert.
384 EXPECT_EQ (etalon_result, test_result);
385 }
386
387
388
TEST_F(MatrixTest,columns_Success)389 TEST_F (MatrixTest, columns_Success)
390 {
391 // Arrange.
392 Matrix<double> test_matrix{
393 {0.0, 0.1, 0.2},
394 {1.0, 1.1, 1.2},
395 {2.0, 2.1, 2.2}
396 };
397 constexpr unsigned int etalon_result {3u};
398
399 // Act.
400 const unsigned int test_result = test_matrix.columns ();
401
402 // Assert.
403 EXPECT_EQ (etalon_result, test_result);
404 }
405
406
407
TEST_F(MatrixTest,rows_Success)408 TEST_F (MatrixTest, rows_Success)
409 {
410 // Arrange.
411 Matrix<double> test_matrix{
412 {0.0, 0.1, 0.2},
413 {1.0, 1.1, 1.2},
414 {2.0, 2.1, 2.2}
415 };
416 constexpr unsigned int etalon_result {3u};
417
418 // Act.
419 const unsigned int test_result = test_matrix.rows ();
420
421 // Assert.
422 EXPECT_EQ (etalon_result, test_result);
423 }
424