10 #define BOOST_TEST_MODULE SampledFunction
11 #include <boost/test/unit_test.hpp>
12 namespace tt = boost::test_tools;
22 template <
typename T,
typename U>
26 T
const inum =
static_cast<T
>(num);
27 if (num == static_cast<float>(inum))
return inum;
29 return (num > U{ 0 })? inum: inum - T{ 1 };
32 static_assert(constexpr_floor<int>(0) == 0);
34 static_assert(constexpr_floor<int>(-2.00) == -2);
35 static_assert(constexpr_floor<int>(-1.75) == -2);
36 static_assert(constexpr_floor<int>(-1.50) == -2);
37 static_assert(constexpr_floor<int>(-1.25) == -2);
38 static_assert(constexpr_floor<int>(-1.00) == -1);
39 static_assert(constexpr_floor<int>(-0.75) == -1);
40 static_assert(constexpr_floor<int>(-0.50) == -1);
41 static_assert(constexpr_floor<int>(-0.00) == +0);
42 static_assert(constexpr_floor<int>(+0.00) == +0);
43 static_assert(constexpr_floor<int>(+0.50) == +0);
44 static_assert(constexpr_floor<int>(+0.75) == +0);
45 static_assert(constexpr_floor<int>(+1.00) == +1);
46 static_assert(constexpr_floor<int>(+1.25) == +1);
47 static_assert(constexpr_floor<int>(+1.50) == +1);
48 static_assert(constexpr_floor<int>(+1.75) == +1);
49 static_assert(constexpr_floor<int>(+2.00) == +2);
54 auto identity = [](
double x){
return x; };
57 constexpr gsl::index nSamples = 16;
58 constexpr gsl::index nSubsamples = 4;
59 constexpr
double min = -2.0;
60 constexpr
double max = min +
static_cast<double>(nSamples / 2);
61 constexpr
double range = max - min;
62 constexpr
double step = range / nSamples;
63 constexpr
double substep = step / nSubsamples;
66 BOOST_TEST_MESSAGE(
"Test settings:"
67 <<
"\nRange: " << min <<
" -- " << max <<
" (range: " << range <<
")"
68 <<
"\nSamples: " << nSamples <<
" (size: " << step <<
")"
69 <<
"\nSubsamples: " << nSubsamples <<
" (size: " << substep <<
")"
79 BOOST_TEST(sampled.size() == nSamples);
80 BOOST_TEST(sampled.nSubsamples() == nSubsamples);
81 BOOST_TEST(sampled.lower() == min);
82 BOOST_TEST(sampled.upper() == max);
83 BOOST_TEST(sampled.rangeSize() == max - min,
close);
84 BOOST_TEST(sampled.stepSize() == step,
close);
85 BOOST_TEST(sampled.substepSize() == substep,
close);
87 for (
auto const iSub:
util::counter(nSubsamples)) BOOST_TEST_CONTEXT(
"Subsample: " << iSub)
90 double const subsampleStart = min + iSub * substep;
92 auto const& subSample = sampled.subsample(iSub);
94 (
"Subsample #" << iSub <<
": " << subSample.size() <<
" samples");
95 auto itSample = subSample.begin();
97 for (
auto const iSample:
util::counter(-nSamples, 2*nSamples+1)) BOOST_TEST_CONTEXT(
"Sample: " << iSample)
99 bool const bInRange = (iSample >= 0) && (iSample < nSamples);
101 double const expected_x
102 =
static_cast<double>(subsampleStart + iSample * step);
103 double const expected_value = identity(expected_x);
106 BOOST_TEST(sampled.value(iSample, iSub) == expected_value);
107 BOOST_TEST(*itSample == expected_value);
108 BOOST_TEST_MESSAGE(
"[" << iSample <<
"] " << *itSample);
113 for (
double const shift: { 0.0, 0.25, 0.50, 0.75 }) BOOST_TEST_CONTEXT(
"Shift: " <<
shift) {
114 double const expected_x_in_the_middle = expected_x +
shift * substep;
116 gsl::index
const stepIndex
117 = sampled.stepIndex(expected_x_in_the_middle, iSub);
119 BOOST_TEST(sampled.isValidStepIndex(stepIndex) == bInRange);
120 BOOST_TEST(stepIndex == iSample);
123 (sampled.closestSubsampleIndex(expected_x_in_the_middle) == iSub);
129 BOOST_TEST((itSample == subSample.end()),
"itSample != subSample.end()");
131 BOOST_TEST(!sampled.isValidStepIndex
132 (sampled.stepIndex(subsampleStart + max - min, iSub))
143 auto identity = [](
double x){
return x; };
148 constexpr gsl::index nSubsamples = 4;
149 constexpr
double min = -2.0;
150 constexpr
double atLeast = +1.0;
151 constexpr
double stopBefore = +8.2;
152 constexpr
double step = 0.5;
153 constexpr
double substep = step / nSubsamples;
158 constexpr
double stopValue = identity(stopBefore);
160 = [](double,
double y){
return (
y < 0) || (
y >= stopValue); };
164 { identity, min, step, stopIf, nSubsamples, atLeast };
166 constexpr gsl::index expected_nSamples
167 = constexpr_floor<gsl::index>((stopBefore - min) / step);
168 constexpr gsl::index expected_max = min + step * expected_nSamples;
169 constexpr gsl::index expected_range = expected_max - min;
174 BOOST_TEST(sampled.nSubsamples() == nSubsamples);
175 BOOST_TEST(sampled.lower() == min);
176 BOOST_TEST(sampled.stepSize() == step,
close);
177 BOOST_TEST(sampled.substepSize() == substep,
close);
179 BOOST_TEST(sampled.upper() == expected_max,
close);
180 BOOST_TEST_REQUIRE(sampled.size() == expected_nSamples);
181 BOOST_TEST(sampled.rangeSize() == expected_range,
close);
184 auto const nSamples = sampled.size();
186 for (
auto const iSub:
util::counter(nSubsamples)) BOOST_TEST_CONTEXT(
"Subsample: " << iSub)
189 double const subsampleStart = min + iSub * substep;
191 auto const& subSample = sampled.subsample(iSub);
193 (
"Subsample #" << iSub <<
": " << subSample.size() <<
" samples");
194 auto itSample = subSample.begin();
196 for (
auto const iSample:
util::counter(-nSamples, 2*nSamples+1)) BOOST_TEST_CONTEXT(
"Sample: " << iSample)
198 bool const bInRange = (iSample >= 0) && (iSample < nSamples);
200 double const expected_x
201 =
static_cast<double>(subsampleStart + iSample * step);
202 double const expected_value = identity(expected_x);
205 BOOST_TEST(sampled.value(iSample, iSub) == expected_value);
206 BOOST_TEST(*itSample == expected_value);
207 BOOST_TEST_MESSAGE(
"[" << iSample <<
"] " << *itSample);
213 BOOST_TEST((itSample == subSample.end()),
"itSample != subSample.end()");
215 BOOST_TEST(!sampled.isValidStepIndex
216 (sampled.stepIndex(subsampleStart + expected_max - min, iSub))
process_name opflash particleana ie x
constexpr T constexpr_floor(U num)
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
process_name opflash particleana ie ie y
Class for a function with precomputed values.
Test of util::counter and support utilities.
print OUTPUT<< EOF;< setup name="Default"version="1.0">< worldref="volWorld"/></setup ></gdml > EOF close(OUTPUT)
Precomputed discrete sampling of a given function.