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
|
#include <array>
#include <vector>
#include "gtest/gtest.h"
#include "test_env.h"
#include "queue.h"
using namespace std;
using namespace srt;
/// Create CUnitQueue with queue size of 4 units.
/// The size of 4 is chosen on purpose, because
/// CUnitQueue::getNextAvailUnit(..) has the following
/// condition `if (m_iCount * 10 > m_iSize * 9)`. With m_iSize = 4
/// it will be false up until m_iCount becomes 4.
/// And there was an issue in getNextAvailUnit(..) in taking
/// the very last element of the queue (it was skipped).
TEST(CUnitQueue, Increase)
{
srt::TestInit srtinit;
const int buffer_size_pkts = 4;
CUnitQueue unit_queue(buffer_size_pkts, 1500);
vector<CUnit*> taken_units;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitTaken(unit);
taken_units.push_back(unit);
}
}
/// Create CUnitQueue with queue size of 4 units.
/// Then after requesting the 5th unit, free the previous
/// four units. This makes the previous queue completely free.
/// Requesting the 5th unit, there would be 3 units available in the
/// beginning of the same queue.
TEST(CUnitQueue, IncreaseAndFree)
{
srt::TestInit srtinit;
const int buffer_size_pkts = 4;
CUnitQueue unit_queue(buffer_size_pkts, 1500);
CUnit* taken_unit = nullptr;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitTaken(unit);
if (taken_unit)
unit_queue.makeUnitFree(taken_unit);
taken_unit = unit;
}
}
/// Create CUnitQueue with queue size of 4 units.
/// Then after requesting the 5th unit, free the previous
/// four units. This makes the previous queue completely free.
/// Requesting the 9th unit, there would be 4 units available in the
/// Thus the test checks if
TEST(CUnitQueue, IncreaseAndFreeGrouped)
{
srt::TestInit srtinit;
const int buffer_size_pkts = 4;
CUnitQueue unit_queue(buffer_size_pkts, 1500);
vector<CUnit*> taken_units;
for (int i = 0; i < 5 * buffer_size_pkts; ++i)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitTaken(unit);
if (taken_units.size() >= buffer_size_pkts)
{
for_each(taken_units.begin(), taken_units.end(),
[&unit_queue](CUnit* u) { unit_queue.makeUnitFree(u); });
taken_units.clear();
}
taken_units.push_back(unit);
EXPECT_LE(unit_queue.capacity(), 2 * buffer_size_pkts)
<< "Buffer capacity should not exceed two queues of 4 units";
}
}
|