blob: 31df7f04ed86420ca67ddac125d3b45611d3430a [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- 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 Shie349ea12014-03-12 01:32:42 -07008#include "tests/fw/dummy-strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -07009
10#include "tests/test-common.hpp"
11
12namespace nfd {
13namespace tests {
14
15BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture)
16
17using fw::Strategy;
18
Junxiao Shibb5105f2014-03-03 12:06:45 -070019BOOST_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 Shie349ea12014-03-12 01:32:42 -070025 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(boost::ref(forwarder), nameP);
26 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(boost::ref(forwarder), nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070027
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 Shie349ea12014-03-12 01:32:42 -070035 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070036 // { '/'=>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 Shie349ea12014-03-12 01:32:42 -070042 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070043 // { '/'=>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 Shie349ea12014-03-12 01:32:42 -070060 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -070061 // { '/'=>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 Shie349ea12014-03-12 01:32:42 -070074 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070075
Junxiao Shie349ea12014-03-12 01:32:42 -070076 BOOST_CHECK(table.insert("ndn:/", nameQ));
77 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070078 // { '/'=>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 Shie349ea12014-03-12 01:32:42 -070086class PStrategyInfo : public fw::StrategyInfo
87{
88};
89
90BOOST_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 Shibb5105f2014-03-03 12:06:45 -0700134BOOST_AUTO_TEST_SUITE_END()
135
136} // namespace tests
137} // namespace nfd