Debian Codegraph @

tests/unit/analyzers/breakdown/statistic.cpp

raw
1 //------------------------------------------------------------------------------
2 // Author: Andrey Kuznetsov
3 // Description: Unit tests
4 // Copyright (c) 2015 EPAM Systems
5 //------------------------------------------------------------------------------
6 /*
7 This file is part of Nfstrace.
8
9 Nfstrace is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, version 2 of the License.
12
13 Nfstrace is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Nfstrace. If not, see <http://www.gnu.org/licenses/>.
20 */
21 //------------------------------------------------------------------------------
22 #include <cstdlib>
23 #include <ctime>
24 #include <iostream>
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28
29 #include "statistics.h"
30 //------------------------------------------------------------------------------
31 using namespace NST::breakdown;
32
33 using ::testing::Return;
34 using ::testing::AtLeast;
35 using ::testing::_;
36 //------------------------------------------------------------------------------
37 namespace
38 {
39 class StatisticTest : public ::testing::Test
40 {
41 protected:
42 size_t count;
43
44 public:
45 void SetUp()
46 {
47 std::srand(std::time(0)); //use current time as seed for random generator
48 count = std::rand() % 100 + 3;
49 }
50
51 void TearDown()
52 {
53 }
54 };
55
56 class Mock
57 {
58 public:
59 MOCK_METHOD0(mock_function, void(void));
60 };
61
62 class Proc
63 {
64 public:
65 Proc()
66 : session(&_session)
67 , rtimestamp(&_rtimestamp)
68 , ctimestamp(&_ctimestamp)
69 {
70 }
71
72 Session _session;
73 timeval _rtimestamp;
74 timeval _ctimestamp;
75 const Session* session;
76 const timeval* rtimestamp;
77 const timeval* ctimestamp;
78 };
79 }
80 //------------------------------------------------------------------------------
81 TEST_F(StatisticTest, statistics)
82 {
83 Statistics stats(count);
84
85 EXPECT_EQ(count, stats.proc_types_count);
86
87 Mock each_procedure_mock;
88 EXPECT_CALL(each_procedure_mock, mock_function())
89 .Times(count);
90
91 stats.for_each_procedure([&](const BreakdownCounter&, size_t) {
92 each_procedure_mock.mock_function();
93 });
94 }
95
96 TEST_F(StatisticTest, sessions_statistics)
97 {
98 Statistics stats(count);
99 Mock each_procedure_mock;
100 Proc proc;
101
102 EXPECT_CALL(each_procedure_mock, mock_function())
103 .Times(1);
104
105 EXPECT_FALSE(stats.has_session());
106 stats.account(&proc, 0);
107 EXPECT_TRUE(stats.has_session());
108
109 stats.for_each_session([&](const Session&) {
110 each_procedure_mock.mock_function();
111 });
112 }
113
114 TEST_F(StatisticTest, statistics_per_session)
115 {
116 Statistics stats(count);
117 Mock each_procedure_mock;
118 Proc proc;
119
120 EXPECT_CALL(each_procedure_mock, mock_function())
121 .Times(count);
122
123 stats.for_each_procedure_in_session(proc._session, [&](const BreakdownCounter&, size_t) {
124 each_procedure_mock.mock_function();
125 });
126
127 stats.account(&proc, 1);
128
129 stats.for_each_procedure_in_session(proc._session, [&](const BreakdownCounter&, size_t) {
130 each_procedure_mock.mock_function();
131 });
132 }
133 //------------------------------------------------------------------------------