Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -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/measurements-accessor.hpp" |
| 8 | #include "fw/forwarder.hpp" |
| 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(TableMeasurementsAccessor) |
| 15 | |
| 16 | class MeasurementsAccessorTestStrategy : public fw::Strategy |
| 17 | { |
| 18 | public: |
| 19 | explicit |
| 20 | MeasurementsAccessorTestStrategy(Forwarder& forwarder) |
| 21 | : Strategy(forwarder) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | virtual |
| 26 | ~MeasurementsAccessorTestStrategy() |
| 27 | |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | virtual void |
| 32 | afterReceiveInterest(const Face& inFace, |
| 33 | const Interest& interest, |
| 34 | shared_ptr<fib::Entry> fibEntry, |
| 35 | shared_ptr<pit::Entry> pitEntry) |
| 36 | { |
| 37 | BOOST_ASSERT(false); |
| 38 | } |
| 39 | |
| 40 | public: // accessors |
| 41 | MeasurementsAccessor& |
| 42 | getMeasurements_accessor() |
| 43 | { |
| 44 | return this->getMeasurements(); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(Access) |
| 49 | { |
| 50 | boost::asio::io_service ioService; |
| 51 | Forwarder forwarder(ioService); |
| 52 | shared_ptr<MeasurementsAccessorTestStrategy> strategy1 = |
| 53 | make_shared<MeasurementsAccessorTestStrategy>(boost::ref(forwarder)); |
| 54 | shared_ptr<MeasurementsAccessorTestStrategy> strategy2 = |
| 55 | make_shared<MeasurementsAccessorTestStrategy>(boost::ref(forwarder)); |
| 56 | |
| 57 | Name nameRoot("ndn:/"); |
| 58 | Name nameA ("ndn:/A"); |
| 59 | Name nameAB ("ndn:/A/B"); |
| 60 | Name nameABC ("ndn:/A/B/C"); |
| 61 | Name nameAD ("ndn:/A/D"); |
| 62 | |
| 63 | Fib& fib = forwarder.getFib(); |
| 64 | fib.insert(nameRoot).first->setStrategy(strategy1); |
| 65 | fib.insert(nameA ).first->setStrategy(strategy2); |
| 66 | fib.insert(nameAB ).first->setStrategy(strategy1); |
| 67 | |
| 68 | MeasurementsAccessor& accessor1 = strategy1->getMeasurements_accessor(); |
| 69 | MeasurementsAccessor& accessor2 = strategy2->getMeasurements_accessor(); |
| 70 | |
| 71 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameRoot)), true); |
| 72 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameA )), false); |
| 73 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAB )), true); |
| 74 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameABC )), true); |
| 75 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAD )), false); |
| 76 | |
| 77 | shared_ptr<measurements::Entry> entryRoot = forwarder.getMeasurements().get(nameRoot); |
| 78 | BOOST_CHECK_NO_THROW(accessor1.getParent(entryRoot)); |
| 79 | |
| 80 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameRoot)), false); |
| 81 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameA )), true); |
| 82 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameAB )), false); |
| 83 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameABC )), false); |
| 84 | BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameAD )), true); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_SUITE_END() |
| 88 | |
| 89 | } // namespace nfd |