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