Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 1 | /* -*- 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 | |
| 26 | namespace ndn { |
| 27 | namespace detail { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(Detail) |
| 31 | BOOST_AUTO_TEST_SUITE(TestCancelHandle) |
| 32 | |
| 33 | static CancelHandle |
| 34 | makeDummyCancelHandle(int& nCancels) |
| 35 | { |
| 36 | return CancelHandle([&] { ++nCancels; }); |
| 37 | } |
| 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(PlainHandle) |
| 40 | |
| 41 | BOOST_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 | |
| 54 | BOOST_AUTO_TEST_SUITE_END() // PlainHandle |
| 55 | |
| 56 | BOOST_AUTO_TEST_SUITE(ScopedHandle) |
| 57 | |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 58 | using ScopedTestHandle = ScopedCancelHandle<CancelHandle>; |
| 59 | |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(ManualCancel) |
| 61 | { |
| 62 | int nCancels = 0; |
| 63 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 64 | ScopedTestHandle hdl = makeDummyCancelHandle(nCancels); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 65 | 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 | |
| 73 | BOOST_AUTO_TEST_CASE(Destruct) |
| 74 | { |
| 75 | int nCancels = 0; |
| 76 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 77 | ScopedTestHandle hdl = makeDummyCancelHandle(nCancels); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(nCancels, 0); |
| 79 | } // hdl goes out of scope |
| 80 | BOOST_CHECK_EQUAL(nCancels, 1); |
| 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(Assign) |
| 84 | { |
| 85 | int nCancels1 = 0, nCancels2 = 0; |
| 86 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 87 | ScopedTestHandle hdl = makeDummyCancelHandle(nCancels1); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 88 | 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 | |
| 95 | BOOST_AUTO_TEST_CASE(Release) |
| 96 | { |
| 97 | int nCancels = 0; |
| 98 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 99 | ScopedTestHandle hdl = makeDummyCancelHandle(nCancels); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 100 | hdl.release(); |
| 101 | hdl.cancel(); // no effect |
| 102 | } // hdl goes out of scope |
| 103 | BOOST_CHECK_EQUAL(nCancels, 0); |
| 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_CASE(MoveConstruct) |
| 107 | { |
| 108 | int nCancels = 0; |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 109 | unique_ptr<ScopedTestHandle> hdl1; |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 110 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 111 | ScopedTestHandle hdl2 = makeDummyCancelHandle(nCancels); |
| 112 | hdl1 = make_unique<ScopedTestHandle>(std::move(hdl2)); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 113 | } // hdl2 goes out of scope |
| 114 | BOOST_CHECK_EQUAL(nCancels, 0); |
| 115 | hdl1.reset(); |
| 116 | BOOST_CHECK_EQUAL(nCancels, 1); |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(MoveAssign) |
| 120 | { |
| 121 | int nCancels = 0; |
| 122 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 123 | ScopedTestHandle hdl1; |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 124 | { |
Davide Pesavento | 720e25c | 2019-07-14 01:33:52 -0400 | [diff] [blame^] | 125 | ScopedTestHandle hdl2 = makeDummyCancelHandle(nCancels); |
Junxiao Shi | ec475a7 | 2019-01-13 21:53:55 +0000 | [diff] [blame] | 126 | 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 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() // ScopedHandle |
| 134 | BOOST_AUTO_TEST_SUITE_END() // TestCancelHandle |
| 135 | BOOST_AUTO_TEST_SUITE_END() // Detail |
| 136 | |
| 137 | } // namespace tests |
| 138 | } // namespace detail |
| 139 | } // namespace ndn |