lumiera_/tests/library/format-cout-test.cpp

160 lines
5.1 KiB
C++
Raw Normal View History

/*
FormatCOUT(Test) - validate automatic string conversion in output
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)
2016, 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
* *****************************************************************/
/** @file format-cout-test.cpp
** unit test \ref FormatCOUT_test
*/
#include "lib/test/run.hpp"
#include "lib/p.hpp"
#include "lib/diff/gen-node.hpp"
#include "lib/meta/util.hpp"
#include "lib/meta/trait.hpp"
#include "lib/format-cout.hpp"
#include <string>
using lib::P;
using lib::makeP;
using lib::diff::GenNode;
using std::string;
namespace util {
namespace test {
2016-01-09 04:05:14 +01:00
namespace { // test fixture
2016-01-09 04:05:14 +01:00
/** opaque class without string conversion */
class Reticent
2016-01-09 04:05:14 +01:00
{
uint neigh_ = 42;
};
2016-01-09 04:05:14 +01:00
using lib::meta::is_basically;
using lib::meta::is_StringLike;
using lib::meta::can_lexical2string;
using lib::meta::can_convertToString;
using lib::meta::use_StringConversion4Stream;
2016-01-09 04:05:14 +01:00
template<typename T>
using BasicallyString = is_basically<T, string>;
template<typename T>
using BasicallyCString = std::is_convertible<T, const char*>;
#define SHOW_CHECK(_EXPR_) cout << STRINGIFY(_EXPR_) << "\t : " << (_EXPR_::value? "Yes":"No") << endl;
#define ANALYSE(_TYPE_) \
cout << "Type: " STRINGIFY(_TYPE_) " ......"<<endl; \
SHOW_CHECK (is_StringLike<_TYPE_>); \
SHOW_CHECK (BasicallyString<_TYPE_>); \
SHOW_CHECK (BasicallyCString<_TYPE_>); \
SHOW_CHECK (std::is_arithmetic<_TYPE_>); \
SHOW_CHECK (can_lexical2string<_TYPE_>); \
SHOW_CHECK (can_convertToString<_TYPE_>); \
SHOW_CHECK (use_StringConversion4Stream<_TYPE_>);
void
showTraits()
{
using CharLit = decltype("literal");
using CharPtr = const char*;
using StringPtr = string *;
using StringRef = string &;
using StringRRef = string &&;
using StrConstRef = string const&;
using GenNodePtr = GenNode*;
using GenNodeRef = GenNode&;
using GenNodeRRef = GenNode&&;
2016-01-09 04:05:14 +01:00
ANALYSE (int);
ANALYSE (char);
2016-01-09 04:05:14 +01:00
ANALYSE (double);
ANALYSE (int64_t);
2016-01-09 04:05:14 +01:00
ANALYSE (string);
ANALYSE (StringPtr);
ANALYSE (StringRef);
ANALYSE (StringRRef);
ANALYSE (StrConstRef);
2016-01-09 04:05:14 +01:00
ANALYSE (CharLit);
ANALYSE (CharPtr)
ANALYSE (Reticent)
ANALYSE (P<Reticent>)
ANALYSE (GenNode)
ANALYSE (GenNodePtr)
ANALYSE (GenNodeRef)
ANALYSE (GenNodeRRef)
2016-01-09 04:05:14 +01:00
ANALYSE (P<GenNode>)
cout << "───────────────────────────╼━━━━━━━━━━╾───────────────────────────"<<endl;
2016-01-09 04:05:14 +01:00
}
}//(end)fixture
/***************************************************************************//**
2016-01-09 04:05:14 +01:00
* @test How to build generic string conversion into `ostream::operator<< `.
* This task (#985) was actually a conglomerate of several chores:
* - sanitise and segregate the type-traits usage
* - disentangle the existing util::str() conversion helper
2016-01-09 04:05:14 +01:00
* - extract a basic form from this helper, which can be placed
* into a header with minimal dependencies. After some consideration,
* I decided to allow `<typeinfo>` in this category, which allows us
* at least to show a type name as fallback
* - distill an essential version of `enable_if`, which can be inlined.
* This allowed us to get rid of `boost::enable_if` finally.
2016-01-09 04:05:14 +01:00
* - build a sensible `operator string()` for our `lib::P` based on this
* - and _finally_, to come up with a templated version of the `ostream`
* inserter `operator<<`, which does not cause too much havoc when
* used by default. The greatest challenge here is to avoid ambiguous
* overloads, yet also to deal with references, `void` and arrays.
*
* @see format-cout.hpp
* @see FormatHelper_test
*/
class FormatCOUT_test
: public Test
{
void
run (Arg)
2016-01-09 04:05:14 +01:00
{
showTraits();
auto silent = makeP<Reticent>();
auto chatty = makeP<GenNode>("Hui", "Buh");
2016-01-09 04:05:14 +01:00
cout << "smart-ptr, no string conv..." << silent <<endl;
cout << "smart-ptr, custom conv......" << chatty <<endl;
cout << "reference, no string conv..." << *silent <<endl;
cout << "reference, custom conv......" << *chatty <<endl;
cout << "pointer, custom conv......" << chatty.get() <<endl;
chatty.reset();
cout << "smart-ptr, NULL pointee....." << chatty <<endl;
cout << "pointer, NULL pointee....." << chatty.get() <<endl;
}
};
LAUNCHER (FormatCOUT_test, "unit common");
}} // namespace util::test