35 #include <gtest/gtest.h> 43 template <
typename NOISE_GEN>
45 const std::vector<float>& expected_covariances,
47 const float allowable_error_of_zero_cov)
49 const size_t dim = expected_means.size();
50 ASSERT_EQ(dim, gen.getDimension());
51 ASSERT_EQ(dim * dim, expected_covariances.size());
54 std::vector<std::vector<float>> results(dim);
55 for (
size_t i = 0; i < 10000; ++i)
57 const auto result = gen(mt);
58 ASSERT_EQ(dim, result.size());
59 for (
size_t j = 0; j < dim; ++j)
61 results[j].push_back(result[j]);
64 std::vector<float> averages;
65 for (
size_t i = 0; i < dim; ++i)
67 const float average = std::accumulate(results[i].begin(), results[i].end(), 0.0) / results[i].size();
68 EXPECT_NEAR(average, expected_means[i], 0.1);
69 averages.push_back(average);
71 for (
size_t i = 0; i < dim; ++i)
73 for (
size_t j = i; j < dim; ++j)
76 for (
size_t n = 0; n < results[i].size(); ++n)
78 covar += (results[i][n] - averages[i]) * (results[j][n] - averages[j]);
80 covar /= results[i].size();
81 const float expected_covar = expected_covariances[i + j * dim];
82 if (expected_covar == 0.0)
84 EXPECT_NEAR(covar, expected_covar, allowable_error_of_zero_cov) <<
"Error at " << i <<
"," << j;
88 EXPECT_GE(covar, expected_covariances[i + j * dim] * 0.9 * 0.9) <<
"Error at " << i <<
"," << j;
89 EXPECT_LE(covar, expected_covariances[i + j * dim] * 1.1 * 1.1) <<
"Error at " << i <<
"," << j;
97 const size_t dim = sigma.size();
99 std::vector<float> result(dim * dim, 0.0);
100 for (
size_t i = 0; i < sigma.size(); ++i)
102 result[i + i * dim] = sigma[i] * sigma[i];
109 std::vector<float> expected_mean;
110 expected_mean.push_back(7.0);
111 expected_mean.push_back(8.0);
112 expected_mean.push_back(9.0);
114 std::vector<float> expected_sigma;
115 expected_sigma.push_back(1.0);
116 expected_sigma.push_back(2.0);
117 expected_sigma.push_back(3.0);
126 const std::vector<float> expected_mean =
130 const std::vector<float> expected_covariance =
142 TEST(NoiseGenerator, MultivariateNoiseGeneratorForState6Dof)
145 const std::vector<float> expected_mean =
147 5.0, -6.0, 7.0, -0.3, 0.2, 0.1
149 const std::vector<float> expected_covariance =
151 2.0, 0.5, 0.6, 0.05, 0.04, 0.0,
152 0.5, 2.5, 0.4, 0.06, 0.07, 0.08,
153 0.6, 0.4, 3.0, 0.09, 0.02, 0.11,
154 0.05, 0.06, 0.09, 0.2, 0.045, 0.035,
155 0.04, 0.07, 0.02, 0.045, 0.15, 0.015,
156 0.0, 0.08, 0.11, 0.035, 0.015, 0.1
160 const mcl_3dl::Vec3 mean_pos(expected_mean[0], expected_mean[1], expected_mean[2]);
162 const State6DOF mean(mean_pos, mean_rot);
170 int main(
int argc,
char** argv)
172 testing::InitGoogleTest(&argc, argv);
174 return RUN_ALL_TESTS();
TEST(NoiseGenerator, DiagonalNoiseGenerator)
int main(int argc, char **argv)
std::vector< float > toCovarianceMatrix(const std::vector< float > &sigma)
void testNoiseGeneratorResults(const std::vector< float > &expected_means, const std::vector< float > &expected_covariances, const NOISE_GEN &gen, const float allowable_error_of_zero_cov)