Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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/>. |
| 24 | **/ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/strategy-choice.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 27 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture) |
| 35 | |
| 36 | using fw::Strategy; |
| 37 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_CASE(Effective) |
| 39 | { |
| 40 | Forwarder forwarder; |
| 41 | Name nameP("ndn:/strategy/P"); |
| 42 | Name nameQ("ndn:/strategy/Q"); |
| 43 | Name nameZ("ndn:/strategy/Z"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 44 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 45 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 46 | |
| 47 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 48 | |
| 49 | // install |
| 50 | BOOST_CHECK_EQUAL(table.install(strategyP), true); |
| 51 | BOOST_CHECK_EQUAL(table.install(strategyQ), true); |
| 52 | BOOST_CHECK_EQUAL(table.install(strategyQ), false); |
| 53 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 54 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 55 | // { '/'=>P } |
| 56 | |
| 57 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 58 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 59 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 60 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 61 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 62 | // { '/'=>P, '/A/B'=>P } |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 65 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 66 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 67 | // same instance |
| 68 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get()); |
| 69 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get()); |
| 70 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get()); |
| 71 | |
| 72 | table.erase("ndn:/A"); // no effect |
| 73 | // { '/'=>P, '/A/B'=>P } |
| 74 | |
| 75 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 76 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 77 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 78 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 79 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 80 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 81 | |
| 82 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 83 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 84 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 85 | |
| 86 | table.erase("ndn:/A/B"); |
| 87 | // { '/'=>P, '/A'=>Q } |
| 88 | |
| 89 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 90 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 91 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ); |
| 92 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 93 | BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 94 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 95 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 96 | BOOST_CHECK(table.insert("ndn:/A", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 97 | // { '/'=>Q, '/A'=>P } |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ); |
| 100 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 101 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 102 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ); |
| 103 | } |
| 104 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame^] | 105 | //XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>)) |
| 106 | // is also failing. There might be a problem with ForwardIterator concept checking. |
| 107 | //BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>)); |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 110 | { |
| 111 | |
| 112 | Forwarder forwarder; |
| 113 | Name nameP("ndn:/strategy/P"); |
| 114 | Name nameQ("ndn:/strategy/Q"); |
| 115 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 116 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
| 117 | |
| 118 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 119 | table.install(strategyP); |
| 120 | table.install(strategyQ); |
| 121 | |
| 122 | table.insert("ndn:/", nameP); |
| 123 | table.insert("ndn:/A/B", nameQ); |
| 124 | table.insert("ndn:/A/B/C", nameP); |
| 125 | table.insert("ndn:/D", nameP); |
| 126 | table.insert("ndn:/E", nameQ); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(table.size(), 5); |
| 129 | |
| 130 | std::map<Name, Name> map; // namespace=>strategyName |
| 131 | for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) { |
| 132 | map[it->getPrefix()] = it->getStrategyName(); |
| 133 | } |
| 134 | BOOST_CHECK_EQUAL(map.size(), 5); |
| 135 | BOOST_CHECK_EQUAL(map["ndn:/"], nameP); |
| 136 | BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ); |
| 137 | BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP); |
| 138 | BOOST_CHECK_EQUAL(map["ndn:/D"], nameP); |
| 139 | BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ); |
| 140 | BOOST_CHECK_EQUAL(map.size(), 5); |
| 141 | } |
| 142 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 143 | class PStrategyInfo : public fw::StrategyInfo |
| 144 | { |
| 145 | }; |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(ClearStrategyInfo) |
| 148 | { |
| 149 | Forwarder forwarder; |
| 150 | Name nameP("ndn:/strategy/P"); |
| 151 | Name nameQ("ndn:/strategy/Q"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 152 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 153 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 154 | |
| 155 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 156 | Measurements& measurements = forwarder.getMeasurements(); |
| 157 | |
| 158 | // install |
| 159 | table.install(strategyP); |
| 160 | table.install(strategyQ); |
| 161 | |
| 162 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
| 163 | // { '/'=>P } |
| 164 | measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 165 | measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 166 | measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 167 | measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 168 | |
| 169 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
| 170 | // { '/'=>P, '/A/B'=>P } |
| 171 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 172 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 173 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 174 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 175 | |
| 176 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
| 177 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 178 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 179 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 180 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 181 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 182 | |
| 183 | table.erase("ndn:/A/B"); |
| 184 | // { '/'=>P, '/A'=>Q } |
| 185 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 186 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 187 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 188 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 189 | } |
| 190 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 191 | BOOST_AUTO_TEST_SUITE_END() |
| 192 | |
| 193 | } // namespace tests |
| 194 | } // namespace nfd |