Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
| 33 | BOOST_AUTO_TEST_CASE(MakeUnique) |
| 34 | { |
| 35 | std::unique_ptr<int> v0 = make_unique<int>(); |
| 36 | std::unique_ptr<int> v1 = make_unique<int>(1728); |
| 37 | BOOST_CHECK_EQUAL(*v0, 0); |
| 38 | BOOST_CHECK_EQUAL(*v1, 1728); |
| 39 | |
| 40 | auto str = make_unique<std::string>("meow"); |
| 41 | BOOST_CHECK_EQUAL(*str, "meow"); |
| 42 | |
| 43 | class Movable |
| 44 | { |
| 45 | public: |
| 46 | Movable() |
| 47 | : value(42) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | Movable(const Movable&) = delete; |
| 52 | Movable(Movable&&) = default; |
| 53 | |
| 54 | public: |
| 55 | int value; |
| 56 | }; |
| 57 | |
| 58 | class Container |
| 59 | { |
| 60 | public: |
| 61 | explicit |
| 62 | Container(Movable m) |
| 63 | : m(std::move(m)) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | Movable m; |
| 69 | }; |
| 70 | |
| 71 | Movable m; |
| 72 | auto ptr = make_unique<Container>(std::move(m)); |
| 73 | BOOST_CHECK_EQUAL(ptr->m.value, 42); |
| 74 | } |
| 75 | |
Weiwei Liu | cfaa1ad | 2016-08-28 16:30:56 -0700 | [diff] [blame] | 76 | BOOST_AUTO_TEST_CASE(Clamp) |
| 77 | { |
| 78 | int x = clamp(5, 1, 10); |
| 79 | BOOST_CHECK_EQUAL(x, 5); |
| 80 | |
| 81 | x = clamp(-5, 1, 10); |
| 82 | BOOST_CHECK_EQUAL(x, 1); |
| 83 | |
| 84 | x = clamp(15, 1, 10); |
| 85 | BOOST_CHECK_EQUAL(x, 10); |
| 86 | |
| 87 | x = clamp(5, 10, 1, std::greater<int>()); |
| 88 | BOOST_CHECK_EQUAL(x, 5); |
| 89 | |
| 90 | x = clamp(-5, 10, 1, std::greater<int>()); |
| 91 | BOOST_CHECK_EQUAL(x, 1); |
| 92 | |
| 93 | x = clamp(15, 10, 1, std::greater<int>()); |
| 94 | BOOST_CHECK_EQUAL(x, 10); |
| 95 | } |
| 96 | |
Junxiao Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 97 | BOOST_AUTO_TEST_SUITE(Optional) |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(Construct) |
| 100 | { |
| 101 | optional<int> o1; |
| 102 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), false); |
| 103 | |
| 104 | optional<int> o2(1); |
| 105 | BOOST_CHECK_EQUAL(static_cast<bool>(o2), true); |
| 106 | BOOST_CHECK_EQUAL(o2.value(), 1); |
| 107 | |
| 108 | optional<int> o3(o2); |
| 109 | BOOST_CHECK_EQUAL(static_cast<bool>(o3), true); |
| 110 | BOOST_CHECK_EQUAL(static_cast<bool>(o2), true); |
| 111 | BOOST_CHECK_EQUAL(o3.value(), 1); |
| 112 | |
| 113 | optional<std::pair<int, int>> o4(in_place, 41, 42); |
| 114 | BOOST_CHECK_EQUAL(static_cast<bool>(o4), true); |
| 115 | BOOST_CHECK_EQUAL(o4.value().first, 41); |
| 116 | BOOST_CHECK_EQUAL(o4.value().second, 42); |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(Assign) |
| 120 | { |
| 121 | optional<int> o1; |
| 122 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), false); |
| 123 | |
| 124 | optional<int> o2(2); |
| 125 | o1 = o2; |
| 126 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), true); |
| 127 | BOOST_CHECK_EQUAL(o1.value(), 2); |
| 128 | |
| 129 | o1 = nullopt; |
| 130 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), false); |
| 131 | |
| 132 | o1 = 18763; |
| 133 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), true); |
| 134 | BOOST_CHECK_EQUAL(o1.value(), 18763); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(Access) |
| 138 | { |
| 139 | optional<int> o1(1); |
| 140 | BOOST_CHECK_EQUAL(*o1, 1); |
| 141 | |
| 142 | optional<std::string> o2("abc"); |
| 143 | BOOST_CHECK_EQUAL(o2->size(), 3); |
| 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(HasValue) |
| 147 | { |
| 148 | optional<int> o1; |
| 149 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), false); |
| 150 | BOOST_CHECK_EQUAL(!o1, true); |
| 151 | if (o1) { |
| 152 | BOOST_ERROR("o1 should evaluate to false"); |
| 153 | } |
| 154 | |
| 155 | optional<int> o2(1); |
| 156 | BOOST_CHECK_EQUAL(static_cast<bool>(o2), true); |
| 157 | BOOST_CHECK_EQUAL(!o2, false); |
| 158 | if (o2) { |
| 159 | } |
| 160 | else { |
| 161 | BOOST_ERROR("o2 should evaluate to true"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(Value) |
| 166 | { |
| 167 | optional<int> o1; |
| 168 | BOOST_CHECK_THROW(o1.value(), bad_optional_access); |
| 169 | |
| 170 | optional<int> o2(2); |
| 171 | BOOST_CHECK_NO_THROW(o2.value()); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(ValueOr) |
| 175 | { |
| 176 | optional<int> o1; |
| 177 | BOOST_CHECK_EQUAL(o1.value_or(3.0), 3); |
| 178 | |
| 179 | optional<int> o2(2); |
| 180 | BOOST_CHECK_EQUAL(o2.value_or(3.0), 2); |
| 181 | } |
| 182 | |
| 183 | BOOST_AUTO_TEST_CASE(Swap) |
| 184 | { |
| 185 | optional<int> o1; |
| 186 | optional<int> o2(2); |
| 187 | |
| 188 | o1.swap(o2); |
| 189 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), true); |
| 190 | BOOST_CHECK_EQUAL(o1.value(), 2); |
| 191 | BOOST_CHECK_EQUAL(static_cast<bool>(o2), false); |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_CASE(Emplace) |
| 195 | { |
| 196 | optional<std::pair<int, int>> o1; |
| 197 | o1.emplace(11, 12); |
| 198 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), true); |
| 199 | BOOST_CHECK_EQUAL(o1.value().first, 11); |
| 200 | BOOST_CHECK_EQUAL(o1.value().second, 12); |
| 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(Compare) |
| 204 | { |
| 205 | optional<int> o0a; |
| 206 | optional<int> o0b; |
| 207 | optional<int> o1a(1); |
| 208 | optional<int> o1b(1); |
| 209 | optional<int> o2(2); |
| 210 | |
| 211 | BOOST_CHECK_EQUAL(o0a == o0b, true); |
| 212 | BOOST_CHECK_EQUAL(o0a != o0b, false); |
| 213 | BOOST_CHECK_EQUAL(o0a < o0b, false); |
| 214 | BOOST_CHECK_EQUAL(o0a <= o0b, true); |
| 215 | BOOST_CHECK_EQUAL(o0a > o0b, false); |
| 216 | BOOST_CHECK_EQUAL(o0a >= o0b, true); |
| 217 | |
| 218 | BOOST_CHECK_EQUAL(o1a == o1b, true); |
| 219 | BOOST_CHECK_EQUAL(o1a != o1b, false); |
| 220 | BOOST_CHECK_EQUAL(o1a < o1b, false); |
| 221 | BOOST_CHECK_EQUAL(o1a <= o1b, true); |
| 222 | BOOST_CHECK_EQUAL(o1a > o1b, false); |
| 223 | BOOST_CHECK_EQUAL(o1a >= o1b, true); |
| 224 | |
| 225 | BOOST_CHECK_EQUAL(o0a == o1a, false); |
| 226 | BOOST_CHECK_EQUAL(o0a != o1a, true); |
| 227 | BOOST_CHECK_EQUAL(o0a < o1a, true); |
| 228 | BOOST_CHECK_EQUAL(o0a <= o1a, true); |
| 229 | BOOST_CHECK_EQUAL(o0a > o1a, false); |
| 230 | BOOST_CHECK_EQUAL(o0a >= o1a, false); |
| 231 | |
| 232 | BOOST_CHECK_EQUAL(o1a == o0a, false); |
| 233 | BOOST_CHECK_EQUAL(o1a != o0a, true); |
| 234 | BOOST_CHECK_EQUAL(o1a < o0a, false); |
| 235 | BOOST_CHECK_EQUAL(o1a <= o0a, false); |
| 236 | BOOST_CHECK_EQUAL(o1a > o0a, true); |
| 237 | BOOST_CHECK_EQUAL(o1a >= o0a, true); |
| 238 | |
| 239 | BOOST_CHECK_EQUAL(o1a == o2, false); |
| 240 | BOOST_CHECK_EQUAL(o1a != o2, true); |
| 241 | BOOST_CHECK_EQUAL(o1a < o2, true); |
| 242 | BOOST_CHECK_EQUAL(o1a <= o2, true); |
| 243 | BOOST_CHECK_EQUAL(o1a > o2, false); |
| 244 | BOOST_CHECK_EQUAL(o1a >= o2, false); |
| 245 | |
| 246 | BOOST_CHECK_EQUAL(o2 == o1a, false); |
| 247 | BOOST_CHECK_EQUAL(o2 != o1a, true); |
| 248 | BOOST_CHECK_EQUAL(o2 < o1a, false); |
| 249 | BOOST_CHECK_EQUAL(o2 <= o1a, false); |
| 250 | BOOST_CHECK_EQUAL(o2 > o1a, true); |
| 251 | BOOST_CHECK_EQUAL(o2 >= o1a, true); |
| 252 | } |
| 253 | |
| 254 | BOOST_AUTO_TEST_CASE(MakeOptional) |
| 255 | { |
| 256 | auto o1 = make_optional(1); |
| 257 | static_assert(std::is_same<decltype(o1), optional<int>>::value, "o1 must be optional<int>"); |
| 258 | BOOST_CHECK_EQUAL(static_cast<bool>(o1), true); |
| 259 | BOOST_CHECK_EQUAL(o1.value(), 1); |
| 260 | |
| 261 | auto o2 = make_optional<std::pair<int, int>>(21, 22); |
| 262 | static_assert(std::is_same<decltype(o2), optional<std::pair<int, int>>>::value, |
| 263 | "o2 must be optional<std::pair<int, int>>"); |
| 264 | BOOST_CHECK_EQUAL(static_cast<bool>(o2), true); |
| 265 | BOOST_CHECK_EQUAL(o2.value().first, 21); |
| 266 | BOOST_CHECK_EQUAL(o2.value().second, 22); |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_SUITE_END() // Optional |
| 270 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 271 | BOOST_AUTO_TEST_CASE(OstreamJoiner) |
| 272 | { |
| 273 | boost::test_tools::output_test_stream os; |
| 274 | |
| 275 | auto joiner1 = ostream_joiner<char>(os, ' '); |
| 276 | auto joiner2 = make_ostream_joiner(os, ' '); |
| 277 | static_assert(std::is_same<decltype(joiner1), decltype(joiner2)>::value, ""); |
| 278 | |
| 279 | std::vector<int> v(5); |
| 280 | std::iota(v.begin(), v.end(), 1); |
| 281 | std::copy(v.begin(), v.end(), joiner2); |
| 282 | BOOST_CHECK(os.is_equal("1 2 3 4 5")); |
| 283 | |
| 284 | auto joiner3 = make_ostream_joiner(os, "..."); |
| 285 | std::copy(v.begin(), v.end(), joiner3); |
| 286 | BOOST_CHECK(os.is_equal("1...2...3...4...5")); |
| 287 | |
| 288 | joiner3 = "one"; |
| 289 | BOOST_CHECK(os.is_equal("one")); |
| 290 | joiner3 = "two"; |
| 291 | BOOST_CHECK(os.is_equal("...two")); |
| 292 | ++joiner3 = "three"; |
| 293 | BOOST_CHECK(os.is_equal("...three")); |
| 294 | joiner3++ = "four"; |
| 295 | BOOST_CHECK(os.is_equal("...four")); |
| 296 | |
| 297 | std::copy(v.begin(), v.end(), make_ostream_joiner(os, "")); |
| 298 | BOOST_CHECK(os.is_equal("12345")); |
| 299 | |
| 300 | std::string delimiter("_"); |
| 301 | std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter)); |
| 302 | BOOST_CHECK(os.is_equal("1_2_3_4_5")); |
| 303 | } |
| 304 | |
Junxiao Shi | 1aecae2 | 2016-08-30 11:23:59 +0000 | [diff] [blame] | 305 | BOOST_AUTO_TEST_SUITE_END() // TestBackports |
| 306 | BOOST_AUTO_TEST_SUITE_END() // Util |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 307 | |
| 308 | } // namespace tests |
| 309 | } // namespace ndn |