Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #include "common/counter.hpp" |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tests { |
| 32 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestCounter) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_CASE(PacketCnt) |
| 36 | { |
| 37 | PacketCounter counter; |
| 38 | |
| 39 | uint64_t observation = counter; // implicit conversion |
| 40 | BOOST_CHECK_EQUAL(observation, 0); |
| 41 | |
| 42 | ++counter; |
| 43 | BOOST_CHECK_EQUAL(counter, 1); |
| 44 | ++counter; |
| 45 | ++counter; |
| 46 | BOOST_CHECK_EQUAL(counter, 3); |
| 47 | |
| 48 | counter.set(2); |
| 49 | BOOST_CHECK_EQUAL(counter, 2); |
| 50 | } |
| 51 | |
| 52 | BOOST_AUTO_TEST_CASE(ByteCnt) |
| 53 | { |
| 54 | ByteCounter counter; |
| 55 | |
| 56 | uint64_t observation = counter; // implicit conversion |
| 57 | BOOST_CHECK_EQUAL(observation, 0); |
| 58 | |
| 59 | counter += 20; |
| 60 | BOOST_CHECK_EQUAL(counter, 20); |
| 61 | counter += 80; |
| 62 | counter += 90; |
| 63 | BOOST_CHECK_EQUAL(counter, 190); |
| 64 | |
| 65 | counter.set(21); |
| 66 | BOOST_CHECK_EQUAL(counter, 21); |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(SizeCnt) |
| 70 | { |
| 71 | std::vector<int> v; |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 72 | SizeCounter<std::vector<int>> counter1(&v); |
| 73 | SizeCounter<std::vector<int>> counter2; |
| 74 | counter2.observe(&v); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 75 | |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 76 | size_t observation1 = counter1; // implicit conversion |
| 77 | size_t observation2 = counter2; |
| 78 | BOOST_CHECK_EQUAL(observation1, 0); |
| 79 | BOOST_CHECK_EQUAL(observation2, 0); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 80 | |
| 81 | v.resize(249); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(counter1, 249); |
| 83 | BOOST_CHECK_EQUAL(counter2, 249); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 84 | |
| 85 | v.resize(98); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(counter1, 98); |
| 87 | BOOST_CHECK_EQUAL(counter2, 98); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_SUITE_END() // TestCounter |
| 91 | |
| 92 | } // namespace tests |
| 93 | } // namespace nfd |