blob: 3ba3539e906a8c3f5775b0e1dc9d3e1750df7ced [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoe28d8752022-03-19 03:55:25 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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 Gawande0421bc62020-05-08 20:42:19 -070019 */
Davide Pesaventocb065f12019-12-27 01:03:34 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "route/nexthop.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050022#include "tests/boost-test.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050023
24namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025namespace test {
26
27BOOST_AUTO_TEST_SUITE(TestNexthop)
28
Davide Pesaventoe28d8752022-03-19 03:55:25 -040029static double
Vince Lehman20fe4a92014-09-09 15:57:59 -050030getHyperbolicAdjustedDecimal(unsigned int i)
31{
32 return static_cast<double>(i)/(10*NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR);
33}
34
Davide Pesaventoe28d8752022-03-19 03:55:25 -040035static uint64_t
Vince Lehman20fe4a92014-09-09 15:57:59 -050036applyHyperbolicFactorAndRound(double d)
37{
38 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
39}
40
41BOOST_AUTO_TEST_CASE(LinkStateSetAndGet)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050042{
Vince Lehman145064a2014-08-23 11:44:16 -050043 NextHop hop1;
44 hop1.setConnectingFaceUri("udp://test/uri");
45 hop1.setRouteCost(12.34);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050046
Vince Lehman145064a2014-08-23 11:44:16 -050047 BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), "udp://test/uri");
48 BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34);
Vince Lehman20fe4a92014-09-09 15:57:59 -050049 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), 12);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050050
Vince Lehman145064a2014-08-23 11:44:16 -050051 NextHop hop2;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050052
Vince Lehman145064a2014-08-23 11:44:16 -050053 hop2.setRouteCost(12.34);
54 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
Vince Lehman20fe4a92014-09-09 15:57:59 -050055}
56
57BOOST_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 Lehman145064a2014-08-23 11:44:16 -050073
74 hop2.setRouteCost(12.35);
75 BOOST_CHECK(hop1.getRouteCostAsAdjustedInteger() < hop2.getRouteCostAsAdjustedInteger());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050076}
77
Vince Lehman20fe4a92014-09-09 15:57:59 -050078BOOST_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 Gawande0421bc62020-05-08 20:42:19 -0700103const 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
114BOOST_AUTO_TEST_CASE(NexthopEncode)
115{
116 NextHop nexthops1;
117 nexthops1.setConnectingFaceUri("/test/nexthop/tlv");
118 nexthops1.setRouteCost(1.65);
119
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400120 BOOST_TEST(nexthops1.wireEncode() == NexthopData, boost::test_tools::per_element());
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700121}
122
123BOOST_AUTO_TEST_CASE(NexthopDecode)
124{
125 NextHop nexthops1;
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400126 nexthops1.wireDecode(ndn::Block{NexthopData});
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700127
128 BOOST_REQUIRE_EQUAL(nexthops1.getConnectingFaceUri(), "/test/nexthop/tlv");
129 BOOST_REQUIRE_EQUAL(nexthops1.getRouteCost(), 1.65);
130}
131
132BOOST_AUTO_TEST_CASE(AdjacencyOutputStream)
133{
134 NextHop nexthops1;
135 nexthops1.setConnectingFaceUri("/test/nexthop/tlv");
136 nexthops1.setRouteCost(99);
137
138 std::ostringstream os;
139 os << nexthops1;
140 BOOST_CHECK_EQUAL(os.str(), "NextHop(Uri: /test/nexthop/tlv, Cost: 99)");
141}
142
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500143BOOST_AUTO_TEST_SUITE_END()
144
Nick Gordonfad8e252016-08-11 14:21:38 -0500145} // namespace test
146} // namespace nlsr