lumiera_/tests/basics/diagnostic-context-test.cpp
Ichthyostega 806db414dd 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

218 lines
6.3 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
DiagnosticContext(Test) - verify thread local stack for collecting diagnostics
Copyright (C)
2011, 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 diagnostic-context-test.cpp
** unit test \ref DiagnosticContext_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/diagnostic-context.hpp"
#include "lib/iter-explorer.hpp"
#include "lib/thread.hpp"
#include <vector>
#include <chrono>
#include <array>
using std::this_thread::sleep_for;
using std::chrono_literals::operator ""us;
namespace lib {
namespace test{
namespace error = lumiera::error;
namespace { // private test setup...
/* WARNING: memory hungry */
const uint NUM_THREADS = 75;
const uint MAX_RAND = 100*1000;
auto isOdd = [](auto val) { return bool (val % 2); };
} // (End) test setup....
using lib::ThreadJoinable;
using LERR_(LOGIC);
using std::rand;
/**
* Subject of this test:
* a thread-local stack of int values
*/
typedef DiagnosticContext<uint> Marker;
typedef std::vector<uint> VecI;
/******************************************************************************//**
* @test verify a diagnostic facility to collect and access contextual information.
* DiagnosticContext frames are placed into automatic storage (as local
* variable within some function scope). Based on thread-local storage,
* the next reachable frame can be accessed from anywhere within
* the callstack. This feature is useful for collecting
* information regarding features cross-cutting
* the usual dependency hierarchy.
* @see lib::DiagnosticContext
* @see lib::ThreadJoinable
*/
class DiagnosticContext_test : public Test
{
virtual void
run (Arg)
{
verify_simpleAccess();
verify_heavilyParallelUsage();
}
/** @test create nested scopes and place a
* DiagnosticContext frame into each.
* Access the next reachable frame.
*/
void
verify_simpleAccess()
{
VERIFY_ERROR (LOGIC, Marker::access());
VecI loggedValues;
Marker zero(0);
CHECK (0 == zero);
CHECK (0 == Marker::access());
{ // nested scope
CHECK (0 == Marker::access());
Marker one(1);
CHECK (1 == Marker::access());
CHECK (1 == one);
CHECK (0 == zero);
{ // nested scope
CHECK (1 == Marker::access());
Marker two(2);
CHECK (2 == Marker::access());
CHECK (2 == two);
CHECK (1 == one);
CHECK (0 == zero);
loggedValues = Marker::extractStack();
}
CHECK (1 == Marker::access());
}
CHECK (0 == Marker::access());
CHECK (3 == loggedValues.size());
CHECK (2 == loggedValues[0]);
CHECK (1 == loggedValues[1]);
CHECK (0 == loggedValues[2]);
}
/** @test verify the thread local property of ContextStack.
* Create several threads, each of which processes a sequence
* of numbers starting from a random initial value down to one.
* Whenever a simple division by two leads to an odd value, this
* value is placed onto the ContextStack. At recursion end, we
* take a snapshot of the full ContextStack and then unwind.
* Thus the captured numbers must from a decreasing sequence
* of odd values.
* @warning this test case seems to cause memory pressure.
* When running the test suite with VSize limit 500MB,
* we frequently got aborts even with 40 threads.
* This is surprising, since all of the lists
* generated in the individual threads are
* of size below 20 elements.
*/
void
verify_heavilyParallelUsage()
{
seedRand();
auto verifyResult = [](VecI sequence)
{
uint prev = 0;
for (uint val : sequence)
{
CHECK (isOdd(val) and val > prev);
prev = val;
}
};
std::array<TestThread, NUM_THREADS> testcases;
auto results = lib::explore(testcases)
.transform([](TestThread& t){ return t.join(); })
.effuse();
for (auto& res : results)
verifyResult (res);
}
/**
* Build a call stack within separate thread and capture diagnostics.
* The actual test operation produces a descending number sequence,
* and only odd values will be captured into the diagnostic stack-
*/
struct TestThread
: ThreadJoinable<VecI>
{
TestThread()
: ThreadJoinable{"test context stack"
,[seed = 1+rani(MAX_RAND)]
{ return descend (seed); }}
{ }
};
static VecI
descend (uint current)
{
if (current < 2)
return Marker::extractStack();
sleep_for (500us);
if (isOdd (current))
{
Marker remember(current);
return descend (current+1);
}
else
return descend (current/2);
}
};
/** Register this test class... */
LAUNCHER (DiagnosticContext_test, "function common");
}} // namespace vault::test