blob: b87f9cdb1edd46e1b4ee166e4118761a0d5ba9c8 [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
58BOOST_AUTO_TEST_CASE(ManualCancel)
59{
60 int nCancels = 0;
61 {
62 ScopedCancelHandle hdl = makeDummyCancelHandle(nCancels);
63 BOOST_CHECK_EQUAL(nCancels, 0);
64
65 hdl.cancel();
66 BOOST_CHECK_EQUAL(nCancels, 1);
67 } // hdl goes out of scope
68 BOOST_CHECK_EQUAL(nCancels, 1);
69}
70
71BOOST_AUTO_TEST_CASE(Destruct)
72{
73 int nCancels = 0;
74 {
75 ScopedCancelHandle hdl = makeDummyCancelHandle(nCancels);
76 BOOST_CHECK_EQUAL(nCancels, 0);
77 } // hdl goes out of scope
78 BOOST_CHECK_EQUAL(nCancels, 1);
79}
80
81BOOST_AUTO_TEST_CASE(Assign)
82{
83 int nCancels1 = 0, nCancels2 = 0;
84 {
85 ScopedCancelHandle hdl = makeDummyCancelHandle(nCancels1);
86 hdl = makeDummyCancelHandle(nCancels2);
87 BOOST_CHECK_EQUAL(nCancels1, 1);
88 BOOST_CHECK_EQUAL(nCancels2, 0);
89 } // hdl goes out of scope
90 BOOST_CHECK_EQUAL(nCancels2, 1);
91}
92
93BOOST_AUTO_TEST_CASE(Release)
94{
95 int nCancels = 0;
96 {
97 ScopedCancelHandle hdl = makeDummyCancelHandle(nCancels);
98 hdl.release();
99 hdl.cancel(); // no effect
100 } // hdl goes out of scope
101 BOOST_CHECK_EQUAL(nCancels, 0);
102}
103
104BOOST_AUTO_TEST_CASE(MoveConstruct)
105{
106 int nCancels = 0;
107 unique_ptr<ScopedCancelHandle> hdl1;
108 {
109 ScopedCancelHandle hdl2 = makeDummyCancelHandle(nCancels);
110 hdl1 = make_unique<ScopedCancelHandle>(std::move(hdl2));
111 } // hdl2 goes out of scope
112 BOOST_CHECK_EQUAL(nCancels, 0);
113 hdl1.reset();
114 BOOST_CHECK_EQUAL(nCancels, 1);
115}
116
117BOOST_AUTO_TEST_CASE(MoveAssign)
118{
119 int nCancels = 0;
120 {
121 ScopedCancelHandle hdl1;
122 {
123 ScopedCancelHandle hdl2 = makeDummyCancelHandle(nCancels);
124 hdl1 = std::move(hdl2);
125 } // hdl2 goes out of scope
126 BOOST_CHECK_EQUAL(nCancels, 0);
127 } // hdl1 goes out of scope
128 BOOST_CHECK_EQUAL(nCancels, 1);
129}
130
131BOOST_AUTO_TEST_SUITE_END() // ScopedHandle
132BOOST_AUTO_TEST_SUITE_END() // TestCancelHandle
133BOOST_AUTO_TEST_SUITE_END() // Detail
134
135} // namespace tests
136} // namespace detail
137} // namespace ndn