blob: 4eaf9b0d25be11978f80a6a6af12df848d0b1c25 [file] [log] [blame]
Junxiao Shie5e2fce2014-02-10 20:01:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5b3feb62016-08-19 01:51:41 +00003 * Copyright (c) 2014-2016, 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"
Junxiao Shie5e2fce2014-02-10 20:01:53 -070029
30namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031namespace tests {
Junxiao Shie5e2fce2014-02-10 20:01:53 -070032
Junxiao Shi39ef2612014-11-29 20:35:19 -070033using fw::StrategyInfo;
34
Junxiao Shie5e2fce2014-02-10 20:01:53 -070035static int g_DummyStrategyInfo_count = 0;
36
Junxiao Shi39ef2612014-11-29 20:35:19 -070037class DummyStrategyInfo : public StrategyInfo
Junxiao Shie5e2fce2014-02-10 20:01:53 -070038{
39public:
Junxiao Shi39ef2612014-11-29 20:35:19 -070040 static constexpr int
41 getTypeId()
42 {
43 return 1;
44 }
45
Junxiao Shie5e2fce2014-02-10 20:01:53 -070046 DummyStrategyInfo(int id)
47 : m_id(id)
48 {
49 ++g_DummyStrategyInfo_count;
50 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070051
Junxiao Shi39ef2612014-11-29 20:35:19 -070052 virtual
53 ~DummyStrategyInfo()
Junxiao Shie5e2fce2014-02-10 20:01:53 -070054 {
55 --g_DummyStrategyInfo_count;
56 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
Junxiao Shie5e2fce2014-02-10 20:01:53 -070058 int m_id;
59};
60
Junxiao Shi39ef2612014-11-29 20:35:19 -070061class DummyStrategyInfo2 : public StrategyInfo
62{
63public:
64 static constexpr int
65 getTypeId()
66 {
67 return 2;
68 }
69
70 DummyStrategyInfo2(int id)
71 : m_id(id)
72 {
73 }
74
75 int m_id;
76};
77
Junxiao Shid9ee45c2014-02-27 15:38:11 -070078BOOST_FIXTURE_TEST_SUITE(TableStrategyInfoHost, BaseFixture)
Junxiao Shie5e2fce2014-02-10 20:01:53 -070079
80BOOST_AUTO_TEST_CASE(SetGetClear)
81{
82 StrategyInfoHost host;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070083
Junxiao Shi39ef2612014-11-29 20:35:19 -070084 BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070085
Junxiao Shie5e2fce2014-02-10 20:01:53 -070086 g_DummyStrategyInfo_count = 0;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070087
Junxiao Shie5e2fce2014-02-10 20:01:53 -070088 shared_ptr<DummyStrategyInfo> info = make_shared<DummyStrategyInfo>(7591);
89 host.setStrategyInfo(info);
Junxiao Shi39ef2612014-11-29 20:35:19 -070090 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
Junxiao Shie5e2fce2014-02-10 20:01:53 -070091 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 7591);
92
93 info.reset(); // unlink local reference
94 // host should still have a reference to info
Junxiao Shi39ef2612014-11-29 20:35:19 -070095 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
Junxiao Shie5e2fce2014-02-10 20:01:53 -070096 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 7591);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070097
Junxiao Shie5e2fce2014-02-10 20:01:53 -070098 host.clearStrategyInfo();
Junxiao Shi39ef2612014-11-29 20:35:19 -070099 BOOST_CHECK(host.getStrategyInfo<DummyStrategyInfo>() == nullptr);
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700100 BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0);
101}
102
Junxiao Shi39ef2612014-11-29 20:35:19 -0700103BOOST_AUTO_TEST_CASE(Create)
104{
105 StrategyInfoHost host;
106
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000107 host.insertStrategyInfo<DummyStrategyInfo>(3503);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700108 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
109 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 3503);
110
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000111 host.insertStrategyInfo<DummyStrategyInfo>(1032);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700112 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
113 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 3503);
114
115 host.setStrategyInfo<DummyStrategyInfo>(nullptr);
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000116 host.insertStrategyInfo<DummyStrategyInfo>(9956);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700117 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
118 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 9956);
119}
120
121BOOST_AUTO_TEST_CASE(Types)
122{
123 StrategyInfoHost host;
124
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000125 host.insertStrategyInfo<DummyStrategyInfo>(8063);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700126 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
127 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063);
128
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000129 host.insertStrategyInfo<DummyStrategyInfo2>(2871);
Junxiao Shi39ef2612014-11-29 20:35:19 -0700130 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr);
131 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo2>()->m_id, 2871);
132
133 BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
134 BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063);
135}
136
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700137BOOST_AUTO_TEST_SUITE_END()
138
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700139} // namespace tests
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700140} // namespace nfd