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 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::tests { |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(TestCounter) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_CASE(PacketCnt) |
| 35 | { |
| 36 | PacketCounter counter; |
| 37 | |
| 38 | uint64_t observation = counter; // implicit conversion |
| 39 | BOOST_CHECK_EQUAL(observation, 0); |
| 40 | |
| 41 | ++counter; |
| 42 | BOOST_CHECK_EQUAL(counter, 1); |
| 43 | ++counter; |
| 44 | ++counter; |
| 45 | BOOST_CHECK_EQUAL(counter, 3); |
| 46 | |
| 47 | counter.set(2); |
| 48 | BOOST_CHECK_EQUAL(counter, 2); |
| 49 | } |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(ByteCnt) |
| 52 | { |
| 53 | ByteCounter counter; |
| 54 | |
| 55 | uint64_t observation = counter; // implicit conversion |
| 56 | BOOST_CHECK_EQUAL(observation, 0); |
| 57 | |
| 58 | counter += 20; |
| 59 | BOOST_CHECK_EQUAL(counter, 20); |
| 60 | counter += 80; |
| 61 | counter += 90; |
| 62 | BOOST_CHECK_EQUAL(counter, 190); |
| 63 | |
| 64 | counter.set(21); |
| 65 | BOOST_CHECK_EQUAL(counter, 21); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(SizeCnt) |
| 69 | { |
| 70 | std::vector<int> v; |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 71 | SizeCounter<std::vector<int>> counter1(&v); |
| 72 | SizeCounter<std::vector<int>> counter2; |
| 73 | counter2.observe(&v); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 74 | |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 75 | size_t observation1 = counter1; // implicit conversion |
| 76 | size_t observation2 = counter2; |
| 77 | BOOST_CHECK_EQUAL(observation1, 0); |
| 78 | BOOST_CHECK_EQUAL(observation2, 0); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 79 | |
| 80 | v.resize(249); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(counter1, 249); |
| 82 | BOOST_CHECK_EQUAL(counter2, 249); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 83 | |
| 84 | v.resize(98); |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(counter1, 98); |
| 86 | BOOST_CHECK_EQUAL(counter2, 98); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE_END() // TestCounter |
| 90 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 91 | } // namespace nfd::tests |