Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 2 | /* |
Junxiao Shi | 00e32fe | 2019-01-14 23:34:32 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 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. |
| 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 | */ |
| 25 | |
| 26 | /** \file |
| 27 | * This test suite tests instantiation logic in strategies. |
| 28 | */ |
| 29 | |
| 30 | // All strategies, sorted alphabetically. |
| 31 | #include "fw/access-strategy.hpp" |
| 32 | #include "fw/asf-strategy.hpp" |
| 33 | #include "fw/best-route-strategy.hpp" |
| 34 | #include "fw/best-route-strategy2.hpp" |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 35 | #include "fw/multicast-strategy.hpp" |
| 36 | #include "fw/ncc-strategy.hpp" |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 37 | #include "fw/self-learning-strategy.hpp" |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 38 | #include "fw/random-strategy.hpp" |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 39 | |
| 40 | #include "tests/test-common.hpp" |
| 41 | #include <boost/mpl/vector.hpp> |
| 42 | |
| 43 | namespace nfd { |
| 44 | namespace fw { |
| 45 | namespace tests { |
| 46 | |
| 47 | using namespace nfd::tests; |
| 48 | |
| 49 | BOOST_AUTO_TEST_SUITE(Fw) |
| 50 | BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation) |
| 51 | |
| 52 | template<typename S, bool CanAcceptParameters, uint64_t MinVersion> |
| 53 | class Test |
| 54 | { |
| 55 | public: |
| 56 | using Strategy = S; |
| 57 | |
| 58 | static bool |
| 59 | canAcceptParameters() |
| 60 | { |
| 61 | return CanAcceptParameters; |
| 62 | } |
| 63 | |
| 64 | static uint64_t |
| 65 | getMinVersion() |
| 66 | { |
| 67 | return MinVersion; |
| 68 | } |
| 69 | |
| 70 | static Name |
| 71 | getVersionedStrategyName(uint64_t version) |
| 72 | { |
| 73 | return S::getStrategyName().getPrefix(-1).appendVersion(version); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | using Tests = boost::mpl::vector< |
| 78 | Test<AccessStrategy, false, 1>, |
Ashlesh Gawande | 92e4ea5 | 2017-07-19 11:38:12 -0500 | [diff] [blame] | 79 | Test<AsfStrategy, true, 3>, |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 80 | Test<BestRouteStrategy, false, 1>, |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 81 | Test<BestRouteStrategy2, false, 5>, |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 82 | Test<MulticastStrategy, false, 3>, |
Teng Liang | 39465c2 | 2018-11-12 19:43:04 -0700 | [diff] [blame] | 83 | Test<NccStrategy, false, 1>, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 84 | Test<SelfLearningStrategy, false, 1>, |
| 85 | Test<RandomStrategy, false, 1> |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 86 | >; |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests) |
| 89 | { |
| 90 | BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1); |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests) |
| 94 | { |
| 95 | BOOST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion()); |
| 96 | uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion(); |
| 97 | BOOST_REQUIRE_LE(T::getMinVersion(), maxVersion); |
| 98 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 99 | FaceTable faceTable; |
| 100 | Forwarder forwarder(faceTable); |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 101 | for (uint64_t version = T::getMinVersion(); version <= maxVersion; ++version) { |
| 102 | Name versionedName = T::getVersionedStrategyName(version); |
| 103 | unique_ptr<typename T::Strategy> instance; |
| 104 | BOOST_CHECK_NO_THROW(instance = make_unique<typename T::Strategy>(forwarder, versionedName)); |
| 105 | BOOST_CHECK_EQUAL(instance->getInstanceName(), versionedName); |
| 106 | |
| 107 | if (!T::canAcceptParameters()) { |
| 108 | Name nameWithParameters = Name(versionedName).append("param"); |
| 109 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, nameWithParameters), std::invalid_argument); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (T::getMinVersion() > 0) { |
| 114 | Name version0Name = T::getVersionedStrategyName(0); |
| 115 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, version0Name), std::invalid_argument); |
| 116 | Name earlyVersionName = T::getVersionedStrategyName(T::getMinVersion() - 1); |
| 117 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, earlyVersionName), std::invalid_argument); |
| 118 | } |
| 119 | |
| 120 | if (maxVersion < std::numeric_limits<uint64_t>::max()) { |
| 121 | Name versionMaxName = T::getVersionedStrategyName(std::numeric_limits<uint64_t>::max()); |
| 122 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, versionMaxName), std::invalid_argument); |
| 123 | Name lateVersionName = T::getVersionedStrategyName(maxVersion + 1); |
| 124 | BOOST_CHECK_THROW(typename T::Strategy(forwarder, lateVersionName), std::invalid_argument); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation |
| 129 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 130 | |
| 131 | } // namespace tests |
| 132 | } // namespace fw |
| 133 | } // namespace nfd |