A Discrete-Event Network Simulator
API
fifo-queue-disc-test-suite.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2017 Universita' degli Studi di Napoli Federico II
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Stefano Avallone <stavallo@unina.it>
19 *
20 */
21
22#include "ns3/test.h"
23#include "ns3/fifo-queue-disc.h"
24#include "ns3/queue.h"
25#include "ns3/packet.h"
26#include "ns3/uinteger.h"
27#include "ns3/string.h"
28#include "ns3/double.h"
29#include "ns3/log.h"
30#include "ns3/simulator.h"
31#include "ns3/object-factory.h"
32#include <vector>
33
34using namespace ns3;
35
43{
44public:
52 virtual ~FifoQueueDiscTestItem ();
53
54 // Delete default constructor, copy constructor and assignment operator to avoid misuse
58
59 virtual void AddHeader (void);
60 virtual bool Mark (void);
61};
62
64 : QueueDiscItem (p, addr, 0)
65{
66}
67
69{
70}
71
72void
74{
75}
76
77bool
79{
80 return false;
81}
82
90{
91public:
93 virtual void DoRun (void);
94private:
99 void RunFifoTest (QueueSizeUnit mode);
107};
108
110 : TestCase ("Sanity check on the fifo queue disc implementation")
111{
112}
113
114void
116{
117 std::vector<uint64_t> uids;
118 Ptr<Packet> p;
120 Address dest;
121 uint32_t modeSize = (q->GetMaxSize ().GetUnit () == QueueSizeUnit::PACKETS ? 1 : pktSize);
122 uint32_t numPackets = qSize / modeSize;
123
124 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), 0, "The queue disc should be empty");
125
126 // create and enqueue numPackets packets and store their UIDs; check they are all enqueued
127 for (uint32_t i = 1; i <= numPackets; i++)
128 {
129 p = Create<Packet> (pktSize);
130 uids.push_back (p->GetUid ());
131 q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest));
132 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), i * modeSize, "There should be " << i << " packet(s) in there");
133 }
134
135 // no room for another packet
136 NS_TEST_ASSERT_MSG_EQ (q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest)),
137 false, "There should be no room for another packet");
138
139 // dequeue and check packet order
140 for (uint32_t i = 1; i <= numPackets; i++)
141 {
142 item = q->Dequeue ();
143 NS_TEST_ASSERT_MSG_EQ ((item != 0), true, "A packet should have been dequeued");
144 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), (numPackets-i) * modeSize, "There should be " << numPackets-i << " packet(s) in there");
145 NS_TEST_ASSERT_MSG_EQ (item->GetPacket ()->GetUid (), uids[i-1], "was this the right packet?");
146 }
147
148 item = q->Dequeue ();
149 NS_TEST_ASSERT_MSG_EQ ((item == 0), true, "There are really no packets in there");
150}
151
152void
154{
155 Ptr<FifoQueueDisc> queue;
156 uint32_t numPackets = 10;
157 uint32_t pktSize = 1000;
158 uint32_t modeSize = (mode == QueueSizeUnit::PACKETS ? 1 : pktSize);
159
160 // test 1: set the limit on the queue disc before initialization
161 queue = CreateObject<FifoQueueDisc> ();
162
163 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
164
165 NS_TEST_ASSERT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
166 QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
167 true, "Verify that we can actually set the attribute MaxSize");
168
169 queue->Initialize ();
170
171 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
172
173
174 // test 2: set the limit on the queue disc after initialization
175 queue = CreateObject<FifoQueueDisc> ();
176
177 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
178
179 queue->Initialize ();
180
181 NS_TEST_ASSERT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
182 QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
183 true, "Verify that we can actually set the attribute MaxSize");
184
185 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
186
187
188 // test 3: set the limit on the internal queue before initialization
189 queue = CreateObject<FifoQueueDisc> ();
190
191 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
192
193 ObjectFactory factory;
194 factory.SetTypeId ("ns3::DropTailQueue<QueueDiscItem>");
195 if (mode == QueueSizeUnit::PACKETS)
196 {
197 factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, numPackets)));
198 }
199 else
200 {
201 factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, numPackets*pktSize)));
202 }
203 queue->AddInternalQueue (factory.Create<QueueDisc::InternalQueue> ());
204
205 queue->Initialize ();
206
207 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
208
209
210 // test 4: set the limit on the internal queue after initialization
211 queue = CreateObject<FifoQueueDisc> ();
212
213 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
214
215 queue->Initialize ();
216
217 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 1, "Verify that the queue disc got an internal queue");
218
219 Ptr<QueueDisc::InternalQueue> iq = queue->GetInternalQueue (0);
220
221 if (mode == QueueSizeUnit::PACKETS)
222 {
223 NS_TEST_ASSERT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
225 true, "Verify that we can actually set the attribute MaxSize on the internal queue");
226 }
227 else
228 {
229 NS_TEST_ASSERT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
231 true, "Verify that we can actually set the attribute MaxSize on the internal queue");
232 }
233
234 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
235}
236
237void
239{
242 Simulator::Destroy ();
243}
244
252{
253public:
255 : TestSuite ("fifo-queue-disc", UNIT)
256 {
257 AddTestCase (new FifoQueueDiscTestCase (), TestCase::QUICK);
258 }
Fifo Queue Disc Test Case.
void RunFifoTest(QueueSizeUnit mode)
Run test function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoRunFifoTest(Ptr< FifoQueueDisc > q, uint32_t qSize, uint32_t pktSize)
Run test function.
Fifo Queue Disc Test Item.
FifoQueueDiscTestItem()=delete
virtual void AddHeader(void)
Add the header to the packet.
virtual bool Mark(void)
Marks the packet as a substitute for dropping it, such as for Explicit Congestion Notification.
FifoQueueDiscTestItem(const FifoQueueDiscTestItem &)=delete
FifoQueueDiscTestItem & operator=(const FifoQueueDiscTestItem &)=delete
Fifo Queue Disc Test Suite.
a polymophic address class
Definition: address.h:91
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
Introspection did not find any typical Config paths.
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:133
Class for representing queue sizes.
Definition: queue-size.h:95
AttributeValue implementation for QueueSize.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:43
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:45
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
FifoQueueDiscTestSuite g_fifoQueueTestSuite
the test suite
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t pktSize
packet size used for the simulation (in bytes)
Definition: wifi-bianchi.cc:89