blob: 5cffcdba257d1f2561829a40c0b0babe0802ee94 [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi57df2882015-11-11 06:12:35 -07004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040026#include "common/counter.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070027
28#include "tests/test-common.hpp"
29
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tests {
Junxiao Shi57df2882015-11-11 06:12:35 -070031
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032BOOST_AUTO_TEST_SUITE(TestCounter)
Junxiao Shi57df2882015-11-11 06:12:35 -070033
34BOOST_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
51BOOST_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
68BOOST_AUTO_TEST_CASE(SizeCnt)
69{
70 std::vector<int> v;
Eric Newberry73bcad32017-04-25 17:57:35 -070071 SizeCounter<std::vector<int>> counter1(&v);
72 SizeCounter<std::vector<int>> counter2;
73 counter2.observe(&v);
Junxiao Shi57df2882015-11-11 06:12:35 -070074
Eric Newberry73bcad32017-04-25 17:57:35 -070075 size_t observation1 = counter1; // implicit conversion
76 size_t observation2 = counter2;
77 BOOST_CHECK_EQUAL(observation1, 0);
78 BOOST_CHECK_EQUAL(observation2, 0);
Junxiao Shi57df2882015-11-11 06:12:35 -070079
80 v.resize(249);
Eric Newberry73bcad32017-04-25 17:57:35 -070081 BOOST_CHECK_EQUAL(counter1, 249);
82 BOOST_CHECK_EQUAL(counter2, 249);
Junxiao Shi57df2882015-11-11 06:12:35 -070083
84 v.resize(98);
Eric Newberry73bcad32017-04-25 17:57:35 -070085 BOOST_CHECK_EQUAL(counter1, 98);
86 BOOST_CHECK_EQUAL(counter2, 98);
Junxiao Shi57df2882015-11-11 06:12:35 -070087}
88
89BOOST_AUTO_TEST_SUITE_END() // TestCounter
90
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040091} // namespace nfd::tests