blob: 4ac33817f8f736d6b7d7d000f2473f394f0ffc56 [file] [log] [blame]
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento187e9f92023-03-20 22:46:22 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04004 *
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>
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040027#include <boost/test/tools/output_test_stream.hpp>
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040030
31BOOST_AUTO_TEST_SUITE(Util)
32BOOST_AUTO_TEST_SUITE(TestOstreamJoiner)
33
34BOOST_AUTO_TEST_CASE(Basic)
35{
36 boost::test_tools::output_test_stream os;
37
38 auto joiner1 = ostream_joiner<char>(os, ' ');
39 auto joiner2 = make_ostream_joiner(os, ' ');
Davide Pesaventofffdd622023-08-28 22:50:43 -040040 static_assert(std::is_same_v<decltype(joiner1), decltype(joiner2)>);
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040041
42 std::vector<int> v(5);
43 std::iota(v.begin(), v.end(), 1);
44 std::copy(v.begin(), v.end(), joiner2);
45 BOOST_CHECK(os.is_equal("1 2 3 4 5"));
46
47 auto joiner3 = make_ostream_joiner(os, "...");
48 std::copy(v.begin(), v.end(), joiner3);
49 BOOST_CHECK(os.is_equal("1...2...3...4...5"));
50
51 joiner3 = "one";
52 BOOST_CHECK(os.is_equal("one"));
53 joiner3 = "two";
54 BOOST_CHECK(os.is_equal("...two"));
55 ++joiner3 = "three";
56 BOOST_CHECK(os.is_equal("...three"));
57 joiner3++ = "four";
58 BOOST_CHECK(os.is_equal("...four"));
59
60 std::copy(v.begin(), v.end(), make_ostream_joiner(os, ""));
61 BOOST_CHECK(os.is_equal("12345"));
62
63 std::string delimiter("_");
64 std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter));
65 BOOST_CHECK(os.is_equal("1_2_3_4_5"));
66}
67
68BOOST_AUTO_TEST_SUITE_END() // TestOstreamJoiner
69BOOST_AUTO_TEST_SUITE_END() // Util
70
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040071} // namespace ndn::tests