blob: 629c3ee012a328d503d55470b97d2613a4d72213 [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/*
3 * Copyright (c) 2013-2018 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
22#include "util/backports.hpp"
23
24#include "boost-test.hpp"
Davide Pesaventocf415762017-02-25 23:46:47 -050025#include <numeric>
Davide Pesavento409cc202015-09-19 14:13:16 +020026
27namespace ndn {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(Util)
31BOOST_AUTO_TEST_SUITE(TestBackports)
32
Junxiao Shib849f612018-04-01 23:52:54 +000033namespace deprecated_test {
34
35// Deprecate a variable.
36static int g_deprecatedVar NDN_CXX_DEPRECATED = 0;
37
38// Deprecate an enum member (a variable).
39enum NamedEnum
40{
41 ModernEnumMember,
42};
43constexpr NamedEnum DeprecatedEnumMember NDN_CXX_DEPRECATED = ModernEnumMember;
44
45// Deprecate a type.
46class ModernType
47{
48public:
49 // Deprecate a function.
50 NDN_CXX_DEPRECATED
51 void
52 deprecatedFunc(int a, NamedEnum b)
53 {
54 }
55};
56typedef ModernType DeprecatedType NDN_CXX_DEPRECATED;
57
58} // namespace deprecated_test
59
60BOOST_AUTO_TEST_CASE(Deprecated)
61{
62 using namespace deprecated_test;
63 DeprecatedType instance;
64 instance.deprecatedFunc(g_deprecatedVar, DeprecatedEnumMember);
65 BOOST_CHECK(true);
66}
67
Davide Pesavento409cc202015-09-19 14:13:16 +020068BOOST_AUTO_TEST_CASE(MakeUnique)
69{
70 std::unique_ptr<int> v0 = make_unique<int>();
71 std::unique_ptr<int> v1 = make_unique<int>(1728);
72 BOOST_CHECK_EQUAL(*v0, 0);
73 BOOST_CHECK_EQUAL(*v1, 1728);
74
75 auto str = make_unique<std::string>("meow");
76 BOOST_CHECK_EQUAL(*str, "meow");
77
78 class Movable
79 {
80 public:
81 Movable()
82 : value(42)
83 {
84 }
85
86 Movable(const Movable&) = delete;
87 Movable(Movable&&) = default;
88
89 public:
90 int value;
91 };
92
93 class Container
94 {
95 public:
96 explicit
97 Container(Movable m)
98 : m(std::move(m))
99 {
100 }
101
102 public:
103 Movable m;
104 };
105
106 Movable m;
107 auto ptr = make_unique<Container>(std::move(m));
108 BOOST_CHECK_EQUAL(ptr->m.value, 42);
109}
110
Weiwei Liucfaa1ad2016-08-28 16:30:56 -0700111BOOST_AUTO_TEST_CASE(Clamp)
112{
113 int x = clamp(5, 1, 10);
114 BOOST_CHECK_EQUAL(x, 5);
115
116 x = clamp(-5, 1, 10);
117 BOOST_CHECK_EQUAL(x, 1);
118
119 x = clamp(15, 1, 10);
120 BOOST_CHECK_EQUAL(x, 10);
121
122 x = clamp(5, 10, 1, std::greater<int>());
123 BOOST_CHECK_EQUAL(x, 5);
124
125 x = clamp(-5, 10, 1, std::greater<int>());
126 BOOST_CHECK_EQUAL(x, 1);
127
128 x = clamp(15, 10, 1, std::greater<int>());
129 BOOST_CHECK_EQUAL(x, 10);
130}
131
Junxiao Shi1aecae22016-08-30 11:23:59 +0000132BOOST_AUTO_TEST_SUITE(Optional)
133
134BOOST_AUTO_TEST_CASE(Construct)
135{
136 optional<int> o1;
137 BOOST_CHECK_EQUAL(static_cast<bool>(o1), false);
138
139 optional<int> o2(1);
140 BOOST_CHECK_EQUAL(static_cast<bool>(o2), true);
141 BOOST_CHECK_EQUAL(o2.value(), 1);
142
143 optional<int> o3(o2);
144 BOOST_CHECK_EQUAL(static_cast<bool>(o3), true);
145 BOOST_CHECK_EQUAL(static_cast<bool>(o2), true);
146 BOOST_CHECK_EQUAL(o3.value(), 1);
147
148 optional<std::pair<int, int>> o4(in_place, 41, 42);
149 BOOST_CHECK_EQUAL(static_cast<bool>(o4), true);
150 BOOST_CHECK_EQUAL(o4.value().first, 41);
151 BOOST_CHECK_EQUAL(o4.value().second, 42);
152}
153
154BOOST_AUTO_TEST_CASE(Assign)
155{
156 optional<int> o1;
157 BOOST_CHECK_EQUAL(static_cast<bool>(o1), false);
158
159 optional<int> o2(2);
160 o1 = o2;
161 BOOST_CHECK_EQUAL(static_cast<bool>(o1), true);
162 BOOST_CHECK_EQUAL(o1.value(), 2);
163
164 o1 = nullopt;
165 BOOST_CHECK_EQUAL(static_cast<bool>(o1), false);
166
167 o1 = 18763;
168 BOOST_CHECK_EQUAL(static_cast<bool>(o1), true);
169 BOOST_CHECK_EQUAL(o1.value(), 18763);
170}
171
172BOOST_AUTO_TEST_CASE(Access)
173{
174 optional<int> o1(1);
175 BOOST_CHECK_EQUAL(*o1, 1);
176
177 optional<std::string> o2("abc");
178 BOOST_CHECK_EQUAL(o2->size(), 3);
179}
180
181BOOST_AUTO_TEST_CASE(HasValue)
182{
183 optional<int> o1;
184 BOOST_CHECK_EQUAL(static_cast<bool>(o1), false);
185 BOOST_CHECK_EQUAL(!o1, true);
186 if (o1) {
187 BOOST_ERROR("o1 should evaluate to false");
188 }
189
190 optional<int> o2(1);
191 BOOST_CHECK_EQUAL(static_cast<bool>(o2), true);
192 BOOST_CHECK_EQUAL(!o2, false);
193 if (o2) {
194 }
195 else {
196 BOOST_ERROR("o2 should evaluate to true");
197 }
198}
199
200BOOST_AUTO_TEST_CASE(Value)
201{
202 optional<int> o1;
203 BOOST_CHECK_THROW(o1.value(), bad_optional_access);
204
205 optional<int> o2(2);
206 BOOST_CHECK_NO_THROW(o2.value());
207}
208
209BOOST_AUTO_TEST_CASE(ValueOr)
210{
211 optional<int> o1;
212 BOOST_CHECK_EQUAL(o1.value_or(3.0), 3);
213
214 optional<int> o2(2);
215 BOOST_CHECK_EQUAL(o2.value_or(3.0), 2);
216}
217
218BOOST_AUTO_TEST_CASE(Swap)
219{
220 optional<int> o1;
221 optional<int> o2(2);
222
223 o1.swap(o2);
224 BOOST_CHECK_EQUAL(static_cast<bool>(o1), true);
225 BOOST_CHECK_EQUAL(o1.value(), 2);
226 BOOST_CHECK_EQUAL(static_cast<bool>(o2), false);
227}
228
Junxiao Shi3688d722018-04-10 05:04:45 +0000229BOOST_AUTO_TEST_CASE(Reset)
230{
231 optional<int> o1(1);
232 o1.reset();
233 BOOST_CHECK_EQUAL(static_cast<bool>(o1), false);
234}
235
Junxiao Shi1aecae22016-08-30 11:23:59 +0000236BOOST_AUTO_TEST_CASE(Emplace)
237{
238 optional<std::pair<int, int>> o1;
239 o1.emplace(11, 12);
240 BOOST_CHECK_EQUAL(static_cast<bool>(o1), true);
241 BOOST_CHECK_EQUAL(o1.value().first, 11);
242 BOOST_CHECK_EQUAL(o1.value().second, 12);
243}
244
245BOOST_AUTO_TEST_CASE(Compare)
246{
247 optional<int> o0a;
248 optional<int> o0b;
249 optional<int> o1a(1);
250 optional<int> o1b(1);
251 optional<int> o2(2);
252
253 BOOST_CHECK_EQUAL(o0a == o0b, true);
254 BOOST_CHECK_EQUAL(o0a != o0b, false);
255 BOOST_CHECK_EQUAL(o0a < o0b, false);
256 BOOST_CHECK_EQUAL(o0a <= o0b, true);
257 BOOST_CHECK_EQUAL(o0a > o0b, false);
258 BOOST_CHECK_EQUAL(o0a >= o0b, true);
259
260 BOOST_CHECK_EQUAL(o1a == o1b, true);
261 BOOST_CHECK_EQUAL(o1a != o1b, false);
262 BOOST_CHECK_EQUAL(o1a < o1b, false);
263 BOOST_CHECK_EQUAL(o1a <= o1b, true);
264 BOOST_CHECK_EQUAL(o1a > o1b, false);
265 BOOST_CHECK_EQUAL(o1a >= o1b, true);
266
267 BOOST_CHECK_EQUAL(o0a == o1a, false);
268 BOOST_CHECK_EQUAL(o0a != o1a, true);
269 BOOST_CHECK_EQUAL(o0a < o1a, true);
270 BOOST_CHECK_EQUAL(o0a <= o1a, true);
271 BOOST_CHECK_EQUAL(o0a > o1a, false);
272 BOOST_CHECK_EQUAL(o0a >= o1a, false);
273
274 BOOST_CHECK_EQUAL(o1a == o0a, false);
275 BOOST_CHECK_EQUAL(o1a != o0a, true);
276 BOOST_CHECK_EQUAL(o1a < o0a, false);
277 BOOST_CHECK_EQUAL(o1a <= o0a, false);
278 BOOST_CHECK_EQUAL(o1a > o0a, true);
279 BOOST_CHECK_EQUAL(o1a >= o0a, true);
280
281 BOOST_CHECK_EQUAL(o1a == o2, false);
282 BOOST_CHECK_EQUAL(o1a != o2, true);
283 BOOST_CHECK_EQUAL(o1a < o2, true);
284 BOOST_CHECK_EQUAL(o1a <= o2, true);
285 BOOST_CHECK_EQUAL(o1a > o2, false);
286 BOOST_CHECK_EQUAL(o1a >= o2, false);
287
288 BOOST_CHECK_EQUAL(o2 == o1a, false);
289 BOOST_CHECK_EQUAL(o2 != o1a, true);
290 BOOST_CHECK_EQUAL(o2 < o1a, false);
291 BOOST_CHECK_EQUAL(o2 <= o1a, false);
292 BOOST_CHECK_EQUAL(o2 > o1a, true);
293 BOOST_CHECK_EQUAL(o2 >= o1a, true);
294}
295
296BOOST_AUTO_TEST_CASE(MakeOptional)
297{
298 auto o1 = make_optional(1);
299 static_assert(std::is_same<decltype(o1), optional<int>>::value, "o1 must be optional<int>");
300 BOOST_CHECK_EQUAL(static_cast<bool>(o1), true);
301 BOOST_CHECK_EQUAL(o1.value(), 1);
302
303 auto o2 = make_optional<std::pair<int, int>>(21, 22);
304 static_assert(std::is_same<decltype(o2), optional<std::pair<int, int>>>::value,
305 "o2 must be optional<std::pair<int, int>>");
306 BOOST_CHECK_EQUAL(static_cast<bool>(o2), true);
307 BOOST_CHECK_EQUAL(o2.value().first, 21);
308 BOOST_CHECK_EQUAL(o2.value().second, 22);
309}
310
311BOOST_AUTO_TEST_SUITE_END() // Optional
312
Davide Pesaventocf415762017-02-25 23:46:47 -0500313BOOST_AUTO_TEST_CASE(OstreamJoiner)
314{
315 boost::test_tools::output_test_stream os;
316
317 auto joiner1 = ostream_joiner<char>(os, ' ');
318 auto joiner2 = make_ostream_joiner(os, ' ');
319 static_assert(std::is_same<decltype(joiner1), decltype(joiner2)>::value, "");
320
321 std::vector<int> v(5);
322 std::iota(v.begin(), v.end(), 1);
323 std::copy(v.begin(), v.end(), joiner2);
324 BOOST_CHECK(os.is_equal("1 2 3 4 5"));
325
326 auto joiner3 = make_ostream_joiner(os, "...");
327 std::copy(v.begin(), v.end(), joiner3);
328 BOOST_CHECK(os.is_equal("1...2...3...4...5"));
329
330 joiner3 = "one";
331 BOOST_CHECK(os.is_equal("one"));
332 joiner3 = "two";
333 BOOST_CHECK(os.is_equal("...two"));
334 ++joiner3 = "three";
335 BOOST_CHECK(os.is_equal("...three"));
336 joiner3++ = "four";
337 BOOST_CHECK(os.is_equal("...four"));
338
339 std::copy(v.begin(), v.end(), make_ostream_joiner(os, ""));
340 BOOST_CHECK(os.is_equal("12345"));
341
342 std::string delimiter("_");
343 std::copy(v.begin(), v.end(), make_ostream_joiner(os, delimiter));
344 BOOST_CHECK(os.is_equal("1_2_3_4_5"));
345}
346
Junxiao Shi1aecae22016-08-30 11:23:59 +0000347BOOST_AUTO_TEST_SUITE_END() // TestBackports
348BOOST_AUTO_TEST_SUITE_END() // Util
Davide Pesavento409cc202015-09-19 14:13:16 +0200349
350} // namespace tests
351} // namespace ndn