Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b849f61 | 2018-04-01 23:52:54 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 75c5601 | 2019-08-31 22:51:27 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/backports.hpp" |
Davide Pesavento | 75c5601 | 2019-08-31 22:51:27 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/util/ostream-joiner.hpp" |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 27 | #include <numeric> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include <boost/test/output_test_stream.hpp> |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace tests { |
| 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(Util) |
| 34 | BOOST_AUTO_TEST_SUITE(TestBackports) |
| 35 | |
Weiwei Liu | cfaa1ad | 2016-08-28 16:30:56 -0700 | [diff] [blame] | 36 | BOOST_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 Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 57 | BOOST_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 Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 91 | BOOST_AUTO_TEST_SUITE_END() // TestBackports |
| 92 | BOOST_AUTO_TEST_SUITE_END() // Util |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 93 | |
| 94 | } // namespace tests |
| 95 | } // namespace ndn |