|
| 1 | +#include <GRT.h> |
| 2 | +#include "gtest/gtest.h" |
| 3 | +using namespace GRT; |
| 4 | + |
| 5 | +//Unit tests for the GRT Matrix class |
| 6 | + |
| 7 | +// Tests the default c'tor. |
| 8 | +TEST(Matrix, DefaultConstructor) { |
| 9 | + Matrix< int > mat; |
| 10 | + |
| 11 | + EXPECT_EQ(0, mat.getSize()); |
| 12 | + EXPECT_EQ(0, mat.getNumRows()); |
| 13 | + EXPECT_EQ(0, mat.getNumCols()); |
| 14 | +} |
| 15 | + |
| 16 | +// Tests the resize c'tor. |
| 17 | +TEST(Matrix, ResizeConstructor) { |
| 18 | + const UINT numRows = 100; |
| 19 | + const UINT numCols = 50; |
| 20 | + Matrix< int > mat( numRows, numCols ); |
| 21 | + EXPECT_EQ(numRows*numCols, mat.getSize()); |
| 22 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 23 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 24 | +} |
| 25 | + |
| 26 | +// Tests the copy c'tor. |
| 27 | +TEST(Matrix, CopyConstructor) { |
| 28 | + const UINT numRows = 100; |
| 29 | + const UINT numCols = 50; |
| 30 | + Matrix< int > mat1( numRows, numCols ); |
| 31 | + EXPECT_EQ(numRows, mat1.getNumRows()); |
| 32 | + EXPECT_EQ(numCols, mat1.getNumCols()); |
| 33 | + Matrix< int > mat2( mat1 ); |
| 34 | + EXPECT_EQ(numRows, mat2.getNumRows()); |
| 35 | + EXPECT_EQ(numCols, mat2.getNumCols()); |
| 36 | +} |
| 37 | + |
| 38 | +// Tests the equals operator. |
| 39 | +TEST(Matrix, EqualsConstructor) { |
| 40 | + const UINT numRows = 100; |
| 41 | + const UINT numCols = 50; |
| 42 | + Matrix< int > mat1( numRows, numCols ); |
| 43 | + EXPECT_EQ(numRows, mat1.getNumRows()); |
| 44 | + EXPECT_EQ(numCols, mat1.getNumCols()); |
| 45 | + Matrix< int > mat2; |
| 46 | + mat2 = mat1; |
| 47 | + EXPECT_EQ(numRows, mat2.getNumRows()); |
| 48 | + EXPECT_EQ(numCols, mat2.getNumCols()); |
| 49 | +} |
| 50 | + |
| 51 | +// Tests the Vector c'tor. |
| 52 | +TEST(Matrix, VectorConstructor) { |
| 53 | + const UINT numRows = 100; |
| 54 | + const UINT numCols = 50; |
| 55 | + Vector< Vector< int > > vec( numRows, Vector< int >( numCols ) ); |
| 56 | + for(UINT i=0; i<numRows; i++){ |
| 57 | + for(UINT j=0; j<numCols; j++){ |
| 58 | + vec[i][j] = 1; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + Matrix< int > mat( vec ); |
| 63 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 64 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 65 | + for(UINT i=0; i<numRows; i++){ |
| 66 | + for(UINT j=0; j<numCols; j++){ |
| 67 | + EXPECT_EQ(mat[i][j],vec[i][j]); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Tests the [] operator |
| 73 | +TEST(Matrix, AccessOperator) { |
| 74 | + const UINT numRows = 100; |
| 75 | + const UINT numCols = 50; |
| 76 | + Matrix< int > mat( numRows, numCols ); |
| 77 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 78 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 79 | + for(UINT i=0; i<numRows; i++){ |
| 80 | + for(UINT j=0; j<numCols; j++){ |
| 81 | + mat[i][j] = i*j; |
| 82 | + } |
| 83 | + } |
| 84 | + for(UINT i=0; i<numRows; i++){ |
| 85 | + for(UINT j=0; j<numCols; j++){ |
| 86 | + EXPECT_EQ(mat[i][j],i*j); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Tests the getRowVector |
| 92 | +TEST(Matrix, GetRowVector) { |
| 93 | + const UINT numRows = 100; |
| 94 | + const UINT numCols = 50; |
| 95 | + Matrix< int > mat( numRows, numCols ); |
| 96 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 97 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 98 | + for(UINT i=0; i<numRows; i++){ |
| 99 | + for(UINT j=0; j<numCols; j++){ |
| 100 | + mat[i][j] = i*j; |
| 101 | + } |
| 102 | + } |
| 103 | + for(UINT i=0; i<numRows; i++){ |
| 104 | + Vector< int > rowVector = mat.getRowVector( i ); |
| 105 | + for(UINT j=0; j<numCols; j++){ |
| 106 | + EXPECT_EQ(rowVector[j],i*j); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Tests the getColVector |
| 112 | +TEST(Matrix, GetColVector) { |
| 113 | + const UINT numRows = 100; |
| 114 | + const UINT numCols = 50; |
| 115 | + Matrix< int > mat( numRows, numCols ); |
| 116 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 117 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 118 | + for(UINT i=0; i<numRows; i++){ |
| 119 | + for(UINT j=0; j<numCols; j++){ |
| 120 | + mat[i][j] = i*j; |
| 121 | + } |
| 122 | + } |
| 123 | + for(UINT j=0; j<numCols; j++){ |
| 124 | + Vector< int > colVector = mat.getColVector( j ); |
| 125 | + for(UINT i=0; i<numRows; i++){ |
| 126 | + EXPECT_EQ(colVector[i],i*j); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Tests the resize |
| 132 | +TEST(Matrix, Resize) { |
| 133 | + const UINT numRows = 100; |
| 134 | + const UINT numCols = 50; |
| 135 | + const UINT newRows = 200; |
| 136 | + const UINT newCols = 100; |
| 137 | + Matrix< int > mat( numRows, numCols ); |
| 138 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 139 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 140 | + EXPECT_TRUE( mat.resize( newRows, newCols ) ); |
| 141 | + EXPECT_EQ(newRows, mat.getNumRows()); |
| 142 | + EXPECT_EQ(newCols, mat.getNumCols()); |
| 143 | +} |
| 144 | + |
| 145 | +// Tests the copy |
| 146 | +TEST(Matrix, Copy) { |
| 147 | + const UINT numRows = 100; |
| 148 | + const UINT numCols = 50; |
| 149 | + Matrix< int > mat1( numRows, numCols ); |
| 150 | + EXPECT_EQ(numRows, mat1.getNumRows()); |
| 151 | + EXPECT_EQ(numCols, mat1.getNumCols()); |
| 152 | + for(UINT i=0; i<numRows; i++){ |
| 153 | + for(UINT j=0; j<numCols; j++){ |
| 154 | + mat1[i][j] = i*j; |
| 155 | + } |
| 156 | + } |
| 157 | + Matrix< int > mat2; |
| 158 | + EXPECT_TRUE( mat2.copy( mat1 ) ); |
| 159 | + EXPECT_EQ(numRows, mat2.getNumRows()); |
| 160 | + EXPECT_EQ(numCols, mat2.getNumCols()); |
| 161 | + for(UINT i=0; i<numRows; i++){ |
| 162 | + for(UINT j=0; j<numCols; j++){ |
| 163 | + EXPECT_EQ( mat1[i][j], mat2[i][j] ); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// Tests the setAllValues |
| 169 | +TEST(Matrix, SetAll) { |
| 170 | + const UINT numRows = 100; |
| 171 | + const UINT numCols = 50; |
| 172 | + Matrix< int > mat( numRows, numCols ); |
| 173 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 174 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 175 | + for(UINT i=0; i<numRows; i++){ |
| 176 | + for(UINT j=0; j<numCols; j++){ |
| 177 | + mat[i][j] = i*j; |
| 178 | + } |
| 179 | + } |
| 180 | + EXPECT_TRUE( mat.setAll( 0 ) ); |
| 181 | + for(UINT i=0; i<numRows; i++){ |
| 182 | + for(UINT j=0; j<numCols; j++){ |
| 183 | + EXPECT_EQ( mat[i][j], 0 ); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +// Tests the setRowVector |
| 189 | +TEST(Matrix, SetRowVector) { |
| 190 | + const UINT numRows = 100; |
| 191 | + const UINT numCols = 50; |
| 192 | + Matrix< int > mat( numRows, numCols ); |
| 193 | + EXPECT_EQ(numRows, mat.getNumRows()); |
| 194 | + EXPECT_EQ(numCols, mat.getNumCols()); |
| 195 | + for(UINT i=0; i<numRows; i++){ |
| 196 | + for(UINT j=0; j<numCols; j++){ |
| 197 | + mat[i][j] = 0; |
| 198 | + } |
| 199 | + } |
| 200 | + Vector< int > vec( numCols ); |
| 201 | + EXPECT_TRUE( vec.setAll( 1000 ) ); |
| 202 | + EXPECT_TRUE( mat.setRowVector( vec, 0 ) ); |
| 203 | + for(UINT i=0; i<numRows; i++){ |
| 204 | + for(UINT j=0; j<numCols; j++){ |
| 205 | + EXPECT_EQ( mat[i][j], i == 0 ? 1000 : 0 ); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +int main(int argc, char **argv) { |
| 211 | + ::testing::InitGoogleTest( &argc, argv ); |
| 212 | + return RUN_ALL_TESTS(); |
| 213 | +} |
0 commit comments