#include #include #include #include #include #include "../src/matrix.h" using namespace std; using namespace pcpo; template class MatrixTest: Matrix { public: static bool runTestMul1(); static bool runTestMul2(); static bool runTestTranspose1(); static bool runTestTranspose2(); static bool runTestEchelon1(); static bool runTestEchelon2(); static bool runTestEchelon3(); static bool runTestRank1(); static bool runTestRank2(); static bool runTestRank3(); static bool runTestSpan1(); }; template bool MatrixTest::runTestMul1() { std::cout << "Testing multiplication 1: "; bool result = false; std::vector> a = { {1,4,7}, {2,5,8}, {3,6,9} }; std::vector> b = { {4,29,0}, {-1,27,2}, {100,5,3} }; std::vector> expected = { {700,172,29}, {803,233,34}, {906,294,39} }; auto actual = Matrix(a) * Matrix(b); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestMul2() { std::cout << "Testing multiplication 2: "; bool result = false; std::vector> a = { {1,6,11}, {2,7,12}, {3,8,13}, {4,9,14}, {5,10,-9} }; std::vector> b = { {43,45,1,9}, {224,7,-2,24}, {12,1,13,-6} }; std::vector> expected = { {1519,87,132,87}, {1798,139,144,114}, {2077,191,156,141}, {2356,243,168,168}, {2347,295,-132,339} }; auto actual = Matrix(a) * Matrix(b); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestTranspose1() { std::cout << "Testing transpose 1: "; bool result = false; std::vector> a = { {1,2,3}, {4,5,6}, {7,8,9} }; std::vector> expected = { {1,4,7}, {2,5,8}, {3,6,9} }; auto matrix = Matrix(a); auto actual = matrix.transpose(); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestTranspose2() { std::cout << "Testing transpose 2: "; bool result = false; std::vector> a = { {1,3}, {2,4}, {3,5} }; std::vector> expected = { {1,2,3}, {4,5,6} }; auto matrix = Matrix(a); auto actual = matrix.transpose(); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestEchelon1() { std::cout << "Testing echelon 1: "; bool result = false; std::vector> a = { {1,4,7}, {2,5,8}, {3,6,9} }; std::vector> expected = { {1,0,-1}, {0,1,2}, {0,0,0} }; auto matrix = Matrix(a); auto actual = matrix.echelon(); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestEchelon2() { std::cout << "Testing echelon 2: "; bool result = false; std::vector> a = { {1,2,1}, {1,4,8}, {1,6,3} }; std::vector> expected = { {1,0,0}, {0,1,0}, {0,0,1} }; auto matrix = Matrix(a); auto actual = matrix.echelon(); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestEchelon3() { std::cout << "Testing echelon 3: "; bool result = false; std::vector> a = { {1,2,4}, {2,4,8}, {4,8,16} }; std::vector> expected = { {1,2,4}, {0,0,0}, {0,0,0} }; auto matrix = Matrix(a); auto actual = matrix.echelon(); auto x = Matrix(expected); result = actual == x; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestRank1() { std::cout << "Testing rank 1: "; bool result = false; std::vector> a = { {1,4,7}, {2,5,8}, {3,6,9} }; int expected = 2; auto matrix = Matrix(a); auto actual = matrix.getRank(); result = actual == expected; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestRank2() { std::cout << "Testing rank 2: "; bool result = false; std::vector> a = { {1,2,4}, {2,4,8}, {4,8,16} }; int expected = 1; auto matrix = Matrix(a); auto actual = matrix.getRank(); result = actual == expected; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestRank3() { std::cout << "Testing rank 3: "; bool result = false; std::vector> a = { {1,2,1}, {1,4,8}, {1,6,3} }; int expected = 3; auto matrix = Matrix(a); auto actual = matrix.getRank(); result = actual == expected; std::cout << (result? "success" : "failed") << "\n"; return result; } template bool MatrixTest::runTestSpan1() { std::cout << "Testing span 1: "; bool result = false; std::vector> a = { {1,1,4}, {0,1,4}, {1,0,0} }; std::vector> expected = { {1,1}, {0,1}, {1,0} }; auto matrix = Matrix(a); auto actual = Matrix::span(matrix); result = actual == Matrix(expected); std::cout << (result? "success" : "failed") << "\n"; return result; } int main() { return !(MatrixTest::runTestMul1() && MatrixTest::runTestMul2() && MatrixTest::runTestTranspose1() && MatrixTest::runTestTranspose2() && MatrixTest::runTestEchelon1() && MatrixTest::runTestEchelon2() && MatrixTest::runTestEchelon3() && MatrixTest::runTestRank1() && MatrixTest::runTestRank2() && MatrixTest::runTestRank3() && MatrixTest::runTestSpan1() && MatrixTest::runTestMul2() && MatrixTest::runTestTranspose1() && MatrixTest::runTestTranspose2() && MatrixTest::runTestEchelon1() && MatrixTest::runTestEchelon2() && MatrixTest::runTestEchelon3() && MatrixTest::runTestRank1() && MatrixTest::runTestRank2() && MatrixTest::runTestRank3() && MatrixTest::runTestSpan1() ); };