akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame^] | 19 | */ |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 20 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 21 | #include "route/nexthop.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 23 | |
| 24 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | namespace test { |
| 26 | |
| 27 | BOOST_AUTO_TEST_SUITE(TestNexthop) |
| 28 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 29 | double |
| 30 | getHyperbolicAdjustedDecimal(unsigned int i) |
| 31 | { |
| 32 | return static_cast<double>(i)/(10*NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR); |
| 33 | } |
| 34 | |
| 35 | uint64_t |
| 36 | applyHyperbolicFactorAndRound(double d) |
| 37 | { |
| 38 | return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d); |
| 39 | } |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(LinkStateSetAndGet) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 42 | { |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 43 | NextHop hop1; |
| 44 | hop1.setConnectingFaceUri("udp://test/uri"); |
| 45 | hop1.setRouteCost(12.34); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 46 | |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), "udp://test/uri"); |
| 48 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), 12); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 50 | |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 51 | NextHop hop2; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 52 | |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 53 | hop2.setRouteCost(12.34); |
| 54 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(HyperbolicSetAndGet) |
| 58 | { |
| 59 | NextHop hop1; |
| 60 | hop1.setHyperbolic(true); |
| 61 | hop1.setConnectingFaceUri("udp://test/uri"); |
| 62 | hop1.setRouteCost(12.34); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), "udp://test/uri"); |
| 65 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34); |
| 66 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), applyHyperbolicFactorAndRound(12.34)); |
| 67 | |
| 68 | NextHop hop2; |
| 69 | hop2.setHyperbolic(true); |
| 70 | |
| 71 | hop2.setRouteCost(12.34); |
| 72 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 73 | |
| 74 | hop2.setRouteCost(12.35); |
| 75 | BOOST_CHECK(hop1.getRouteCostAsAdjustedInteger() < hop2.getRouteCostAsAdjustedInteger()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(HyperbolicRound) |
| 79 | { |
| 80 | NextHop hop1; |
| 81 | hop1.setHyperbolic(true); |
| 82 | hop1.setConnectingFaceUri("udp://test/uri"); |
| 83 | hop1.setRouteCost(1 + getHyperbolicAdjustedDecimal(6)); |
| 84 | |
| 85 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), "udp://test/uri"); |
| 86 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 1 + getHyperbolicAdjustedDecimal(6)); |
| 87 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), |
| 88 | applyHyperbolicFactorAndRound((1 + getHyperbolicAdjustedDecimal(6)))); |
| 89 | |
| 90 | NextHop hop2; |
| 91 | hop2.setHyperbolic(true); |
| 92 | |
| 93 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(6)); |
| 94 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
| 95 | |
| 96 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(5)); |
| 97 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
| 98 | |
| 99 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(4)); |
| 100 | BOOST_CHECK(hop1.getRouteCostAsAdjustedInteger() > hop2.getRouteCostAsAdjustedInteger()); |
| 101 | } |
| 102 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame^] | 103 | const uint8_t NexthopData[] = |
| 104 | { |
| 105 | // Header |
| 106 | 0x8f, 0x1d, |
| 107 | // Uri |
| 108 | 0x8d, 0x11, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x6e, 0x65, 0x78, 0x74, 0x68, 0x6f, |
| 109 | 0x70, 0x2f, 0x74, 0x6c, 0x76, |
| 110 | // Cost |
| 111 | 0x86, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 |
| 112 | }; |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(NexthopEncode) |
| 115 | { |
| 116 | NextHop nexthops1; |
| 117 | nexthops1.setConnectingFaceUri("/test/nexthop/tlv"); |
| 118 | nexthops1.setRouteCost(1.65); |
| 119 | |
| 120 | const ndn::Block& wire = nexthops1.wireEncode(); |
| 121 | BOOST_REQUIRE_EQUAL_COLLECTIONS(NexthopData, |
| 122 | NexthopData + sizeof(NexthopData), |
| 123 | wire.begin(), wire.end()); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(NexthopDecode) |
| 127 | { |
| 128 | NextHop nexthops1; |
| 129 | |
| 130 | nexthops1.wireDecode(ndn::Block(NexthopData, sizeof(NexthopData))); |
| 131 | |
| 132 | BOOST_REQUIRE_EQUAL(nexthops1.getConnectingFaceUri(), "/test/nexthop/tlv"); |
| 133 | BOOST_REQUIRE_EQUAL(nexthops1.getRouteCost(), 1.65); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(AdjacencyOutputStream) |
| 137 | { |
| 138 | NextHop nexthops1; |
| 139 | nexthops1.setConnectingFaceUri("/test/nexthop/tlv"); |
| 140 | nexthops1.setRouteCost(99); |
| 141 | |
| 142 | std::ostringstream os; |
| 143 | os << nexthops1; |
| 144 | BOOST_CHECK_EQUAL(os.str(), "NextHop(Uri: /test/nexthop/tlv, Cost: 99)"); |
| 145 | } |
| 146 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 147 | BOOST_AUTO_TEST_SUITE_END() |
| 148 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 149 | } // namespace test |
| 150 | } // namespace nlsr |