* 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.
295 lines
8.9 KiB
C++
295 lines
8.9 KiB
C++
/*
|
||
ReplaceableItem(Test) - adapter to take snapshot from non-assignable values
|
||
|
||
Copyright (C)
|
||
2017, Hermann Vosseler <Ichthyostega@web.de>
|
||
|
||
**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.
|
||
|
||
* *****************************************************************/
|
||
|
||
|
||
|
||
#include "lib/test/run.hpp"
|
||
#include "lib/test/test-helper.hpp"
|
||
#include "lib/util.hpp"
|
||
#include "lib/time/timevalue.hpp"
|
||
|
||
#include "lib/replaceable-item.hpp"
|
||
|
||
|
||
|
||
|
||
namespace lib {
|
||
namespace wrapper {
|
||
namespace test{
|
||
|
||
using ::Test;
|
||
using lib::test::randStr;
|
||
using lib::test::randTime;
|
||
using util::isSameObject;
|
||
|
||
using time::Time;
|
||
using time::Duration;
|
||
using std::string;
|
||
using std::swap;
|
||
|
||
|
||
|
||
namespace { // Test helper: yet another ctor/dtor counting class
|
||
|
||
long cntTracker = 0;
|
||
|
||
struct Tracker
|
||
{
|
||
uint i_;
|
||
|
||
Tracker() : i_(rani(500)) { ++cntTracker; }
|
||
Tracker(Tracker const& ot) : i_(ot.i_) { ++cntTracker; }
|
||
Tracker(uint i) : i_(i) { ++cntTracker; }
|
||
~Tracker() { --cntTracker; }
|
||
};
|
||
|
||
struct NonAssign
|
||
: Tracker
|
||
{
|
||
using Tracker::Tracker;
|
||
NonAssign& operator= (NonAssign const&) =delete;
|
||
};
|
||
|
||
|
||
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_; }
|
||
|
||
} // (END) Test helpers
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/***************************************************************************//**
|
||
* @test scrutinise an adapter to snapshot non-assignable values.
|
||
* - create instantiations for various types
|
||
* - both assignable and non-assignable types
|
||
* - empty-construct and copy construct the adapter
|
||
* - perform assignments and even content swapping
|
||
* - use counting to verify proper instance management
|
||
* - compare by delegating to element comparison
|
||
*
|
||
* @see replaceable-item.hpp
|
||
* @see steam::control::MementoTie
|
||
*/
|
||
class ReplaceableItem_test : public Test
|
||
{
|
||
|
||
|
||
virtual void
|
||
run (Arg)
|
||
{
|
||
seedRand();
|
||
ulong l1 (1 + rani(1000));
|
||
ulong l2 (1 + rani(1000));
|
||
string s1 (randStr(50));
|
||
string s2 (randStr(50));
|
||
const char* cp (s1.c_str());
|
||
|
||
Time t1{randTime()}, t2{randTime()};
|
||
Duration d1{randTime()}, d2{randTime()};
|
||
|
||
verifyUsage<ulong> (l1, l2);
|
||
verifyUsage<ulong*> (&l1, &l2);
|
||
|
||
verifyUsage<string> (s1, s2);
|
||
verifyUsage<string*> (&s1, &s2);
|
||
|
||
verifyUsage<string> (s2, cp);
|
||
verifyUsage<string> (cp, "Lumiera");
|
||
verifyUsage<const char*> (cp, "Lumiera");
|
||
|
||
// non-assignable types...
|
||
verifyUsage<Time> (t1, t2);
|
||
verifyUsage<Time> (t1, d1);
|
||
verifyUsage<Duration> (d1, t2);
|
||
verifyUsage<Duration> (d1, d2);
|
||
|
||
verifyNonComparableElements();
|
||
verifyOnlyMoveConstructible();
|
||
verifySaneInstanceHandling();
|
||
verifyWrappedPtr ();
|
||
}
|
||
|
||
|
||
template<typename X, typename Y>
|
||
void
|
||
verifyUsage (X she, Y he)
|
||
{
|
||
using It = ReplaceableItem<X>;
|
||
|
||
It one{she}, two{he};
|
||
REQUIRE (one != two);
|
||
CHECK (two == he);
|
||
CHECK (one == she);
|
||
CHECK (sizeof(one) == sizeof(X));
|
||
CHECK (sizeof(two) == sizeof(X));
|
||
|
||
It copy1{she};
|
||
It copy2;
|
||
|
||
CHECK (one == copy1);
|
||
CHECK (one != copy2);
|
||
CHECK (two != copy1);
|
||
CHECK (two != copy2);
|
||
|
||
CHECK (copy2 == NullValue<X>::get());
|
||
|
||
copy2 = he; // assign from value
|
||
CHECK (one == copy1);
|
||
CHECK (one != copy2);
|
||
CHECK (two != copy1);
|
||
CHECK (two == copy2);
|
||
|
||
std::swap (copy1, copy2); // possibly move construction / move assignment
|
||
CHECK (one != copy1);
|
||
CHECK (one == copy2);
|
||
CHECK (two == copy1);
|
||
CHECK (two != copy2);
|
||
|
||
copy1 = copy1; // self assignment (is skipped)
|
||
copy2 = one; // assignment of an identical value
|
||
|
||
CHECK (copy1 == he);
|
||
CHECK (copy2 == she);
|
||
CHECK (one == she);
|
||
CHECK (two == he);
|
||
|
||
copy1 = It{}; // copy assignment from anonymous holder
|
||
CHECK (copy1 == NullValue<X>::get());
|
||
CHECK (copy1 != he);
|
||
};
|
||
|
||
|
||
/** @test verify that ctor and dtor calls are balanced,
|
||
* even when assigning and self-assigning.
|
||
* @note Tracker uses the simple implementation for assignable values,
|
||
* while NonAssign uses the embedded-buffer implementation
|
||
*/
|
||
void
|
||
verifySaneInstanceHandling()
|
||
{
|
||
cntTracker = 0;
|
||
{
|
||
Tracker t1;
|
||
Tracker t2;
|
||
|
||
verifyUsage<Tracker> (t1, t2);
|
||
verifyUsage<Tracker*> (&t1, &t2);
|
||
verifyUsage<Tracker> (t1, t2.i_);
|
||
verifyUsage<Tracker, Tracker&> (t1, t2);
|
||
|
||
NonAssign u1;
|
||
NonAssign u2;
|
||
verifyUsage<NonAssign> (u1, u2);
|
||
verifyUsage<NonAssign*> (&u1, &u2);
|
||
verifyUsage<NonAssign> (u1, u2.i_);
|
||
verifyUsage<NonAssign, NonAssign&> (u1, u2);
|
||
verifyUsage<Tracker> (u1, u2);
|
||
}
|
||
CHECK (2 == cntTracker); // surviving singleton instances
|
||
} // NullValue<Tracker> and NullValue<NonAssign>
|
||
|
||
|
||
/** @test verify especially that we can handle and re-"assign" an embedded pointer */
|
||
void
|
||
verifyWrappedPtr ()
|
||
{
|
||
int x = 5;
|
||
ReplaceableItem<int*> ptrWrap;
|
||
CHECK (ptrWrap.get() == NULL);
|
||
|
||
ptrWrap = &x;
|
||
CHECK (5 == *ptrWrap.get());
|
||
CHECK (&x == ptrWrap.get());
|
||
|
||
*ptrWrap.get() += 5;
|
||
CHECK (x == 10);
|
||
|
||
CHECK ( isSameObject (*ptrWrap.get(), x));
|
||
CHECK (!isSameObject ( ptrWrap.get(), x));
|
||
}
|
||
|
||
|
||
|
||
/** @test verify we can handle elements without comparison operator */
|
||
void
|
||
verifyNonComparableElements ()
|
||
{
|
||
struct Wrap
|
||
{
|
||
int i = -10 + rani(21);
|
||
};
|
||
|
||
ReplaceableItem<Wrap> w1 =Wrap{},
|
||
w2 =Wrap{};
|
||
|
||
int i = w1.get().i,
|
||
j = w2.get().i;
|
||
|
||
swap (w1,w2);
|
||
|
||
CHECK (i == w2.get().i);
|
||
CHECK (j == w1.get().i);
|
||
|
||
// w1 == w2; // does not compile since comparison of Wraps is undefined
|
||
}
|
||
|
||
|
||
|
||
/** @test handle elements that allow nothing but move construction
|
||
* @todo conceptually, the whole point of this container is the ability
|
||
* to snapshot elements which allow nothing but move construction.
|
||
* Seemingly, GCC-4.9 does not fully implement perfect forwarding ///////////////////////TICKET #1059 : re-visit with never compiler
|
||
*/
|
||
void
|
||
verifyOnlyMoveConstructible ()
|
||
{
|
||
struct Cagey
|
||
{
|
||
int i = -10 + rani(21);
|
||
|
||
Cagey(Cagey && privy)
|
||
: i(55)
|
||
{
|
||
swap (i, privy.i);
|
||
}
|
||
// Cagey(Cagey const&) =delete; //////////////////////////////////////////TICKET #1059 : should be deleted for this test to make any sense
|
||
Cagey(Cagey const&) =default;
|
||
Cagey() =default;
|
||
};
|
||
|
||
ReplaceableItem<Cagey> uc1 {std::move (Cagey{})},
|
||
uc2 {std::move (Cagey{})};
|
||
|
||
int i = uc1.get().i,
|
||
j = uc2.get().i;
|
||
|
||
swap (uc1,uc2); //////////////////////////////////////////TICKET #1059
|
||
|
||
CHECK (i == uc2.get().i);
|
||
CHECK (j == uc1.get().i);
|
||
|
||
ReplaceableItem<Cagey> occult {std::move (uc1)}; //////////////////////////////////////////TICKET #1059 : should use the move ctor but uses the copy ctor
|
||
CHECK (j == occult.get().i);
|
||
// CHECK (55 == uc1.get().i); //////////////////////////////////////////TICKET #1059
|
||
}
|
||
};
|
||
|
||
LAUNCHER (ReplaceableItem_test, "unit common");
|
||
|
||
|
||
}}} // namespace lib::wrapper::test
|
||
|