blob: e35fe5525774d04f9ab5fb6adfd67ecaf8579bbd [file] [log] [blame]
Junxiao Shiec475a72019-01-13 21:53:55 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Junxiao Shiec475a72019-01-13 21:53:55 +00004 *
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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040026namespace ndn::tests {
27
28using ndn::detail::CancelHandle;
Junxiao Shiec475a72019-01-13 21:53:55 +000029
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 Pesavento47ce2ee2023-05-09 01:33:33 -040058using ScopedTestHandle = detail::ScopedCancelHandle<CancelHandle>;
Davide Pesavento720e25c2019-07-14 01:33:52 -040059
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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400137} // namespace ndn::tests