You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/cross-platform/matrix/3-code-1.md
+52-29Lines changed: 52 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,12 +23,13 @@ In the Matrix processing library, you implement two types of checks:
23
23
24
24
The idea here is to make the program fail in a noticeable way. Of course, in a real world application, the error should be caught and dealt with by the application, if it can. Error handling, and especially recovering from errors, can be a complex topic.
25
25
26
-
At the top of file `include/Matrix/Matrix.h`, include `<cassert>` to get the C-style assertions declarations for checks in `Debug` mode only:
26
+
At the top of file `include/Matrix/Matrix.h`, include `<cassert>` to get the C-style assertions declarations for checks in `Debug` mode only, as well as `<cstddef>` which provides standard C declaration like `size_t`:
27
27
28
28
```CPP
29
29
#pragma once
30
30
31
31
#include<cassert>
32
+
#include<cstddef>
32
33
33
34
namespaceMatComp {
34
35
```
@@ -44,7 +45,7 @@ const Version &getVersion();
44
45
/// and the EXIT_FAILURE error code. It will also print the file name (\p
45
46
/// fileName) and line number (\p lineNumber) that caused that application to
@@ -109,10 +110,11 @@ The Matrix data structure has the following private data members:
109
110
Modern C++ offers constructs in the language to deal safely with memory; you will use `std::unique_ptr` which guaranties that the Matrix class will be safe from a whole range of memory management errors.
110
111
111
112
Add the following includes at the top of `include/Matrix/Matrix.h`, right under
This constructs a valid `Matrix` if it contains elements), and the `uninitializedConstruct` test checks that two valid matrices of different types and dimensions can be constructed.
347
+
You should also update the `booleanConversion` test in this file to check for boolean conversion for valid matrices so it now looks like:
348
+
349
+
```CPP
350
+
TEST(Matrix, booleanConversion) {
351
+
EXPECT_FALSE(Matrix<int8_t>());
352
+
EXPECT_FALSE(Matrix<double>());
353
+
354
+
EXPECT_TRUE(Matrix<int8_t>(1, 1));
355
+
EXPECT_TRUE(Matrix<double>(1, 1));
356
+
}
357
+
```
351
358
352
359
Compile and test again, all should pass:
353
360
@@ -374,6 +381,35 @@ ninja check
374
381
[ PASSED ] 4 tests.
375
382
```
376
383
384
+
Another constructor that is missing is one that will create and initialize matrices to a known value. Let's add it to `Matrix` in `include/Matrix/Matrix.h`:
385
+
386
+
```CPP
387
+
/// Construct a \p numRows x \p numCols Matrix with all elements
388
+
/// initialized to value \p val.
389
+
Matrix(size_t numRows, size_t numCols, Ty val) : Matrix(numRows, numCols) {
390
+
allocate(getNumElements());
391
+
for (size_t i = 0; i < getNumElements(); i++)
392
+
data[i] = val;
393
+
}
394
+
```
395
+
396
+
Add boolean conversion tests for this new constructor by modifying `booleanConversion` in `tests/Matrix.cpp` so it looks like:
397
+
398
+
```CPP
399
+
TEST(Matrix, booleanConversion) {
400
+
EXPECT_FALSE(Matrix<int8_t>());
401
+
EXPECT_FALSE(Matrix<double>());
402
+
403
+
EXPECT_TRUE(Matrix<int8_t>(1, 1));
404
+
EXPECT_TRUE(Matrix<double>(1, 1));
405
+
406
+
EXPECT_TRUE(Matrix<int8_t>(1, 1, 1));
407
+
EXPECT_TRUE(Matrix<double>(1, 1, 2.0));
408
+
}
409
+
```
410
+
411
+
You should be getting the pattern now: each new feature or method comes with tests.
412
+
377
413
The `Matrix` class is missing two important methods:
378
414
- A *getter*, to read the matrix element at (row, col).
379
415
- A *setter*, to modify the matrix element at (row, col).
@@ -397,20 +433,6 @@ Add them now in the public section of `Matrix` in `include/Matrix/Matrix.h`:
397
433
}
398
434
```
399
435
400
-
Another constructor that is missing is one that will create and initialize matrices to a known value. Let's add it to `Matrix` in `include/Matrix/Matrix.h`:
401
-
402
-
```CPP
403
-
/// Construct a \p numRows x \p numColumns Matrix with all elements
404
-
/// initialized to value \p val.
405
-
Matrix(size_t numRows, size_t numCols, Ty val) : Matrix(numRows, numCols) {
406
-
allocate(getNumElements());
407
-
for (size_t i = 0; i < getNumElements(); i++)
408
-
data[i] = val;
409
-
}
410
-
```
411
-
412
-
You should be getting the pattern now.
413
-
414
436
Add tests for those 3 methods in `tests/Matrix.cpp`:
415
437
416
438
```CPP
@@ -503,7 +525,7 @@ The C++ `std::initializer_list` enables users to provide a list of literal
503
525
values (in row major order) to use to initialize the matrix with:
504
526
505
527
```CPP
506
-
/// Construct a \p numRows x \p numColumns Matrix with elements
528
+
/// Construct a \p numRows x \p numCols Matrix with elements
507
529
/// initialized from the values from \p il in row-major order.
508
530
Matrix(size_t numRows, size_t numCols, std::initializer_list<Ty> il)
509
531
: Matrix(numRows, numCols) {
@@ -862,7 +884,7 @@ ninja check
862
884
[----------] 16 tests from Matrix (0 ms total)
863
885
864
886
[----------] Global test environment tear-down
865
-
[==========] 16 tests from 3 test suites ran. (0 ms total)
887
+
[==========] 16 tests from 1 test suite ran. (0 ms total)
866
888
[ PASSED ] 16 tests.
867
889
```
868
890
@@ -941,6 +963,7 @@ Add these to the public section of `Matrix` in `include/Matrix/Matrix.h`:
941
963
return false;
942
964
return true;
943
965
}
966
+
944
967
/// Returns true iff matrices do not compare equal.
0 commit comments