blob: 46a0ed30f2a2416a15e0087c448d207e391391da [file] [log] [blame]
Junxiao Shi7f012472017-12-07 20:40:47 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shia7ab06d2018-01-29 22:28:47 +00003 * Copyright (c) 2013-2018 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
22#include "mgmt/nfd/cs-info.hpp"
23
24#include "boost-test.hpp"
25#include <boost/lexical_cast.hpp>
26
27namespace ndn {
28namespace nfd {
29namespace tests {
30
31BOOST_AUTO_TEST_SUITE(Mgmt)
32BOOST_AUTO_TEST_SUITE(Nfd)
33BOOST_AUTO_TEST_SUITE(TestCsInfo)
34
35static CsInfo
36makeCsInfo()
37{
38 return CsInfo()
Junxiao Shia7ab06d2018-01-29 22:28:47 +000039 .setCapacity(20177)
40 .setEnableAdmit(false)
41 .setEnableServe(true)
42 .setNEntries(5509)
Junxiao Shi7f012472017-12-07 20:40:47 +000043 .setNHits(12951)
44 .setNMisses(28179);
45}
46
47BOOST_AUTO_TEST_CASE(Encode)
48{
49 CsInfo csi1 = makeCsInfo();
50 Block wire = csi1.wireEncode();
51
52 static const uint8_t EXPECTED[] = {
Junxiao Shia7ab06d2018-01-29 22:28:47 +000053 0x80, 0x13, // CsInfo
54 0x83, 0x02, 0x4E, 0xD1, // Capacity
55 0x6C, 0x01, 0x02, // Flags
56 0x87, 0x02, 0x15, 0x85, // NCsEntries
Junxiao Shi7f012472017-12-07 20:40:47 +000057 0x81, 0x02, 0x32, 0x97, // NHits
58 0x82, 0x02, 0x6E, 0x13, // NMisses
59 };
60 BOOST_CHECK_EQUAL_COLLECTIONS(wire.begin(), wire.end(), EXPECTED, EXPECTED + sizeof(EXPECTED));
61
62 CsInfo csi2(wire);
Junxiao Shibe86d982018-02-07 14:20:29 +000063 BOOST_CHECK_EQUAL(csi2.getCapacity(), 20177);
64 BOOST_CHECK_EQUAL(csi2.getEnableAdmit(), false);
65 BOOST_CHECK_EQUAL(csi2.getEnableServe(), true);
66 BOOST_CHECK_EQUAL(csi2.getNEntries(), 5509);
67 BOOST_CHECK_EQUAL(csi2.getNHits(), 12951);
68 BOOST_CHECK_EQUAL(csi2.getNMisses(), 28179);
Junxiao Shi7f012472017-12-07 20:40:47 +000069}
70
71BOOST_AUTO_TEST_CASE(Equality)
72{
73 CsInfo csi1, csi2;
74 BOOST_CHECK_EQUAL(csi1, csi2);
75
76 csi1 = makeCsInfo();
77 BOOST_CHECK_NE(csi1, csi2);
78 csi2 = csi1;
79 BOOST_CHECK_EQUAL(csi1, csi2);
80
Junxiao Shia7ab06d2018-01-29 22:28:47 +000081 csi2.setCapacity(csi2.getCapacity() + 1);
Junxiao Shi7f012472017-12-07 20:40:47 +000082 BOOST_CHECK_NE(csi1, csi2);
Junxiao Shia7ab06d2018-01-29 22:28:47 +000083 csi2 = csi1;
84
85 csi2.setEnableAdmit(!csi2.getEnableAdmit());
86 BOOST_CHECK_NE(csi1, csi2);
87 csi2 = csi1;
88
89 csi2.setEnableServe(!csi2.getEnableServe());
90 BOOST_CHECK_NE(csi1, csi2);
91 csi2 = csi1;
92
93 csi2.setNEntries(csi2.getNEntries() + 1);
94 BOOST_CHECK_NE(csi1, csi2);
95 csi2 = csi1;
96
97 csi2.setNHits(csi2.getNHits() + 1);
98 BOOST_CHECK_NE(csi1, csi2);
99 csi2 = csi1;
100
101 csi2.setNMisses(csi2.getNMisses() + 1);
102 BOOST_CHECK_NE(csi1, csi2);
103 csi2 = csi1;
Junxiao Shi7f012472017-12-07 20:40:47 +0000104}
105
106BOOST_AUTO_TEST_CASE(Print)
107{
108 CsInfo csi;
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000109 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
110 "CS: 0 entries, 0 max, admit disabled, serve disabled, 0 hits, 0 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000111
112 csi = makeCsInfo();
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000113 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
114 "CS: 5509 entries, 20177 max, admit disabled, serve enabled, 12951 hits, 28179 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000115
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000116 csi.setEnableAdmit(true).setNHits(1).setNMisses(1);
117 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
118 "CS: 5509 entries, 20177 max, admit enabled, serve enabled, 1 hit, 1 miss");
Junxiao Shi7f012472017-12-07 20:40:47 +0000119}
120
121BOOST_AUTO_TEST_SUITE_END() // TestCsInfo
122BOOST_AUTO_TEST_SUITE_END() // Nfd
123BOOST_AUTO_TEST_SUITE_END() // Mgmt
124
125} // namespace tests
126} // namespace nfd
127} // namespace ndn