blob: 6ff2c4cddc97873cf48faffd4511056e70597685 [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/*
3 * Copyright (c) 2013-2018 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 Pesavento409cc202015-09-19 14:13:16 +020023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25
Davide Pesaventocf415762017-02-25 23:46:47 -050026#include <numeric>
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include <boost/test/output_test_stream.hpp>
Davide Pesavento409cc202015-09-19 14:13:16 +020028
29namespace ndn {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(Util)
33BOOST_AUTO_TEST_SUITE(TestBackports)
34
Weiwei Liucfaa1ad2016-08-28 16:30:56 -070035BOOST_AUTO_TEST_CASE(Clamp)
36{
37 int x = clamp(5, 1, 10);
38 BOOST_CHECK_EQUAL(x, 5);
39
40 x = clamp(-5, 1, 10);
41 BOOST_CHECK_EQUAL(x, 1);
42
43 x = clamp(15, 1, 10);
44 BOOST_CHECK_EQUAL(x, 10);
45
46 x = clamp(5, 10, 1, std::greater<int>());
47 BOOST_CHECK_EQUAL(x, 5);
48
49 x = clamp(-5, 10, 1, std::greater<int>());
50 BOOST_CHECK_EQUAL(x, 1);
51
52 x = clamp(15, 10, 1, std::greater<int>());
53 BOOST_CHECK_EQUAL(x, 10);
54}
55
Davide Pesaventocf415762017-02-25 23:46:47 -050056BOOST_AUTO_TEST_CASE(OstreamJoiner)
57{
58 boost::test_tools::output_test_stream os;
59
60 auto joiner1 = ostream_joiner<char>(os, ' ');
61 auto joiner2 = make_ostream_joiner(os, ' ');
62 static_assert(std::is_same<decltype(joiner1), decltype(joiner2)>::value, "");
63
64 std::vector<int> v(5);
65 std::iota(v.begin(), v.end(), 1);
66 std::copy(v.begin(), v.end(), joiner2);
67 BOOST_CHECK(os.is_equal("1 2 3 4 5"));
68
69 auto joiner3 = make_ostream_joiner(os, "...");
70 std::copy(v.begin(), v.end(), joiner3);
71 BOOST_CHECK(os.is_equal("1...2...3...4...5"));
72
73 joiner3 = "one";
74 BOOST_CHECK(os.is_equal("one"));
75 joiner3 = "two";
76 BOOST_CHECK(os.is_equal("...two"));
77 ++joiner3 = "three";
78 BOOST_CHECK(os.is_equal("...three"));
79 joiner3++ = "four";
80 BOOST_CHECK(os.is_equal("...four"));
81
82 std::copy(v.begin(), v.end(), make_ostream_joiner(os, ""));
83 BOOST_CHECK(os.is_equal("12345"));
84
85 std::string delimiter("_");
86 std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter));
87 BOOST_CHECK(os.is_equal("1_2_3_4_5"));
88}
89
Junxiao Shi1aecae22016-08-30 11:23:59 +000090BOOST_AUTO_TEST_SUITE_END() // TestBackports
91BOOST_AUTO_TEST_SUITE_END() // Util
Davide Pesavento409cc202015-09-19 14:13:16 +020092
93} // namespace tests
94} // namespace ndn