Skip to content

Commit d3415ca

Browse files
committed
filter tests
1 parent 94bf2c4 commit d3415ca

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

src/filter/core/test/simulator/measurements.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,55 +84,76 @@ std::vector<Measurements<T>> reset_position_measurements(
8484
}
8585

8686
template <typename T>
87-
std::vector<Measurements<T>> add_bad_measurements(
88-
const std::vector<Measurements<T>>& measurements,
89-
const MeasurementConfig<T>& config)
87+
class BadMeasurements final
9088
{
91-
constexpr T X = 2000;
92-
constexpr T X_AFTER_RESET = 500;
93-
constexpr T V = 30;
94-
constexpr T PROBABILITY = T{1} / 20;
95-
constexpr int COUNT_AFTER_RESET = 2;
89+
static constexpr T X = 2000;
90+
static constexpr T X_AFTER_RESET = 500;
91+
static constexpr T V = 30;
92+
static constexpr T PROBABILITY = T{1} / 20;
93+
static constexpr int COUNT_AFTER_RESET = 2;
9694

97-
PCG engine;
95+
const MeasurementConfig<T>* const config_;
9896

99-
int count_after_reset = 0;
97+
PCG engine_;
98+
int count_after_reset_ = 0;
10099

101-
const auto x = [&](Measurements<T>& m)
100+
void update_position(Measurements<T>& m)
102101
{
103102
if (!m.position)
104103
{
105104
return;
106105
}
107-
if (m.time >= config.reset_max_time && count_after_reset < COUNT_AFTER_RESET)
106+
107+
if (m.time >= config_->reset_max_time && count_after_reset_ < COUNT_AFTER_RESET)
108108
{
109-
++count_after_reset;
110-
m.position->value += random_sign(X_AFTER_RESET, engine);
109+
++count_after_reset_;
110+
m.position->value += random_sign(X_AFTER_RESET, engine_);
111111
}
112-
else if (std::bernoulli_distribution(PROBABILITY)(engine))
112+
else if (std::bernoulli_distribution(PROBABILITY)(engine_))
113113
{
114-
m.position->value += random_sign(X, engine);
114+
m.position->value += random_sign(X, engine_);
115115
}
116-
};
116+
}
117117

118-
const auto v = [&](Measurements<T>& m)
118+
void update_speed(Measurements<T>& m)
119119
{
120120
if (!m.speed)
121121
{
122122
return;
123123
}
124-
m.speed->value *= config.speed_factor;
125-
if (std::bernoulli_distribution(PROBABILITY)(engine))
124+
125+
m.speed->value *= config_->speed_factor;
126+
if (std::bernoulli_distribution(PROBABILITY)(engine_))
126127
{
127128
m.speed->value += V;
128129
}
129-
};
130+
}
131+
132+
public:
133+
explicit BadMeasurements(const MeasurementConfig<T>* const config)
134+
: config_(config)
135+
{
136+
ASSERT(config_);
137+
}
138+
139+
void update(Measurements<T>& m)
140+
{
141+
update_position(m);
142+
update_speed(m);
143+
}
144+
};
145+
146+
template <typename T>
147+
std::vector<Measurements<T>> add_bad_measurements(
148+
const std::vector<Measurements<T>>& measurements,
149+
const MeasurementConfig<T>& config)
150+
{
151+
BadMeasurements<T> bm(&config);
130152

131153
std::vector<Measurements<T>> res(measurements);
132154
for (Measurements<T>& m : std::ranges::drop_view(res, 5))
133155
{
134-
x(m);
135-
v(m);
156+
bm.update(m);
136157
}
137158
return res;
138159
}
@@ -184,10 +205,14 @@ template <typename T>
184205
SimulatorMeasurements<T> prepare_measurements(const std::vector<Measurements<T>>& measurements)
185206
{
186207
const MeasurementConfig<T> config = measurement_config<T>();
208+
187209
const std::vector<Measurements<T>> v = reset_position_measurements(measurements, config);
188-
return {.variance_correction = std::make_unique<VarianceCorrectionImpl<T>>(),
210+
211+
return {
212+
.variance_correction = std::make_unique<VarianceCorrectionImpl<T>>(),
189213
.config = config,
190-
.measurements = add_bad_measurements(v, config)};
214+
.measurements = add_bad_measurements(v, config),
215+
};
191216
}
192217

193218
#define INSTANTIATION(T) \

0 commit comments

Comments
 (0)