2009-12-29 03:42:33 +01:00
|
|
|
|
/*
|
2014-11-10 04:00:39 +01:00
|
|
|
|
UtilForeach(Test) - helpers to perform something for each element
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file util-foreach-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref UtilForeach_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-12-29 04:39:27 +01:00
|
|
|
|
#include "lib/util-foreach.hpp"
|
2009-12-29 03:42:33 +01:00
|
|
|
|
#include "lib/iter-adapter.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-04-03 22:42:48 +02:00
|
|
|
|
#include <functional>
|
2009-12-29 03:42:33 +01:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
|
|
|
|
|
|
|
using util::for_each;
|
|
|
|
|
|
using util::has_any;
|
|
|
|
|
|
using util::and_all;
|
|
|
|
|
|
|
|
|
|
|
|
using boost::lexical_cast;
|
2014-04-03 22:42:48 +02:00
|
|
|
|
using std::function;
|
|
|
|
|
|
using std::ref;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
using std::cout;
|
|
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::vector<int> VecI;
|
|
|
|
|
|
typedef lib::RangeIter<VecI::iterator> RangeI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace{ // Test data and operations
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
// Placeholder for argument in bind-expressions
|
|
|
|
|
|
std::_Placeholder<1> _1;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VecI
|
|
|
|
|
|
buildTestNumberz (uint count)
|
|
|
|
|
|
{
|
|
|
|
|
|
VecI numbers;
|
|
|
|
|
|
numbers.reserve(count);
|
|
|
|
|
|
while (count)
|
|
|
|
|
|
numbers.push_back(count--);
|
|
|
|
|
|
|
|
|
|
|
|
return numbers;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == functions to bind and invoke == */
|
|
|
|
|
|
bool
|
|
|
|
|
|
plainFunc (int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
cout <<':'<< i;
|
|
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
function1 (int i, int j)
|
|
|
|
|
|
{
|
|
|
|
|
|
return plainFunc(i+j);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
function2 (int i, int j, int& k)
|
|
|
|
|
|
{
|
2010-01-04 13:34:51 +01:00
|
|
|
|
k += i + j;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
return plainFunc(k);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define _NL_ cout << endl;
|
2009-12-31 03:25:25 +01:00
|
|
|
|
#define ANNOUNCE(_LABEL_) cout << "---:" << STRINGIFY(_LABEL_) << endl;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
} // (End) test data and operations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/*****************************************************************//**
|
2009-12-29 03:42:33 +01:00
|
|
|
|
* @test Invoking an operation for each element of a collection.
|
|
|
|
|
|
* Covers the various flavours of these convenience helpers:
|
2025-06-07 23:59:57 +02:00
|
|
|
|
* They might operate either on a STL container (providing
|
2009-12-29 03:42:33 +01:00
|
|
|
|
* \c begin() and \c end() functions), or at a "Lumiera
|
|
|
|
|
|
* Forward Iterator", which is incremented and dereferenced
|
|
|
|
|
|
* for each value it yields, until exhaustion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Moreover for each of these cases, there are additional
|
|
|
|
|
|
* overloads allowing to create a bind-expression on-the fly.
|
|
|
|
|
|
* As a further variation of this scheme, an predicate can be
|
2025-06-07 23:59:57 +02:00
|
|
|
|
* evaluated for each element, either with universal quantisation
|
2009-12-29 03:42:33 +01:00
|
|
|
|
* (results are && combined), or with existential quantisation.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class UtilForeach_test : public Test
|
|
|
|
|
|
{
|
2024-11-15 19:02:48 +01:00
|
|
|
|
uint NUM_ELMS{0};
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2024-11-15 19:02:48 +01:00
|
|
|
|
virtual void
|
2009-12-29 03:42:33 +01:00
|
|
|
|
run (Arg arg)
|
|
|
|
|
|
{
|
2024-11-15 19:02:48 +01:00
|
|
|
|
NUM_ELMS = firstVal (arg, 10);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
VecI container = buildTestNumberz (NUM_ELMS);
|
|
|
|
|
|
RangeI iterator(container.begin(), container.end());
|
|
|
|
|
|
|
|
|
|
|
|
check_foreach_plain (container);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
check_foreach_plain (iterator);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
check_foreach_bind (container);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
check_foreach_bind (iterator);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
check_foreach_bind_const (container);
|
|
|
|
|
|
|
2009-12-29 03:42:33 +01:00
|
|
|
|
check_foreach_memFun (container);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
check_foreach_memFun (iterator);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
check_foreach_lambda (container);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
check_foreach_lambda (iterator);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
check_existence_quant (container);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
check_existence_quant (iterator);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (int(NUM_ELMS) ==container[0]);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
|
|
|
|
|
|
check_ref_argument_bind (container);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (int(NUM_ELMS) ==container[0]);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
|
|
|
|
|
|
check_ref_argument_bind (iterator);
|
2014-05-10 02:14:38 +02:00
|
|
|
|
CHECK (90+int(NUM_ELMS) ==container[0]);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
// changes got propagated through the iterator
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
|
check_wrapped_container_passing(container);
|
|
|
|
|
|
|
2009-12-29 04:30:11 +01:00
|
|
|
|
check_invoke_on_each ();
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test invoke a simple free function, given
|
|
|
|
|
|
* as reference, function pointer or functor.
|
|
|
|
|
|
* The invoked test function will print its argument
|
|
|
|
|
|
*/
|
|
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_foreach_plain (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_foreach_plain);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
function<bool(int)> func(plainFunc);
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll, plainFunc); _NL_
|
|
|
|
|
|
for_each (coll, &plainFunc); _NL_
|
|
|
|
|
|
for_each (coll, func); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
and_all (coll, plainFunc); _NL_
|
|
|
|
|
|
and_all (coll, &plainFunc); _NL_
|
|
|
|
|
|
and_all (coll, func); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
has_any (coll, plainFunc); _NL_
|
|
|
|
|
|
has_any (coll, &plainFunc); _NL_
|
|
|
|
|
|
has_any (coll, func); _NL_
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test bind additional parameters on-the-fly,
|
|
|
|
|
|
* including the possibility to use a placeholder
|
|
|
|
|
|
* to denote the position of the variable parameter */
|
|
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_foreach_bind (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_foreach_bind);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
function<bool(int,int)> fun1(function1);
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll, function1, 10, _1 ); _NL_
|
|
|
|
|
|
for_each (coll, &function1,10, _1 ); _NL_
|
|
|
|
|
|
for_each (coll, fun1, 10, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
and_all (coll, function1, 10, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, &function1, 10, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, fun1, 10, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
has_any (coll, function1, 10, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, &function1, 10, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, fun1, 10, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll, function1, _1, _1 ); _NL_
|
|
|
|
|
|
for_each (coll, &function1,_1, _1 ); _NL_
|
|
|
|
|
|
for_each (coll, fun1, _1, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
and_all (coll, function1, _1, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, &function1, _1, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, fun1, _1, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
has_any (coll, function1, _1, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, &function1, _1, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, fun1, _1, _1 ); _NL_
|
|
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
//does not compile.....
|
|
|
|
|
|
// for_each (coll, function1, 10, 20, _1 );
|
|
|
|
|
|
// for_each (coll, function1, _1, _2 );
|
|
|
|
|
|
// for_each (coll, function1, 10 );
|
2010-01-04 14:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test under some circumstances, it is even possible
|
|
|
|
|
|
* to take a ref to the data in the input sequence,
|
2014-05-10 02:14:38 +02:00
|
|
|
|
* or to a summation variable. In the example performed here,
|
|
|
|
|
|
* the function to be applied takes the 3rd argument by reference
|
|
|
|
|
|
* and assigns the sum of first and second argument to this parameter.
|
|
|
|
|
|
* If we us a bind variable at that position, we end up assigning
|
|
|
|
|
|
* by reference to the values contained in the collection.
|
2010-01-04 14:23:15 +01:00
|
|
|
|
* @note in case of invoking this test with a Lumiera Forward Iterator,
|
|
|
|
|
|
* the changes go through to the original container, in spite of
|
|
|
|
|
|
* passing the iterator by value. This behaviour is correct, as
|
|
|
|
|
|
* an iterator is an reference-like object
|
2010-01-07 04:40:10 +01:00
|
|
|
|
*/
|
2010-01-04 14:23:15 +01:00
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_ref_argument_bind (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (assign_to_input);
|
2014-04-28 01:34:03 +02:00
|
|
|
|
function<bool(int,int,int&)> fun2(function2);
|
2010-01-04 14:23:15 +01:00
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
for_each (coll, function2, 5, 5, _1 ); _NL_
|
|
|
|
|
|
for_each (coll, &function2,5, 5, _1 ); _NL_
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
and_all (coll, function2, 5, 5, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, &function2, 5, 5, _1 ); _NL_
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
has_any (coll, function2, 5, 5, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, &function2, 5, 5, _1 ); _NL_
|
2010-01-04 14:23:15 +01:00
|
|
|
|
|
2014-05-10 02:14:38 +02:00
|
|
|
|
// note: in C++11, the reference parameters are passed through
|
|
|
|
|
|
// even when wrapping the function or function pointer into a function object,
|
2010-01-04 14:23:15 +01:00
|
|
|
|
for_each (coll,fun2, 5, 5, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, fun2, 5, 5, _1 ); _NL_
|
2010-01-04 13:34:51 +01:00
|
|
|
|
has_any (coll, fun2, 5, 5, _1 ); _NL_
|
2014-05-10 02:14:38 +02:00
|
|
|
|
// at that point we have added 9 * (5+5) to the value at position zero.
|
|
|
|
|
|
// (note that the has_any only evaluates the function once)
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2014-05-10 02:14:38 +02:00
|
|
|
|
|
|
|
|
|
|
// the following test assigns the sum via the ref argument to a local variable.
|
2009-12-29 03:42:33 +01:00
|
|
|
|
int sum=0;
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (assign_to_var);
|
2010-01-04 13:34:51 +01:00
|
|
|
|
for_each (coll, function2, -10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
for_each (coll, &function2,-10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
for_each (coll, fun2, -10, _1, ref(sum) ); _NL_
|
2009-12-31 03:25:25 +01:00
|
|
|
|
cout << "sum=" << sum << endl;
|
|
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
sum=0;
|
|
|
|
|
|
and_all (coll, function2, -10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
and_all (coll, &function2, -10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
and_all (coll, fun2, -10, _1, ref(sum) ); _NL_
|
2009-12-31 03:25:25 +01:00
|
|
|
|
cout << "sum=" << sum << endl;
|
|
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
sum=0;
|
|
|
|
|
|
has_any (coll, function2, -10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
has_any (coll, &function2, -10, _1, ref(sum) ); _NL_
|
|
|
|
|
|
has_any (coll, fun2, -10, _1, ref(sum) ); _NL_
|
2009-12-31 03:25:25 +01:00
|
|
|
|
cout << "sum=" << sum << endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test the input sequence can be also taken
|
|
|
|
|
|
* from a const container (for iterators this
|
|
|
|
|
|
* obviously doesn't make sense */
|
|
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_foreach_bind_const (CO const& coll)
|
|
|
|
|
|
{
|
|
|
|
|
|
ANNOUNCE (check_foreach_bind_const);
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll,function1, 10, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, function1, 10, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, function1, 10, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll,function1, _1, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, function1, _1, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, function1, _1, _1 ); _NL_
|
|
|
|
|
|
|
|
|
|
|
|
int sum=0;
|
|
|
|
|
|
|
|
|
|
|
|
for_each (coll,function2, _1, _1, ref(sum) ); _NL_
|
|
|
|
|
|
and_all (coll, function2, _1, _1, ref(sum) ); _NL_
|
|
|
|
|
|
has_any (coll, function2, _1, _1, ref(sum) ); _NL_
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Dummy
|
|
|
|
|
|
{
|
|
|
|
|
|
int sum_;
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
fun (int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
sum_ += i;
|
|
|
|
|
|
return plainFunc (sum_);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @test bind a member function to be invoked for each element */
|
|
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_foreach_memFun (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_foreach_memFun);
|
|
|
|
|
|
|
2009-12-29 03:42:33 +01:00
|
|
|
|
Dummy dummy;
|
2009-12-31 03:25:25 +01:00
|
|
|
|
dummy.sum_ = 0;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
for_each (coll, &Dummy::fun, dummy, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, &Dummy::fun, dummy, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, &Dummy::fun, dummy, _1 ); _NL_
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
for_each (coll, &Dummy::fun, &dummy, _1 ); _NL_
|
|
|
|
|
|
and_all (coll, &Dummy::fun, &dummy, _1 ); _NL_
|
|
|
|
|
|
has_any (coll, &Dummy::fun, &dummy, _1 ); _NL_
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
cout << "sum=" << dummy.sum_ << endl;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
/** @test use lambda-expressions, to be invoked for each element */
|
2009-12-29 03:42:33 +01:00
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_foreach_lambda (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_foreach_lambda);
|
|
|
|
|
|
uint sum(0);
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
for_each (coll, [&sum] (uint entry) { sum += entry; });
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (sum == (NUM_ELMS+1) * NUM_ELMS/2);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
CHECK (!and_all (coll, [] (uint elm) { return elm - 1; }));
|
|
|
|
|
|
CHECK ( has_any (coll, [] (uint elm) { return elm + 1; }));
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test verify the logic of universal and existential quantisation.
|
2014-05-12 01:12:45 +02:00
|
|
|
|
* Using lambda expressions as predicates */
|
2009-12-29 03:42:33 +01:00
|
|
|
|
template<typename CO>
|
|
|
|
|
|
void
|
|
|
|
|
|
check_existence_quant (CO coll)
|
|
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_existence_quant);
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
CHECK ( and_all (coll, [] (uint elm) { return 0 < elm; }));
|
|
|
|
|
|
CHECK (!and_all (coll, [] (uint elm) { return 1 < elm; }));
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
2024-11-15 19:02:48 +01:00
|
|
|
|
CHECK ( has_any (coll, [] (uint elm) { return 0 < elm; }));
|
|
|
|
|
|
CHECK ( has_any (coll, [this](uint elm) { return elm == NUM_ELMS; }));
|
|
|
|
|
|
CHECK (!has_any (coll, [this](uint elm) { return elm > NUM_ELMS; }));
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct TestElm
|
|
|
|
|
|
{
|
|
|
|
|
|
uint n_;
|
|
|
|
|
|
TestElm(uint i) : n_(i) {}
|
|
|
|
|
|
|
2009-12-31 03:25:25 +01:00
|
|
|
|
bool operation() { return plainFunc (n_); }
|
2009-12-29 03:42:33 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test the binding can also be used to \em dispatch an operation
|
|
|
|
|
|
* on each element within a object collection: here the parameter
|
|
|
|
|
|
* is used as \c this pointer to specify the object instance */
|
|
|
|
|
|
void
|
2009-12-29 04:30:11 +01:00
|
|
|
|
check_invoke_on_each ()
|
2009-12-29 03:42:33 +01:00
|
|
|
|
{
|
2009-12-31 03:25:25 +01:00
|
|
|
|
ANNOUNCE (check_invoke_on_each);
|
|
|
|
|
|
|
2009-12-29 04:30:11 +01:00
|
|
|
|
std::vector<TestElm> elms;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
for (uint i=0; i<6; ++i)
|
|
|
|
|
|
elms.push_back (TestElm(i));
|
|
|
|
|
|
|
2009-12-29 04:30:11 +01:00
|
|
|
|
std::vector<TestElm*> elmPtrs;
|
2009-12-29 03:42:33 +01:00
|
|
|
|
for (uint i=0; i<6; ++i)
|
2009-12-29 04:30:11 +01:00
|
|
|
|
elmPtrs.push_back (& elms[i]);
|
2009-12-29 03:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
// fed the element pointer as "this" pointer of the member function
|
|
|
|
|
|
for_each (elmPtrs, &TestElm::operation, _1 ); _NL_
|
|
|
|
|
|
and_all (elmPtrs, &TestElm::operation, _1 ); _NL_
|
|
|
|
|
|
has_any (elmPtrs, &TestElm::operation, _1 ); _NL_
|
|
|
|
|
|
|
2010-01-04 13:34:51 +01:00
|
|
|
|
// the same works with copies of the elements as well...
|
2009-12-29 03:42:33 +01:00
|
|
|
|
for_each (elms, &TestElm::operation, _1 ); _NL_
|
|
|
|
|
|
and_all (elms, &TestElm::operation, _1 ); _NL_
|
|
|
|
|
|
has_any (elms, &TestElm::operation, _1 ); _NL_
|
2010-01-04 13:34:51 +01:00
|
|
|
|
|
|
|
|
|
|
// note: it seems not to be possible to create a binder, which takes the "*this"-Argument by ref
|
2009-12-29 03:42:33 +01:00
|
|
|
|
}
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
2014-05-10 02:14:38 +02:00
|
|
|
|
/** @test pass the collection to be iterated in various ways
|
2010-01-07 04:40:10 +01:00
|
|
|
|
* - anonymous temporary
|
|
|
|
|
|
* - smart pointer
|
|
|
|
|
|
* - pointer
|
|
|
|
|
|
* - const&
|
|
|
|
|
|
* @note We do modifications using a lambda expression with a
|
|
|
|
|
|
* side-effect. The container passed in is always modified,
|
|
|
|
|
|
* disregarding const! (In case of the anonymous temporary
|
|
|
|
|
|
* the modifications get discarded after reaching the end
|
|
|
|
|
|
* of the for_each expression
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
check_wrapped_container_passing (VecI coll)
|
|
|
|
|
|
{
|
|
|
|
|
|
ANNOUNCE (wrapped_container_passing);
|
|
|
|
|
|
|
|
|
|
|
|
#define SHOW_CONTAINER for_each (coll, plainFunc); _NL_
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
int counter = NUM_ELMS;
|
|
|
|
|
|
auto assign_and_decrement = [&] (int& entry)
|
|
|
|
|
|
{
|
|
|
|
|
|
entry = counter--;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2010-01-07 04:40:10 +01:00
|
|
|
|
// use a const reference to pass the container...
|
|
|
|
|
|
VecI const& passByConstRef (coll);
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
for_each (passByConstRef, assign_and_decrement );
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
SHOW_CONTAINER
|
|
|
|
|
|
// indeed got modifications into the original container!
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (0 == counter);
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
// passing anonymous temporary
|
2014-05-12 01:12:45 +02:00
|
|
|
|
for_each (buildTestNumberz(NUM_ELMS), assign_and_decrement );
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
// passing a smart-ptr managed copy
|
2014-04-03 22:42:48 +02:00
|
|
|
|
std::shared_ptr<VecI> bySmartPtr (new VecI (coll));
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
for_each (bySmartPtr, assign_and_decrement );
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
|
|
|
|
|
// both didn't influence the original container
|
|
|
|
|
|
SHOW_CONTAINER
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (-2*int(NUM_ELMS) == counter);
|
|
|
|
|
|
CHECK (bySmartPtr->back() == counter+1);
|
2010-01-07 04:40:10 +01:00
|
|
|
|
|
2014-05-10 02:14:38 +02:00
|
|
|
|
// passing the container by pointer is also possible
|
2010-01-07 04:40:10 +01:00
|
|
|
|
const VecI * const passByConstPointer (&coll);
|
|
|
|
|
|
|
2014-05-12 01:12:45 +02:00
|
|
|
|
for_each (passByConstPointer, assign_and_decrement );
|
2010-01-07 04:40:10 +01:00
|
|
|
|
SHOW_CONTAINER
|
2014-05-10 02:14:38 +02:00
|
|
|
|
// ...and does indeed influence the original container
|
2010-01-07 04:40:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-12-29 03:42:33 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LAUNCHER (UtilForeach_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace util::test
|
|
|
|
|
|
|