diff --git a/Makefile b/Makefile index 012a5e1..5a8b826 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ all: test -test: +build: g++ -Wall -pedantic tests/run.cpp -o tests/SparseMatrix-tests + +test: build ./tests/SparseMatrix-tests.exe debug: diff --git a/README.md b/README.md index b99ff10..16e16e9 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,6 @@ C++ implementation of sparse matrix using [CRS format](http://netlib.org/linalg/ [![Buy me a Coffee](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KWQJ7VTXZMZLS) -## Installation - -To use this library simply include the implementation file: - -```cpp -#include "/src/SparseMatrix/SparseMatrix.cpp" -``` - - ## Usage ### Creation @@ -21,11 +12,11 @@ To use this library simply include the implementation file: SparseMatrix comes as a template class, so we have to specify the element type. ```cpp -SparseMatrix matrix(3); // 3×3 matrix of integers -SparseMatrix matrix2(4, 5); // 4×5 matrix - 4 rows, 5 columns +SparseMatrix::SparseMatrix matrix(3); // 3×3 matrix of integers +SparseMatrix::SparseMatrix matrix2(4, 5); // 4×5 matrix - 4 rows, 5 columns -SparseMatrix matrix3(matrix); // copy constructor -SparseMatrix matrix4 = matrix2; // deep copy assignment +SparseMatrix::SparseMatrix matrix3(matrix); // copy constructor +SparseMatrix::SparseMatrix matrix4 = matrix2; // deep copy assignment ``` All values are now equal to `()`, which for type `int` is `0`. @@ -52,10 +43,10 @@ SparseMatrix is implemented as an immutable object - all operations create new m Number of columns in the matrix has to be the same as the size of the vector, otherwise `InvalidDimensionsException` is thrown. ```cpp -SparseMatrix mat(4, 5); -vector vec(5, 2); +SparseMatrix::SparseMatrix mat(4, 5); +std::vector vec(5, 2); -vector result; +std::vector result; result = mat.multiply(vec); // method result = mat * vec; // operator ``` @@ -65,10 +56,10 @@ result = mat * vec; // operator Number of columns in the left matrix must be same as number of rows in the right matrix, otherwise `InvalidDimensionsException` is thrown. ```cpp -SparseMatrix matrixA(2, 3); -SparseMatrix matrixB(3, 4); +SparseMatrix::SparseMatrix matrixA(2, 3); +SparseMatrix::SparseMatrix matrixB(3, 4); -SparseMatrix product; // will be of size 2×4 +SparseMatrix::SparseMatrix product; // will be of size 2×4 product = matrixA.multiply(matrixB); // method product = matrixA * matrixB; // operator ``` @@ -78,14 +69,14 @@ product = matrixA * matrixB; // operator You can also add and subtract matrices together. Both matrices has to have same dimentions, otherwise `InvalidDimensionsException` is thrown. ```cpp -SparseMatrix matrixA(4, 7); -SparseMatrix matrixB(4, 7); +SparseMatrix::SparseMatrix matrixA(4, 7); +SparseMatrix::SparseMatrix matrixB(4, 7); -SparseMatrix sum; +SparseMatrix::SparseMatrix sum; sum = matrixA.add(matrixB); // method sum = matrixA + matrixB; // operator -SparseMatrix diff; +SparseMatrix::SparseMatrix diff; diff = matrixA.subtract(matrixB); // method diff = matrixA - matrixB; // operator ``` @@ -93,8 +84,8 @@ diff = matrixA - matrixB; // operator #### Matrix-Matrix comparison ```cpp -SparseMatrix matrixA(3); -SparseMatrix matrixB(3); +SparseMatrix::SparseMatrix matrixA(3); +SparseMatrix::SparseMatrix matrixB(3); bool areSame = matrixA == matrixB; // true ``` @@ -113,7 +104,7 @@ int cols = matrix.getColumnCount(); Basic output streaming is implemented. Note that output operator must be implemented for the element type as well. ```cpp -SparseMatrix matrix(3, 4); +SparseMatrix::SparseMatrix matrix(3, 4); matrix.set(2, 1, 1); matrix.set(7, 1, 3); matrix.set(4, 2, 2); @@ -142,7 +133,7 @@ Make sure your type implements: * product `operator *` * output `operator <<` to be able to nicely output the matrix -You can see a simple custom element example in [the tests](tests/cases/custom-type.h). +You can see a simple custom element example in [the tests](tests/cases/custom-type.cpp). ----------- diff --git a/src/SparseMatrix/SparseMatrix.cpp b/src/SparseMatrix/SparseMatrix.cpp index b3d6e00..c0f4726 100644 --- a/src/SparseMatrix/SparseMatrix.cpp +++ b/src/SparseMatrix/SparseMatrix.cpp @@ -11,370 +11,373 @@ #include "exceptions.h" #include "SparseMatrix.h" -using namespace std; - -// === CREATION ============================================== - -template -SparseMatrix::SparseMatrix(int n) +namespace SparseMatrix { - this->construct(n, n); -} + // === CREATION ============================================== -template -SparseMatrix::SparseMatrix(int rows, int columns) -{ - this->construct(rows, columns); -} + template + SparseMatrix::SparseMatrix(int n) + { + this->construct(n, n); + } -template -SparseMatrix::SparseMatrix(const SparseMatrix & matrix) -{ - this->deepCopy(matrix); -} + template + SparseMatrix::SparseMatrix(int rows, int columns) + { + this->construct(rows, columns); + } -template -SparseMatrix & SparseMatrix::operator = (const SparseMatrix & matrix) -{ - if (&matrix != this) { - this->destruct(); + template + SparseMatrix::SparseMatrix(const SparseMatrix & matrix) + { this->deepCopy(matrix); } - return *this; -} - -template -void SparseMatrix::deepCopy(const SparseMatrix & matrix) -{ - this->m = matrix.m; - this->n = matrix.n; - this->rows = new vector(*(matrix.rows)); + template + SparseMatrix & SparseMatrix::operator = (const SparseMatrix & matrix) + { + if (&matrix != this) { + this->destruct(); + this->deepCopy(matrix); + } - if (matrix.vals != NULL) { - this->cols = new vector(*(matrix.cols)); - this->vals = new vector(*(matrix.vals)); + return *this; } -} -template -SparseMatrix::~SparseMatrix(void) -{ - this->destruct(); -} + template + void SparseMatrix::deepCopy(const SparseMatrix & matrix) + { + this->m = matrix.m; + this->n = matrix.n; + this->rows = new std::vector(*(matrix.rows)); + if (matrix.vals != NULL) { + this->cols = new std::vector(*(matrix.cols)); + this->vals = new std::vector(*(matrix.vals)); + } + } -template -void SparseMatrix::construct(int rows, int columns) -{ - if (rows < 1 || columns < 1) { - throw InvalidDimensionsException("Matrix dimensions cannot be zero or negative."); + + template + SparseMatrix::~SparseMatrix(void) + { + this->destruct(); } - this->m = rows; - this->n = columns; - this->vals = NULL; - this->cols = NULL; - this->rows = new vector(rows + 1, 1); -} + template + void SparseMatrix::construct(int rows, int columns) + { + if (rows < 1 || columns < 1) { + throw InvalidDimensionsException("Matrix dimensions cannot be zero or negative."); + } + this->m = rows; + this->n = columns; -template -void SparseMatrix::destruct(void) -{ - if (this->vals != NULL) { - delete this->vals; - delete this->cols; + this->vals = NULL; + this->cols = NULL; + this->rows = new std::vector(rows + 1, 1); } - delete this->rows; -} + template + void SparseMatrix::destruct(void) + { + if (this->vals != NULL) { + delete this->vals; + delete this->cols; + } -// === GETTERS / SETTERS ============================================== + delete this->rows; + } -template -int SparseMatrix::getRowCount(void) const -{ - return this->m; -} + // === GETTERS / SETTERS ============================================== + + template + int SparseMatrix::getRowCount(void) const + { + return this->m; + } -template -int SparseMatrix::getColumnCount(void) const -{ - return this->n; -} + template + int SparseMatrix::getColumnCount(void) const + { + return this->n; + } -// === VALUES ============================================== -template -T SparseMatrix::get(int row, int col) const -{ - this->validateCoordinates(row, col); + // === VALUES ============================================== - int currCol; + template + T SparseMatrix::get(int row, int col) const + { + this->validateCoordinates(row, col); - for (int pos = (*(this->rows))[row - 1] - 1; pos < (*(this->rows))[row] - 1; ++pos) { - currCol = (*(this->cols))[pos]; + int currCol; - if (currCol == col) { - return (*(this->vals))[pos]; + for (int pos = (*(this->rows))[row - 1] - 1; pos < (*(this->rows))[row] - 1; ++pos) { + currCol = (*(this->cols))[pos]; - } else if (currCol > col) { - break; + if (currCol == col) { + return (*(this->vals))[pos]; + + } else if (currCol > col) { + break; + } } + + return T(); } - return T(); -} + template + SparseMatrix & SparseMatrix::set(T val, int row, int col) + { + this->validateCoordinates(row, col); -template -SparseMatrix & SparseMatrix::set(T val, int row, int col) -{ - this->validateCoordinates(row, col); + int pos = (*(this->rows))[row - 1] - 1; + int currCol = 0; - int pos = (*(this->rows))[row - 1] - 1; - int currCol = 0; + for (; pos < (*(this->rows))[row] - 1; pos++) { + currCol = (*(this->cols))[pos]; - for (; pos < (*(this->rows))[row] - 1; pos++) { - currCol = (*(this->cols))[pos]; - - if (currCol >= col) { - break; + if (currCol >= col) { + break; + } } - } - if (currCol != col) { - if (!(val == T())) { - this->insert(pos, row, col, val); - } + if (currCol != col) { + if (!(val == T())) { + this->insert(pos, row, col, val); + } + + } else if (val == T()) { + this->remove(pos, row); - } else if (val == T()) { - this->remove(pos, row); + } else { + (*(this->vals))[pos] = val; + } - } else { - (*(this->vals))[pos] = val; + return *this; } - return *this; -} + // === OPERATIONS ============================================== -// === OPERATIONS ============================================== + template + std::vector SparseMatrix::multiply(const std::vector & x) const + { + if (this->n != (int) x.size()) { + throw InvalidDimensionsException("Cannot multiply: Matrix column count and vector size don't match."); + } -template -vector SparseMatrix::multiply(const vector & x) const -{ - if (this->n != (int) x.size()) { - throw InvalidDimensionsException("Cannot multiply: Matrix column count and vector size don't match."); - } + std::vector result(this->m, T()); - vector result(this->m, T()); + if (this->vals != NULL) { // only if any value set + for (int i = 0; i < this->m; i++) { + T sum = T(); + for (int j = (*(this->rows))[i]; j < (*(this->rows))[i + 1]; j++) { + sum = sum + (*(this->vals))[j - 1] * x[(*(this->cols))[j - 1] - 1]; + } - if (this->vals != NULL) { // only if any value set - for (int i = 0; i < this->m; i++) { - T sum = T(); - for (int j = (*(this->rows))[i]; j < (*(this->rows))[i + 1]; j++) { - sum = sum + (*(this->vals))[j - 1] * x[(*(this->cols))[j - 1] - 1]; + result[i] = sum; } - - result[i] = sum; } + + return result; } - return result; -} + template + std::vector SparseMatrix::operator * (const std::vector & x) const + { + return this->multiply(x); + } -template -vector SparseMatrix::operator * (const vector & x) const -{ - return this->multiply(x); -} + template + SparseMatrix SparseMatrix::multiply(const SparseMatrix & m) const + { + if (this->n != m.m) { + throw InvalidDimensionsException("Cannot multiply: Left matrix column count and right matrix row count don't match."); + } -template -SparseMatrix SparseMatrix::multiply(const SparseMatrix & m) const -{ - if (this->n != m.m) { - throw InvalidDimensionsException("Cannot multiply: Left matrix column count and right matrix row count don't match."); - } + SparseMatrix result(this->m, m.n); - SparseMatrix result(this->m, m.n); + T a; - T a; + // TODO: more efficient? + // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf - // TODO: more efficient? - // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf + for (int i = 1; i <= this->m; i++) { + for (int j = 1; j <= m.n; j++) { + a = T(); - for (int i = 1; i <= this->m; i++) { - for (int j = 1; j <= m.n; j++) { - a = T(); + for (int k = 1; k <= this->n; k++) { + a = a + this->get(i, k) * m.get(k, j); + } - for (int k = 1; k <= this->n; k++) { - a = a + this->get(i, k) * m.get(k, j); + result.set(a, i, j); } - - result.set(a, i, j); } - } - return result; -} + return result; + } -template -SparseMatrix SparseMatrix::operator * (const SparseMatrix & m) const -{ - return this->multiply(m); -} + template + SparseMatrix SparseMatrix::operator * (const SparseMatrix & m) const + { + return this->multiply(m); + } -template -SparseMatrix SparseMatrix::add(const SparseMatrix & m) const -{ - if (this->m != m.m || this->n != m.n) { - throw InvalidDimensionsException("Cannot add: matrices dimensions don't match."); - } + template + SparseMatrix SparseMatrix::add(const SparseMatrix & m) const + { + if (this->m != m.m || this->n != m.n) { + throw InvalidDimensionsException("Cannot add: matrices dimensions don't match."); + } - SparseMatrix result(this->m, this->n); + SparseMatrix result(this->m, this->n); - // TODO: more efficient? - // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf + // TODO: more efficient? + // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf - for (int i = 1; i <= this->m; i++) { - for (int j = 1; j <= this->n; j++) { - result.set(this->get(i, j) + m.get(i, j), i, j); + for (int i = 1; i <= this->m; i++) { + for (int j = 1; j <= this->n; j++) { + result.set(this->get(i, j) + m.get(i, j), i, j); + } } - } - return result; -} + return result; + } -template -SparseMatrix SparseMatrix::operator + (const SparseMatrix & m) const -{ - return this->add(m); -} + template + SparseMatrix SparseMatrix::operator + (const SparseMatrix & m) const + { + return this->add(m); + } -template -SparseMatrix SparseMatrix::subtract(const SparseMatrix & m) const -{ - if (this->m != m.m || this->n != m.n) { - throw InvalidDimensionsException("Cannot subtract: matrices dimensions don't match."); - } + template + SparseMatrix SparseMatrix::subtract(const SparseMatrix & m) const + { + if (this->m != m.m || this->n != m.n) { + throw InvalidDimensionsException("Cannot subtract: matrices dimensions don't match."); + } - SparseMatrix result(this->m, this->n); + SparseMatrix result(this->m, this->n); - // TODO: more efficient? - // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf + // TODO: more efficient? + // @see http://www.math.tamu.edu/~srobertp/Courses/Math639_2014_Sp/CRSDescription/CRSStuff.pdf - for (int i = 1; i <= this->m; i++) { - for (int j = 1; j <= this->n; j++) { - result.set(this->get(i, j) - m.get(i, j), i, j); + for (int i = 1; i <= this->m; i++) { + for (int j = 1; j <= this->n; j++) { + result.set(this->get(i, j) - m.get(i, j), i, j); + } } - } - return result; -} + return result; + } -template -SparseMatrix SparseMatrix::operator - (const SparseMatrix & m) const -{ - return this->subtract(m); -} + template + SparseMatrix SparseMatrix::operator - (const SparseMatrix & m) const + { + return this->subtract(m); + } -// === HELPERS / VALIDATORS ============================================== + // === HELPERS / VALIDATORS ============================================== -template -void SparseMatrix::validateCoordinates(int row, int col) const -{ - if (row < 1 || col < 1 || row > this->m || col > this->n) { - throw InvalidCoordinatesException("Coordinates out of range."); + template + void SparseMatrix::validateCoordinates(int row, int col) const + { + if (row < 1 || col < 1 || row > this->m || col > this->n) { + throw InvalidCoordinatesException("Coordinates out of range."); + } } -} -template -void SparseMatrix::insert(int index, int row, int col, T val) -{ - if (this->vals == NULL) { - this->vals = new vector(1, val); - this->cols = new vector(1, col); + template + void SparseMatrix::insert(int index, int row, int col, T val) + { + if (this->vals == NULL) { + this->vals = new std::vector(1, val); + this->cols = new std::vector(1, col); - } else { - this->vals->insert(this->vals->begin() + index, val); - this->cols->insert(this->cols->begin() + index, col); - } + } else { + this->vals->insert(this->vals->begin() + index, val); + this->cols->insert(this->cols->begin() + index, col); + } - for (int i = row; i <= this->m; i++) { - (*(this->rows))[i] += 1; + for (int i = row; i <= this->m; i++) { + (*(this->rows))[i] += 1; + } } -} -template -void SparseMatrix::remove(int index, int row) -{ - this->vals->erase(this->vals->begin() + index); - this->cols->erase(this->cols->begin() + index); + template + void SparseMatrix::remove(int index, int row) + { + this->vals->erase(this->vals->begin() + index); + this->cols->erase(this->cols->begin() + index); - for (int i = row; i <= this->m; i++) { - (*(this->rows))[i] -= 1; + for (int i = row; i <= this->m; i++) { + (*(this->rows))[i] -= 1; + } } -} -// === FRIEND FUNCTIONS ========================================= + // === FRIEND FUNCTIONS ========================================= -template -bool operator == (const SparseMatrix & a, const SparseMatrix & b) -{ - return ((a.vals == NULL && b.vals == NULL) - || (a.vals != NULL && b.vals != NULL && *(a.vals) == *(b.vals))) - && ((a.cols == NULL && b.cols == NULL) - || (a.cols != NULL && b.cols != NULL && *(a.cols) == *(b.cols))) - && *(a.rows) == *(b.rows); -} + template + bool operator == (const SparseMatrix & a, const SparseMatrix & b) + { + return ((a.vals == NULL && b.vals == NULL) + || (a.vals != NULL && b.vals != NULL && *(a.vals) == *(b.vals))) + && ((a.cols == NULL && b.cols == NULL) + || (a.cols != NULL && b.cols != NULL && *(a.cols) == *(b.cols))) + && *(a.rows) == *(b.rows); + } -template -bool operator != (const SparseMatrix & a, const SparseMatrix & b) -{ - return !(a == b); -} + template + bool operator != (const SparseMatrix & a, const SparseMatrix & b) + { + return !(a == b); + } -template -ostream & operator << (ostream & os, const SparseMatrix & matrix) -{ - for (int i = 1; i <= matrix.m; i++) { - for (int j = 1; j <= matrix.n; j++) { - if (j != 1) { - os << " "; + template + std::ostream & operator << (std::ostream & os, const SparseMatrix & matrix) + { + for (int i = 1; i <= matrix.m; i++) { + for (int j = 1; j <= matrix.n; j++) { + if (j != 1) { + os << " "; + } + + os << matrix.get(i, j); } - os << matrix.get(i, j); + if (i < matrix.m) { + os << std::endl; + } } - if (i < matrix.m) { - os << endl; - } + return os; } - return os; } diff --git a/src/SparseMatrix/SparseMatrix.h b/src/SparseMatrix/SparseMatrix.h index bce1769..2eb6300 100644 --- a/src/SparseMatrix/SparseMatrix.h +++ b/src/SparseMatrix/SparseMatrix.h @@ -13,82 +13,85 @@ #include #include - using namespace std; - - template - class SparseMatrix + namespace SparseMatrix { - public: + template + class SparseMatrix + { + + public: + + // === CREATION ============================================== - // === CREATION ============================================== + SparseMatrix(int n); // square matrix n×n + SparseMatrix(int rows, int columns); // general matrix - SparseMatrix(int n); // square matrix n×n - SparseMatrix(int rows, int columns); // general matrix + SparseMatrix(const SparseMatrix & m); // copy constructor + SparseMatrix & operator = (const SparseMatrix & m); - SparseMatrix(const SparseMatrix & m); // copy constructor - SparseMatrix & operator = (const SparseMatrix & m); + ~SparseMatrix(void); - ~SparseMatrix(void); + // === GETTERS / SETTERS ============================================== - // === GETTERS / SETTERS ============================================== + int getRowCount(void) const; + int getColumnCount(void) const; - int getRowCount(void) const; - int getColumnCount(void) const; + // === VALUES ============================================== - // === VALUES ============================================== + T get(int row, int col) const; + SparseMatrix & set(T val, int row, int col); - T get(int row, int col) const; - SparseMatrix & set(T val, int row, int col); + // === OPERATIONS ============================================== - // === OPERATIONS ============================================== + std::vector multiply(const std::vector & x) const; + std::vector operator * (const std::vector & x) const; - vector multiply(const vector & x) const; - vector operator * (const vector & x) const; + SparseMatrix multiply(const SparseMatrix & m) const; + SparseMatrix operator * (const SparseMatrix & m) const; - SparseMatrix multiply(const SparseMatrix & m) const; - SparseMatrix operator * (const SparseMatrix & m) const; + SparseMatrix add(const SparseMatrix & m) const; + SparseMatrix operator + (const SparseMatrix & m) const; - SparseMatrix add(const SparseMatrix & m) const; - SparseMatrix operator + (const SparseMatrix & m) const; + SparseMatrix subtract(const SparseMatrix & m) const; + SparseMatrix operator - (const SparseMatrix & m) const; - SparseMatrix subtract(const SparseMatrix & m) const; - SparseMatrix operator - (const SparseMatrix & m) const; + // === FRIEND FUNCTIONS ========================================= - // === FRIEND FUNCTIONS ========================================= + template + friend bool operator == (const SparseMatrix & a, const SparseMatrix & b); - template - friend bool operator == (const SparseMatrix & a, const SparseMatrix & b); + template + friend bool operator != (const SparseMatrix & a, const SparseMatrix & b); - template - friend bool operator != (const SparseMatrix & a, const SparseMatrix & b); + template + friend std::ostream & operator << (std::ostream & os, const SparseMatrix & matrix); - template - friend ostream & operator << (ostream & os, const SparseMatrix & matrix); + protected: - protected: + int m, n; - int m, n; + std::vector * vals; + std::vector * rows, * cols; - vector * vals; - vector * rows, * cols; + // === HELPERS / VALIDATORS ============================================== - // === HELPERS / VALIDATORS ============================================== + void construct(int m, int n); + void destruct(void); + void deepCopy(const SparseMatrix & m); + void validateCoordinates(int row, int col) const; + void insert(int index, int row, int col, T val); + void remove(int index, int row); - void construct(int m, int n); - void destruct(void); - void deepCopy(const SparseMatrix & m); - void validateCoordinates(int row, int col) const; - void insert(int index, int row, int col, T val); - void remove(int index, int row); + }; - }; + } #endif diff --git a/src/SparseMatrix/exceptions.h b/src/SparseMatrix/exceptions.h index c05663a..5489e58 100644 --- a/src/SparseMatrix/exceptions.h +++ b/src/SparseMatrix/exceptions.h @@ -12,54 +12,57 @@ #include - using namespace std; - - class Exception : public exception + namespace SparseMatrix { - public: + class Exception : public std::exception + { - explicit Exception(const string & message) : exception(), message(message) - {} + public: + explicit Exception(const std::string & message) : exception(), message(message) + {} - virtual ~Exception(void) throw () - {} + virtual ~Exception(void) throw () + {} - inline string getMessage(void) const - { - return this->message; - } + inline std::string getMessage(void) const + { + return this->message; + } - protected: - string message; + protected: - }; + std::string message; + }; - class InvalidDimensionsException : public Exception - { - public: + class InvalidDimensionsException : public Exception + { - InvalidDimensionsException(const string & message) : Exception(message) - {} + public: - }; + InvalidDimensionsException(const std::string & message) : Exception(message) + {} + }; - class InvalidCoordinatesException : public Exception - { - public: + class InvalidCoordinatesException : public Exception + { + + public: + + InvalidCoordinatesException(const std::string & message) : Exception(message) + {} - InvalidCoordinatesException(const string & message) : Exception(message) - {} + }; - }; + } -#endif \ No newline at end of file +#endif diff --git a/tests/cases/addition.h b/tests/cases/addition.cpp similarity index 57% rename from tests/cases/addition.h rename to tests/cases/addition.cpp index 9573b09..d67ba1f 100644 --- a/tests/cases/addition.h +++ b/tests/cases/addition.cpp @@ -12,81 +12,81 @@ void _additionFail1(void) { - SparseMatrix a(3, 4), b(3, 5); + SparseMatrix::SparseMatrix a(3, 4), b(3, 5); a.add(b); } void testAdditionFail1(void) { - cout << "add() fail #1..." << flush; + std::cout << "add() fail #1..." << std::flush; assertException("InvalidDimensionsException", _additionFail1); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _additionFail2(void) { - SparseMatrix a(3, 4), b(4, 4); + SparseMatrix::SparseMatrix a(3, 4), b(4, 4); a.add(b); } void testAdditionFail2(void) { - cout << "add() fail #2..." << flush; + std::cout << "add() fail #2..." << std::flush; assertException("InvalidDimensionsException", _additionFail2); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _additionFail3(void) { - SparseMatrix a(3, 4), b(4, 5); + SparseMatrix::SparseMatrix a(3, 4), b(4, 5); a.add(b); } void testAdditionFail3(void) { - cout << "add() fail #3..." << flush; + std::cout << "add() fail #3..." << std::flush; assertException("InvalidDimensionsException", _additionFail3); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void testAddition(void) { for (int N = 0; N < 5e3; N++) { - cout << "\rmatrices addition... #" << N + 1 << flush; + std::cout << "\rmatrices addition... #" << N + 1 << std::flush; // generate random matrices int rows = rand() % 16 + 1; int cols = rand() % 16 + 1; - vector > classicMatrixA = generateRandomMatrix(rows, cols); + std::vector > classicMatrixA = generateRandomMatrix(rows, cols); SparseMatrixMock sparseMatrixA = SparseMatrixMock::fromVectors(classicMatrixA); - vector > classicMatrixB = generateRandomMatrix(rows, cols); + std::vector > classicMatrixB = generateRandomMatrix(rows, cols); SparseMatrixMock sparseMatrixB = SparseMatrixMock::fromVectors(classicMatrixB); // calculate result manually - vector > manualResult = addMatrices(classicMatrixA, classicMatrixB); + std::vector > manualResult = addMatrices(classicMatrixA, classicMatrixB); // method - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA.add(sparseMatrixB), manualResult, "Incorrect matrices addition" ); // operator - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA + sparseMatrixB, manualResult, "Incorrect matrices addition (operator +)" ); } - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/constructor.h b/tests/cases/constructor.cpp similarity index 61% rename from tests/cases/constructor.h rename to tests/cases/constructor.cpp index 687be14..3101f6b 100644 --- a/tests/cases/constructor.h +++ b/tests/cases/constructor.cpp @@ -12,55 +12,55 @@ void _constructorFail1(void) { - SparseMatrix(0); + SparseMatrix::SparseMatrix(0); } void testConstructorFail1(void) { - cout << "constructor fail #1..." << flush; + std::cout << "constructor fail #1..." << std::flush; assertException("InvalidDimensionsException", _constructorFail1); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _constructorFail2(void) { - SparseMatrix(0, 1); + SparseMatrix::SparseMatrix(0, 1); } void testConstructorFail2(void) { - cout << "constructor fail #2..." << flush; + std::cout << "constructor fail #2..." << std::flush; assertException("InvalidDimensionsException", _constructorFail2); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _constructorFail3(void) { - SparseMatrix(1, 0); + SparseMatrix::SparseMatrix(1, 0); } void testConstructorFail3(void) { - cout << "constructor fail #3..." << flush; + std::cout << "constructor fail #3..." << std::flush; assertException("InvalidDimensionsException", _constructorFail3); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _constructorFail4(void) { - SparseMatrix(0, 0); + SparseMatrix::SparseMatrix(0, 0); } void testConstructorFail4(void) { - cout << "constructor fail #4..." << flush; + std::cout << "constructor fail #4..." << std::flush; assertException("InvalidDimensionsException", _constructorFail4); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/crs-format.h b/tests/cases/crs-format.cpp similarity index 62% rename from tests/cases/crs-format.h rename to tests/cases/crs-format.cpp index ea9a8b9..a1d3347 100644 --- a/tests/cases/crs-format.h +++ b/tests/cases/crs-format.cpp @@ -12,7 +12,7 @@ void testInternalStorage(void) { - cout << "internal storage..." << flush; + std::cout << "internal storage..." << std::flush; /* "Standard" matrix @@ -35,14 +35,14 @@ void testInternalStorage(void) .set(3, 3, 3) .set(2, 3, 4); - vector rowPointers1; + std::vector rowPointers1; rowPointers1.push_back(1); rowPointers1.push_back(4); rowPointers1.push_back(6); rowPointers1.push_back(8); - assertEquals >(rowPointers1, *(m1.getRowPointers()), "Incorrect internal row pointers"); + assertEquals >(rowPointers1, *(m1.getRowPointers()), "Incorrect internal row pointers"); - vector columnPointers1; + std::vector columnPointers1; columnPointers1.push_back(1); columnPointers1.push_back(3); columnPointers1.push_back(4); @@ -50,9 +50,9 @@ void testInternalStorage(void) columnPointers1.push_back(2); columnPointers1.push_back(3); columnPointers1.push_back(4); - assertEquals >(columnPointers1, *(m1.getColumnPointers()), "Incorrect internal column pointers"); + assertEquals >(columnPointers1, *(m1.getColumnPointers()), "Incorrect internal column pointers"); - vector values1; + std::vector values1; values1.push_back(1); values1.push_back(4); values1.push_back(5); @@ -60,7 +60,7 @@ void testInternalStorage(void) values1.push_back(-1); values1.push_back(3); values1.push_back(2); - assertEquals >(values1, *(m1.getValues()), "Incorrect internal values storage"); + assertEquals >(values1, *(m1.getValues()), "Incorrect internal values storage"); /* @@ -82,28 +82,28 @@ void testInternalStorage(void) . set(1, 3, 2) . set(4, 3, 4); - vector rowPointers2; + std::vector rowPointers2; rowPointers2.push_back(1); rowPointers2.push_back(3); rowPointers2.push_back(3); rowPointers2.push_back(6); - assertEquals >(rowPointers2, *(m2.getRowPointers()), "Incorrect internal row pointers"); + assertEquals >(rowPointers2, *(m2.getRowPointers()), "Incorrect internal row pointers"); - vector columnPointers2; + std::vector columnPointers2; columnPointers2.push_back(1); columnPointers2.push_back(4); columnPointers2.push_back(1); columnPointers2.push_back(2); columnPointers2.push_back(4); - assertEquals >(columnPointers2, *(m2.getColumnPointers()), "Incorrect internal column pointers"); + assertEquals >(columnPointers2, *(m2.getColumnPointers()), "Incorrect internal column pointers"); - vector values2; + std::vector values2; values2.push_back(10); values2.push_back(2); values2.push_back(3); values2.push_back(1); values2.push_back(4); - assertEquals >(values2, *(m2.getValues()), "Incorrect internal values storage"); + assertEquals >(values2, *(m2.getValues()), "Incorrect internal values storage"); /* @@ -118,30 +118,30 @@ void testInternalStorage(void) SparseMatrixMock m3 = m2; m3.set(5, 2, 2); - vector rowPointers3; + std::vector rowPointers3; rowPointers3.push_back(1); rowPointers3.push_back(3); rowPointers3.push_back(4); rowPointers3.push_back(7); - assertEquals >(rowPointers3, *(m3.getRowPointers()), "Incorrect internal row pointers"); + assertEquals >(rowPointers3, *(m3.getRowPointers()), "Incorrect internal row pointers"); - vector columnPointers3; + std::vector columnPointers3; columnPointers3.push_back(1); columnPointers3.push_back(4); columnPointers3.push_back(2); columnPointers3.push_back(1); columnPointers3.push_back(2); columnPointers3.push_back(4); - assertEquals >(columnPointers3, *(m3.getColumnPointers()), "Incorrect internal column pointers"); + assertEquals >(columnPointers3, *(m3.getColumnPointers()), "Incorrect internal column pointers"); - vector values3; + std::vector values3; values3.push_back(10); values3.push_back(2); values3.push_back(5); values3.push_back(3); values3.push_back(1); values3.push_back(4); - assertEquals >(values3, *(m3.getValues()), "Incorrect internal values storage"); + assertEquals >(values3, *(m3.getValues()), "Incorrect internal values storage"); /* Previous matrix with removed the only non-zero element on 2nd row (should be equal to 2nd matrix) @@ -155,9 +155,9 @@ void testInternalStorage(void) SparseMatrixMock m4 = m3; m4.set(0, 2, 2); - assertEquals >(rowPointers2, *(m4.getRowPointers()), "Incorrect internal row pointers"); - assertEquals >(columnPointers2, *(m4.getColumnPointers()), "Incorrect internal column pointers"); - assertEquals >(values2, *(m4.getValues()), "Incorrect internal values storage"); + assertEquals >(rowPointers2, *(m4.getRowPointers()), "Incorrect internal row pointers"); + assertEquals >(columnPointers2, *(m4.getColumnPointers()), "Incorrect internal column pointers"); + assertEquals >(values2, *(m4.getValues()), "Incorrect internal values storage"); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/custom-type.h b/tests/cases/custom-type.cpp similarity index 75% rename from tests/cases/custom-type.h rename to tests/cases/custom-type.cpp index f37060b..33b8959 100644 --- a/tests/cases/custom-type.h +++ b/tests/cases/custom-type.cpp @@ -7,16 +7,15 @@ */ #include +#include #include #include "../inc/testslib.h" #include "../inc/SparseMatrixMock.h" -using namespace std; - -string joinStrings(const string & a, const string & b) +std::string joinStrings(const std::string & a, const std::string & b) { - string c = a; + std::string c = a; if (a.length() && b.length()) { c += " "; @@ -38,7 +37,7 @@ struct person {} - person(const string & f, const string & l) : firstname(f), lastname(l) + person(const std::string & f, const std::string & l) : firstname(f), lastname(l) {} @@ -70,12 +69,12 @@ struct person } - string firstname, lastname; + std::string firstname, lastname; }; -ostream & operator << (ostream & os, const person & p) +std::ostream & operator << (std::ostream & os, const person & p) { os << p.firstname << ", " << p.lastname; return os; @@ -84,32 +83,32 @@ ostream & operator << (ostream & os, const person & p) void testElementTypes(void) { - cout << "custom element types..." << flush; + std::cout << "custom element types..." << std::flush; // addition - SparseMatrix a(4, 5); + SparseMatrix::SparseMatrix a(4, 5); a.set(person("John", "Doe"), 3, 2); - SparseMatrix b(4, 5); + SparseMatrix::SparseMatrix b(4, 5); b.set(person("Foo", "Bar"), 3, 2); - SparseMatrix sum = a.add(b); + SparseMatrix::SparseMatrix sum = a.add(b); assertEquals(person("John Foo", "Doe Bar"), sum.get(3, 2)); // subtraction - SparseMatrix diff = a.subtract(b); + SparseMatrix::SparseMatrix diff = a.subtract(b); assertEquals(person("Bar", "Doe"), diff.get(3, 2)); // matrix-matrix multiplication - SparseMatrix c(5, 3); + SparseMatrix::SparseMatrix c(5, 3); c.set(person("Foo", "Bar"), 2, 3); - SparseMatrix product = a.multiply(c); + SparseMatrix::SparseMatrix product = a.multiply(c); for (int i = 1, rows = product.getRowCount(); i <= rows; i++) { for (int j = 1, cols = product.getColumnCount(); j <= cols; j++) { @@ -127,18 +126,18 @@ void testElementTypes(void) // vector-matrix multiplication - vector people; + std::vector people; people.push_back(person("John", "Doe")); people.push_back(person("Foo", "Bar")); people.push_back(person("Willy", "Wonka")); people.push_back(person("Jon", "Snow")); people.push_back(person("Bridget", "Johnes")); - vector result = a.multiply(people); + std::vector result = a.multiply(people); assertEquals(person(), result[0]); assertEquals(person(), result[1]); assertEquals(person("John Bar", "Doe Foo"), result[2]); assertEquals(person(), result[3]); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/multiplication.h b/tests/cases/multiplication.cpp similarity index 52% rename from tests/cases/multiplication.h rename to tests/cases/multiplication.cpp index fdcfce1..dca5e18 100644 --- a/tests/cases/multiplication.h +++ b/tests/cases/multiplication.cpp @@ -12,96 +12,96 @@ void _multiplicationFail1(void) { - SparseMatrix m(3, 4); - vector x(3, 1); + SparseMatrix::SparseMatrix m(3, 4); + std::vector x(3, 1); m.multiply(x); } void testMultiplicationFail1(void) { - cout << "multiply() fail #1..." << flush; + std::cout << "multiply() fail #1..." << std::flush; assertException("InvalidDimensionsException", _multiplicationFail1); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _multiplicationFail2(void) { - SparseMatrix a(3, 4), b(5, 6); + SparseMatrix::SparseMatrix a(3, 4), b(5, 6); a.multiply(b); } void testMultiplicationFail2(void) { - cout << "multiply() fail #2..." << flush; + std::cout << "multiply() fail #2..." << std::flush; assertException("InvalidDimensionsException", _multiplicationFail2); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void testVectorMultiplication(void) { for (int N = 0; N < 5e3; N++) { - cout << "\rvector multiplication... #" << N + 1 << flush; + std::cout << "\rvector multiplication... #" << N + 1 << std::flush; // generate random vector and matrix int rows = rand() % 16 + 1; int cols = rand() % 16 + 1; - vector vec = generateRandomVector(cols); + std::vector vec = generateRandomVector(cols); - vector > classicMatrix = generateRandomMatrix(rows, cols); + std::vector > classicMatrix = generateRandomMatrix(rows, cols); SparseMatrixMock sparseMatrix = SparseMatrixMock::fromVectors(classicMatrix); // calculate result manually - vector manualResult = multiplyMatrixByVector(classicMatrix, vec); + std::vector manualResult = multiplyMatrixByVector(classicMatrix, vec); // method - assertEquals >(manualResult, sparseMatrix.multiply(vec), "Incorrect vector multiplication"); + assertEquals >(manualResult, sparseMatrix.multiply(vec), "Incorrect vector multiplication"); // operator - assertEquals >(manualResult, sparseMatrix * vec, "Incorrect vector multiplication (operator *)"); + assertEquals >(manualResult, sparseMatrix * vec, "Incorrect vector multiplication (operator *)"); } - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void testMatricesMultiplication(void) { for (int N = 0; N < 5e3; N++) { - cout << "\rmatrices multiplication... #" << N + 1 << flush; + std::cout << "\rmatrices multiplication... #" << N + 1 << std::flush; // generate random matrices int rowsA = rand() % 16 + 1; int colsArowsB = rand() % 16 + 1; int colsB = rand() % 16 + 1; - vector > classicMatrixA = generateRandomMatrix(rowsA, colsArowsB); + std::vector > classicMatrixA = generateRandomMatrix(rowsA, colsArowsB); SparseMatrixMock sparseMatrixA = SparseMatrixMock::fromVectors(classicMatrixA); - vector > classicMatrixB = generateRandomMatrix(colsArowsB, colsB); + std::vector > classicMatrixB = generateRandomMatrix(colsArowsB, colsB); SparseMatrixMock sparseMatrixB = SparseMatrixMock::fromVectors(classicMatrixB); // calculate result manually - vector > manualResult = multiplyMatrices(classicMatrixA, classicMatrixB); + std::vector > manualResult = multiplyMatrices(classicMatrixA, classicMatrixB); // method - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA.multiply(sparseMatrixB), manualResult, "Incorrect matrices multiplication" ); // operator - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA * sparseMatrixB, manualResult, "Incorrect matrices multiplication (operator *)" ); } - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/output.h b/tests/cases/output.cpp similarity index 55% rename from tests/cases/output.h rename to tests/cases/output.cpp index 747abbe..ab699a3 100644 --- a/tests/cases/output.h +++ b/tests/cases/output.cpp @@ -12,14 +12,14 @@ void testOutput(void) { - cout << "output..." << flush; + std::cout << "output..." << std::flush; - ostringstream oss; - string output; + std::ostringstream oss; + std::string output; - SparseMatrix m(3, 3); + SparseMatrix::SparseMatrix m(3, 3); oss << m; - assertEquals("0 0 0\n0 0 0\n0 0 0", oss.str()); + assertEquals("0 0 0\n0 0 0\n0 0 0", oss.str()); m.set(7, 1, 3) .set(5, 2, 2) @@ -27,7 +27,7 @@ void testOutput(void) oss.str(""); oss << m; - assertEquals("0 0 7\n0 5 0\n3 0 0", oss.str()); + assertEquals("0 0 7\n0 5 0\n3 0 0", oss.str()); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/subtraction.h b/tests/cases/subtraction.cpp similarity index 57% rename from tests/cases/subtraction.h rename to tests/cases/subtraction.cpp index c3ed5f6..e44bc2c 100644 --- a/tests/cases/subtraction.h +++ b/tests/cases/subtraction.cpp @@ -12,81 +12,81 @@ void _subtractionFail1(void) { - SparseMatrix a(3, 4), b(3, 5); + SparseMatrix::SparseMatrix a(3, 4), b(3, 5); a.subtract(b); } void testSubtractionFail1(void) { - cout << "subtract() fail #1..." << flush; + std::cout << "subtract() fail #1..." << std::flush; assertException("InvalidDimensionsException", _subtractionFail1); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _subtractionFail2(void) { - SparseMatrix a(3, 4), b(4, 4); + SparseMatrix::SparseMatrix a(3, 4), b(4, 4); a.subtract(b); } void testSubtractionFail2(void) { - cout << "subtract() fail #2..." << flush; + std::cout << "subtract() fail #2..." << std::flush; assertException("InvalidDimensionsException", _subtractionFail2); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _subtractionFail3(void) { - SparseMatrix a(3, 4), b(4, 5); + SparseMatrix::SparseMatrix a(3, 4), b(4, 5); a.subtract(b); } void testSubtractionFail3(void) { - cout << "subtract() fail #3..." << flush; + std::cout << "subtract() fail #3..." << std::flush; assertException("InvalidDimensionsException", _subtractionFail3); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void testSubtraction(void) { for (int N = 0; N < 5e3; N++) { - cout << "\rmatrices subtraction... #" << N + 1 << flush; + std::cout << "\rmatrices subtraction... #" << N + 1 << std::flush; // generate random matrices int rows = rand() % 16 + 1; int cols = rand() % 16 + 1; - vector > classicMatrixA = generateRandomMatrix(rows, cols); + std::vector > classicMatrixA = generateRandomMatrix(rows, cols); SparseMatrixMock sparseMatrixA = SparseMatrixMock::fromVectors(classicMatrixA); - vector > classicMatrixB = generateRandomMatrix(rows, cols); + std::vector > classicMatrixB = generateRandomMatrix(rows, cols); SparseMatrixMock sparseMatrixB = SparseMatrixMock::fromVectors(classicMatrixB); // calculate result manually - vector > manualResult = subtractMatrices(classicMatrixA, classicMatrixB); + std::vector > manualResult = subtractMatrices(classicMatrixA, classicMatrixB); // method - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA.subtract(sparseMatrixB), manualResult, "Incorrect matrices subtract" ); // operator - assertEquals, vector > >( + assertEquals, std::vector > >( sparseMatrixA - sparseMatrixB, manualResult, "Incorrect matrices subtract (operator -)" ); } - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/cases/get-set.h b/tests/cases/values.cpp similarity index 66% rename from tests/cases/get-set.h rename to tests/cases/values.cpp index 653bde6..1c1e293 100644 --- a/tests/cases/get-set.h +++ b/tests/cases/values.cpp @@ -12,39 +12,39 @@ void _getFail(void) { - SparseMatrix m(1, 1); + SparseMatrix::SparseMatrix m(1, 1); m.get(2, 1); } void testGetFail(void) { - cout << "get() fail..." << flush; + std::cout << "get() fail..." << std::flush; assertException("InvalidCoordinatesException", _getFail); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void _setFail(void) { - SparseMatrix m(3, 4); + SparseMatrix::SparseMatrix m(3, 4); m.set(-1, 4, 0); } void testSetFail(void) { - cout << "set() fail..." << flush; + std::cout << "set() fail..." << std::flush; assertException("InvalidCoordinatesException", _setFail); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } void testGettersAndSetters(void) { - cout << "getters/setters..." << flush; + std::cout << "getters/setters..." << std::flush; - SparseMatrix m(3); + SparseMatrix::SparseMatrix m(3); for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { assertEquals(0, m.get(i, j)); @@ -54,5 +54,5 @@ void testGettersAndSetters(void) m.set(-4, 1, 3); assertEquals(-4, m.get(1, 3)); - cout << " OK" << endl; + std::cout << " OK" << std::endl; } diff --git a/tests/inc/SparseMatrixMock.h b/tests/inc/SparseMatrixMock.h index d7bbe11..31e4c88 100644 --- a/tests/inc/SparseMatrixMock.h +++ b/tests/inc/SparseMatrixMock.h @@ -13,8 +13,6 @@ #include #include "../../src/SparseMatrix/SparseMatrix.h" - using namespace std; - /** * This class is used only for testing purposes @@ -22,35 +20,35 @@ * @internal */ template - class SparseMatrixMock : public SparseMatrix + class SparseMatrixMock : public SparseMatrix::SparseMatrix { public: - SparseMatrixMock(int n) : SparseMatrix(n) + SparseMatrixMock(int n) : SparseMatrix::SparseMatrix(n) {} - SparseMatrixMock(int rows, int columns) : SparseMatrix(rows, columns) + SparseMatrixMock(int rows, int columns) : SparseMatrix::SparseMatrix(rows, columns) {} /** @return Non-empty values in the matrix */ - vector * getValues(void) + std::vector * getValues(void) { return this->vals; } /** @return Column pointers */ - vector * getColumnPointers(void) + std::vector * getColumnPointers(void) { return this->cols; } /** @return Row pointers */ - vector * getRowPointers(void) + std::vector * getRowPointers(void) { return this->rows; } @@ -62,9 +60,9 @@ * @param os output stream * @return void */ - void printInfo(ostream & os) const + void printInfo(std::ostream & os) const { - vector::iterator intIt; + std::vector::iterator intIt; os << "rows (" << this->rows->size() << "): ["; @@ -78,7 +76,7 @@ os << "]"; - os << endl << "cols"; + os << std::endl << "cols"; if (this->cols == NULL) { os << ": NULL"; @@ -96,12 +94,12 @@ os << "]"; } - os << endl << "vals"; + os << std::endl << "vals"; if (this->vals == NULL) { os << ": NULL"; } else { - typename vector::iterator valIt; + typename std::vector::iterator valIt; os << " (" << this->vals->size() << "): ["; @@ -119,7 +117,7 @@ /** @return Constructed SparseMatrix */ - static SparseMatrixMock fromVectors(vector > vec) + static SparseMatrixMock fromVectors(std::vector > vec) { SparseMatrixMock matrix(vec.size(), vec[0].size()); @@ -136,7 +134,7 @@ template - bool operator == (const SparseMatrix & sparse, const vector > & classical) + bool operator == (const SparseMatrix::SparseMatrix & sparse, const std::vector > & classical) { for (int i = 0, rows = classical.size(); i < rows; i++) { for (int j = 0, cols = classical[i].size(); j < cols; j++) { diff --git a/tests/inc/helpers.h b/tests/inc/helpers.h index f7df40f..6c303ed 100644 --- a/tests/inc/helpers.h +++ b/tests/inc/helpers.h @@ -13,15 +13,13 @@ #include #include - using namespace std; - // === GENERATORS ========================================= template - vector generateRandomVector(int size) + std::vector generateRandomVector(int size) { - vector vector(size, 0); + std::vector vector(size, 0); for (int i = 0; i < size; i++) { vector[i] = rand() % 101; @@ -32,9 +30,9 @@ template - vector > generateRandomMatrix(int rows, int columns) + std::vector > generateRandomMatrix(int rows, int columns) { - vector > matrix(rows, vector(columns, 0)); + std::vector > matrix(rows, std::vector(columns, 0)); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { @@ -49,12 +47,12 @@ // === STANDARD OPERATIONS ========================================= template - vector > addMatrices(const vector > & a, const vector > & b) + std::vector > addMatrices(const std::vector > & a, const std::vector > & b) { int rows = a.size(); int cols = a.front().size(); - vector > result(rows, vector(cols, 0)); + std::vector > result(rows, std::vector(cols, 0)); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { @@ -67,12 +65,12 @@ template - vector > subtractMatrices(const vector > & a, const vector > & b) + std::vector > subtractMatrices(const std::vector > & a, const std::vector > & b) { int rows = a.size(); int cols = a.front().size(); - vector > result(rows, vector(cols, 0)); + std::vector > result(rows, std::vector(cols, 0)); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { @@ -85,12 +83,12 @@ template - vector multiplyMatrixByVector(const vector > & m, const vector & v) + std::vector multiplyMatrixByVector(const std::vector > & m, const std::vector & v) { int rows = m.size(); int cols = v.size(); - vector result(rows, 0); + std::vector result(rows, 0); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { @@ -103,13 +101,13 @@ template - vector > multiplyMatrices(const vector > & a, const vector > & b) + std::vector > multiplyMatrices(const std::vector > & a, const std::vector > & b) { int rowsA = a.size(); int colsA = a.front().size(); int colsB = b.front().size(); - vector > result(rowsA, vector(colsB, 0)); + std::vector > result(rowsA, std::vector(colsB, 0)); for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { @@ -128,7 +126,7 @@ // === OUTPUT HELPERS ========================================= template - ostream & operator << (ostream & os, const vector & v) + std::ostream & operator << (std::ostream & os, const std::vector & v) { os << "["; diff --git a/tests/inc/testslib.h b/tests/inc/testslib.h index 4579571..6a878d5 100644 --- a/tests/inc/testslib.h +++ b/tests/inc/testslib.h @@ -18,15 +18,13 @@ #include #include - using namespace std; - - class FailureException : public exception + class FailureException : public std::exception { public: - FailureException(const string & message) : exception(), message(message) + FailureException(const std::string & message) : std::exception(), message(message) {} @@ -34,7 +32,7 @@ {} - inline string getMessage(void) const + inline std::string getMessage(void) const { return this->message; } @@ -42,7 +40,7 @@ protected: - string message; + std::string message; }; @@ -52,11 +50,11 @@ try { callback(); - } catch (const exception & e) { - string actualClass(typeid(e).name()); + } catch (const std::exception & e) { + std::string actualClass(typeid(e).name()); if (strstr(actualClass.c_str(), exceptionClass) == NULL) { - ostringstream oss; + std::ostringstream oss; oss << "Exception class '" << exceptionClass << "' expected, but '" << actualClass << "' thrown."; throw FailureException(oss.str()); @@ -73,15 +71,15 @@ void assertEquals(const T & a, const T & b, const char * message = NULL) { if (!(a == b)) { - ostringstream oss; + std::ostringstream oss; if (message == NULL) { - oss << "Objects not equal when they should be." << endl; + oss << "Objects not equal when they should be." << std::endl; } else { - oss << message << endl; + oss << message << std::endl; } - oss << a << endl << "expected, but" << endl << b << " given"; + oss << a << std::endl << "expected, but" << std::endl << b << " given"; throw FailureException(oss.str()); } } @@ -91,15 +89,15 @@ void assertEquals(const X & a, const Y & b, const char * message = NULL) { if (!(a == b)) { - ostringstream oss; + std::ostringstream oss; if (message == NULL) { - oss << "Objects not equal when they should be." << endl; + oss << "Objects not equal when they should be." << std::endl; } else { - oss << message << endl; + oss << message << std::endl; } - oss << a << endl << "expected, but" << endl << b << " given"; + oss << a << std::endl << "expected, but" << std::endl << b << " given"; throw FailureException(oss.str()); } } diff --git a/tests/run.cpp b/tests/run.cpp index 30a275c..8d78de8 100644 --- a/tests/run.cpp +++ b/tests/run.cpp @@ -13,16 +13,14 @@ #include "inc/helpers.h" #include "../src/SparseMatrix/SparseMatrix.cpp" -#include "cases/constructor.h" -#include "cases/get-set.h" -#include "cases/multiplication.h" -#include "cases/addition.h" -#include "cases/subtraction.h" -#include "cases/crs-format.h" -#include "cases/output.h" -#include "cases/custom-type.h" - -using namespace std; +#include "cases/constructor.cpp" +#include "cases/values.cpp" +#include "cases/multiplication.cpp" +#include "cases/addition.cpp" +#include "cases/subtraction.cpp" +#include "cases/crs-format.cpp" +#include "cases/output.cpp" +#include "cases/custom-type.cpp" int main(int argc, char ** argv) @@ -55,7 +53,7 @@ int main(int argc, char ** argv) testElementTypes(); } catch (const FailureException & e) { - cout << " - FAIL: '" << e.getMessage() << "'" << endl; + std::cout << " - FAIL: '" << e.getMessage() << "'" << std::endl; return 1; }