Skip to content

Commit

Permalink
More test cases and fixes for linalg headers
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Aug 1, 2024
1 parent 56fca6e commit fdbe182
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 36 deletions.
9 changes: 3 additions & 6 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ template <typename T> class graph {
* @param g the graph we want to copy
*/
graph(const graph &g) : adj(g.adj), _elements(g._elements), _type(g._type) {



}

/**
Expand Down Expand Up @@ -583,9 +580,9 @@ template <typename T> 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) {



}

/**
Expand Down
54 changes: 46 additions & 8 deletions src/linalg/mat_1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include <iostream>
#include <optional>
#include <vector>
#include <initializer_list>
#endif

/**
*@brief Class for 1-dimensional Matrix
*
*/
template <typename T, size_t SIZE> class Mat1d {
template <typename T, size_t SIZE>
class Mat1d {
private:
T *arr;
size_t _size;
Expand Down Expand Up @@ -43,6 +45,18 @@ template <typename T, size_t SIZE> 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<T> 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
Expand All @@ -62,7 +76,7 @@ template <typename T, size_t SIZE> 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++) {
Expand Down Expand Up @@ -100,7 +114,7 @@ template <typename T, size_t SIZE> 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;

Expand Down Expand Up @@ -131,7 +145,7 @@ template <typename T, size_t SIZE> class Mat1d {
*@return true if &this is equal to mat
*@return false otherwise
*/
bool operator==(Mat1d &mat) const {
bool operator==(const Mat1d<T, SIZE> &mat) const {
if (mat.size() != _size) {
return false;
}
Expand All @@ -144,15 +158,25 @@ template <typename T, size_t SIZE> 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<T, SIZE> &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 << " ";
}
Expand All @@ -162,10 +186,24 @@ template <typename T, size_t SIZE> class Mat1d {
}
};

/**
* @brief Template specialization for comparing Mat1d objects of different sizes
*/
template <typename T, size_t SIZE1, size_t SIZE2>
bool operator==(const Mat1d<T, SIZE1> &mat1, const Mat1d<T, SIZE2> &mat2) {
return false; // Matrices of different sizes are not equal
}

template <typename T, size_t SIZE1, size_t SIZE2>
bool operator!=(const Mat1d<T, SIZE1> &mat1, const Mat1d<T, SIZE2> &mat2) {
return !(mat1 == mat2); // Uses the == operator specialization
}

/**
* @brief Iterator class
*/
template <typename T, size_t SIZE> class Mat1d<T, SIZE>::Iterator {
template <typename T, size_t SIZE>
class Mat1d<T, SIZE>::Iterator {
private:
size_t _size;
size_t index;
Expand All @@ -180,7 +218,7 @@ template <typename T, size_t SIZE> class Mat1d<T, SIZE>::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];
}
Expand Down
14 changes: 8 additions & 6 deletions src/linalg/mat_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <typename T, size_t ROWS, size_t COLS> class Mat2d {
*/
explicit Mat2d(std::vector<std::vector<T>> 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 ||
Expand Down Expand Up @@ -92,7 +92,7 @@ template <typename T, size_t ROWS, size_t COLS> 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");
}
Expand All @@ -103,6 +103,7 @@ template <typename T, size_t ROWS, size_t COLS> class Mat2d {
} catch (std::logic_error &e) {
std::cerr << e.what() << '\n';
}
return *(this);
}

/**
Expand Down Expand Up @@ -192,7 +193,6 @@ class Mat2d<T, ROWS, COLS>::Iterator {
size_t _cols;
size_t _index;
size_t _size;
Mat1d<T, COLS> mat;

public:
/**
Expand All @@ -205,7 +205,7 @@ class Mat2d<T, ROWS, COLS>::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];
Expand Down Expand Up @@ -272,14 +272,16 @@ class Mat2d<T, ROWS, COLS>::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<T, COLS> the 1-Dimensional Matrix in the current row
*/
Mat1d<T, COLS> operator*() {
template <typename A, size_t CCOLS>
Mat1d<A, CCOLS> operator*() {
Mat1d<T, COLS> mat(0);
size_t index = 0;
for (size_t j = this->_index * _cols; j < this->_index * _cols + _rows;
++j) {
Expand Down
62 changes: 46 additions & 16 deletions tests/linalg/mat1d/mat_1d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,50 @@ TEST_CASE("testing copy constructor of mat_1d") {
REQUIRE(v3 == v4);
}

// TEST_CASE("testing iterators for Mat1d class"){
// Mat1d<char, 5> mat({'a','b','c','d','e'});
// std::vector<char> v = {'a','b','c','d','e'};
// std::vector<char> 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<char, 5> mat({'a','b','c','d','e'});
std::vector<char> v = {'a','b','c','d','e'};
std::vector<char> check;
for(auto it = mat.begin(); it != mat.end(); it++){
check.push_back(*(it));
}
REQUIRE(v == check);

Mat1d<std::string, 5> mat2({"hello", "world", "we", "are", "csrt"});
std::vector<std::string> v2 = {"hello", "world", "we", "are", "csrt"};
std::vector<std::string> check2;
for(auto it = mat2.begin(); it != mat2.end(); it++){
check2.push_back(*(it));
}
REQUIRE(v2 == check2);
}

TEST_CASE("testing operator = [1]") {
Mat1d<int, 5> mat({1, 2, 3, 4, 5});
Mat1d<int, 5> mat2;
mat2 = mat;
REQUIRE(mat == mat2);
}

// Mat1d<std::string, 5> mat2({"hello", "world", "we", "are", "csrt"});
// std::vector<std::string> v2 = {"hello", "world", "we", "are", "csrt"};
// std::vector<std::string> 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<int, 5> mat({1, 2, 3, 4, 5});
Mat1d<int, 6> mat2({1, 2, 3, 4, 5, 6});
bool eq = (mat == mat2);
REQUIRE(eq == 0);
}

TEST_CASE("testing operator << for exceptions") {
Mat1d<char, 5> mat({'a', 'b', 'c', 'd', 'e'});
CHECK_NOTHROW(std::cout << mat << '\n');
Mat1d<std::string, 2> mat2({"hello", "world"});
CHECK_NOTHROW(std::cout << mat2 << '\n');
Mat1d<int, 5> mat3({1, 2, 3, 4, 5});
CHECK_NOTHROW(std::cout << mat3 << '\n');
}

TEST_CASE("testing constructor with one value for Mat1d class [1]") {
Mat1d<int, 5> mat(5);
for(auto it = mat.begin(); it != mat.end(); it++){
REQUIRE(*(it) == 5);
}
}
35 changes: 35 additions & 0 deletions tests/linalg/mat2d/mat_2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, 5, 5> mat(5);
REQUIRE(mat.size() == 25);
REQUIRE(mat.cols() == 5);
REQUIRE(mat.rows() == 5);
Mat1d<int, 5> check({5, 5, 5, 5, 5});
REQUIRE(mat(0) == check);
}

TEST_CASE("testing operator << for Mat2d class") {
Mat2d<char, 5, 5> mat('a');
CHECK_NOTHROW(std::cout << mat << '\n');
Mat2d<int, 5, 5> mat2(5);
CHECK_NOTHROW(std::cout << mat2 << '\n');
Mat2d<std::string, 10, 10> mat3("CSRT");
CHECK_NOTHROW(std::cout << mat3 << '\n');
}

TEST_CASE("testing copy constructor for Mat2d class") {
Mat2d<int, 3, 3> mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
Mat2d<int, 3, 3> mat2(mat);
for(int i = 0; i<3; i++){
REQUIRE(mat(i) == mat2(i));
}
}

TEST_CASE("testing operator = for Mat2d class") {
Mat2d<int, 10, 10> mat(10);
Mat2d<int, 10, 10> mat2;
mat2 = mat;
for(int i = 0; i<10; i++){
REQUIRE(mat(i) == mat2(i));
}
}

0 comments on commit fdbe182

Please sign in to comment.