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 | /* |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, 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 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 29 | static const ndn::FaceUri faceUri1("udp4://192.168.3.1:6363"); |
| 30 | static const ndn::FaceUri faceUri2("udp4://192.168.3.2:6363"); |
| 31 | |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 32 | static double |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 33 | getHyperbolicAdjustedDecimal(unsigned int i) |
| 34 | { |
| 35 | return static_cast<double>(i)/(10*NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR); |
| 36 | } |
| 37 | |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 38 | static uint64_t |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 39 | applyHyperbolicFactorAndRound(double d) |
| 40 | { |
| 41 | return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d); |
| 42 | } |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(LinkStateSetAndGet) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 45 | { |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 46 | NextHop hop1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 47 | hop1.setConnectingFaceUri(faceUri1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 48 | hop1.setRouteCost(12.34); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 49 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), 12); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 52 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 53 | NextHop hop2(faceUri2, 12.34); |
| 54 | BOOST_CHECK_NE(hop1, hop2); |
| 55 | BOOST_CHECK_LT(hop1, hop2); |
| 56 | BOOST_CHECK_LE(hop1, hop2); |
| 57 | BOOST_CHECK_GT(hop2, hop1); |
| 58 | BOOST_CHECK_GE(hop2, hop1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 59 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 60 | hop2.setConnectingFaceUri(faceUri1); |
| 61 | BOOST_CHECK_EQUAL(hop1, hop2); |
| 62 | BOOST_CHECK_LE(hop1, hop2); |
| 63 | BOOST_CHECK_GE(hop2, hop1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(HyperbolicSetAndGet) |
| 68 | { |
| 69 | NextHop hop1; |
| 70 | hop1.setHyperbolic(true); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 71 | hop1.setConnectingFaceUri(faceUri1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 72 | hop1.setRouteCost(12.34); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 73 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34); |
| 75 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), applyHyperbolicFactorAndRound(12.34)); |
| 76 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 77 | NextHop hop2(faceUri2, 12.34); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 78 | hop2.setHyperbolic(true); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 79 | BOOST_CHECK_NE(hop1, hop2); |
| 80 | BOOST_CHECK_LT(hop1, hop2); |
| 81 | BOOST_CHECK_LE(hop1, hop2); |
| 82 | BOOST_CHECK_GT(hop2, hop1); |
| 83 | BOOST_CHECK_GE(hop2, hop1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 84 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 85 | hop2.setConnectingFaceUri(faceUri1); |
| 86 | BOOST_CHECK_EQUAL(hop1, hop2); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 88 | |
| 89 | hop2.setRouteCost(12.35); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 90 | BOOST_CHECK_LT(hop1, hop2); |
| 91 | BOOST_CHECK_LT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(HyperbolicRound) |
| 95 | { |
| 96 | NextHop hop1; |
| 97 | hop1.setHyperbolic(true); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 98 | hop1.setConnectingFaceUri(faceUri1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 99 | hop1.setRouteCost(1 + getHyperbolicAdjustedDecimal(6)); |
| 100 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 101 | BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(hop1.getRouteCost(), 1 + getHyperbolicAdjustedDecimal(6)); |
| 103 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), |
| 104 | applyHyperbolicFactorAndRound((1 + getHyperbolicAdjustedDecimal(6)))); |
| 105 | |
| 106 | NextHop hop2; |
| 107 | hop2.setHyperbolic(true); |
| 108 | |
| 109 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(6)); |
| 110 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
| 111 | |
| 112 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(5)); |
| 113 | BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
| 114 | |
| 115 | hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(4)); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 116 | BOOST_CHECK_GT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger()); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 119 | const uint8_t NexthopData[] = |
| 120 | { |
| 121 | // Header |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 122 | 0x8f, 0x23, |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 123 | // Uri |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 124 | 0x8d, 0x17, 0x75, 0x64, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, |
| 125 | 0x2e, 0x33, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33, |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 126 | // Cost |
| 127 | 0x86, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 |
| 128 | }; |
| 129 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 130 | BOOST_AUTO_TEST_CASE(Encode) |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 131 | { |
| 132 | NextHop nexthops1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 133 | nexthops1.setConnectingFaceUri(faceUri1); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 134 | nexthops1.setRouteCost(1.65); |
| 135 | |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 136 | BOOST_TEST(nexthops1.wireEncode() == NexthopData, boost::test_tools::per_element()); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 139 | BOOST_AUTO_TEST_CASE(Decode) |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 140 | { |
| 141 | NextHop nexthops1; |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 142 | nexthops1.wireDecode(ndn::Block{NexthopData}); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 144 | BOOST_REQUIRE_EQUAL(nexthops1.getConnectingFaceUri(), faceUri1); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 145 | BOOST_REQUIRE_EQUAL(nexthops1.getRouteCost(), 1.65); |
| 146 | } |
| 147 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 148 | BOOST_AUTO_TEST_CASE(OutputStream) |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 149 | { |
| 150 | NextHop nexthops1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 151 | nexthops1.setConnectingFaceUri(faceUri1); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 152 | nexthops1.setRouteCost(99); |
| 153 | |
| 154 | std::ostringstream os; |
| 155 | os << nexthops1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame^] | 156 | BOOST_CHECK_EQUAL(os.str(), "NextHop(Uri: udp4://192.168.3.1:6363, Cost: 99)"); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 159 | BOOST_AUTO_TEST_SUITE_END() |
| 160 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 161 | } // namespace test |
| 162 | } // namespace nlsr |