Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/strategy-info-host.hpp" |
| 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 34 | using fw::StrategyInfo; |
| 35 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 36 | static int g_DummyStrategyInfo_count = 0; |
| 37 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 38 | class DummyStrategyInfo : public StrategyInfo, noncopyable |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 41 | static constexpr int |
| 42 | getTypeId() |
| 43 | { |
| 44 | return 1; |
| 45 | } |
| 46 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 47 | DummyStrategyInfo(int id) |
| 48 | : m_id(id) |
| 49 | { |
| 50 | ++g_DummyStrategyInfo_count; |
| 51 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 53 | virtual |
| 54 | ~DummyStrategyInfo() |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 55 | { |
| 56 | --g_DummyStrategyInfo_count; |
| 57 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 59 | public: |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 60 | int m_id; |
| 61 | }; |
| 62 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 63 | class DummyStrategyInfo2 : public StrategyInfo, noncopyable |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 64 | { |
| 65 | public: |
| 66 | static constexpr int |
| 67 | getTypeId() |
| 68 | { |
| 69 | return 2; |
| 70 | } |
| 71 | |
| 72 | DummyStrategyInfo2(int id) |
| 73 | : m_id(id) |
| 74 | { |
| 75 | } |
| 76 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 77 | public: |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 78 | int m_id; |
| 79 | }; |
| 80 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 81 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 82 | BOOST_FIXTURE_TEST_SUITE(TestStrategyInfoHost, GlobalIoFixture) |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(Basic) |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 85 | { |
| 86 | StrategyInfoHost host; |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 87 | g_DummyStrategyInfo_count = 0; |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 88 | bool isNew = false; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 90 | DummyStrategyInfo* info = nullptr; |
| 91 | std::tie(info, isNew) = host.insertStrategyInfo<DummyStrategyInfo>(3503); |
| 92 | BOOST_CHECK_EQUAL(isNew, true); |
| 93 | BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 1); |
| 94 | BOOST_REQUIRE(info != nullptr); |
| 95 | BOOST_CHECK_EQUAL(info->m_id, 3503); |
| 96 | BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>(), info); |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 98 | DummyStrategyInfo* info2 = nullptr; |
| 99 | std::tie(info2, isNew) = host.insertStrategyInfo<DummyStrategyInfo>(1032); |
| 100 | BOOST_CHECK_EQUAL(isNew, false); |
| 101 | BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 1); |
| 102 | BOOST_CHECK_EQUAL(info2, info); |
| 103 | BOOST_CHECK_EQUAL(info->m_id, 3503); |
| 104 | BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>(), info); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 106 | host.clearStrategyInfo(); |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 107 | BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr); |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0); |
| 109 | } |
| 110 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(Types) |
| 112 | { |
| 113 | StrategyInfoHost host; |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 114 | g_DummyStrategyInfo_count = 0; |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 5b3feb6 | 2016-08-19 01:51:41 +0000 | [diff] [blame] | 116 | host.insertStrategyInfo<DummyStrategyInfo>(8063); |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 117 | BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr); |
| 118 | BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063); |
| 119 | |
Junxiao Shi | 5b3feb6 | 2016-08-19 01:51:41 +0000 | [diff] [blame] | 120 | host.insertStrategyInfo<DummyStrategyInfo2>(2871); |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 121 | BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr); |
| 122 | BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo2>()->m_id, 2871); |
| 123 | |
| 124 | BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr); |
| 125 | BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063); |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 126 | |
| 127 | BOOST_CHECK_EQUAL(host.eraseStrategyInfo<DummyStrategyInfo>(), 1); |
| 128 | BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr); |
| 129 | BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0); |
| 130 | BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr); |
| 131 | |
| 132 | BOOST_CHECK_EQUAL(host.eraseStrategyInfo<DummyStrategyInfo>(), 0); |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Junxiao Shi | fc02186 | 2016-08-25 21:51:18 +0000 | [diff] [blame] | 135 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyInfoHost |
| 136 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 138 | } // namespace tests |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 139 | } // namespace nfd |