blob: 2e951ddeabf96c21764e41b6a1b867e313301299 [file] [log] [blame]
Junxiao Shiec475a72019-01-13 21:53:55 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2019 Regents of the University of California.
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 "ndn-cxx/detail/cancel-handle.hpp"
23
24#include "tests/boost-test.hpp"
25
26namespace ndn {
27namespace detail {
28namespace tests {
29
30BOOST_AUTO_TEST_SUITE(Detail)
31BOOST_AUTO_TEST_SUITE(TestCancelHandle)
32
33static CancelHandle
34makeDummyCancelHandle(int& nCancels)
35{
36 return CancelHandle([&] { ++nCancels; });
37}
38
39BOOST_AUTO_TEST_SUITE(PlainHandle)
40
41BOOST_AUTO_TEST_CASE(ManualCancel)
42{
43 int nCancels = 0;
44 auto hdl = makeDummyCancelHandle(nCancels);
45 BOOST_CHECK_EQUAL(nCancels, 0);
46
47 hdl.cancel();
48 BOOST_CHECK_EQUAL(nCancels, 1);
49
50 hdl = CancelHandle();
51 BOOST_CHECK_EQUAL(nCancels, 1);
52}
53
54BOOST_AUTO_TEST_SUITE_END() // PlainHandle
55
56BOOST_AUTO_TEST_SUITE(ScopedHandle)
57
Davide Pesavento720e25c2019-07-14 01:33:52 -040058using ScopedTestHandle = ScopedCancelHandle<CancelHandle>;
59
Junxiao Shiec475a72019-01-13 21:53:55 +000060BOOST_AUTO_TEST_CASE(ManualCancel)
61{
62 int nCancels = 0;
63 {
Davide Pesavento720e25c2019-07-14 01:33:52 -040064 ScopedTestHandle hdl = makeDummyCancelHandle(nCancels);
Junxiao Shiec475a72019-01-13 21:53:55 +000065 BOOST_CHECK_EQUAL(nCancels, 0);
66
67 hdl.cancel();
68 BOOST_CHECK_EQUAL(nCancels, 1);
69 } // hdl goes out of scope
70 BOOST_CHECK_EQUAL(nCancels, 1);
71}
72
73BOOST_AUTO_TEST_CASE(Destruct)
74{
75 int nCancels = 0;
76 {
Davide Pesavento720e25c2019-07-14 01:33:52 -040077 ScopedTestHandle hdl = makeDummyCancelHandle(nCancels);
Junxiao Shiec475a72019-01-13 21:53:55 +000078 BOOST_CHECK_EQUAL(nCancels, 0);
79 } // hdl goes out of scope
80 BOOST_CHECK_EQUAL(nCancels, 1);
81}
82
83BOOST_AUTO_TEST_CASE(Assign)
84{
85 int nCancels1 = 0, nCancels2 = 0;
86 {
Davide Pesavento720e25c2019-07-14 01:33:52 -040087 ScopedTestHandle hdl = makeDummyCancelHandle(nCancels1);
Junxiao Shiec475a72019-01-13 21:53:55 +000088 hdl = makeDummyCancelHandle(nCancels2);
89 BOOST_CHECK_EQUAL(nCancels1, 1);
90 BOOST_CHECK_EQUAL(nCancels2, 0);
91 } // hdl goes out of scope
92 BOOST_CHECK_EQUAL(nCancels2, 1);
93}
94
95BOOST_AUTO_TEST_CASE(Release)
96{
97 int nCancels = 0;
98 {
Davide Pesavento720e25c2019-07-14 01:33:52 -040099 ScopedTestHandle hdl = makeDummyCancelHandle(nCancels);
Junxiao Shiec475a72019-01-13 21:53:55 +0000100 hdl.release();
101 hdl.cancel(); // no effect
102 } // hdl goes out of scope
103 BOOST_CHECK_EQUAL(nCancels, 0);
104}
105
106BOOST_AUTO_TEST_CASE(MoveConstruct)
107{
108 int nCancels = 0;
Davide Pesavento720e25c2019-07-14 01:33:52 -0400109 unique_ptr<ScopedTestHandle> hdl1;
Junxiao Shiec475a72019-01-13 21:53:55 +0000110 {
Davide Pesavento720e25c2019-07-14 01:33:52 -0400111 ScopedTestHandle hdl2 = makeDummyCancelHandle(nCancels);
112 hdl1 = make_unique<ScopedTestHandle>(std::move(hdl2));
Junxiao Shiec475a72019-01-13 21:53:55 +0000113 } // hdl2 goes out of scope
114 BOOST_CHECK_EQUAL(nCancels, 0);
115 hdl1.reset();
116 BOOST_CHECK_EQUAL(nCancels, 1);
117}
118
119BOOST_AUTO_TEST_CASE(MoveAssign)
120{
121 int nCancels = 0;
122 {
Davide Pesavento720e25c2019-07-14 01:33:52 -0400123 ScopedTestHandle hdl1;
Junxiao Shiec475a72019-01-13 21:53:55 +0000124 {
Davide Pesavento720e25c2019-07-14 01:33:52 -0400125 ScopedTestHandle hdl2 = makeDummyCancelHandle(nCancels);
Junxiao Shiec475a72019-01-13 21:53:55 +0000126 hdl1 = std::move(hdl2);
127 } // hdl2 goes out of scope
128 BOOST_CHECK_EQUAL(nCancels, 0);
129 } // hdl1 goes out of scope
130 BOOST_CHECK_EQUAL(nCancels, 1);
131}
132
133BOOST_AUTO_TEST_SUITE_END() // ScopedHandle
134BOOST_AUTO_TEST_SUITE_END() // TestCancelHandle
135BOOST_AUTO_TEST_SUITE_END() // Detail
136
137} // namespace tests
138} // namespace detail
139} // namespace ndn