lumiera_documentation/tests/lib/meta/iterable-classification-test.cpp

174 lines
6.8 KiB
C++
Raw Permalink Normal View History

/*
IterableClassification(Test) - detecting iterability of a generic type
2010-12-17 23:28:49 +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)
2010, Hermann Vosseler <Ichthyostega@web.de>
2010-12-17 23:28:49 +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-17 23:28:49 +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
* *****************************************************************/
/** @file iterable-classification-test.cpp
** unit test \ref IterableClassification_test
** @see iter-adapter.hpp
** @see lib/meta/trait.hpp
** @see lib/meta/duck-detector.hpp
*/
#include "test/run.hpp"
#include "lib/meta/trait.hpp"
#include "lib/diff/record.hpp"
#include "lib/time/timevalue.hpp"
#include "lib/itertools.hpp"
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
using std::string;
using std::vector;
using std::declval;
using lib::time::TimeVar;
2011-12-03 02:56:50 +01:00
namespace lib {
namespace meta {
namespace test {
namespace { // a custom test container....
/// @note: a bare type definition is sufficient here....
struct TestSource
{
TestSource(uint num);
using RawIter = vector<int>::iterator;
using iterator = RangeIter<RawIter>;
iterator begin() ;
iterator end() ;
};
inline auto transformedSource(TestSource& src){ return lib::transformIter (src.begin(), [](int){ return long(0); }); }
inline auto filteredSource (TestSource& src){ return lib::filterIter (src.begin(), [](int){ return true; }); }
/// @note Dummy type that exposes a »*State Core*« interface.
struct DummyCore
{
bool checkPoint() const;
uint& yield() const;
void iterNext();
friend bool operator== (DummyCore const&, DummyCore const&);
};
}//(End) test containers
/*******************************************************************************//**
* @test verify the (static) classification/detection of iterables.
* Using some SFINAE trickery, it is possible to detect the following
* - a _STL-like container_ with `begin()` and `end()`
* - a _Lumiera Forward Iterator_
* - a _State Core_
* This test ensures this compile time detection works as intended;
* it suffices thus to define some minimal types in order to access
* the generated meta function value.
* @see iter-adapter.hpp explanation of _Lumiera Forward Iterator_ and _State Core_
* @todo 2026 many years later and this type detection has become a cornerstone
* of Lumiera's iterator pipeline framework. Since the language level has
* been raised recently to C++23, it should be considered to replace
* the SFINAE-based traits with Concept definitions.
*/
class IterableClassification_test : public Test
{
void
2025-06-07 23:59:57 +02:00
run (Arg)
{
// define a bunch of STL containers
using TimeVector = std::vector<TimeVar>;
using LongSet = std::multiset<long> ;
using CharMap = std::map<int,char> ;
using BoolList = std::list<bool> ;
using ShortDeque = std::deque<ushort> ;
using CustomCont = TestSource ;
// some types that comply with the "Lumiera Forward Iterator" concept
using LumieraRangeIter = TestSource::iterator;
using LumieraFilteredIter = decltype( filteredSource (declval<TestSource&>()) );
using LumieraTransformedIter = decltype( transformedSource (declval<TestSource&>()) );
using CustomLumieraIter = diff::Record<string>::iterator;
// some types that comply with the "State Core" concept
using CustomStateCore = DummyCore;
using LumieraCoreIter = IterableDecorator<DummyCore>;
// detect STL iteration
CHECK ( true == can_STL_ForEach<TimeVector>::value );
CHECK ( true == can_STL_ForEach<LongSet> ::value );
CHECK ( true == can_STL_ForEach<CharMap> ::value );
CHECK ( true == can_STL_ForEach<BoolList> ::value );
CHECK ( true == can_STL_ForEach<ShortDeque>::value );
CHECK ( true == can_STL_ForEach<CustomCont>::value );
CHECK (false == can_STL_ForEach<LumieraRangeIter> ::value );
CHECK (false == can_STL_ForEach<LumieraFilteredIter> ::value );
CHECK (false == can_STL_ForEach<LumieraTransformedIter>::value );
CHECK (false == can_STL_ForEach<CustomLumieraIter> ::value );
CHECK (false == can_STL_ForEach<CustomStateCore>::value );
CHECK (false == can_STL_ForEach<LumieraCoreIter>::value );
// detect Lumiera Forward Iterator
CHECK (false == can_LumieraIter<TimeVector>::value );
CHECK (false == can_LumieraIter<LongSet> ::value );
CHECK (false == can_LumieraIter<CharMap> ::value );
CHECK (false == can_LumieraIter<BoolList> ::value );
CHECK (false == can_LumieraIter<ShortDeque>::value );
CHECK (false == can_LumieraIter<CustomCont>::value );
CHECK ( true == can_LumieraIter<LumieraRangeIter> ::value );
CHECK ( true == can_LumieraIter<LumieraFilteredIter> ::value );
CHECK ( true == can_LumieraIter<LumieraTransformedIter>::value );
CHECK ( true == can_LumieraIter<CustomLumieraIter> ::value );
CHECK (false == can_LumieraIter<CustomStateCore>::value );
CHECK ( true == can_LumieraIter<LumieraCoreIter>::value ); // Note: Lumiera iterator capability added as wrapper
// detect State Core
CHECK (false == is_StateCore<TimeVector>::value );
CHECK (false == is_StateCore<LongSet> ::value );
CHECK (false == is_StateCore<CharMap> ::value );
CHECK (false == is_StateCore<BoolList> ::value );
CHECK (false == is_StateCore<ShortDeque>::value );
CHECK (false == is_StateCore<CustomCont>::value );
CHECK (false == is_StateCore<LumieraRangeIter> ::value );
CHECK (false == is_StateCore<LumieraFilteredIter> ::value );
CHECK (false == is_StateCore<LumieraTransformedIter>::value );
CHECK (false == is_StateCore<CustomLumieraIter> ::value );
CHECK ( true == is_StateCore<CustomStateCore>::value );
CHECK ( true == is_StateCore<LumieraCoreIter>::value );
2025-06-07 23:59:57 +02:00
}
};
/** Register this test class... */
LAUNCHER (IterableClassification_test, "unit common");
}}} // namespace lib::meta::test