Skip to content

Commit

Permalink
formatting updates to test_helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
jsrehak authored and Weixiong (Victor) Zheng committed Oct 8, 2018
1 parent 98dff03 commit f21fe93
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
22 changes: 11 additions & 11 deletions src/test_helpers/test_helper_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,56 @@ double RandomDouble(double min, double max) {
if (min >= max)
throw std::runtime_error("Min must be less than max");
double random_zero_one =
static_cast<double>(rand())/static_cast<double>(RAND_MAX);
static_cast<double>(std::rand())/static_cast<double>(RAND_MAX);
// multiply by total range and add to min
return (random_zero_one * (max - min)) + min;
}

std::vector<double> RandomVector(size_t n, double min, double max) {
std::vector<double> RandomVector(std::size_t n, double min, double max) {

if (n == 0u)
throw std::runtime_error("Vector length must be > 0");

std::vector<double> return_vector;
for (size_t i = 0; i < n; ++i) {
for (std::size_t i = 0; i < n; ++i) {
return_vector.push_back(RandomDouble(min, max));
}
return return_vector;
}

std::unordered_map<int, std::vector<double>>
RandomIntVectorMap(size_t map_size, size_t vector_size, double min,
RandomIntVectorMap(std::size_t map_size, std::size_t vector_size, double min,
double max) {
if (map_size == 0u)
throw std::runtime_error("IntVectorMap requires map size of at least 1");

std::unordered_map<int, std::vector<double>> return_map;

do {
int material_id = rand()%(map_size*10);
int material_id = std::rand()%(map_size*10);
return_map.insert({material_id, RandomVector(vector_size, min, max)});
} while (return_map.size() < map_size);

return return_map;
}
dealii::FullMatrix<double> RandomMatrix(size_t m, size_t n, double min,
dealii::FullMatrix<double> RandomMatrix(std::size_t m, std::size_t n, double min,
double max) {
dealii::FullMatrix<double> return_matrix(m, n);
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
for (std::size_t i = 0; i < m; ++i) {
for (std::size_t j = 0; j < n; ++j) {
return_matrix.set(i, j, RandomDouble(min, max));
}
}
return return_matrix;
}

std::unordered_map<int, dealii::FullMatrix<double>>
RandomIntMatrixMap(size_t map_size, size_t m, size_t n, double min, double max) {

RandomIntMatrixMap(std::size_t map_size, std::size_t m, std::size_t n,
double min, double max) {
std::unordered_map<int, dealii::FullMatrix<double>> return_map;

do {
int material_id = rand()%(map_size*10);
int material_id = std::rand()%(map_size*10);
return_map.insert({material_id, RandomMatrix(m, n, min, max)});
} while (return_map.size() < map_size);

Expand Down
14 changes: 6 additions & 8 deletions src/test_helpers/test_helper_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <unordered_map>
#include <vector>

#include <iostream>

#include <deal.II/lac/full_matrix.h>

namespace btest {
Expand All @@ -16,29 +14,29 @@ namespace btest {
double RandomDouble(double min, double max);

//! Generates a vector populated with n random doubles between min and max
std::vector<double> RandomVector(size_t n, double min, double max);
std::vector<double> RandomVector(std::size_t n, double min, double max);

//! Generates a random unordered map of ints to vector<double>.
/*! Generates a random unordered map of ints to vector<double> map_size keys to
vectors of length vector_size with values between min and max.
*/

std::unordered_map<int, std::vector<double>> RandomIntVectorMap(
size_t map_size = 4, size_t vector_size = 3, double min = 0,
std::size_t map_size = 4, std::size_t vector_size = 3, double min = 0,
double max = 100);

//! Generates a random dealii::FullMatrix<double>.
/*! Generates a random dealii::FullMatrix<double> of dimensions
\f$\mathcal{R}^{m \times n}\f$ with random double values between min and max.
*/
dealii::FullMatrix<double> RandomMatrix(size_t m, size_t n, double min = 0,
double max = 100);
dealii::FullMatrix<double> RandomMatrix(std::size_t m, std::size_t n,
double min = 0, double max = 100);

//! Generates a random unordered map of ints to dealii::FullMatrix<double>

std::unordered_map<int, dealii::FullMatrix<double>>
RandomIntMatrixMap(size_t map_size = 4, size_t m = 5, size_t n = 5,
double min = 0, double max = 100);
RandomIntMatrixMap(std::size_t map_size = 4, std::size_t m = 5,
std::size_t n = 5, double min = 0, double max = 100);

} // namespace btest

Expand Down
45 changes: 23 additions & 22 deletions src/test_helpers/tests/helper_function_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class TestHelperFunctionTest : public ::testing::Test {
};

TEST_F(TestHelperFunctionTest, RandomDoubleTest) {

const std::vector<std::pair<double, double>> test_pairs = {
{0, 100}, {-50, 50}, {-50, 0}, {0, 20.4}, {-102.4, 293}};

Expand Down Expand Up @@ -99,23 +98,24 @@ TEST_F(TestHelperFunctionTest, RandomIntVectorMapTest) {
TEST_F(TestHelperFunctionTest, RandomMatrixTest) {
// Generate 10 random matrices and verify properties
for (int i = 0; i < 10; ++i) {
size_t n = rand()%10 + 1;
size_t m = rand()%10 + 1;
std::size_t n = std::rand()%10 + 1;
std::size_t m = std::rand()%10 + 1;

double val1 = -200 + static_cast<double>(rand()) / RAND_MAX*(400);
double val2 = -200 + static_cast<double>(rand()) / RAND_MAX*(400);
double min = (val1 > val2) ? val2 : val1;
double max = (val1 > val2) ? val1 : val2;
double val1 = -200 + static_cast<double>(std::rand()) / RAND_MAX*(400);
double val2 = -200 + static_cast<double>(std::rand()) / RAND_MAX*(400);
double min_value = (val1 > val2) ? val2 : val1;
double max_value = (val1 > val2) ? val1 : val2;

std::ostringstream test_msg;
test_msg << "Matrix Size: " << m << "x" << n << "; min/max: "
<< min << "/" << max;
<< min_value << "/" << max_value;

dealii::FullMatrix<double> matrix = btest::RandomMatrix(m, n, min, max);
dealii::FullMatrix<double> matrix = btest::RandomMatrix(m, n, min_value,
max_value);

for (auto cit = matrix.begin(); cit < matrix.end(); ++cit)
EXPECT_THAT((*cit).value(), ::testing::AllOf(::testing::Ge(min),
::testing::Le(max)))
EXPECT_THAT((*cit).value(), ::testing::AllOf(::testing::Ge(min_value),
::testing::Le(max_value)))
<< test_msg.str();

EXPECT_EQ(matrix.m(), m) << test_msg.str();
Expand All @@ -126,20 +126,21 @@ TEST_F(TestHelperFunctionTest, RandomMatrixTest) {
TEST_F(TestHelperFunctionTest, RandomIntMatrixMapTest) {
// Generate 20 random int to matrix maps and verify properties
for (int i = 0; i < 20; ++i) {
size_t n = rand()%10 + 1;
size_t m = rand()%10 + 1;
size_t map_size = rand()%10 + 1;
std::size_t n = std::rand()%10 + 1;
std::size_t m = std::rand()%10 + 1;
std::size_t map_size = std::rand()%10 + 1;

double val1 = -200 + static_cast<double>(rand()) / RAND_MAX*(400);
double val2 = -200 + static_cast<double>(rand()) / RAND_MAX*(400);
double min = (val1 > val2) ? val2 : val1;
double max = (val1 > val2) ? val1 : val2;
double val1 = -200 + static_cast<double>(std::rand()) / RAND_MAX*(400);
double val2 = -200 + static_cast<double>(std::rand()) / RAND_MAX*(400);
double min_value = (val1 > val2) ? val2 : val1;
double max_value = (val1 > val2) ? val1 : val2;

std::ostringstream test_msg;
test_msg << "Map size: " << map_size << "; Matrix Size: " << m << "x"
<< n << "; min/max: " << min << "/" << max;
<< n << "; min/max: " << min_value << "/" << max_value;

auto matrix_map = btest::RandomIntMatrixMap(map_size, m, n, min, max);
auto matrix_map = btest::RandomIntMatrixMap(map_size, m, n, min_value,
max_value);

EXPECT_EQ(matrix_map.size(), map_size) << test_msg.str();

Expand All @@ -149,8 +150,8 @@ TEST_F(TestHelperFunctionTest, RandomIntMatrixMapTest) {
<< test_msg.str();

for (auto cit = mat.second.begin(); cit < mat.second.end(); ++cit)
EXPECT_THAT((*cit).value(), ::testing::AllOf(::testing::Ge(min),
::testing::Le(max)))
EXPECT_THAT((*cit).value(), ::testing::AllOf(::testing::Ge(min_value),
::testing::Le(max_value)))
<< test_msg.str();

EXPECT_EQ(mat.second.m(), m) << test_msg.str();
Expand Down

0 comments on commit f21fe93

Please sign in to comment.