2009-12-20 04:33:24 +01:00
|
|
|
|
/*
|
|
|
|
|
|
ItemWrapper(Test) - wrapping and holding arbitrary values, pointers and references
|
2010-12-10 02:55:40 +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-10 02:55:40 +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-10 02:55:40 +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-20 04:33:24 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file item-wrapper-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref ItemWrapper_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2024-11-13 02:23:23 +01:00
|
|
|
|
#include "lib/format-cout.hpp"
|
|
|
|
|
|
#include "lib/random.hpp"
|
2009-12-20 04:33:24 +01:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
2025-05-30 23:21:33 +02:00
|
|
|
|
#include "lib/item-wrapper.hpp"
|
2025-02-16 23:16:46 +01:00
|
|
|
|
#include "lib/wrapper-function-result.hpp"
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
|
#include <functional>
|
2009-12-21 07:52:58 +01:00
|
|
|
|
#include <iostream>
|
2017-12-02 04:35:16 +01:00
|
|
|
|
#include <memory>
|
2009-12-20 04:33:24 +01:00
|
|
|
|
#include <string>
|
2009-12-23 01:36:11 +01:00
|
|
|
|
#include <vector>
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2009-12-21 05:44:29 +01:00
|
|
|
|
namespace wrapper {
|
2009-12-20 04:33:24 +01:00
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
2009-12-23 04:46:09 +01:00
|
|
|
|
using lib::test::randStr;
|
|
|
|
|
|
using lib::test::showSizeof;
|
2009-12-20 04:33:24 +01:00
|
|
|
|
using util::isSameObject;
|
2009-12-23 04:46:09 +01:00
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
|
using std::placeholders::_1;
|
|
|
|
|
|
using std::ref;
|
2017-12-02 04:35:16 +01:00
|
|
|
|
using std::shared_ptr;
|
2009-12-23 01:36:11 +01:00
|
|
|
|
using std::vector;
|
2009-12-20 04:33:24 +01:00
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
namespace { // Test helper: yet another ctor/dtor counting class
|
|
|
|
|
|
|
|
|
|
|
|
long cntTracker = 0;
|
2009-12-23 04:46:09 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
struct Tracker
|
|
|
|
|
|
{
|
|
|
|
|
|
uint i_;
|
|
|
|
|
|
|
2024-11-13 02:23:23 +01:00
|
|
|
|
Tracker() : i_(rani(500)) { ++cntTracker; }
|
|
|
|
|
|
Tracker(Tracker const& ot) : i_(ot.i_) { ++cntTracker; }
|
|
|
|
|
|
~Tracker() { --cntTracker; }
|
2009-12-21 07:52:58 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool operator== (Tracker const& t1, Tracker const& t2) { return t1.i_ == t2.i_; }
|
|
|
|
|
|
bool operator!= (Tracker const& t1, Tracker const& t2) { return t1.i_ != t2.i_; }
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-23 04:46:09 +01:00
|
|
|
|
|
|
|
|
|
|
/// to be bound as test function....
|
|
|
|
|
|
int&
|
|
|
|
|
|
pickElement (vector<int>& vec, size_t idx)
|
|
|
|
|
|
{
|
|
|
|
|
|
return vec[idx];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function<int&(size_t)>
|
|
|
|
|
|
pickElement_ofVector (vector<int>& vec)
|
|
|
|
|
|
{
|
2014-04-03 22:42:48 +02:00
|
|
|
|
return std::bind (pickElement, ref(vec), _1 );
|
2009-12-23 04:46:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
} // (END) Test helpers
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/***************************************************************************//**
|
2009-12-23 04:46:09 +01:00
|
|
|
|
* @test use the ItemWrapper to define inline-storage holding values,
|
|
|
|
|
|
* pointers and references. Verify correct behaviour in each case,
|
|
|
|
|
|
* including (self)assignment, empty check, invalid dereferentiation.
|
2010-12-17 22:51:27 +01:00
|
|
|
|
*
|
2009-12-23 04:46:09 +01:00
|
|
|
|
* @see wrapper.hpp
|
2009-12-20 04:33:24 +01:00
|
|
|
|
*/
|
|
|
|
|
|
class ItemWrapper_test : public Test
|
|
|
|
|
|
{
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
2009-12-20 04:33:24 +01:00
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
|
|
|
|
|
|
|
|
|
|
|
ulong l1 (rani (1000));
|
|
|
|
|
|
ulong l2 (rani (1000));
|
2009-12-21 06:19:56 +01:00
|
|
|
|
string s1 (randStr(50));
|
|
|
|
|
|
string s2 (randStr(50));
|
2025-07-02 22:18:39 +02:00
|
|
|
|
CStr cp (s1.c_str());
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-21 06:19:56 +01:00
|
|
|
|
verifyWrapper<ulong> (l1, l2);
|
|
|
|
|
|
verifyWrapper<ulong&> (l1, l2);
|
|
|
|
|
|
verifyWrapper<ulong*> (&l1, &l2);
|
|
|
|
|
|
verifyWrapper<ulong*> ((0), &l2);
|
|
|
|
|
|
verifyWrapper<ulong*> (&l1, (0));
|
|
|
|
|
|
verifyWrapper<ulong const&> (l1, l2);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-21 06:19:56 +01:00
|
|
|
|
verifyWrapper<string> (s1, s2);
|
|
|
|
|
|
verifyWrapper<string&> (s1, s2);
|
|
|
|
|
|
verifyWrapper<string*> (&s1, &s2);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2025-07-02 22:18:39 +02:00
|
|
|
|
verifyWrapper<CStr> (cp, "Lumiera");
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
|
|
|
|
|
|
verifySaneInstanceHandling();
|
2017-12-02 04:35:16 +01:00
|
|
|
|
verifySaneMoveHandling();
|
2009-12-20 04:33:24 +01:00
|
|
|
|
verifyWrappedRef ();
|
2009-12-23 01:36:11 +01:00
|
|
|
|
|
|
|
|
|
|
verifyFunctionResult ();
|
2009-12-25 05:24:49 +01:00
|
|
|
|
verifyFunctionRefResult ();
|
2009-12-20 04:33:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
|
|
void
|
2009-12-21 06:19:56 +01:00
|
|
|
|
verifyWrapper (X val, X otherVal)
|
2009-12-20 04:33:24 +01:00
|
|
|
|
{
|
2009-12-21 06:19:56 +01:00
|
|
|
|
const ItemWrapper<X> wrap(val);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (wrap);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
cout << "ItemWrapper: " << showSizeof(wrap) << endl;
|
|
|
|
|
|
|
2009-12-20 04:33:24 +01:00
|
|
|
|
ItemWrapper<X> copy1 (wrap);
|
|
|
|
|
|
ItemWrapper<X> copy2;
|
|
|
|
|
|
ItemWrapper<X> empty;
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (copy1);
|
|
|
|
|
|
CHECK (!copy2);
|
|
|
|
|
|
CHECK (false == bool(empty));
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (wrap == copy1);
|
|
|
|
|
|
CHECK (wrap != copy2);
|
|
|
|
|
|
CHECK (wrap != empty);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
copy2 = copy1;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (copy2);
|
|
|
|
|
|
CHECK (wrap == copy2);
|
|
|
|
|
|
CHECK (wrap != empty);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
copy2 = otherVal;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (copy2);
|
|
|
|
|
|
CHECK (wrap != copy2);
|
|
|
|
|
|
CHECK (wrap != empty);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (val == *wrap);
|
|
|
|
|
|
CHECK (val == *copy1);
|
|
|
|
|
|
CHECK (val != *copy2);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
VERIFY_ERROR (BOTTOM_VALUE, *empty );
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (otherVal == *copy2);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
copy1 = copy2;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (otherVal == *copy1);
|
|
|
|
|
|
CHECK (otherVal == *copy2);
|
|
|
|
|
|
CHECK (wrap != copy1);
|
|
|
|
|
|
CHECK (wrap != copy2);
|
2009-12-23 04:46:09 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
copy1 = empty; // assign empty to discard value
|
2009-12-23 04:46:09 +01:00
|
|
|
|
copy1 = copy1; // self-assign empty value
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!copy1);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
copy1 = copy2;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (otherVal == *copy1);
|
2009-12-21 07:52:58 +01:00
|
|
|
|
copy1 = copy1; // self-assign (will be suppressed)
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (otherVal == *copy1);
|
2009-12-23 04:46:09 +01:00
|
|
|
|
copy1 = *copy1; // self-assign also detected in this case
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (otherVal == *copy2);
|
2009-12-21 07:52:58 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (copy1);
|
2009-12-21 05:44:29 +01:00
|
|
|
|
copy1.reset();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!copy1);
|
|
|
|
|
|
CHECK (empty == copy1);
|
|
|
|
|
|
CHECK (copy2 != copy1);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
VERIFY_ERROR (BOTTOM_VALUE, *copy1 );
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
/** @test verify that ctor and dtor calls are balanced,
|
|
|
|
|
|
* even when assigning and self-assigning.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verifySaneInstanceHandling()
|
|
|
|
|
|
{
|
|
|
|
|
|
cntTracker = 0;
|
|
|
|
|
|
{
|
|
|
|
|
|
Tracker t1;
|
|
|
|
|
|
Tracker t2;
|
|
|
|
|
|
|
|
|
|
|
|
verifyWrapper<Tracker> (t1, t2);
|
|
|
|
|
|
verifyWrapper<Tracker&> (t1, t2);
|
|
|
|
|
|
verifyWrapper<Tracker*> (&t1, &t2);
|
2010-12-17 22:51:27 +01:00
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
}
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (0 == cntTracker);
|
2009-12-21 07:52:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-02 04:35:16 +01:00
|
|
|
|
/** @test proper handling of move and rvalue references */
|
|
|
|
|
|
void
|
|
|
|
|
|
verifySaneMoveHandling()
|
|
|
|
|
|
{
|
|
|
|
|
|
using Data = shared_ptr<int>;
|
|
|
|
|
|
using Wrap = ItemWrapper<Data>;
|
|
|
|
|
|
|
|
|
|
|
|
Data data{new int(12345)};
|
|
|
|
|
|
CHECK (1 == data.use_count());
|
|
|
|
|
|
|
|
|
|
|
|
Wrap wrap{data};
|
|
|
|
|
|
CHECK (2 == data.use_count());
|
|
|
|
|
|
CHECK (12345 == **wrap);
|
|
|
|
|
|
CHECK (isSameObject (*data, **wrap));
|
|
|
|
|
|
CHECK (!isSameObject (data, *wrap));
|
|
|
|
|
|
|
|
|
|
|
|
Wrap wcopy{wrap};
|
|
|
|
|
|
CHECK (3 == data.use_count());
|
|
|
|
|
|
|
|
|
|
|
|
Wrap wmove{move (wcopy)};
|
|
|
|
|
|
CHECK (3 == data.use_count());
|
|
|
|
|
|
CHECK (not wcopy);
|
|
|
|
|
|
CHECK (wmove);
|
|
|
|
|
|
|
|
|
|
|
|
wcopy = move(wmove);
|
|
|
|
|
|
CHECK (3 == data.use_count());
|
|
|
|
|
|
CHECK (not wmove);
|
|
|
|
|
|
CHECK (wcopy);
|
|
|
|
|
|
|
|
|
|
|
|
Wrap wmove2{move (data)};
|
|
|
|
|
|
CHECK (0 == data.use_count());
|
|
|
|
|
|
CHECK (3 == wmove2->use_count());
|
|
|
|
|
|
CHECK (not data);
|
|
|
|
|
|
CHECK (wmove2);
|
|
|
|
|
|
CHECK (wrap);
|
|
|
|
|
|
|
|
|
|
|
|
wmove2 = move (wcopy);
|
|
|
|
|
|
CHECK (2 == wmove2->use_count());
|
|
|
|
|
|
CHECK (not wcopy);
|
|
|
|
|
|
CHECK (wmove2);
|
|
|
|
|
|
CHECK (wrap);
|
|
|
|
|
|
|
|
|
|
|
|
wmove2 = move (wrap);
|
|
|
|
|
|
CHECK (1 == wmove2->use_count());
|
|
|
|
|
|
CHECK (not wrap);
|
|
|
|
|
|
CHECK (wmove2);
|
|
|
|
|
|
|
|
|
|
|
|
wmove2 = move (wmove);
|
|
|
|
|
|
CHECK (not wcopy);
|
|
|
|
|
|
CHECK (not wmove);
|
|
|
|
|
|
CHECK (not wmove2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-21 07:52:58 +01:00
|
|
|
|
/** @test verify especially that we can wrap and handle
|
|
|
|
|
|
* a reference "value" in a pointer-like manner
|
|
|
|
|
|
*/
|
2009-12-20 04:33:24 +01:00
|
|
|
|
void
|
|
|
|
|
|
verifyWrappedRef ()
|
|
|
|
|
|
{
|
|
|
|
|
|
int x = 5;
|
|
|
|
|
|
ItemWrapper<int&> refWrap;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!refWrap);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
refWrap = x;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (refWrap);
|
|
|
|
|
|
CHECK (5 == *refWrap);
|
|
|
|
|
|
CHECK (x == *refWrap);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
*refWrap += 5;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (x == 10);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
ItemWrapper<int*> ptrWrap (& *refWrap);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( isSameObject (**ptrWrap, x));
|
|
|
|
|
|
CHECK (!isSameObject ( *ptrWrap, &x));
|
2009-12-20 04:33:24 +01:00
|
|
|
|
**ptrWrap += 13;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (x == 23);
|
2009-12-20 04:33:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-13 02:23:23 +01:00
|
|
|
|
static auto produceResult() { return rani(); }
|
|
|
|
|
|
|
2009-12-23 01:36:11 +01:00
|
|
|
|
/** @test verify an extension built on top of the ItemWrapper:
|
2009-12-25 05:24:49 +01:00
|
|
|
|
* a function which remembers the last result. As a simple test,
|
2024-11-13 02:23:23 +01:00
|
|
|
|
* we bind a static helper function to produce a random value
|
|
|
|
|
|
* and remember the result returned last.
|
2009-12-25 05:24:49 +01:00
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verifyFunctionResult()
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
FunctionResult<int(void)> randomVal (produceResult);
|
2009-12-25 05:24:49 +01:00
|
|
|
|
|
|
|
|
|
|
// function was never invoked, thus the remembered result is NIL
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!randomVal);
|
2009-12-25 05:24:49 +01:00
|
|
|
|
VERIFY_ERROR (BOTTOM_VALUE, *randomVal );
|
|
|
|
|
|
|
|
|
|
|
|
int v1 = randomVal();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (v1 == *randomVal);
|
|
|
|
|
|
CHECK (v1 == *randomVal);
|
|
|
|
|
|
CHECK (v1 == *randomVal);
|
|
|
|
|
|
CHECK (randomVal);
|
2009-12-25 05:24:49 +01:00
|
|
|
|
|
|
|
|
|
|
int v2;
|
|
|
|
|
|
do v2 = randomVal();
|
|
|
|
|
|
while (v1 == v2);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (v2 == *randomVal);
|
|
|
|
|
|
CHECK (v2 == *randomVal);
|
|
|
|
|
|
CHECK (v1 != *randomVal);
|
2009-12-25 05:24:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test verify an extension built on top of the ItemWrapper:
|
|
|
|
|
|
* a function which remembers the last result. Here we use
|
|
|
|
|
|
* a test function, which picks a member of an vector and
|
2009-12-23 04:46:09 +01:00
|
|
|
|
* returns a \em reference to it. Thus the cached "result"
|
|
|
|
|
|
* can be used to access and change the values within the
|
|
|
|
|
|
* original vector. In a real world usage scenario, such a
|
|
|
|
|
|
* function could be an (expensive) data structure access.
|
|
|
|
|
|
*/
|
2009-12-23 01:36:11 +01:00
|
|
|
|
void
|
2009-12-25 05:24:49 +01:00
|
|
|
|
verifyFunctionRefResult()
|
2009-12-23 01:36:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
vector<int> testVec;
|
|
|
|
|
|
for (uint i=0; i<10; ++i)
|
|
|
|
|
|
testVec.push_back(i);
|
|
|
|
|
|
|
2009-12-23 04:46:09 +01:00
|
|
|
|
FunctionResult<int&(size_t)> funRes (pickElement_ofVector(testVec));
|
2009-12-23 01:36:11 +01:00
|
|
|
|
|
|
|
|
|
|
// function was never invoked, thus the remembered result is NIL
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!funRes);
|
2009-12-23 01:36:11 +01:00
|
|
|
|
VERIFY_ERROR (BOTTOM_VALUE, *funRes );
|
|
|
|
|
|
|
|
|
|
|
|
int& r5 = funRes (5);
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (funRes); // indicates existence of cached result
|
2009-12-25 05:24:49 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (5 == r5);
|
|
|
|
|
|
CHECK (isSameObject (r5, testVec[5]));
|
2009-12-23 01:36:11 +01:00
|
|
|
|
|
2009-12-23 04:46:09 +01:00
|
|
|
|
int& r5x = *funRes;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isSameObject (r5, r5x));
|
2009-12-23 01:36:11 +01:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( isSameObject (r5, *funRes));
|
2009-12-23 04:46:09 +01:00
|
|
|
|
int& r7 = funRes (7);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!isSameObject (r5, *funRes));
|
|
|
|
|
|
CHECK ( isSameObject (r7, *funRes));
|
2009-12-23 01:36:11 +01:00
|
|
|
|
|
|
|
|
|
|
-- r5x;
|
|
|
|
|
|
++ *funRes;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (5-1 == testVec[5]);
|
|
|
|
|
|
CHECK (7+1 == testVec[7]);
|
|
|
|
|
|
CHECK (7+1 == r7);
|
2009-12-23 01:36:11 +01:00
|
|
|
|
}
|
2009-12-20 04:33:24 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LAUNCHER (ItemWrapper_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-21 05:44:29 +01:00
|
|
|
|
}}} // namespace lib::wrapper::test
|
2009-12-20 04:33:24 +01:00
|
|
|
|
|