LUMIERA.clone/tests/library/custom-shared-ptr-test.cpp

326 lines
10 KiB
C++
Raw Normal View History

2010-04-02 21:51:39 +02:00
/*
CustomSharedPtr(Test) - ref counting, equality and comparisons
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)
2008, 2010, Hermann Vosseler <Ichthyostega@web.de>
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.
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
* *****************************************************************/
2010-04-02 21:51:39 +02:00
/** @file custom-shared-ptr-test.cpp
** unit test \ref CustomSharedPtr_test
*/
2010-04-02 21:51:39 +02:00
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/util.hpp"
#include "lib/p.hpp"
namespace lib {
namespace test{
using std::string;
using std::shared_ptr;
using std::weak_ptr;
2010-04-02 21:51:39 +02:00
using lumiera::error::LUMIERA_ERROR_ASSERTION;
struct X
2010-04-02 21:51:39 +02:00
{
long x_;
explicit X(long x=0) : x_(x) {}
2010-04-02 21:51:39 +02:00
operator long () { return x_; }
std::strong_ordering operator<=>(X const&) const = default;
2010-04-02 21:51:39 +02:00
virtual ~X() {} // using RTTI
};
struct XX : public X
{
long xx_;
XX(long x=0) : X(x), xx_(x+1) {}
};
/************************************************************//**
2010-04-02 21:51:39 +02:00
* @test assure correct behaviour of lumiera's custom shared-ptr,
* including ADL of operators, shared ownership, typing
* and ordering
* @see lumiera::P
*/
class CustomSharedPtr_test : public Test
{
virtual void
run (Arg)
2010-04-02 21:51:39 +02:00
{
check_refcounting ();
check_shared_ownership ();
check_ownership_transfer ();
2010-04-02 21:51:39 +02:00
check_type_relations ();
check_ordering ();
}
/** @test smart-ptr basic behaviour */
void
check_refcounting ()
{
P<X> p1 (new X(7));
CHECK (p1);
CHECK (1 == p1.use_count());
CHECK (7 == p1->x_);
2010-04-02 21:51:39 +02:00
{
P<X> p2 (new X(9));
CHECK (1 == p2.use_count());
2010-04-02 21:51:39 +02:00
p2.swap (p1);
CHECK (1 == p1.use_count());
CHECK (1 == p2.use_count());
2010-04-02 21:51:39 +02:00
p2 = p1;
CHECK (2 == p1.use_count());
CHECK (2 == p2.use_count());
2010-04-02 21:51:39 +02:00
}
CHECK (1 == p1.use_count());
CHECK (9 == p1->x_);
2010-04-02 21:51:39 +02:00
p1.reset();
CHECK (0 == p1.use_count());
CHECK (!p1);
2010-04-02 21:51:39 +02:00
}
/** @test cooperation with other shared-ptr types */
void
check_shared_ownership ()
{
P<X> pX (new X(22));
CHECK (pX);
CHECK (1 == pX.use_count());
2010-04-02 21:51:39 +02:00
weak_ptr<X> wX (pX);
CHECK (wX.lock());
CHECK (1 == pX.use_count());
2010-04-02 21:51:39 +02:00
shared_ptr<X> sp1 (wX);
shared_ptr<X> sp2 (pX);
2010-04-02 21:51:39 +02:00
shared_ptr<X> sp3; sp3 = pX;
CHECK (22 == sp3->x_);
CHECK (4 == pX.use_count());
CHECK (*pX == *sp1);
CHECK (*sp1 == *sp2);
CHECK (*sp2 == *sp3);
2010-04-02 21:51:39 +02:00
P<X> pX2;
pX2.swap(pX);
CHECK (!pX);
CHECK (0 == pX.use_count());
CHECK (4 == pX2.use_count());
2010-04-02 21:51:39 +02:00
2016-12-23 04:23:03 +01:00
P<X, P<X>> pXX (pX2); // a different type, but compatible pointers
2010-04-02 21:51:39 +02:00
pX2 = pX;
CHECK (!pX2);
CHECK (0 == pX2.use_count());
CHECK (4 == pXX.use_count());
2010-04-02 21:51:39 +02:00
sp3 = sp2 = sp1 = pX;
CHECK (22 == pXX->x_);
CHECK (1 == pXX.use_count());
CHECK (!sp1);
CHECK (!sp2);
CHECK (!sp3);
2010-04-02 21:51:39 +02:00
CHECK (22 == wX.lock()->x_);
CHECK (1 == pXX.use_count());
2010-04-02 21:51:39 +02:00
pXX.reset();
CHECK (!pXX);
CHECK (!wX.lock());
2010-04-02 21:51:39 +02:00
}
void
check_ownership_transfer ()
{
std::unique_ptr<X> up (new X(23));
CHECK (up.get());
P<X> pX (std::move(up));
CHECK (!up.get());
CHECK (pX);
CHECK (1 == pX.use_count());
CHECK (23 == pX->x_);
}
2010-04-02 21:51:39 +02:00
/** @test building type relationships on smart-ptrs */
void
check_type_relations ()
{
2016-12-23 04:23:03 +01:00
P<X> pX; // Base: shared_ptr<X>
P<XX> pX1; // Base: shared_ptr<XX>
P<XX,P<X>> pX2; // Base: P<X>
P<XX,shared_ptr<X>> pX3; // Base: shared_ptr<X>
P<XX,shared_ptr<long>> pLo;// Base: shared_ptr<long> (rather nonsense, but well...)
P<X,string> pLoL; // Base: std::string
P<string> pLoLoL; // Base: shared_ptr<string>
2010-04-02 21:51:39 +02:00
CHECK (INSTANCEOF (shared_ptr<X>, &pX));
2010-04-02 21:51:39 +02:00
CHECK ( INSTANCEOF (shared_ptr<XX>, &pX1));
// CHECK (!INSTANCEOF (shared_ptr<X>, &pX1)); // doesn't compile (no RTTI) -- that's correct
// CHECK (!INSTANCEOF (P<X>, &pX1)); // similar, type mismatch detected by compiler
2010-04-02 21:51:39 +02:00
CHECK ( INSTANCEOF (shared_ptr<X>, &pX2));
// CHECK (!INSTANCEOF (shared_ptr<XX>, &pX2));
CHECK ( INSTANCEOF (P<X>, &pX2));
2010-04-02 21:51:39 +02:00
CHECK ( INSTANCEOF (shared_ptr<X>, &pX3));
// CHECK (!INSTANCEOF (shared_ptr<XX>, &pX3));
// CHECK (!INSTANCEOF (P<X>, &pX3));
2010-04-02 21:51:39 +02:00
CHECK ( INSTANCEOF (shared_ptr<long>, &pLo));
// CHECK (!INSTANCEOF (shared_ptr<X>, &pLo));
// CHECK (!INSTANCEOF (P<X>, &pLo));
2010-04-02 21:51:39 +02:00
// CHECK (!INSTANCEOF (shared_ptr<long>, &pLoL));
// CHECK (!INSTANCEOF (shared_ptr<X>, &pLoL));
// CHECK (!INSTANCEOF (P<X>, &pLoL));
CHECK ( INSTANCEOF (string, &pLoL));
2010-04-02 21:51:39 +02:00
CHECK ( INSTANCEOF (shared_ptr<string>, &pLoLoL));
// CHECK (!INSTANCEOF (string, &pLoLoL));
// CHECK (!INSTANCEOF (shared_ptr<X>, &pLoLoL));
2010-04-02 21:51:39 +02:00
pX = pX1; // OK: pointee subtype...
pX = pX2; // invokes shared_ptr<X>::operator= (shared_ptr<X> const&)
pX = pX3;
// pX = pLo; // similar, but long* not assignable to X*
// pX = pLoL; // similar, but string* not assignable to X*
// pX = pLoLoL; // same...
// you won't be able to do much with the "LoLo"-Types,
2010-04-02 21:51:39 +02:00
// as their types and pointee types's relations don't match
pX.reset (new XX(5));
CHECK (5 == *pX); // implicit conversion from X to long
2010-04-02 21:51:39 +02:00
pX2 = pX; // works, because both are implemented in terms of shared_ptr<X>
CHECK (5 == pX2->x_);
CHECK (6 == pX2->xx_); // using the XX interface (performing dynamic downcast)
2010-04-02 21:51:39 +02:00
pX3.reset (new X(7)); // again works because implemented in terms of shared_ptr<X>
pX2 = pX3; // same
CHECK (pX2); // both contain indeed a valid pointer....
CHECK (pX3);
CHECK (! pX2.get()); // but dynamic cast to XX at access fails
CHECK (! pX3.get());
2010-04-02 21:51:39 +02:00
}
/** @test equality and ordering operators forwarded to pointee */
void
check_ordering ()
{
typedef P<X> PX;
typedef P<XX,PX> PXX;
PX pX1 (new X(3));
PX pX2 (new XX(5));
PX pX3, pX4, pX5, pX6;
PXX pXX (new XX(7));
pX3 = pXX;
pX4.reset(new X(*pXX));
CHECK ( (pX1 == pX1)); // reflexivity
CHECK (!(pX1 != pX1));
CHECK (!(pX1 < pX1));
CHECK (!(pX1 > pX1));
CHECK ( (pX1 <= pX1));
CHECK ( (pX1 >= pX1));
CHECK (!(pX1 == pX2)); // compare to same ptr type with larger pointee of subtype
CHECK ( (pX1 != pX2));
CHECK ( (pX1 < pX2));
CHECK (!(pX1 > pX2));
CHECK ( (pX1 <= pX2));
CHECK (!(pX1 >= pX2));
CHECK (!(pX2 == pXX)); // compare to ptr subtype with larger pointee of same subtype
CHECK ( (pX2 != pXX));
CHECK ( (pX2 < pXX));
CHECK (!(pX2 > pXX));
CHECK ( (pX2 <= pXX));
CHECK (!(pX2 >= pXX));
CHECK (!(pX1 == pXX)); // transitively compare to ptr subtype with larger pointee of subtype
CHECK ( (pX1 != pXX));
CHECK ( (pX1 < pXX));
CHECK (!(pX1 > pXX));
CHECK ( (pX1 <= pXX));
CHECK (!(pX1 >= pXX));
CHECK ( (pX3 == pXX)); // compare ptr to subtype ptr both referring to same pointee
CHECK (!(pX3 != pXX));
CHECK (!(pX3 < pXX));
CHECK (!(pX3 > pXX));
CHECK ( (pX3 <= pXX));
CHECK ( (pX3 >= pXX));
CHECK ( (pX4 == pXX)); // compare ptr to subtype ptr both referring to different but equal pointees
CHECK (!(pX4 != pXX));
CHECK (!(pX4 < pXX));
CHECK (!(pX4 > pXX));
CHECK ( (pX4 <= pXX));
CHECK ( (pX4 >= pXX));
CHECK (!(pXX == pX5)); // compare subtype ptr to empty ptr: "unequal but not orderable"
CHECK ( (pXX != pX5));
CHECK ( (pX5 == pX6)); // compare two empty ptrs: "equal, equivalent but not orderable"
CHECK (!(pX5 != pX6));
2010-04-02 21:51:39 +02:00
// order relations on NIL pointers disallowed
#if false ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
VERIFY_ERROR (ASSERTION, pXX < pX5 );
VERIFY_ERROR (ASSERTION, pXX > pX5 );
VERIFY_ERROR (ASSERTION, pXX <= pX5 );
VERIFY_ERROR (ASSERTION, pXX >= pX5 );
VERIFY_ERROR (ASSERTION, pX5 < pXX );
VERIFY_ERROR (ASSERTION, pX5 > pXX );
VERIFY_ERROR (ASSERTION, pX5 <= pXX );
VERIFY_ERROR (ASSERTION, pX5 >= pXX );
VERIFY_ERROR (ASSERTION, pX5 < pX6 );
VERIFY_ERROR (ASSERTION, pX5 > pX6 );
VERIFY_ERROR (ASSERTION, pX5 <= pX6 );
VERIFY_ERROR (ASSERTION, pX5 >= pX6 );
#endif ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
}
};
/** Register this test class... */
LAUNCHER (CustomSharedPtr_test, "unit common");
}} // namespace lib::test