blob: 2eaf2f95e55eb8efbc44e2b46b80271099ca00fd [file] [log] [blame]
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2020 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "ndn-cxx/util/ostream-joiner.hpp"
23
24#include "tests/boost-test.hpp"
25
26#include <numeric>
27
28#if BOOST_VERSION >= 105900
29#include <boost/test/tools/output_test_stream.hpp>
30#else
31#include <boost/test/output_test_stream.hpp>
32#endif
33
34namespace ndn {
35namespace tests {
36
37BOOST_AUTO_TEST_SUITE(Util)
38BOOST_AUTO_TEST_SUITE(TestOstreamJoiner)
39
40BOOST_AUTO_TEST_CASE(Basic)
41{
42 boost::test_tools::output_test_stream os;
43
44 auto joiner1 = ostream_joiner<char>(os, ' ');
45 auto joiner2 = make_ostream_joiner(os, ' ');
46 static_assert(std::is_same<decltype(joiner1), decltype(joiner2)>::value, "");
47
48 std::vector<int> v(5);
49 std::iota(v.begin(), v.end(), 1);
50 std::copy(v.begin(), v.end(), joiner2);
51 BOOST_CHECK(os.is_equal("1 2 3 4 5"));
52
53 auto joiner3 = make_ostream_joiner(os, "...");
54 std::copy(v.begin(), v.end(), joiner3);
55 BOOST_CHECK(os.is_equal("1...2...3...4...5"));
56
57 joiner3 = "one";
58 BOOST_CHECK(os.is_equal("one"));
59 joiner3 = "two";
60 BOOST_CHECK(os.is_equal("...two"));
61 ++joiner3 = "three";
62 BOOST_CHECK(os.is_equal("...three"));
63 joiner3++ = "four";
64 BOOST_CHECK(os.is_equal("...four"));
65
66 std::copy(v.begin(), v.end(), make_ostream_joiner(os, ""));
67 BOOST_CHECK(os.is_equal("12345"));
68
69 std::string delimiter("_");
70 std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter));
71 BOOST_CHECK(os.is_equal("1_2_3_4_5"));
72}
73
74BOOST_AUTO_TEST_SUITE_END() // TestOstreamJoiner
75BOOST_AUTO_TEST_SUITE_END() // Util
76
77} // namespace tests
78} // namespace ndn