10 #define BOOST_TEST_MODULE ( RealComparisons_test ) 11 #include <boost/test/unit_test.hpp> 12 #include <boost/iterator/indirect_iterator.hpp> 26 #include <type_traits> 30 template <
typename Iter,
typename Cont>
40 std::is_same<
typename decltype(
r)::value_type,
typename Iter::value_type>()
43 (std::is_same<
typename decltype(
r)::reference,
typename Iter::reference>());
45 BOOST_TEST((
r.begin() == v.begin()));
46 BOOST_TEST((
r.end() == v.end()));
48 BOOST_TEST(
r.empty() == v.empty());
49 BOOST_TEST(
r.size() == v.size());
58 BOOST_TEST(n == v.size());
64 template <
typename Iter,
typename Cont>
74 std::is_same<
typename decltype(
r)::value_type,
typename Iter::value_type>()
77 (std::is_same<
typename decltype(
r)::reference,
typename Iter::reference>());
79 BOOST_TEST((
r.begin() == v.cbegin()));
80 BOOST_TEST((
r.end() == v.cend()));
82 BOOST_TEST(
r.empty() == v.empty());
83 BOOST_TEST(
r.size() == v.size());
92 BOOST_TEST(n == v.size());
98 template <
typename Iter,
typename Cont>
107 std::vector<typename Iter::pointer> ptrs;
109 (v.begin(), v.end(), std::back_inserter(ptrs), [](
auto& v){
return &v; });
112 (ptrs, [](
auto&& iter){
return boost::make_indirect_iterator(iter); });
116 std::is_same<
typename decltype(r)::value_type,
typename Iter::value_type>()
119 BOOST_TEST(r.empty() == v.empty());
120 BOOST_TEST(r.size() == v.size());
124 BOOST_TEST(&rangeValue == &
value);
125 BOOST_TEST(rangeValue ==
value);
128 BOOST_TEST(n == v.size());
134 template <
typename Iter,
typename Cont>
144 using pointer_t =
typename std::iterator_traits<Iter>::pointer;
145 using reference_t =
typename std::iterator_traits<Iter>::reference;
147 std::vector<pointer_t> ptrs;
149 (v.begin(), v.end(), std::back_inserter(ptrs), [](
auto& v){
return &v; });
152 (ptrs, [](
auto* ptr) -> reference_t {
return *ptr; });
156 std::is_same<
typename decltype(r)::value_type,
typename Iter::value_type>()
159 BOOST_TEST(r.empty() == v.empty());
160 BOOST_TEST(r.size() == v.size());
164 BOOST_TEST(&rangeValue == &
value);
165 BOOST_TEST(rangeValue ==
value);
168 BOOST_TEST(n == v.size());
174 template <
typename Iter,
typename Cont>
184 using value_t =
typename std::iterator_traits<Iter>::value_type;
185 using pointer_t = std::unique_ptr<value_t>;
187 std::vector<pointer_t> ptrs;
189 v.begin(), v.end(), std::back_inserter(ptrs),
190 [](
auto& v){
return std::make_unique<value_t>(v); }
196 static_assert(std::is_same<
typename decltype(r)::value_type, value_t>());
198 BOOST_TEST(r.empty() == v.empty());
199 BOOST_TEST(r.size() == v.size());
203 BOOST_TEST(&rangeValue == &
value);
204 BOOST_TEST(rangeValue ==
value);
207 BOOST_TEST(n == v.size());
218 decltype(r)::value_type
s = 0;
219 for (
auto v: r) s += v;
226 std::iota(v.begin(), v.end(), 1);
228 auto span5 =
util::span(v.begin(), v.begin() + 5);
229 std::cout <<
"Sum of 5 numbers: " <<
sum(span5) <<
std::endl;
231 auto span8 =
util::span(v.begin(), v.begin() + 8);
232 std::cout <<
"Sum of 8 numbers: " <<
sum(span8) <<
std::endl;
235 std::cout <<
"Sum of all numbers: " <<
sum(full_span) <<
std::endl;
271 float const salt = 3.0;
272 constexpr std::size_t
N = 10;
274 std::vector<std::unique_ptr<float>>
data;
276 data.push_back(std::make_unique<float>(salt * i));
278 float sum = accumulate(data);
279 float const expectedSum = (N * (N - 1) / 2) * salt;
281 BOOST_TEST(sum == expectedSum);
332 float const salt = 3.0;
333 constexpr std::size_t
N = 10;
335 std::vector<std::unique_ptr<float>>
data;
337 data.push_back(std::make_unique<float>(salt * i));
339 float sum = accumulate(data);
340 float const expectedSum = (N * (N - 1) / 2) * salt;
342 BOOST_TEST(sum == expectedSum);
348 constexpr std::size_t
N = 10;
349 const float factor = 3.0F;
351 std::vector<std::unique_ptr<float>>
data;
353 data.push_back(std::make_unique<float>(i));
358 BOOST_TEST(*ptr == factor * i);
364 analyse_accumulate();
374 using TestVector_t = std::vector<int>;
377 test_span<TestVector_t::iterator>(ev);
378 test_const_span<TestVector_t::const_iterator>(ev);
380 TestVector_t v3 { 1, 2, 3 };
381 test_span<TestVector_t::iterator>(v3);
382 test_const_span<TestVector_t::const_iterator>(v3);
384 TestVector_t
const cv4 { 1, 2, 3, 4 };
385 test_span<TestVector_t::const_iterator>(cv4);
386 test_const_span<TestVector_t::const_iterator>(cv4);
395 using TestVector_t = std::vector<int>;
398 test_adapted_span<TestVector_t::iterator>(ev);
400 TestVector_t v3 { 1, 2, 3 };
401 test_adapted_span<TestVector_t::iterator>(v3);
403 TestVector_t
const cv4 { 1, 2, 3, 4 };
404 test_adapted_span<TestVector_t::const_iterator>(cv4);
412 using TestVector_t = std::vector<int>;
415 test_transformed_span<TestVector_t::iterator>(ev);
416 test_transformed_span_with_unmoveable_values<TestVector_t::iterator>(ev);
418 TestVector_t v3 { 1, 2, 3 };
419 test_transformed_span<TestVector_t::iterator>(v3);
421 TestVector_t
const cv4 { 1, 2, 3, 4 };
422 test_transformed_span<TestVector_t::const_iterator>(cv4);
span(IterB &&b, IterE &&e, Adaptor &&adaptor) -> span< decltype(adaptor(std::forward< IterB >(b))), decltype(adaptor(std::forward< IterE >(e))) >
Definition of util::zip().
An object with a begin and end iterator.
std::vector< int > Data_t
Definition of util::enumerate().
auto make_span(BIter begin, EIter end)
Creates a span from specified iterators (can use constructor instead).
Provides a few simple operations for use in generic programming.
int sum(util::span< Data_t::iterator > r)
auto make_adapted_span(BIter begin, EIter end, Adaptor &&adaptor)
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
void test_const_span(Cont &v)
BOOST_AUTO_TEST_CASE(span_testcase)
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
void test_transformed_span_with_unmoveable_values(Cont &v)
auto make_transformed_span(BIter begin, EIter end, Op &&op)
Test of util::counter and support utilities.
void test_transformed_span(Cont &v)
decltype(auto) dereference()
Returns a functor that returns *ptr of its argument ptr.
float accumulate(std::vector< std::unique_ptr< float >> const &v)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
void test_adapted_span(Cont &v)
QTextStream & endl(QTextStream &s)
auto make_const_span(Cont &cont)
Creates a span with constant iterator access from a container type.