diff --git a/src/classes/graph/graph.h b/src/classes/graph/graph.h index e9ee5d63..7a192131 100644 --- a/src/classes/graph/graph.h +++ b/src/classes/graph/graph.h @@ -57,9 +57,6 @@ template class graph { * @param g the graph we want to copy */ graph(const graph &g) : adj(g.adj), _elements(g._elements), _type(g._type) { - - - } /** @@ -583,9 +580,9 @@ template class weighted_graph { * @param g the graph we want to copy */ explicit weighted_graph(const weighted_graph &g) : adj(g.adj), _elements(g._elements), _type(g._type) { - - - + + + } /** diff --git a/src/linalg/mat_1d.h b/src/linalg/mat_1d.h index f0b058bf..027d9302 100644 --- a/src/linalg/mat_1d.h +++ b/src/linalg/mat_1d.h @@ -9,13 +9,15 @@ #include #include #include +#include #endif /** *@brief Class for 1-dimensional Matrix * */ -template class Mat1d { +template +class Mat1d { private: T *arr; size_t _size; @@ -43,6 +45,18 @@ template class Mat1d { } } + /** + *@brief constructor for Mat1d class with initializer list + *@param il the initializer list that we want to use to initialize the array + */ + explicit Mat1d(std::initializer_list il) : _size(SIZE) { + arr = new T[_size]; + if (il.size() != _size) { + throw std::logic_error("Initializer list doesn't have the same size as the constructed array"); + } + std::copy(il.begin(), il.end(), arr); + } + /** *@brief constructor for Mat1d class with input value *@param val the value that we want all the elements of the array to have @@ -62,7 +76,7 @@ template class Mat1d { explicit Mat1d(Mat1d &mat) : _size(SIZE) { try { if (mat.size() != _size) { - throw std::logic_error("Tried to copy matrixes with different sizes"); + throw std::logic_error("Tried to copy matrices with different sizes"); } this->arr = new T[_size]; for (size_t i = 0; i < _size; i++) { @@ -100,7 +114,7 @@ template class Mat1d { *@return size_t the size of the array * */ - size_t size() { return this->_size; } + size_t size() const { return this->_size; } class Iterator; @@ -131,7 +145,7 @@ template class Mat1d { *@return true if &this is equal to mat *@return false otherwise */ - bool operator==(Mat1d &mat) const { + bool operator==(const Mat1d &mat) const { if (mat.size() != _size) { return false; } @@ -144,15 +158,25 @@ template class Mat1d { return true; } + /** + *@brief operator != for Mat1d class + *@param mat the matrix to compare + *@return true if &this is not equal to mat + *@return false otherwise + */ + bool operator!=(const Mat1d &mat) const { + return !(*this == mat); + } + /** *@brief operator << for Mat1d class *@param mat the matrix * */ - friend std::ostream &operator<<(std::ostream &out, Mat1d &mat) { + friend std::ostream &operator<<(std::ostream &out, const Mat1d &mat) { out << '['; for (size_t i = 0; i < mat.size(); i++) { - out << mat[i]; + out << mat.arr[i]; if (i != mat.size() - 1) { out << " "; } @@ -162,10 +186,24 @@ template class Mat1d { } }; +/** + * @brief Template specialization for comparing Mat1d objects of different sizes + */ +template +bool operator==(const Mat1d &mat1, const Mat1d &mat2) { + return false; // Matrices of different sizes are not equal +} + +template +bool operator!=(const Mat1d &mat1, const Mat1d &mat2) { + return !(mat1 == mat2); // Uses the == operator specialization +} + /** * @brief Iterator class */ -template class Mat1d::Iterator { +template +class Mat1d::Iterator { private: size_t _size; size_t index; @@ -180,7 +218,7 @@ template class Mat1d::Iterator { */ explicit Iterator(size_t _index, size_t _size, T *_arr) noexcept : index(_index), _size(_size), arr(new T[_size]) { - + for (size_t i = 0; i < _size; i++) { arr[i] = _arr[i]; } diff --git a/src/linalg/mat_2d.h b/src/linalg/mat_2d.h index 0e8d7465..8526d51a 100644 --- a/src/linalg/mat_2d.h +++ b/src/linalg/mat_2d.h @@ -28,7 +28,7 @@ template class Mat2d { */ explicit Mat2d(std::vector> v = {}) : arr(new T[ROWS * COLS]), _cols(COLS), _rows(ROWS), _size(ROWS * COLS) { - + if (!v.empty()) { try { if (v.size() * v[0].size() != _size || v[0].size() != _cols || @@ -92,7 +92,7 @@ template class Mat2d { return *(this); } try { - if (mat.size() != _size || mat.cols() != _cols || mat.rows != _rows) { + if (mat.size() != _size || mat.cols() != _cols || mat.rows() != _rows) { throw std::logic_error( "Tried to copy matrixes with different dimensions"); } @@ -103,6 +103,7 @@ template class Mat2d { } catch (std::logic_error &e) { std::cerr << e.what() << '\n'; } + return *(this); } /** @@ -192,7 +193,6 @@ class Mat2d::Iterator { size_t _cols; size_t _index; size_t _size; - Mat1d mat; public: /** @@ -205,7 +205,7 @@ class Mat2d::Iterator { */ explicit Iterator(T *_arr, size_t rows, size_t cols, size_t index) noexcept : _rows(rows), _cols(cols), _index(index), _size(rows * cols) { - + arr = new T[_size]; for (size_t i = 0; i < _size; i++) { arr[i] = _arr[i]; @@ -272,14 +272,16 @@ class Mat2d::Iterator { *@return false otherwise */ bool operator!=(const Iterator &it) { - return it.arr[it.index] != arr[this->index]; + return it.arr[it._index] != arr[this->_index]; } /** *@brief operator * for Iterator class *@return Mat1d the 1-Dimensional Matrix in the current row */ - Mat1d operator*() { + template + Mat1d operator*() { + Mat1d mat(0); size_t index = 0; for (size_t j = this->_index * _cols; j < this->_index * _cols + _rows; ++j) { diff --git a/tests/linalg/mat1d/mat_1d.cc b/tests/linalg/mat1d/mat_1d.cc index 84824e0d..6d27861c 100644 --- a/tests/linalg/mat1d/mat_1d.cc +++ b/tests/linalg/mat1d/mat_1d.cc @@ -63,20 +63,50 @@ TEST_CASE("testing copy constructor of mat_1d") { REQUIRE(v3 == v4); } -// TEST_CASE("testing iterators for Mat1d class"){ -// Mat1d mat({'a','b','c','d','e'}); -// std::vector v = {'a','b','c','d','e'}; -// std::vector check; -// for(auto it = mat.begin(); it != mat.end(); it++){ -// check.push_back(*(it)); -// } -// REQUIRE(v == check); +TEST_CASE("testing iterators for Mat1d class"){ + Mat1d mat({'a','b','c','d','e'}); + std::vector v = {'a','b','c','d','e'}; + std::vector check; + for(auto it = mat.begin(); it != mat.end(); it++){ + check.push_back(*(it)); + } + REQUIRE(v == check); + + Mat1d mat2({"hello", "world", "we", "are", "csrt"}); + std::vector v2 = {"hello", "world", "we", "are", "csrt"}; + std::vector check2; + for(auto it = mat2.begin(); it != mat2.end(); it++){ + check2.push_back(*(it)); + } + REQUIRE(v2 == check2); +} + +TEST_CASE("testing operator = [1]") { + Mat1d mat({1, 2, 3, 4, 5}); + Mat1d mat2; + mat2 = mat; + REQUIRE(mat == mat2); +} -// Mat1d mat2({"hello", "world", "we", "are", "csrt"}); -// std::vector v2 = {"hello", "world", "we", "are", "csrt"}; -// std::vector check2; -// for(auto it = mat2.begin(); it != mat2.end(); it++){ -// check2.push_back(*(it)); -// } -// REQUIRE(v2 == check2); -//} +TEST_CASE("testing operator == with falsy size") { + Mat1d mat({1, 2, 3, 4, 5}); + Mat1d mat2({1, 2, 3, 4, 5, 6}); + bool eq = (mat == mat2); + REQUIRE(eq == 0); +} + +TEST_CASE("testing operator << for exceptions") { + Mat1d mat({'a', 'b', 'c', 'd', 'e'}); + CHECK_NOTHROW(std::cout << mat << '\n'); + Mat1d mat2({"hello", "world"}); + CHECK_NOTHROW(std::cout << mat2 << '\n'); + Mat1d mat3({1, 2, 3, 4, 5}); + CHECK_NOTHROW(std::cout << mat3 << '\n'); +} + +TEST_CASE("testing constructor with one value for Mat1d class [1]") { + Mat1d mat(5); + for(auto it = mat.begin(); it != mat.end(); it++){ + REQUIRE(*(it) == 5); + } +} diff --git a/tests/linalg/mat2d/mat_2d.cc b/tests/linalg/mat2d/mat_2d.cc index 893e1b79..6e4ff368 100644 --- a/tests/linalg/mat2d/mat_2d.cc +++ b/tests/linalg/mat2d/mat_2d.cc @@ -43,3 +43,38 @@ TEST_CASE("testing iterators for Mat2d class") { REQUIRE(ww == true); REQUIRE(ww2 == true); } + +TEST_CASE("testing constructor with one value [1]") { + Mat2d mat(5); + REQUIRE(mat.size() == 25); + REQUIRE(mat.cols() == 5); + REQUIRE(mat.rows() == 5); + Mat1d check({5, 5, 5, 5, 5}); + REQUIRE(mat(0) == check); +} + +TEST_CASE("testing operator << for Mat2d class") { + Mat2d mat('a'); + CHECK_NOTHROW(std::cout << mat << '\n'); + Mat2d mat2(5); + CHECK_NOTHROW(std::cout << mat2 << '\n'); + Mat2d mat3("CSRT"); + CHECK_NOTHROW(std::cout << mat3 << '\n'); +} + +TEST_CASE("testing copy constructor for Mat2d class") { + Mat2d mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); + Mat2d mat2(mat); + for(int i = 0; i<3; i++){ + REQUIRE(mat(i) == mat2(i)); + } +} + +TEST_CASE("testing operator = for Mat2d class") { + Mat2d mat(10); + Mat2d mat2; + mat2 = mat; + for(int i = 0; i<10; i++){ + REQUIRE(mat(i) == mat2(i)); + } +}