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 | /* |
| 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "util/backports.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 25 | #include <numeric> |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(Util) |
| 31 | BOOST_AUTO_TEST_SUITE(TestBackports) |
| 32 | |
Weiwei Liu | cfaa1ad | 2016-08-28 16:30:56 -0700 | [diff] [blame] | 33 | BOOST_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 Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 54 | BOOST_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 Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 88 | BOOST_AUTO_TEST_SUITE_END() // TestBackports |
| 89 | BOOST_AUTO_TEST_SUITE_END() // Util |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 90 | |
| 91 | } // namespace tests |
| 92 | } // namespace ndn |