| 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 <sstream> |
| 25 |
|
| 26 |
#include <gmock/gmock.h> |
| 27 |
#include <gtest/gtest.h> |
| 28 |
|
| 29 |
#include "latencies.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 LatencyTest : public ::testing::Test |
| 40 |
{ |
| 41 |
protected: |
| 42 |
size_t count; |
| 43 |
timeval t1; |
| 44 |
timeval t2; |
| 45 |
|
| 46 |
public: |
| 47 |
void SetUp() |
| 48 |
{ |
| 49 |
t1.tv_sec = 10; |
| 50 |
t2.tv_sec = 2; |
| 51 |
|
| 52 |
t1.tv_usec = 12; |
| 53 |
t2.tv_usec = 4; |
| 54 |
|
| 55 |
std::srand(std::time(0)); //use current time as seed for random generator |
| 56 |
count = std::rand() % 100 + 3; |
| 57 |
} |
| 58 |
|
| 59 |
void TearDown() |
| 60 |
{ |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
//------------------------------------------------------------------------------ |
| 65 |
TEST_F(LatencyTest, max_min) |
| 66 |
{ |
| 67 |
Latencies latency; |
| 68 |
|
| 69 |
EXPECT_EQ(0U, latency.get_count()); |
| 70 |
|
| 71 |
latency.add(t1); |
| 72 |
latency.add(t2); |
| 73 |
|
| 74 |
EXPECT_EQ(2U, latency.get_count()); |
| 75 |
|
| 76 |
EXPECT_EQ(t2.tv_sec, latency.get_min().tv_sec); |
| 77 |
EXPECT_EQ(t2.tv_usec, latency.get_min().tv_usec); |
| 78 |
|
| 79 |
EXPECT_EQ(t1.tv_sec, latency.get_max().tv_sec); |
| 80 |
EXPECT_EQ(t1.tv_usec, latency.get_max().tv_usec); |
| 81 |
} |
| 82 |
|
| 83 |
TEST_F(LatencyTest, avg) |
| 84 |
{ |
| 85 |
Latencies latency; |
| 86 |
|
| 87 |
EXPECT_EQ(0.0, latency.get_avg()); |
| 88 |
|
| 89 |
latency.add(t1); |
| 90 |
|
| 91 |
EXPECT_NEAR(10.0, latency.get_avg(), 0.0001); |
| 92 |
|
| 93 |
latency.add(t2); |
| 94 |
|
| 95 |
EXPECT_NEAR(6.0, latency.get_avg(), 0.0001); |
| 96 |
} |
| 97 |
|
| 98 |
TEST_F(LatencyTest, convert_timeval_to_sec) |
| 99 |
{ |
| 100 |
/* This test checks to_sec() function and rounding its result to smaller |
| 101 |
* precision via std::ios_base::precision() |
| 102 |
* |
| 103 |
* Background: |
| 104 |
* The sizeof(long double) may be different on some platforms. F.e.: |
| 105 |
* on Linux x86_64 the sizeof(long double) == 16 and == 10 on x86 or mips |
| 106 |
* So, binary representation of the same value are different. Result of |
| 107 |
* rounding a value to lower precision may be different. |
| 108 |
* |
| 109 |
* We use conversion to double (its sizeof is 8) in to_sec() for more |
| 110 |
* predictable conversions and rounding on various platforms. |
| 111 |
*/ |
| 112 |
|
| 113 |
struct timeval input; |
| 114 |
input.tv_sec = 0; |
| 115 |
input.tv_usec = 500; |
| 116 |
|
| 117 |
const auto sec = to_sec(input); |
| 118 |
|
| 119 |
EXPECT_NEAR(0.0005, sec, std::numeric_limits<double>::epsilon()); |
| 120 |
|
| 121 |
std::stringstream ss; |
| 122 |
|
| 123 |
ss.precision(6); |
| 124 |
ss << std::fixed << sec << ' '; |
| 125 |
ss.precision(4); |
| 126 |
ss << std::fixed << sec << ' '; |
| 127 |
ss.precision(3); |
| 128 |
ss << std::fixed << sec; |
| 129 |
|
| 130 |
EXPECT_EQ("0.000500 0.0005 0.001", ss.str()); |
| 131 |
} |
| 132 |
//------------------------------------------------------------------------------ |