-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathtest_counters.cpp
106 lines (82 loc) · 2.58 KB
/
test_counters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas ([email protected])
*
*/
#include <gtest/gtest.h>
#include <bm/bm_sim/counters.h>
#include <bm/bm_sim/packet.h>
#include <bm/bm_sim/phv.h>
#include <bm/bm_sim/phv_source.h>
#include <random>
using namespace bm;
// Google Test fixture for counter tests
class CountersTest : public ::testing::Test {
protected:
using counter_value_t = Counter::counter_value_t;
protected:
PHVFactory phv_factory;
std::mt19937 gen;
std::uniform_int_distribution<size_t> dis;
std::unique_ptr<PHVSourceIface> phv_source{nullptr};
static constexpr size_t min_pkt_size = 64;
static constexpr size_t max_pkt_size = 4096;
CountersTest()
: dis(min_pkt_size, max_pkt_size),
phv_source(PHVSourceIface::make_phv_source()) { }
virtual void SetUp() {
phv_source->set_phv_factory(0, &phv_factory);
}
// virtual void TearDown() { }
Packet get_pkt(size_t pkt_size) {
// dummy packet, won't be parsed
return Packet::make_new(pkt_size, PacketBuffer(pkt_size * 2),
phv_source.get());
}
};
// Really simple tests, but then it is a really simple object
TEST_F(CountersTest, SimpleTest) {
counter_value_t bytes, packets;
Counter c;
c.query_counter(&bytes, &packets);
ASSERT_EQ(0u, bytes);
ASSERT_EQ(0u, packets);
const size_t nb_pkts = 10;
size_t byte_count = 0;
for (size_t i = 0; i < nb_pkts; ++i) {
const size_t pkt_size = dis(gen);
const Packet pkt = get_pkt(pkt_size);
byte_count += pkt_size;
c.increment_counter(pkt);
c.query_counter(&bytes, &packets);
ASSERT_EQ(byte_count, bytes);
ASSERT_EQ(i + 1, packets);
}
c.reset_counter();
c.query_counter(&bytes, &packets);
ASSERT_EQ(0u, bytes);
ASSERT_EQ(0u, packets);
}
// pretty useless ...
TEST_F(CountersTest, CounterArray) {
counter_value_t bytes, packets;
CounterArray c_array("counter", 0, 128);
for (Counter &c : c_array) {
c.query_counter(&bytes, &packets);
ASSERT_EQ(0u, bytes);
ASSERT_EQ(0u, packets);
}
}