blob: 9e4a437f65d2ac3cf1a00e6cb4c68e0bd9a26084 [file] [log] [blame]
Junxiao Shi7f012472017-12-07 20:40:47 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Junxiao Shi7f012472017-12-07 20:40:47 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/nfd/cs-info.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040023#include "ndn-cxx/util/concepts.hpp"
Junxiao Shi7f012472017-12-07 20:40:47 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040026
Junxiao Shi7f012472017-12-07 20:40:47 +000027#include <boost/lexical_cast.hpp>
28
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::nfd;
Junxiao Shi7f012472017-12-07 20:40:47 +000032
Davide Pesavento152ef442023-04-22 02:02:29 -040033BOOST_CONCEPT_ASSERT((StatusDatasetItem<CsInfo>));
34
Junxiao Shi7f012472017-12-07 20:40:47 +000035BOOST_AUTO_TEST_SUITE(Mgmt)
36BOOST_AUTO_TEST_SUITE(Nfd)
37BOOST_AUTO_TEST_SUITE(TestCsInfo)
38
39static CsInfo
40makeCsInfo()
41{
42 return CsInfo()
Junxiao Shia7ab06d2018-01-29 22:28:47 +000043 .setCapacity(20177)
44 .setEnableAdmit(false)
45 .setEnableServe(true)
46 .setNEntries(5509)
Junxiao Shi7f012472017-12-07 20:40:47 +000047 .setNHits(12951)
48 .setNMisses(28179);
49}
50
51BOOST_AUTO_TEST_CASE(Encode)
52{
53 CsInfo csi1 = makeCsInfo();
54 Block wire = csi1.wireEncode();
55
56 static const uint8_t EXPECTED[] = {
Junxiao Shia7ab06d2018-01-29 22:28:47 +000057 0x80, 0x13, // CsInfo
58 0x83, 0x02, 0x4E, 0xD1, // Capacity
59 0x6C, 0x01, 0x02, // Flags
60 0x87, 0x02, 0x15, 0x85, // NCsEntries
Junxiao Shi7f012472017-12-07 20:40:47 +000061 0x81, 0x02, 0x32, 0x97, // NHits
62 0x82, 0x02, 0x6E, 0x13, // NMisses
63 };
64 BOOST_CHECK_EQUAL_COLLECTIONS(wire.begin(), wire.end(), EXPECTED, EXPECTED + sizeof(EXPECTED));
65
66 CsInfo csi2(wire);
Junxiao Shibe86d982018-02-07 14:20:29 +000067 BOOST_CHECK_EQUAL(csi2.getCapacity(), 20177);
68 BOOST_CHECK_EQUAL(csi2.getEnableAdmit(), false);
69 BOOST_CHECK_EQUAL(csi2.getEnableServe(), true);
70 BOOST_CHECK_EQUAL(csi2.getNEntries(), 5509);
71 BOOST_CHECK_EQUAL(csi2.getNHits(), 12951);
72 BOOST_CHECK_EQUAL(csi2.getNMisses(), 28179);
Junxiao Shi7f012472017-12-07 20:40:47 +000073}
74
75BOOST_AUTO_TEST_CASE(Equality)
76{
77 CsInfo csi1, csi2;
78 BOOST_CHECK_EQUAL(csi1, csi2);
79
80 csi1 = makeCsInfo();
81 BOOST_CHECK_NE(csi1, csi2);
82 csi2 = csi1;
83 BOOST_CHECK_EQUAL(csi1, csi2);
84
Junxiao Shia7ab06d2018-01-29 22:28:47 +000085 csi2.setCapacity(csi2.getCapacity() + 1);
Junxiao Shi7f012472017-12-07 20:40:47 +000086 BOOST_CHECK_NE(csi1, csi2);
Junxiao Shia7ab06d2018-01-29 22:28:47 +000087 csi2 = csi1;
88
89 csi2.setEnableAdmit(!csi2.getEnableAdmit());
90 BOOST_CHECK_NE(csi1, csi2);
91 csi2 = csi1;
92
93 csi2.setEnableServe(!csi2.getEnableServe());
94 BOOST_CHECK_NE(csi1, csi2);
95 csi2 = csi1;
96
97 csi2.setNEntries(csi2.getNEntries() + 1);
98 BOOST_CHECK_NE(csi1, csi2);
99 csi2 = csi1;
100
101 csi2.setNHits(csi2.getNHits() + 1);
102 BOOST_CHECK_NE(csi1, csi2);
103 csi2 = csi1;
104
105 csi2.setNMisses(csi2.getNMisses() + 1);
106 BOOST_CHECK_NE(csi1, csi2);
107 csi2 = csi1;
Junxiao Shi7f012472017-12-07 20:40:47 +0000108}
109
110BOOST_AUTO_TEST_CASE(Print)
111{
112 CsInfo csi;
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000113 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
Davide Pesaventoef654dd2019-07-18 20:02:44 -0400114 "CsInfo: 0 entries, 0 max, admit disabled, serve disabled, 0 hits, 0 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000115
116 csi = makeCsInfo();
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000117 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
Davide Pesaventoef654dd2019-07-18 20:02:44 -0400118 "CsInfo: 5509 entries, 20177 max, admit disabled, serve enabled, 12951 hits, 28179 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000119
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000120 csi.setEnableAdmit(true).setNHits(1).setNMisses(1);
121 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
Davide Pesaventoef654dd2019-07-18 20:02:44 -0400122 "CsInfo: 5509 entries, 20177 max, admit enabled, serve enabled, 1 hit, 1 miss");
Junxiao Shi7f012472017-12-07 20:40:47 +0000123}
124
125BOOST_AUTO_TEST_SUITE_END() // TestCsInfo
126BOOST_AUTO_TEST_SUITE_END() // Nfd
127BOOST_AUTO_TEST_SUITE_END() // Mgmt
128
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400129} // namespace ndn::tests