Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 4 | * |
| 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 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 32 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 33 | BOOST_AUTO_TEST_SUITE(TestCsInfo) |
| 34 | |
| 35 | static CsInfo |
| 36 | makeCsInfo() |
| 37 | { |
| 38 | return CsInfo() |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 39 | .setCapacity(20177) |
| 40 | .setEnableAdmit(false) |
| 41 | .setEnableServe(true) |
| 42 | .setNEntries(5509) |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 43 | .setNHits(12951) |
| 44 | .setNMisses(28179); |
| 45 | } |
| 46 | |
| 47 | BOOST_AUTO_TEST_CASE(Encode) |
| 48 | { |
| 49 | CsInfo csi1 = makeCsInfo(); |
| 50 | Block wire = csi1.wireEncode(); |
| 51 | |
| 52 | static const uint8_t EXPECTED[] = { |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 53 | 0x80, 0x13, // CsInfo |
| 54 | 0x83, 0x02, 0x4E, 0xD1, // Capacity |
| 55 | 0x6C, 0x01, 0x02, // Flags |
| 56 | 0x87, 0x02, 0x15, 0x85, // NCsEntries |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 57 | 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 Shi | be86d98 | 2018-02-07 14:20:29 +0000 | [diff] [blame] | 63 | 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 Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | BOOST_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 Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 81 | csi2.setCapacity(csi2.getCapacity() + 1); |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 82 | BOOST_CHECK_NE(csi1, csi2); |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 83 | 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 Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | BOOST_AUTO_TEST_CASE(Print) |
| 107 | { |
| 108 | CsInfo csi; |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi), |
| 110 | "CS: 0 entries, 0 max, admit disabled, serve disabled, 0 hits, 0 misses"); |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 111 | |
| 112 | csi = makeCsInfo(); |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 113 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi), |
| 114 | "CS: 5509 entries, 20177 max, admit disabled, serve enabled, 12951 hits, 28179 misses"); |
Junxiao Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 115 | |
Junxiao Shi | a7ab06d | 2018-01-29 22:28:47 +0000 | [diff] [blame] | 116 | 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 Shi | 7f01247 | 2017-12-07 20:40:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_SUITE_END() // TestCsInfo |
| 122 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 123 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 124 | |
| 125 | } // namespace tests |
| 126 | } // namespace nfd |
| 127 | } // namespace ndn |