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