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 copy constructor and assignment operator to avoid misuse
57
58 virtual void AddHeader (void);
59 virtual bool Mark (void);
60
61private:
63};
64
66 : QueueDiscItem (p, addr, 0)
67{
68}
69
71{
72}
73
74void
76{
77}
78
79bool
81{
82 return false;
83}
84
92{
93public:
95 virtual void DoRun (void);
96private:
101 void RunFifoTest (QueueSizeUnit mode);
109};
110
112 : TestCase ("Sanity check on the fifo queue disc implementation")
113{
114}
115
116void
118{
119 std::vector<uint64_t> uids;
120 Ptr<Packet> p;
122 Address dest;
123 uint32_t modeSize = (q->GetMaxSize ().GetUnit () == QueueSizeUnit::PACKETS ? 1 : pktSize);
124 uint32_t numPackets = qSize / modeSize;
125
126 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), 0, "The queue disc should be empty");
127
128 // create and enqueue numPackets packets and store their UIDs; check they are all enqueued
129 for (uint32_t i = 1; i <= numPackets; i++)
130 {
131 p = Create<Packet> (pktSize);
132 uids.push_back (p->GetUid ());
133 q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest));
134 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), i * modeSize, "There should be " << i << " packet(s) in there");
135 }
136
137 // no room for another packet
138 NS_TEST_ASSERT_MSG_EQ (q->Enqueue (Create<FifoQueueDiscTestItem> (p, dest)),
139 false, "There should be no room for another packet");
140
141 // dequeue and check packet order
142 for (uint32_t i = 1; i <= numPackets; i++)
143 {
144 item = q->Dequeue ();
145 NS_TEST_ASSERT_MSG_EQ ((item != 0), true, "A packet should have been dequeued");
146 NS_TEST_ASSERT_MSG_EQ (q->GetCurrentSize ().GetValue (), (numPackets-i) * modeSize, "There should be " << numPackets-i << " packet(s) in there");
147 NS_TEST_ASSERT_MSG_EQ (item->GetPacket ()->GetUid (), uids[i-1], "was this the right packet?");
148 }
149
150 item = q->Dequeue ();
151 NS_TEST_ASSERT_MSG_EQ ((item == 0), true, "There are really no packets in there");
152}
153
154void
156{
157 Ptr<FifoQueueDisc> queue;
158 uint32_t numPackets = 10;
159 uint32_t pktSize = 1000;
160 uint32_t modeSize = (mode == QueueSizeUnit::PACKETS ? 1 : pktSize);
161
162 // test 1: set the limit on the queue disc before initialization
163 queue = CreateObject<FifoQueueDisc> ();
164
165 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
166
167 NS_TEST_ASSERT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
168 QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
169 true, "Verify that we can actually set the attribute MaxSize");
170
171 queue->Initialize ();
172
173 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
174
175
176 // test 2: set the limit on the queue disc after initialization
177 queue = CreateObject<FifoQueueDisc> ();
178
179 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
180
181 queue->Initialize ();
182
183 NS_TEST_ASSERT_MSG_EQ (queue->SetAttributeFailSafe ("MaxSize",
184 QueueSizeValue (QueueSize (mode, numPackets*modeSize))),
185 true, "Verify that we can actually set the attribute MaxSize");
186
187 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
188
189
190 // test 3: set the limit on the internal queue before initialization
191 queue = CreateObject<FifoQueueDisc> ();
192
193 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
194
195 ObjectFactory factory;
196 factory.SetTypeId ("ns3::DropTailQueue<QueueDiscItem>");
197 if (mode == QueueSizeUnit::PACKETS)
198 {
199 factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, numPackets)));
200 }
201 else
202 {
203 factory.Set ("MaxSize", QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, numPackets*pktSize)));
204 }
205 queue->AddInternalQueue (factory.Create<QueueDisc::InternalQueue> ());
206
207 queue->Initialize ();
208
209 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
210
211
212 // test 4: set the limit on the internal queue after initialization
213 queue = CreateObject<FifoQueueDisc> ();
214
215 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 0, "Verify that the queue disc has no internal queue");
216
217 queue->Initialize ();
218
219 NS_TEST_ASSERT_MSG_EQ (queue->GetNInternalQueues (), 1, "Verify that the queue disc got an internal queue");
220
221 Ptr<QueueDisc::InternalQueue> iq = queue->GetInternalQueue (0);
222
223 if (mode == QueueSizeUnit::PACKETS)
224 {
225 NS_TEST_ASSERT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
227 true, "Verify that we can actually set the attribute MaxSize on the internal queue");
228 }
229 else
230 {
231 NS_TEST_ASSERT_MSG_EQ (iq->SetAttributeFailSafe ("MaxSize",
233 true, "Verify that we can actually set the attribute MaxSize on the internal queue");
234 }
235
236 DoRunFifoTest (queue, numPackets*modeSize, pktSize);
237}
238
239void
241{
244 Simulator::Destroy ();
245}
246
254{
255public:
257 : TestSuite ("fifo-queue-disc", UNIT)
258 {
259 AddTestCase (new FifoQueueDiscTestCase (), TestCase::QUICK);
260 }
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.
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