blob: 9e499af36dd44a3aa311c40ba1a61d1a2839ac53 [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);
63 BOOST_CHECK_EQUAL(csi1, csi2);
64}
65
66BOOST_AUTO_TEST_CASE(Equality)
67{
68 CsInfo csi1, csi2;
69 BOOST_CHECK_EQUAL(csi1, csi2);
70
71 csi1 = makeCsInfo();
72 BOOST_CHECK_NE(csi1, csi2);
73 csi2 = csi1;
74 BOOST_CHECK_EQUAL(csi1, csi2);
75
Junxiao Shia7ab06d2018-01-29 22:28:47 +000076 csi2.setCapacity(csi2.getCapacity() + 1);
Junxiao Shi7f012472017-12-07 20:40:47 +000077 BOOST_CHECK_NE(csi1, csi2);
Junxiao Shia7ab06d2018-01-29 22:28:47 +000078 csi2 = csi1;
79
80 csi2.setEnableAdmit(!csi2.getEnableAdmit());
81 BOOST_CHECK_NE(csi1, csi2);
82 csi2 = csi1;
83
84 csi2.setEnableServe(!csi2.getEnableServe());
85 BOOST_CHECK_NE(csi1, csi2);
86 csi2 = csi1;
87
88 csi2.setNEntries(csi2.getNEntries() + 1);
89 BOOST_CHECK_NE(csi1, csi2);
90 csi2 = csi1;
91
92 csi2.setNHits(csi2.getNHits() + 1);
93 BOOST_CHECK_NE(csi1, csi2);
94 csi2 = csi1;
95
96 csi2.setNMisses(csi2.getNMisses() + 1);
97 BOOST_CHECK_NE(csi1, csi2);
98 csi2 = csi1;
Junxiao Shi7f012472017-12-07 20:40:47 +000099}
100
101BOOST_AUTO_TEST_CASE(Print)
102{
103 CsInfo csi;
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000104 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
105 "CS: 0 entries, 0 max, admit disabled, serve disabled, 0 hits, 0 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000106
107 csi = makeCsInfo();
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000108 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
109 "CS: 5509 entries, 20177 max, admit disabled, serve enabled, 12951 hits, 28179 misses");
Junxiao Shi7f012472017-12-07 20:40:47 +0000110
Junxiao Shia7ab06d2018-01-29 22:28:47 +0000111 csi.setEnableAdmit(true).setNHits(1).setNMisses(1);
112 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi),
113 "CS: 5509 entries, 20177 max, admit enabled, serve enabled, 1 hit, 1 miss");
Junxiao Shi7f012472017-12-07 20:40:47 +0000114}
115
116BOOST_AUTO_TEST_SUITE_END() // TestCsInfo
117BOOST_AUTO_TEST_SUITE_END() // Nfd
118BOOST_AUTO_TEST_SUITE_END() // Mgmt
119
120} // namespace tests
121} // namespace nfd
122} // namespace ndn