blob: e4323e42f3ca020cc93e1d8a04a7ea0a86e00065 [file] [log] [blame]
Junxiao Shie5e2fce2014-02-10 20:01:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi39ef2612014-11-29 20:35:19 -070024 */
Junxiao Shie5e2fce2014-02-10 20:01:53 -070025
26#include "table/strategy-info-host.hpp"
27
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shie5e2fce2014-02-10 20:01:53 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shie5e2fce2014-02-10 20:01:53 -070033
Junxiao Shi39ef2612014-11-29 20:35:19 -070034using fw::StrategyInfo;
35
Junxiao Shie5e2fce2014-02-10 20:01:53 -070036static int g_DummyStrategyInfo_count = 0;
37
Junxiao Shifc021862016-08-25 21:51:18 +000038class DummyStrategyInfo : public StrategyInfo, noncopyable
Junxiao Shie5e2fce2014-02-10 20:01:53 -070039{
40public:
Junxiao Shi39ef2612014-11-29 20:35:19 -070041 static constexpr int
42 getTypeId()
43 {
44 return 1;
45 }
46
Junxiao Shie5e2fce2014-02-10 20:01:53 -070047 DummyStrategyInfo(int id)
48 : m_id(id)
49 {
50 ++g_DummyStrategyInfo_count;
51 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070052
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040053 ~DummyStrategyInfo() override
Junxiao Shie5e2fce2014-02-10 20:01:53 -070054 {
55 --g_DummyStrategyInfo_count;
56 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
Junxiao Shifc021862016-08-25 21:51:18 +000058public:
Junxiao Shie5e2fce2014-02-10 20:01:53 -070059 int m_id;
60};
61
Junxiao Shifc021862016-08-25 21:51:18 +000062class DummyStrategyInfo2 : public StrategyInfo, noncopyable
Junxiao Shi39ef2612014-11-29 20:35:19 -070063{
64public:
65 static constexpr int
66 getTypeId()
67 {
68 return 2;
69 }
70
71 DummyStrategyInfo2(int id)
72 : m_id(id)
73 {
74 }
75
Junxiao Shifc021862016-08-25 21:51:18 +000076public:
Junxiao Shi39ef2612014-11-29 20:35:19 -070077 int m_id;
78};
79
Junxiao Shifc021862016-08-25 21:51:18 +000080BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040081BOOST_FIXTURE_TEST_SUITE(TestStrategyInfoHost, GlobalIoFixture)
Junxiao Shie5e2fce2014-02-10 20:01:53 -070082
Junxiao Shifc021862016-08-25 21:51:18 +000083BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shie5e2fce2014-02-10 20:01:53 -070084{
85 StrategyInfoHost host;
Junxiao Shie5e2fce2014-02-10 20:01:53 -070086 g_DummyStrategyInfo_count = 0;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070087
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040088 auto [info, isNew] = host.insertStrategyInfo<DummyStrategyInfo>(3503);
Junxiao Shifc021862016-08-25 21:51:18 +000089 BOOST_CHECK_EQUAL(isNew, true);
90 BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 1);
91 BOOST_REQUIRE(info != nullptr);
92 BOOST_CHECK_EQUAL(info->m_id, 3503);
93 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>(), info);
Junxiao Shie5e2fce2014-02-10 20:01:53 -070094
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040095 auto [info2, isNew2] = host.insertStrategyInfo<DummyStrategyInfo>(1032);
96 BOOST_CHECK_EQUAL(isNew2, false);
Junxiao Shifc021862016-08-25 21:51:18 +000097 BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 1);
98 BOOST_CHECK_EQUAL(info2, info);
99 BOOST_CHECK_EQUAL(info->m_id, 3503);
100 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>(), info);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700101
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700102 host.clearStrategyInfo();
Junxiao Shi39ef2612014-11-29 20:35:19 -0700103 BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr);
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700104 BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0);
105}
106
Junxiao Shi39ef2612014-11-29 20:35:19 -0700107BOOST_AUTO_TEST_CASE(Types)
108{
109 StrategyInfoHost host;
Junxiao Shifc021862016-08-25 21:51:18 +0000110 g_DummyStrategyInfo_count = 0;
Junxiao Shi39ef2612014-11-29 20:35:19 -0700111
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000112 host.insertStrategyInfo<DummyStrategyInfo>(8063);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700113 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
114 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063);
115
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000116 host.insertStrategyInfo<DummyStrategyInfo2>(2871);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700117 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr);
118 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo2>()->m_id, 2871);
119
120 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
121 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063);
Junxiao Shifc021862016-08-25 21:51:18 +0000122
123 BOOST_CHECK_EQUAL(host.eraseStrategyInfo<DummyStrategyInfo>(), 1);
124 BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr);
125 BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0);
126 BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr);
127
128 BOOST_CHECK_EQUAL(host.eraseStrategyInfo<DummyStrategyInfo>(), 0);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700129}
130
Junxiao Shifc021862016-08-25 21:51:18 +0000131BOOST_AUTO_TEST_SUITE_END() // TestStrategyInfoHost
132BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700133
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700134} // namespace tests
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700135} // namespace nfd