blob: 9d80c1f25db87d5482ad575bc684dff04c245690 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
* See AUTHORS.md for complete list of NLSR authors and contributors.
*
* NLSR is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "route/nexthop.hpp"
#include "tests/boost-test.hpp"
namespace nlsr {
namespace test {
BOOST_AUTO_TEST_SUITE(TestNexthop)
static const ndn::FaceUri faceUri1("udp4://192.168.3.1:6363");
static const ndn::FaceUri faceUri2("udp4://192.168.3.2:6363");
static double
getHyperbolicAdjustedDecimal(unsigned int i)
{
return static_cast<double>(i)/(10*NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR);
}
static uint64_t
applyHyperbolicFactorAndRound(double d)
{
return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
}
BOOST_AUTO_TEST_CASE(LinkStateSetAndGet)
{
NextHop hop1;
hop1.setConnectingFaceUri(faceUri1);
hop1.setRouteCost(12.34);
BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34);
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), 12);
NextHop hop2(faceUri2, 12.34);
BOOST_CHECK_NE(hop1, hop2);
BOOST_CHECK_LT(hop1, hop2);
BOOST_CHECK_LE(hop1, hop2);
BOOST_CHECK_GT(hop2, hop1);
BOOST_CHECK_GE(hop2, hop1);
hop2.setConnectingFaceUri(faceUri1);
BOOST_CHECK_EQUAL(hop1, hop2);
BOOST_CHECK_LE(hop1, hop2);
BOOST_CHECK_GE(hop2, hop1);
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
}
BOOST_AUTO_TEST_CASE(HyperbolicSetAndGet)
{
NextHop hop1;
hop1.setHyperbolic(true);
hop1.setConnectingFaceUri(faceUri1);
hop1.setRouteCost(12.34);
BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34);
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), applyHyperbolicFactorAndRound(12.34));
NextHop hop2(faceUri2, 12.34);
hop2.setHyperbolic(true);
BOOST_CHECK_NE(hop1, hop2);
BOOST_CHECK_LT(hop1, hop2);
BOOST_CHECK_LE(hop1, hop2);
BOOST_CHECK_GT(hop2, hop1);
BOOST_CHECK_GE(hop2, hop1);
hop2.setConnectingFaceUri(faceUri1);
BOOST_CHECK_EQUAL(hop1, hop2);
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
hop2.setRouteCost(12.35);
BOOST_CHECK_LT(hop1, hop2);
BOOST_CHECK_LT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
}
BOOST_AUTO_TEST_CASE(HyperbolicRound)
{
NextHop hop1;
hop1.setHyperbolic(true);
hop1.setConnectingFaceUri(faceUri1);
hop1.setRouteCost(1 + getHyperbolicAdjustedDecimal(6));
BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
BOOST_CHECK_EQUAL(hop1.getRouteCost(), 1 + getHyperbolicAdjustedDecimal(6));
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(),
applyHyperbolicFactorAndRound((1 + getHyperbolicAdjustedDecimal(6))));
NextHop hop2;
hop2.setHyperbolic(true);
hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(6));
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(5));
BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(4));
BOOST_CHECK_GT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
}
const uint8_t NexthopData[] =
{
// Header
0x8f, 0x23,
// Uri
0x8d, 0x17, 0x75, 0x64, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38,
0x2e, 0x33, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33,
// Cost
0x86, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66
};
BOOST_AUTO_TEST_CASE(Encode)
{
NextHop nexthops1;
nexthops1.setConnectingFaceUri(faceUri1);
nexthops1.setRouteCost(1.65);
BOOST_TEST(nexthops1.wireEncode() == NexthopData, boost::test_tools::per_element());
}
BOOST_AUTO_TEST_CASE(Decode)
{
NextHop nexthops1;
nexthops1.wireDecode(ndn::Block{NexthopData});
BOOST_REQUIRE_EQUAL(nexthops1.getConnectingFaceUri(), faceUri1);
BOOST_REQUIRE_EQUAL(nexthops1.getRouteCost(), 1.65);
}
BOOST_AUTO_TEST_CASE(OutputStream)
{
NextHop nexthops1;
nexthops1.setConnectingFaceUri(faceUri1);
nexthops1.setRouteCost(99);
std::ostringstream os;
os << nexthops1;
BOOST_CHECK_EQUAL(os.str(), "NextHop(Uri: udp4://192.168.3.1:6363, Cost: 99)");
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace test
} // namespace nlsr