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