blob: b73465bc7edb2b30dc27f6740b3b38d414f2add3 [file] [log] [blame]
Davide Pesavento409cc202015-09-19 14:13:16 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib849f612018-04-01 23:52:54 +00002/*
Davide Pesavento75c56012019-08-31 22:51:27 -04003 * Copyright (c) 2013-2019 Regents of the University of California.
Davide Pesavento409cc202015-09-19 14:13:16 +02004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/backports.hpp"
Davide Pesavento75c56012019-08-31 22:51:27 -040023#include "ndn-cxx/util/ostream-joiner.hpp"
Davide Pesavento409cc202015-09-19 14:13:16 +020024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26
Davide Pesaventocf415762017-02-25 23:46:47 -050027#include <numeric>
Davide Pesavento21a4ea52019-12-22 13:35:37 -050028
29#if BOOST_VERSION >= 105900
30#include <boost/test/tools/output_test_stream.hpp>
31#else
Davide Pesavento7e780642018-11-24 15:51:34 -050032#include <boost/test/output_test_stream.hpp>
Davide Pesavento21a4ea52019-12-22 13:35:37 -050033#endif
Davide Pesavento409cc202015-09-19 14:13:16 +020034
35namespace ndn {
36namespace tests {
37
38BOOST_AUTO_TEST_SUITE(Util)
39BOOST_AUTO_TEST_SUITE(TestBackports)
40
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070041BOOST_AUTO_TEST_CASE(Clamp)
42{
43 int x = clamp(5, 1, 10);
44 BOOST_CHECK_EQUAL(x, 5);
45
46 x = clamp(-5, 1, 10);
47 BOOST_CHECK_EQUAL(x, 1);
48
49 x = clamp(15, 1, 10);
50 BOOST_CHECK_EQUAL(x, 10);
51
52 x = clamp(5, 10, 1, std::greater<int>());
53 BOOST_CHECK_EQUAL(x, 5);
54
55 x = clamp(-5, 10, 1, std::greater<int>());
56 BOOST_CHECK_EQUAL(x, 1);
57
58 x = clamp(15, 10, 1, std::greater<int>());
59 BOOST_CHECK_EQUAL(x, 10);
60}
61
Davide Pesaventocf415762017-02-25 23:46:47 -050062BOOST_AUTO_TEST_CASE(OstreamJoiner)
63{
64 boost::test_tools::output_test_stream os;
65
66 auto joiner1 = ostream_joiner<char>(os, ' ');
67 auto joiner2 = make_ostream_joiner(os, ' ');
68 static_assert(std::is_same<decltype(joiner1), decltype(joiner2)>::value, "");
69
70 std::vector<int> v(5);
71 std::iota(v.begin(), v.end(), 1);
72 std::copy(v.begin(), v.end(), joiner2);
73 BOOST_CHECK(os.is_equal("1 2 3 4 5"));
74
75 auto joiner3 = make_ostream_joiner(os, "...");
76 std::copy(v.begin(), v.end(), joiner3);
77 BOOST_CHECK(os.is_equal("1...2...3...4...5"));
78
79 joiner3 = "one";
80 BOOST_CHECK(os.is_equal("one"));
81 joiner3 = "two";
82 BOOST_CHECK(os.is_equal("...two"));
83 ++joiner3 = "three";
84 BOOST_CHECK(os.is_equal("...three"));
85 joiner3++ = "four";
86 BOOST_CHECK(os.is_equal("...four"));
87
88 std::copy(v.begin(), v.end(), make_ostream_joiner(os, ""));
89 BOOST_CHECK(os.is_equal("12345"));
90
91 std::string delimiter("_");
92 std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter));
93 BOOST_CHECK(os.is_equal("1_2_3_4_5"));
94}
95
Junxiao Shi1aecae22016-08-30 11:23:59 +000096BOOST_AUTO_TEST_SUITE_END() // TestBackports
97BOOST_AUTO_TEST_SUITE_END() // Util
Davide Pesavento409cc202015-09-19 14:13:16 +020098
99} // namespace tests
100} // namespace ndn