Commit be58a7bf authored by GILLES Sebastien's avatar GILLES Sebastien

#1470 Refactor PrintNelt, and add it and PrintTuple to the test suite.

parent 968fa24e
......@@ -11,6 +11,7 @@
#include <sstream>
#include <vector>
#include <string>
#include <list>
#include <deque>
#define BOOST_TEST_MODULE print_containers
......@@ -62,5 +63,26 @@ BOOST_AUTO_TEST_CASE(vector_of_simple_variant)
}
BOOST_AUTO_TEST_CASE(list_print_n_elt)
{
std::list<unsigned int> primes { 2, 3, 5, 7, 11, 13 };
std::ostringstream oconv;
Utilities::PrintNelt<3>(primes, oconv, ", ", "{", ", ... }");
BOOST_CHECK_EQUAL(oconv.str(), "{2, 3, 5, ... }");
}
BOOST_AUTO_TEST_CASE(print_tuple)
{
std::tuple<int, std::string, double, char> tuple { 5, "Hello", 3.14, 'a'};
std::ostringstream oconv;
Utilities::PrintTuple(tuple, oconv, ", ", "{ ", "}");
BOOST_CHECK_EQUAL(oconv.str(), "{ 5, \"Hello\", 3.14, \"a\"}");
}
PRAGMA_DIAGNOSTIC(pop)
......@@ -16,7 +16,7 @@
# define MOREFEM_x_UTILITIES_x_CONTAINERS_x_PRINT_HPP_
# include <algorithm>
# include <cassert>
# include <iosfwd>
# include <tuple>
......@@ -36,7 +36,6 @@ namespace MoReFEM
namespace Utilities
{
/*!
* \brief Print the content of a non associative container (list, vector, deque, array, etc...)
*
......@@ -77,7 +76,8 @@ namespace MoReFEM
*
* \tparam N Number of elements to display.
* \tparam StreamT Type of output stream considered
* \tparam ContainerT Type of the container to be displayed
* \tparam ContainerT Type of the container to be displayed. It must define the method 'size()' (so
* std::forward_list won't work for instance).
*
* \param[in,out] stream Output stream in which container will be displayed
* \param[in] container Container displayed
......@@ -96,7 +96,7 @@ namespace MoReFEM
*/
template
<
unsigned int N,
std::size_t N,
class ContainerT,
typename StreamT = std::ostream,
typename StringT1 = std::string,
......
......@@ -38,7 +38,7 @@ namespace MoReFEM
template
<
unsigned int N,
std::size_t N,
class ContainerT,
typename StreamT,
typename StringT1,
......@@ -50,24 +50,15 @@ namespace MoReFEM
{
stream << opener;
auto it = container.cbegin();
auto end = container.cend();
auto size = container.size();
(void) end; // avoid compilation warning in release mode
const auto stop = std::min(size, static_cast<decltype(size)>(N));
for (decltype(size) i = 0; i + 1 < stop; ++it, ++i)
{
assert(it != end);
stream << *it << separator;
}
Internal::PrintNS::SeparatorFacility separator_facility(stream, separator);
if (size > 0u)
stream << *it;
auto it = container.cbegin();
auto end = it;
std::advance(end,
static_cast<typename ContainerT::difference_type>(std::min(N, container.size())));
if (N < size)
stream << separator << "...";
for (; it != end; ++it)
separator_facility << *it;
stream << closer;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment