Skip to content

Commit

Permalink
core: tests: Remove un-necessary heap allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Jun 7, 2020
1 parent 6216ab9 commit cfd8156
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
17 changes: 9 additions & 8 deletions src/core/unit_tests/random_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define BOOST_TEST_MODULE PRNG test
#define BOOST_TEST_DYN_LINK
#include <array>
#include <boost/test/unit_test.hpp>
#include <tuple>

Expand All @@ -39,9 +40,9 @@ BOOST_AUTO_TEST_CASE(test_noise_statistics) {
std::vector<std::vector<double>> covariance;
std::vector<std::vector<double>> correlation;
std::tie(means, variances, covariance, correlation) = noise_statistics(
[&value]() -> std::vector<VariantVectorXd> {
[&value]() -> std::array<VariantVectorXd, 1> {
value *= -1;
return {{Utils::Vector2d{value, -value}}};
return {Utils::Vector2d{value, -value}};
},
sample_size);
// check pooled mean and variance
Expand All @@ -66,8 +67,8 @@ BOOST_AUTO_TEST_CASE(test_noise_uniform_1d) {
std::vector<std::vector<double>> covariance;
std::vector<std::vector<double>> correlation;
std::tie(means, variances, covariance, correlation) = noise_statistics(
[counter = 0]() mutable -> std::vector<VariantVectorXd> {
return {{Random::noise_uniform<RNGSalt::NPTISOV, 1>(counter++, 0)}};
[counter = 0]() mutable -> std::array<VariantVectorXd, 1> {
return {Random::noise_uniform<RNGSalt::NPTISOV, 1>(counter++, 0)};
},
sample_size);
// check pooled mean and variance
Expand All @@ -83,8 +84,8 @@ BOOST_AUTO_TEST_CASE(test_noise_uniform_3d) {
std::vector<std::vector<double>> covariance;
std::vector<std::vector<double>> correlation;
std::tie(means, variances, covariance, correlation) = noise_statistics(
[counter = 0]() mutable -> std::vector<VariantVectorXd> {
return {{Random::noise_uniform<RNGSalt::LANGEVIN>(counter++, 0)}};
[counter = 0]() mutable -> std::array<VariantVectorXd, 1> {
return {Random::noise_uniform<RNGSalt::LANGEVIN>(counter++, 0)};
},
sample_size);
// check pooled mean and variance
Expand All @@ -108,9 +109,9 @@ BOOST_AUTO_TEST_CASE(test_noise_gaussian_4d) {
std::vector<std::vector<double>> covariance;
std::vector<std::vector<double>> correlation;
std::tie(means, variances, covariance, correlation) = noise_statistics(
[counter = 0]() mutable -> std::vector<VariantVectorXd> {
[counter = 0]() mutable -> std::array<VariantVectorXd, 1> {
return {
{Random::noise_gaussian<RNGSalt::BROWNIAN_WALK, 4>(counter++, 0)}};
Random::noise_gaussian<RNGSalt::BROWNIAN_WALK, 4>(counter++, 0)};
},
sample_size);
// check pooled mean and variance
Expand Down
35 changes: 22 additions & 13 deletions src/core/unit_tests/thermostats_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define BOOST_TEST_MODULE Thermostats test
#define BOOST_TEST_DYN_LINK
#include <array>
#include <boost/test/unit_test.hpp>
#include <cmath>
#include <limits>
Expand Down Expand Up @@ -218,11 +219,11 @@ BOOST_AUTO_TEST_CASE(test_noise_statistics) {
p1.p.identity = 1;

auto const correlation = std::get<3>(noise_statistics(
[&p1, &p2, &thermostat]() -> std::vector<VariantVectorXd> {
[&p1, &p2, &thermostat]() -> std::array<VariantVectorXd, 3> {
thermostat.rng_increment();
return {{friction_thermo_langevin(thermostat, p1),
-friction_thermo_langevin(thermostat, p1),
friction_thermo_langevin(thermostat, p2)}};
return {friction_thermo_langevin(thermostat, p1),
-friction_thermo_langevin(thermostat, p1),
friction_thermo_langevin(thermostat, p2)};
},
sample_size));
for (size_t i = 0; i < correlation.size(); ++i) {
Expand All @@ -248,19 +249,22 @@ BOOST_AUTO_TEST_CASE(test_brownian_randomness) {
auto p = particle_factory();
#ifdef ROTATION
p.p.rotation = ROTATION_X | ROTATION_Y | ROTATION_Z;
constexpr size_t noise_size = 4;
#else
constexpr size_t noise_size = 2;
#endif

auto const correlation = std::get<3>(noise_statistics(
[&p, &thermostat]() -> std::vector<VariantVectorXd> {
[&p, &thermostat]() -> std::array<VariantVectorXd, noise_size> {
thermostat.rng_increment();
return {{
return {
bd_random_walk(thermostat, p, time_step),
bd_random_walk_vel(thermostat, p),
#ifdef ROTATION
bd_random_walk_rot(thermostat, p, time_step),
bd_random_walk_vel_rot(thermostat, p),
#endif
}};
};
},
sample_size));
for (size_t i = 0; i < correlation.size(); ++i) {
Expand All @@ -276,16 +280,21 @@ BOOST_AUTO_TEST_CASE(test_langevin_randomness) {
constexpr size_t const sample_size = 500'000;
auto thermostat = thermostat_factory<LangevinThermostat>();
auto p = particle_factory();
#ifdef ROTATION
constexpr size_t noise_size = 2;
#else
constexpr size_t noise_size = 1;
#endif

auto const correlation = std::get<3>(noise_statistics(
[&p, &thermostat]() -> std::vector<VariantVectorXd> {
[&p, &thermostat]() -> std::array<VariantVectorXd, noise_size> {
thermostat.rng_increment();
return {{
return {
friction_thermo_langevin(thermostat, p),
#ifdef ROTATION
friction_thermo_langevin_rotation(thermostat, p),
#endif
}};
};
},
sample_size));
for (size_t i = 0; i < correlation.size(); ++i) {
Expand All @@ -310,13 +319,13 @@ BOOST_AUTO_TEST_CASE(test_npt_iso_randomness) {
auto p = particle_factory();

auto const correlation = std::get<3>(noise_statistics(
[&p, &thermostat]() -> std::vector<VariantVectorXd> {
[&p, &thermostat]() -> std::array<VariantVectorXd, 3> {
thermostat.rng_increment();
return {{
return {
friction_therm0_nptiso<1>(thermostat, p.m.v, 0),
friction_therm0_nptiso<2>(thermostat, p.m.v, 0),
friction_thermV_nptiso(thermostat, 1.5),
}};
};
},
sample_size));
for (size_t i = 0; i < correlation.size(); ++i) {
Expand Down

0 comments on commit cfd8156

Please sign in to comment.