Skip to content

Commit

Permalink
tests: separated helper functions into standalone file
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Feb 23, 2018
1 parent 8fd780c commit 18e40c5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 49 deletions.
69 changes: 69 additions & 0 deletions tests/inc/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* This file is part of the SparseMatrix library
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/Sparse-Matrix
*/

#ifndef __HELPERS_H__

#define __HELPERS_H__

#include <vector>
#include <iostream>

using namespace std;


// === GENERATORS =========================================

template<typename T>
vector<T> generateRandomVector(int size)
{
vector<T> vector(size, 0);

for (int i = 0; i < size; i++) {
vector[i] = rand() % 101;
}

return vector;
}


template<typename T>
vector<vector<T> > generateRandomMatrix(int rows, int columns)
{
vector<vector<T> > matrix(rows, vector<int>(columns, 0));

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = rand() % 101;
}
}

return matrix;
}


// === OUTPUT HELPERS =========================================

template<typename T>
ostream & operator << (ostream & os, const vector<T> & v)
{
os << "[";

for (int i = 0, len = v.size(); i < len; i++) {
if (i != 0) {
os << ", ";
}

os << v[i];
}

os << "]";

return os;
}

#endif
49 changes: 0 additions & 49 deletions tests/inc/testslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,53 +104,4 @@
}
}


template<typename T>
vector<T> generateRandomVector(int size)
{
vector<T> vector(size, 0);

for (int i = 0; i < size; i++) {
vector[i] = rand() % 101;
}

return vector;
}


template<typename T>
vector<vector<T> > generateRandomMatrix(int rows, int columns)
{
vector<vector<T> > matrix(rows, vector<int>(columns, 0));

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = rand() % 101;
}
}

return matrix;
}


// === OUTPUT HELPERS =========================================

template<typename T>
ostream & operator << (ostream & os, const vector<T> & v)
{
os << "[";

for (int i = 0, len = v.size(); i < len; i++) {
if (i != 0) {
os << ", ";
}

os << v[i];
}

os << "]";

return os;
}

#endif
1 change: 1 addition & 0 deletions tests/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdlib>
#include <iostream>
#include "inc/testslib.h"
#include "inc/helpers.h"

#include "../src/SparseMatrix/SparseMatrix.cpp"
#include "cases/constructor.h"
Expand Down

0 comments on commit 18e40c5

Please sign in to comment.