Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "table/strategy-choice.hpp" |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 8 | #include "tests/fw/dummy-strategy.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 9 | |
| 10 | #include "tests/test-common.hpp" |
| 11 | |
| 12 | namespace nfd { |
| 13 | namespace tests { |
| 14 | |
| 15 | BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture) |
| 16 | |
| 17 | using fw::Strategy; |
| 18 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 19 | BOOST_AUTO_TEST_CASE(Effective) |
| 20 | { |
| 21 | Forwarder forwarder; |
| 22 | Name nameP("ndn:/strategy/P"); |
| 23 | Name nameQ("ndn:/strategy/Q"); |
| 24 | Name nameZ("ndn:/strategy/Z"); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 25 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(boost::ref(forwarder), nameP); |
| 26 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(boost::ref(forwarder), nameQ); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 27 | |
| 28 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 29 | |
| 30 | // install |
| 31 | BOOST_CHECK_EQUAL(table.install(strategyP), true); |
| 32 | BOOST_CHECK_EQUAL(table.install(strategyQ), true); |
| 33 | BOOST_CHECK_EQUAL(table.install(strategyQ), false); |
| 34 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 35 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 36 | // { '/'=>P } |
| 37 | |
| 38 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 39 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 40 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 41 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 42 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 43 | // { '/'=>P, '/A/B'=>P } |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 46 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 47 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 48 | // same instance |
| 49 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get()); |
| 50 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get()); |
| 51 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get()); |
| 52 | |
| 53 | table.erase("ndn:/A"); // no effect |
| 54 | // { '/'=>P, '/A/B'=>P } |
| 55 | |
| 56 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 57 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 58 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 59 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 60 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 61 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 62 | |
| 63 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 64 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 65 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 66 | |
| 67 | table.erase("ndn:/A/B"); |
| 68 | // { '/'=>P, '/A'=>Q } |
| 69 | |
| 70 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 71 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 72 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ); |
| 73 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 74 | BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 76 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 77 | BOOST_CHECK(table.insert("ndn:/A", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 78 | // { '/'=>Q, '/A'=>P } |
| 79 | |
| 80 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ); |
| 81 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 82 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 83 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ); |
| 84 | } |
| 85 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 86 | class PStrategyInfo : public fw::StrategyInfo |
| 87 | { |
| 88 | }; |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(ClearStrategyInfo) |
| 91 | { |
| 92 | Forwarder forwarder; |
| 93 | Name nameP("ndn:/strategy/P"); |
| 94 | Name nameQ("ndn:/strategy/Q"); |
| 95 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(boost::ref(forwarder), nameP); |
| 96 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(boost::ref(forwarder), nameQ); |
| 97 | |
| 98 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 99 | Measurements& measurements = forwarder.getMeasurements(); |
| 100 | |
| 101 | // install |
| 102 | table.install(strategyP); |
| 103 | table.install(strategyQ); |
| 104 | |
| 105 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
| 106 | // { '/'=>P } |
| 107 | measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 108 | measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 109 | measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 110 | measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 111 | |
| 112 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
| 113 | // { '/'=>P, '/A/B'=>P } |
| 114 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 115 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 116 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 117 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 118 | |
| 119 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
| 120 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 121 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 122 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 123 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 124 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 125 | |
| 126 | table.erase("ndn:/A/B"); |
| 127 | // { '/'=>P, '/A'=>Q } |
| 128 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 129 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 130 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 131 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 132 | } |
| 133 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 134 | BOOST_AUTO_TEST_SUITE_END() |
| 135 | |
| 136 | } // namespace tests |
| 137 | } // namespace nfd |