* 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.
182 lines
7.3 KiB
C++
182 lines
7.3 KiB
C++
/*
|
||
SpecialJobFun(Test) - verify a disposable configurable job functor
|
||
|
||
Copyright (C)
|
||
2023, 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.
|
||
|
||
* *****************************************************************/
|
||
|
||
/** @file special-job-fun-test.cpp
|
||
** unit test \ref SpecialJobFun_test
|
||
*/
|
||
|
||
|
||
#include "lib/test/run.hpp"
|
||
#include "lib/test/test-helper.hpp"
|
||
//#include "vault/real-clock.hpp"
|
||
//#include "lib/time/timevalue.hpp"
|
||
#include "vault/gear/special-job-fun.hpp"
|
||
#include "lib/format-cout.hpp" ////////////////////////////////////TODO Moo-oh
|
||
#include "lib/test/diagnostic-output.hpp"//////////////////////////TODO TOD-oh
|
||
#include "lib/test/tracking-dummy.hpp"
|
||
//#include "lib/util.hpp"
|
||
|
||
//#include <array>
|
||
//#include <functional>
|
||
|
||
//using lib::time::Time;
|
||
//using lib::time::FSecs;
|
||
|
||
//using util::isnil;
|
||
//using util::isSameObject;
|
||
//using lib::test::randStr;
|
||
//using lib::test::randTime;
|
||
using lib::test::Dummy;
|
||
//using std::array;
|
||
|
||
|
||
namespace vault{
|
||
namespace gear {
|
||
namespace test {
|
||
|
||
namespace { // shorthands and parameters for test...
|
||
|
||
|
||
}//(End)test definitions
|
||
|
||
|
||
|
||
|
||
/*****************************************************************//**
|
||
* @test verify a self-managing one-time render job functor.
|
||
* @see TestChainLoad_test::usageExample
|
||
* @see TestChainLoad::ScheduleCtx::continuation()
|
||
* @see special-job-fun.hpp
|
||
*/
|
||
class SpecialJobFun_test : public Test
|
||
{
|
||
|
||
virtual void
|
||
run (Arg)
|
||
{
|
||
simpleUsage();
|
||
verifyLifecycle();
|
||
}
|
||
|
||
|
||
/** @test demonstrate simple usage by λ-binding
|
||
* @todo WIP 12/23 🔁 define ⟶ ✔ implement
|
||
*/
|
||
void
|
||
simpleUsage()
|
||
{
|
||
bool hit{false}; // create directly from λ
|
||
SpecialJobFun specialFun{[&](JobParameter){ hit=true; }};
|
||
|
||
CHECK (specialFun);
|
||
Job funJob{specialFun
|
||
,InvocationInstanceID()
|
||
,Time::ANYTIME
|
||
};
|
||
|
||
funJob.triggerJob();
|
||
CHECK (hit);
|
||
CHECK (not specialFun);
|
||
}
|
||
|
||
|
||
|
||
/** @test verify storage and lifecycle management
|
||
* - use a instance-tracking marker implanted into the functor
|
||
* - verify no memory is leaked and the tracker instance is deallocated
|
||
* - verify the single tracker instance indeed lives in the JobFunctor
|
||
* - investigate the usage count of the front-end handle
|
||
* - verify the front-end can be copied without impact on the JobFunctor
|
||
* - verify the heap allocated functor keeps itself alive
|
||
* even when the front-end handle is already gone.
|
||
* - verify the functor de-allocates itself after latst invocation
|
||
* @todo WIP 12/23 ✔ define ⟶ ✔ implement
|
||
*/
|
||
void
|
||
verifyLifecycle()
|
||
{
|
||
CHECK (0 == Dummy::checksum());
|
||
{ // Note: ▽▽▽ tracker in λ-closure
|
||
SpecialJobFun funTrack{[tracker=Dummy(23)]
|
||
(JobParameter param) mutable
|
||
{
|
||
int mark = param.invoKey.part.a;
|
||
tracker.setVal (mark);
|
||
}}; // △△△ invocation should alter checksum
|
||
|
||
// one Dummy instance was implanted
|
||
CHECK (23 == Dummy::checksum());
|
||
InvocationInstanceID hiddenMessage;
|
||
hiddenMessage.part.a = 55;
|
||
Job funJob{funTrack
|
||
,hiddenMessage
|
||
,Time::ANYTIME
|
||
};
|
||
|
||
CHECK (23 == Dummy::checksum());
|
||
funJob.triggerJob();
|
||
CHECK (55 == Dummy::checksum()); // the `funJob` front-end handle still keeps it alive
|
||
} // but when this front-end goes out of scope...
|
||
CHECK (0 == Dummy::checksum()); // the implanted tracker is also gone
|
||
|
||
{ // another investigation with the same technique...
|
||
auto trackingLambda = [tracker=Dummy(23)]
|
||
(JobParameter param) mutable
|
||
{
|
||
int mark = param.invoKey.part.a;
|
||
tracker.setVal (mark);
|
||
};
|
||
CHECK (23 == Dummy::checksum());
|
||
|
||
SpecialJobFun frontEnd{move(trackingLambda)}; // this time the λ is moved in..
|
||
CHECK (23 == Dummy::checksum()); // the embedded tracker was copied into the Functor in heap memory
|
||
CHECK (2 == frontEnd.use_count()); // Note: both the front-end and the Functor in heap hold a use-reference
|
||
|
||
auto otherHandle = frontEnd; // copy of front-end...
|
||
CHECK (3 == frontEnd.use_count()); // ...so there are three usages of the front-end handle now
|
||
CHECK (23 == Dummy::checksum()); // ...but only one tracker instance (in heap)
|
||
|
||
frontEnd = SpecialJobFun(); // re-assign one front-end handle with an empty instance
|
||
CHECK (0 == frontEnd.use_count()); // thus `frontEnd` is no longer attached to the active instance
|
||
CHECK (2 == otherHandle.use_count()); // but the other copy still is
|
||
CHECK (not frontEnd);
|
||
CHECK (otherHandle);
|
||
|
||
InvocationInstanceID hiddenMessage;
|
||
hiddenMessage.part.a = 55;
|
||
Job funJob{otherHandle // Note: don't pass the handle here, rather a JobFunctor& is extracted
|
||
,hiddenMessage
|
||
,Time::ANYTIME
|
||
};
|
||
|
||
CHECK (2 == otherHandle.use_count());
|
||
CHECK (23 == Dummy::checksum());
|
||
|
||
otherHandle = SpecialJobFun(); // now kill even the last front-end handle we had
|
||
CHECK (0 == otherHandle.use_count()); // thus _we_ have no way to reach the Functor in heap
|
||
CHECK (23 == Dummy::checksum()); // yet it stays alive, since it was not invoked yet
|
||
|
||
funJob.triggerJob(); // after invocation, the Functor in heap memory self-destructs
|
||
CHECK (0 == Dummy::checksum()); // since it did hold the last reference
|
||
}
|
||
CHECK (0 == Dummy::checksum());
|
||
}
|
||
};
|
||
|
||
|
||
/** Register this test class... */
|
||
LAUNCHER (SpecialJobFun_test, "unit engine");
|
||
|
||
|
||
|
||
}}} // namespace vault::gear::test
|