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);
125 const std::vector<float> expected_mean = {-1.0, 2.0, -3.0};
126 const std::vector<float> expected_covariance =
137 TEST(NoiseGenerator, MultivariateNoiseGeneratorForState6Dof)
139 const std::vector<float> expected_mean = {5.0, -6.0, 7.0, -0.3, 0.2, 0.1};
140 const std::vector<float> expected_covariance =
142 2.000, 0.500, 0.600, 0.050, 0.040, 0.000,
143 0.500, 2.500, 0.400, 0.060, 0.070, 0.080,
144 0.600, 0.400, 3.000, 0.090, 0.020, 0.110,
145 0.050, 0.060, 0.090, 0.200, 0.045, 0.035,
146 0.040, 0.070, 0.020, 0.045, 0.150, 0.015,
147 0.000, 0.080, 0.110, 0.035, 0.015, 0.100,
150 const mcl_3dl::Vec3 mean_pos(expected_mean[0], expected_mean[1], expected_mean[2]);
152 const State6DOF mean(mean_pos, mean_rot);
160 int main(
int argc,
char** argv)
162 testing::InitGoogleTest(&argc, argv);
164 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)