A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
packet-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) 2005,2006 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/packet.h"
21 #include "ns3/packet-tag-list.h"
22 #include "ns3/test.h"
23 #include "ns3/unused.h"
24 #include <limits> // std:numeric_limits
25 #include <string>
26 #include <cstdarg>
27 #include <iostream>
28 #include <iomanip>
29 #include <ctime>
30 
31 using namespace ns3;
32 
33 //-----------------------------------------------------------------------------
34 // Unit tests
35 //-----------------------------------------------------------------------------
36 namespace {
37 
38 class ATestTagBase : public Tag
39 {
40 public:
41  ATestTagBase () : m_error (false), m_data (0) {}
42  ATestTagBase (uint8_t data) : m_error (false), m_data (data) {}
43  virtual int GetData () const {
44  int result = (int)m_data;
45  return result;
46  }
47  bool m_error;
48  uint8_t m_data;
49 };
50 
51 template <int N>
52 class ATestTag : public ATestTagBase
53 {
54 public:
55  static TypeId GetTypeId (void) {
56  std::ostringstream oss;
57  oss << "anon::ATestTag<" << N << ">";
58  static TypeId tid = TypeId (oss.str ().c_str ())
59  .SetParent<Tag> ()
61  .HideFromDocumentation ()
62  ;
63  return tid;
64  }
65  virtual TypeId GetInstanceTypeId (void) const {
66  return GetTypeId ();
67  }
68  virtual uint32_t GetSerializedSize (void) const {
69  return N + sizeof(m_data);
70  }
71  virtual void Serialize (TagBuffer buf) const {
72  buf.WriteU8 (m_data);
73  for (uint32_t i = 0; i < N; ++i)
74  {
75  buf.WriteU8 (N);
76  }
77  }
78  virtual void Deserialize (TagBuffer buf) {
79  m_data = buf.ReadU8 ();
80  for (uint32_t i = 0; i < N; ++i)
81  {
82  uint8_t v = buf.ReadU8 ();
83  if (v != N)
84  {
85  m_error = true;
86  }
87  }
88  }
89  virtual void Print (std::ostream &os) const {
90  os << N << "(" << m_data << ")";
91  }
93  : ATestTagBase () {}
94  ATestTag (uint8_t data)
95  : ATestTagBase (data) {}
96 };
97 
98 class ATestHeaderBase : public Header
99 {
100 public:
101  ATestHeaderBase () : Header (), m_error (false) {}
102  bool m_error;
103 };
104 
105 template <int N>
107 {
108 public:
109  static TypeId GetTypeId (void) {
110  std::ostringstream oss;
111  oss << "anon::ATestHeader<" << N << ">";
112  static TypeId tid = TypeId (oss.str ().c_str ())
113  .SetParent<Header> ()
115  .HideFromDocumentation ()
116  ;
117  return tid;
118  }
119  virtual TypeId GetInstanceTypeId (void) const {
120  return GetTypeId ();
121  }
122  virtual uint32_t GetSerializedSize (void) const {
123  return N;
124  }
125  virtual void Serialize (Buffer::Iterator iter) const {
126  for (uint32_t i = 0; i < N; ++i)
127  {
128  iter.WriteU8 (N);
129  }
130  }
131  virtual uint32_t Deserialize (Buffer::Iterator iter) {
132  for (uint32_t i = 0; i < N; ++i)
133  {
134  uint8_t v = iter.ReadU8 ();
135  if (v != N)
136  {
137  m_error = true;
138  }
139  }
140  return N;
141  }
142  virtual void Print (std::ostream &os) const {
143  }
145  : ATestHeaderBase () {}
146 
147 };
148 
149 class ATestTrailerBase : public Trailer
150 {
151 public:
152  ATestTrailerBase () : Trailer (), m_error (false) {}
153  bool m_error;
154 };
155 
156 template <int N>
158 {
159 public:
160  static TypeId GetTypeId (void) {
161  std::ostringstream oss;
162  oss << "anon::ATestTrailer<" << N << ">";
163  static TypeId tid = TypeId (oss.str ().c_str ())
164  .SetParent<Header> ()
166  .HideFromDocumentation ()
167  ;
168  return tid;
169  }
170  virtual TypeId GetInstanceTypeId (void) const {
171  return GetTypeId ();
172  }
173  virtual uint32_t GetSerializedSize (void) const {
174  return N;
175  }
176  virtual void Serialize (Buffer::Iterator iter) const {
177  iter.Prev (N);
178  for (uint32_t i = 0; i < N; ++i)
179  {
180  iter.WriteU8 (N);
181  }
182  }
183  virtual uint32_t Deserialize (Buffer::Iterator iter) {
184  iter.Prev (N);
185  for (uint32_t i = 0; i < N; ++i)
186  {
187  uint8_t v = iter.ReadU8 ();
188  if (v != N)
189  {
190  m_error = true;
191  }
192  }
193  return N;
194  }
195  virtual void Print (std::ostream &os) const {
196  }
198  : ATestTrailerBase () {}
199 
200 };
201 
202 
203 struct Expected
204 {
205  Expected (uint32_t n_, uint32_t start_, uint32_t end_)
206  : n (n_), start (start_), end (end_) {}
207 
208  uint32_t n;
209  uint32_t start;
210  uint32_t end;
211 };
212 
213 }
214 
215 // tag name, start, end
216 #define E(a,b,c) a,b,c
217 
218 #define CHECK(p, n, ...) \
219  DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__)
220 
221 class PacketTest : public TestCase
222 {
223 public:
224  PacketTest ();
225  virtual void DoRun (void);
226 private:
227  void DoCheck (Ptr<const Packet> p, const char *file, int line, uint32_t n, ...);
228 };
229 
230 
232  : TestCase ("Packet") {
233 }
234 
235 void
236 PacketTest::DoCheck (Ptr<const Packet> p, const char *file, int line, uint32_t n, ...)
237 {
238  std::vector<struct Expected> expected;
239  va_list ap;
240  va_start (ap, n);
241  for (uint32_t k = 0; k < n; ++k)
242  {
243  uint32_t N = va_arg (ap, uint32_t);
244  uint32_t start = va_arg (ap, uint32_t);
245  uint32_t end = va_arg (ap, uint32_t);
246  expected.push_back (Expected (N, start, end));
247  }
248  va_end (ap);
249 
251  uint32_t j = 0;
252  while (i.HasNext () && j < expected.size ())
253  {
254  ByteTagIterator::Item item = i.Next ();
255  struct Expected e = expected[j];
256  std::ostringstream oss;
257  oss << "anon::ATestTag<" << e.n << ">";
258  NS_TEST_EXPECT_MSG_EQ_INTERNAL (item.GetTypeId ().GetName (), oss.str (), "trivial", file, line);
259  NS_TEST_EXPECT_MSG_EQ_INTERNAL (item.GetStart (), e.start, "trivial", file, line);
260  NS_TEST_EXPECT_MSG_EQ_INTERNAL (item.GetEnd (), e.end, "trivial", file, line);
261  ATestTagBase *tag = dynamic_cast<ATestTagBase *> (item.GetTypeId ().GetConstructor () ());
262  NS_TEST_EXPECT_MSG_NE (tag, 0, "trivial");
263  item.GetTag (*tag);
264  NS_TEST_EXPECT_MSG_EQ (tag->m_error, false, "trivial");
265  delete tag;
266  j++;
267  }
268  NS_TEST_EXPECT_MSG_EQ (i.HasNext (), false, "Nothing left");
269  NS_TEST_EXPECT_MSG_EQ (j, expected.size (), "Size match");
270 }
271 
272 void
274 {
275  Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
276  Ptr<Packet> pkt2 = Create<Packet> (reinterpret_cast<const uint8_t*> (" world"), 6);
277  Ptr<Packet> packet = Create<Packet> ();
278  packet->AddAtEnd (pkt1);
279  packet->AddAtEnd (pkt2);
280 
281  NS_TEST_EXPECT_MSG_EQ (packet->GetSize (), 11, "trivial");
282 
283  uint8_t *buf = new uint8_t[packet->GetSize ()];
284  packet->CopyData (buf, packet->GetSize ());
285 
286  std::string msg = std::string (reinterpret_cast<const char *>(buf),
287  packet->GetSize ());
288  delete [] buf;
289 
290  NS_TEST_EXPECT_MSG_EQ (msg, "hello world", "trivial");
291 
292 
293  Ptr<const Packet> p = Create<Packet> (1000);
294 
295  p->AddByteTag (ATestTag<1> ());
296  CHECK (p, 1, E (1, 0, 1000));
297  Ptr<const Packet> copy = p->Copy ();
298  CHECK (copy, 1, E (1, 0, 1000));
299 
300  p->AddByteTag (ATestTag<2> ());
301  CHECK (p, 2, E (1, 0, 1000), E (2, 0, 1000));
302  CHECK (copy, 1, E (1, 0, 1000));
303 
304  {
305  Packet c0 = *copy;
306  Packet c1 = *copy;
307  c0 = c1;
308  CHECK (&c0, 1, E (1, 0, 1000));
309  CHECK (&c1, 1, E (1, 0, 1000));
310  CHECK (copy, 1, E (1, 0, 1000));
311  c0.AddByteTag (ATestTag<10> ());
312  CHECK (&c0, 2, E (1, 0, 1000), E (10, 0, 1000));
313  CHECK (&c1, 1, E (1, 0, 1000));
314  CHECK (copy, 1, E (1, 0, 1000));
315  }
316 
317  Ptr<Packet> frag0 = p->CreateFragment (0, 10);
318  Ptr<Packet> frag1 = p->CreateFragment (10, 90);
319  Ptr<const Packet> frag2 = p->CreateFragment (100, 900);
320  frag0->AddByteTag (ATestTag<3> ());
321  CHECK (frag0, 3, E (1, 0, 10), E (2, 0, 10), E (3, 0, 10));
322  frag1->AddByteTag (ATestTag<4> ());
323  CHECK (frag1, 3, E (1, 0, 90), E (2, 0, 90), E (4, 0, 90));
324  frag2->AddByteTag (ATestTag<5> ());
325  CHECK (frag2, 3, E (1, 0, 900), E (2, 0, 900), E (5, 0, 900));
326 
327  frag1->AddAtEnd (frag2);
328  CHECK (frag1, 6, E (1, 0, 90), E (2, 0, 90), E (4, 0, 90), E (1, 90, 990), E (2, 90, 990), E (5, 90, 990));
329 
330  CHECK (frag0, 3, E (1, 0, 10), E (2, 0, 10), E (3, 0, 10));
331  frag0->AddAtEnd (frag1);
332  CHECK (frag0, 9,
333  E (1, 0, 10), E (2, 0, 10), E (3, 0, 10),
334  E (1, 10, 100), E (2, 10, 100), E (4, 10, 100),
335  E (1, 100, 1000), E (2, 100, 1000), E (5, 100, 1000));
336 
337 
338  // force caching a buffer of the right size.
339  frag0 = Create<Packet> (1000);
340  frag0->AddHeader (ATestHeader<10> ());
341  frag0 = 0;
342 
343  p = Create<Packet> (1000);
344  p->AddByteTag (ATestTag<20> ());
345  CHECK (p, 1, E (20, 0, 1000));
346  frag0 = p->CreateFragment (10, 90);
347  CHECK (p, 1, E (20, 0, 1000));
348  CHECK (frag0, 1, E (20, 0, 90));
349  p = 0;
350  frag0->AddHeader (ATestHeader<10> ());
351  CHECK (frag0, 1, E (20, 10, 100));
352 
353  {
354  Ptr<Packet> tmp = Create<Packet> (100);
355  tmp->AddByteTag (ATestTag<20> ());
356  CHECK (tmp, 1, E (20, 0, 100));
357  tmp->AddHeader (ATestHeader<10> ());
358  CHECK (tmp, 1, E (20, 10, 110));
359  ATestHeader<10> h;
360  tmp->RemoveHeader (h);
361  CHECK (tmp, 1, E (20, 0, 100));
362  tmp->AddHeader (ATestHeader<10> ());
363  CHECK (tmp, 1, E (20, 10, 110));
364 
365  tmp = Create<Packet> (100);
366  tmp->AddByteTag (ATestTag<20> ());
367  CHECK (tmp, 1, E (20, 0, 100));
368  tmp->AddTrailer (ATestTrailer<10> ());
369  CHECK (tmp, 1, E (20, 0, 100));
370  ATestTrailer<10> t;
371  tmp->RemoveTrailer (t);
372  CHECK (tmp, 1, E (20, 0, 100));
373  tmp->AddTrailer (ATestTrailer<10> ());
374  CHECK (tmp, 1, E (20, 0, 100));
375 
376  }
377 
378  {
379  Ptr<Packet> tmp = Create<Packet> (0);
380  tmp->AddHeader (ATestHeader<156> ());
381  tmp->AddByteTag (ATestTag<20> ());
382  CHECK (tmp, 1, E (20, 0, 156));
383  tmp->RemoveAtStart (120);
384  CHECK (tmp, 1, E (20, 0, 36));
385  Ptr<Packet> a = Create<Packet> (0);
386  a->AddAtEnd (tmp);
387  CHECK (a, 1, E (20, 0, 36));
388  }
389 
390  {
391  Ptr<Packet> tmp = Create<Packet> (0);
392  tmp->AddByteTag (ATestTag<20> ());
393  CHECK (tmp, 0, E (20, 0, 0));
394  }
395  {
396  Ptr<Packet> tmp = Create<Packet> (1000);
397  tmp->AddByteTag (ATestTag<20> ());
398  CHECK (tmp, 1, E (20, 0, 1000));
399  tmp->RemoveAtStart (1000);
400  CHECK (tmp, 0, E (0,0,0));
401  Ptr<Packet> a = Create<Packet> (10);
402  a->AddByteTag (ATestTag<10> ());
403  CHECK (a, 1, E (10, 0, 10));
404  tmp->AddAtEnd (a);
405  CHECK (tmp, 1, E (10, 0, 10));
406  }
407 
408  {
409  Packet p;
410  ATestTag<10> a;
411  p.AddPacketTag (a);
412  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (a), true, "trivial");
413  ATestTag<11> b;
414  p.AddPacketTag (b);
415  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (b), true, "trivial");
416  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (a), true, "trivial");
417  Packet copy = p;
418  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (b), true, "trivial");
419  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (a), true, "trivial");
420  ATestTag<12> c;
421  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (c), false, "trivial");
422  copy.AddPacketTag (c);
423  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (c), true, "trivial");
424  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (b), true, "trivial");
425  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (a), true, "trivial");
426  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (c), false, "trivial");
427  copy.RemovePacketTag (b);
428  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (b), false, "trivial");
429  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (b), true, "trivial");
430  p.RemovePacketTag (a);
431  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (a), false, "trivial");
432  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (a), true, "trivial");
433  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (c), false, "trivial");
434  NS_TEST_EXPECT_MSG_EQ (copy.PeekPacketTag (c), true, "trivial");
435  p.RemoveAllPacketTags ();
436  NS_TEST_EXPECT_MSG_EQ (p.PeekPacketTag (b), false, "trivial");
437  }
438 
439  {
442  Ptr<Packet> tmp = Create<Packet> (1000);
443  tmp->AddByteTag (ATestTag<20> ());
444  CHECK (tmp, 1, E (20, 0, 1000));
445  tmp->AddHeader (ATestHeader<2> ());
446  CHECK (tmp, 1, E (20, 2, 1002));
447  tmp->RemoveAtStart (1);
448  CHECK (tmp, 1, E (20, 1, 1001));
449 #if 0
450  tmp->PeekData ();
451  CHECK (tmp, 1, E (20, 1, 1001));
452 #endif
453  }
454 }
455 //--------------------------------------
457 {
458 public:
460  virtual ~PacketTagListTest ();
461 private:
462  void DoRun (void);
463  void CheckRef (const PacketTagList & ref,
464  ATestTagBase & t,
465  const char * msg,
466  bool miss = false);
467  void CheckRefList (const PacketTagList & ref,
468  const char * msg,
469  int miss = 0);
470  int RemoveTime (const PacketTagList & ref,
471  ATestTagBase & t,
472  const char * msg = 0);
473  int AddRemoveTime (const bool verbose = false);
474 };
475 
477  : TestCase ("PacketTagListTest: ")
478 {
479 }
480 
482 {
483 }
484 
485 void
487  ATestTagBase & t,
488  const char * msg,
489  bool miss)
490 {
491  int expect = t.GetData (); // the value we should find
492  bool found = ref.Peek (t); // rewrites t with actual value
493  NS_TEST_EXPECT_MSG_EQ (found, !miss,
494  msg << ": ref contains "
495  << t.GetTypeId ().GetName ());
496  if (found) {
497  NS_TEST_EXPECT_MSG_EQ (t.GetData (), expect,
498  msg << ": ref " << t.GetTypeId ().GetName ()
499  << " = " << expect);
500  }
501 }
502 
503  // A set of tags with data value 1, to check COW
504 #define MAKE_TEST_TAGS \
505  ATestTag<1> t1 (1); \
506  ATestTag<2> t2 (1); \
507  ATestTag<3> t3 (1); \
508  ATestTag<4> t4 (1); \
509  ATestTag<5> t5 (1); \
510  ATestTag<6> t6 (1); \
511  ATestTag<7> t7 (1); \
512  const int tagLast = 7; /* length of ref PacketTagList */ \
513  NS_UNUSED (tagLast) /* silence warnings */
514 
515 
516 
517 void
519  const char * msg,
520  int miss /* = 0 */)
521 {
523  CheckRef (ptl, t1, msg, miss == 1);
524  CheckRef (ptl, t2, msg, miss == 2);
525  CheckRef (ptl, t3, msg, miss == 3);
526  CheckRef (ptl, t4, msg, miss == 4);
527  CheckRef (ptl, t5, msg, miss == 5);
528  CheckRef (ptl, t6, msg, miss == 6);
529  CheckRef (ptl, t7, msg, miss == 7);
530 }
531 
532 int
534  ATestTagBase & t,
535  const char * msg /* = 0 */)
536 {
537  const int reps = 10000;
538  std::vector< PacketTagList > ptv(reps, ref);
539  int start = clock ();
540  for (int i = 0; i < reps; ++i) {
541  ptv[i].Remove (t);
542  }
543  int stop = clock ();
544  int delta = stop - start;
545  if (msg) {
546  std::cout << GetName () << "remove time: " << msg << ": " << std::setw (8)
547  << delta << " ticks to remove "
548  << reps << " times"
549  << std::endl;
550  }
551  return delta;
552 }
553 
554 int
555 PacketTagListTest::AddRemoveTime (const bool verbose /* = false */)
556 {
557  const int reps = 100000;
558  PacketTagList ptl;
559  ATestTag <2> t(2);
560  int start = clock ();
561  for (int i = 0; i < reps; ++i) {
562  ptl.Add (t);
563  ptl.Remove (t);
564  }
565  int stop = clock ();
566  int delta = stop - start;
567  if (verbose) {
568  std::cout << GetName () << "add/remove time: " << std::setw (8)
569  << delta << " ticks to add+remove "
570  << reps << " times"
571  << std::endl;
572  }
573  return delta;
574 }
575 
576 void
578 {
579  std::cout << GetName () << "begin" << std::endl;
580 
582 
583  PacketTagList ref; // empty list
584  ref.Add (t1); // last
585  ref.Add (t2); // post merge
586  ref.Add (t3); // merge successor
587  ref.Add (t4); // merge
588  ref.Add (t5); // merge precursor
589  ref.Add (t6); // pre-merge
590  ref.Add (t7); // first
591 
592  { // Peek
593  std::cout << GetName () << "check Peek (missing tag) returns false"
594  << std::endl;;
595  ATestTag<10> t10;
596  NS_TEST_EXPECT_MSG_EQ (ref.Peek (t10), false, "missing tag");
597  }
598 
599  { // Copy ctor, assignment
600  std::cout << GetName () << "check copy and assignment" << std::endl;
601  { PacketTagList ptl (ref);
602  CheckRefList (ref, "copy ctor orig");
603  CheckRefList (ptl, "copy ctor copy");
604  }
605  { PacketTagList ptl = ref;
606  CheckRefList (ref, "assignment orig");
607  CheckRefList (ptl, "assignment copy");
608  }
609  }
610 
611  { // Removal
612 # define RemoveCheck(n) \
613  { PacketTagList p ## n = ref; \
614  p ## n .Remove ( t ## n ); \
615  CheckRefList (ref, "remove " #n " orig"); \
616  CheckRefList (p ## n, "remove " #n " copy", n); \
617  }
618 
619  { // Remove single tags from list
620  std::cout << GetName () << "check removal of each tag" << std::endl;
621  RemoveCheck (1);
622  RemoveCheck (2);
623  RemoveCheck (3);
624  RemoveCheck (4);
625  RemoveCheck (5);
626  RemoveCheck (6);
627  RemoveCheck (7);
628  }
629 
630  { // Remove in the presence of a merge
631  std::cout << GetName () << "check removal doesn't disturb merge "
632  << std::endl;
633  PacketTagList ptl = ref;
634  ptl.Remove (t7);
635  ptl.Remove (t6);
636  ptl.Remove (t5);
637 
638  PacketTagList mrg = ptl; // merged list
639  ATestTag<8> m5 (1);
640  mrg.Add (m5); // ptl and mrg differ
641  ptl.Add (t5);
642  ptl.Add (t6);
643  ptl.Add (t7);
644 
645  CheckRefList (ref, "post merge, orig");
646  CheckRefList (ptl, "post merge, long chain");
647  const char * msg = "post merge, short chain";
648  CheckRef (mrg, t1, msg, false);
649  CheckRef (mrg, t2, msg, false);
650  CheckRef (mrg, t3, msg, false);
651  CheckRef (mrg, t4, msg, false);
652  CheckRef (mrg, m5, msg, false);
653  }
654 # undef RemoveCheck
655  } // Removal
656 
657  { // Replace
658 
659  std::cout << GetName () << "check replacing each tag" << std::endl;
660 
661 # define ReplaceCheck(n) \
662  t ## n .m_data = 2; \
663  { PacketTagList p ## n = ref; \
664  p ## n .Replace ( t ## n ); \
665  CheckRefList (ref, "replace " #n " orig"); \
666  CheckRef (p ## n, t ## n, "replace " #n " copy"); \
667  }
668 
669  ReplaceCheck (1);
670  ReplaceCheck (2);
671  ReplaceCheck (3);
672  ReplaceCheck (4);
673  ReplaceCheck (5);
674  ReplaceCheck (6);
675  ReplaceCheck (7);
676  }
677 
678  { // Timing
679  std::cout << GetName () << "add+remove timing" << std::endl;
680  int flm = std::numeric_limits<int>::max ();
681  const int nIterations = 100;
682  for (int i = 0; i < nIterations; ++i) {
683  int now = AddRemoveTime ();
684  if (now < flm) flm = now;
685  }
686  std::cout << GetName () << "min add+remove time: "
687  << std::setw (8) << flm << " ticks"
688  << std::endl;
689 
690  std::cout << GetName () << "remove timing" << std::endl;
691  // tags numbered from 1, so add one for (unused) entry at 0
692  std::vector <int> rmn (tagLast + 1, std::numeric_limits<int>::max ());
693  for (int i = 0; i < nIterations; ++i) {
694  for (int j = 1; j <= tagLast; ++j) {
695  int now = 0;
696  switch (j) {
697  case 7: now = RemoveTime (ref, t7); break;
698  case 6: now = RemoveTime (ref, t6); break;
699  case 5: now = RemoveTime (ref, t5); break;
700  case 4: now = RemoveTime (ref, t4); break;
701  case 3: now = RemoveTime (ref, t3); break;
702  case 2: now = RemoveTime (ref, t2); break;
703  case 1: now = RemoveTime (ref, t1); break;
704  } // switch
705 
706  if (now < rmn[j]) rmn[j] = now;
707  } // for tag j
708  } // for iteration i
709  for (int j = tagLast; j > 0; --j) {
710  std::cout << GetName () << "min remove time: t"
711  << j << ": "
712  << std::setw (8) << rmn[j] << " ticks"
713  << std::endl;
714  }
715  } // Timing
716 
717 }
718 
719 //-----------------------------------------------------------------------------
721 {
722 public:
723  PacketTestSuite ();
724 };
725 
727  : TestSuite ("packet", UNIT)
728 {
729  AddTestCase (new PacketTest, TestCase::QUICK);
730  AddTestCase (new PacketTagListTest, TestCase::QUICK);
731 }
732 
bool HasNext(void) const
Definition: packet.cc:65
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
TypeId AddConstructor(void)
Definition: type-id.h:418
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
virtual void Serialize(Buffer::Iterator iter) const
virtual uint32_t Deserialize(Buffer::Iterator iter)
A suite of tests to run.
Definition: test.h:1105
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
List of the packet tags stored in a packet.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:265
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
encapsulates test code
Definition: test.h:929
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
network packets
Definition: packet.h:223
virtual void Print(std::ostream &os) const
bool Peek(Tag &tag) const
Find a tag and return its value.
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
iterator in a Buffer instance
Definition: buffer.h:98
Callback< ObjectBase * > GetConstructor(void) const
Definition: type-id.cc:722
void Add(Tag const &tag) const
Add a tag to the head of this branch.
#define MAKE_TEST_TAGS
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:228
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:317
uint8_t const * PeekData(void) const NS_DEPRECATED
Definition: packet.cc:368
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:863
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:869
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:57
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:353
void Prev(void)
go backward by one byte
Definition: buffer.h:858
uint8_t data[writeSize]
void DoRun(void)
Implementation to actually run this TestCase.
ByteTagIterator GetByteTagIterator(void) const
Retiurns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:818
Item Next(void)
Definition: packet.cc:70
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:44
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
tag a set of bytes in a packet
Definition: tag.h:36
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:284
Iterator over the set of byte tags in a packet.
Definition: packet.h:50
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:300
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition: test.h:704
std::string GetName(void) const
Definition: type-id.cc:657
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
TypeId GetTypeId(void) const
Definition: packet.cc:34
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
#define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:219
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:49
read and write tag data
Definition: tag-buffer.h:51
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:39
void WriteU8(uint8_t data)
Definition: buffer.h:876
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:848
#define ReplaceCheck(n)
static PacketTestSuite g_packetTestSuite
virtual uint32_t Deserialize(Buffer::Iterator iter)
uint8_t ReadU8(void)
Definition: buffer.h:1028
#define RemoveCheck(n)
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
std::string GetName(void) const
Definition: test.cc:253
virtual void Serialize(Buffer::Iterator iter) const
void DoCheck(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
a unique identifier for an interface.
Definition: type-id.h:49
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
bool verbose
#define E(a, b, c)
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:808
#define CHECK(p, n,...)
int AddRemoveTime(const bool verbose=false)