20 #include "ns3/packet.h"
21 #include "ns3/packet-tag-list.h"
23 #include "ns3/unused.h"
44 int result = (int)m_data;
56 std::ostringstream oss;
57 oss <<
"anon::ATestTag<" << N <<
">";
61 .HideFromDocumentation ()
69 return N +
sizeof(m_data);
73 for (uint32_t i = 0; i < N; ++i)
80 for (uint32_t i = 0; i < N; ++i)
89 virtual void Print (std::ostream &os)
const {
90 os << N <<
"(" << m_data <<
")";
110 std::ostringstream oss;
111 oss <<
"anon::ATestHeader<" << N <<
">";
113 .SetParent<Header> ()
115 .HideFromDocumentation ()
126 for (uint32_t i = 0; i < N; ++i)
132 for (uint32_t i = 0; i < N; ++i)
134 uint8_t v = iter.
ReadU8 ();
142 virtual void Print (std::ostream &os)
const {
161 std::ostringstream oss;
162 oss <<
"anon::ATestTrailer<" << N <<
">";
164 .SetParent<Header> ()
166 .HideFromDocumentation ()
178 for (uint32_t i = 0; i < N; ++i)
185 for (uint32_t i = 0; i < N; ++i)
187 uint8_t v = iter.
ReadU8 ();
195 virtual void Print (std::ostream &os)
const {
205 Expected (uint32_t n_, uint32_t start_, uint32_t end_)
206 : n (n_),
start (start_), end (end_) {}
216 #define E(a,b,c) a,b,c
218 #define CHECK(p, n, ...) \
219 DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__)
225 virtual void DoRun (
void);
238 std::vector<struct Expected> expected;
241 for (uint32_t k = 0; k < n; ++k)
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));
252 while (i.
HasNext () && j < expected.size ())
255 struct Expected e = expected[j];
256 std::ostringstream oss;
257 oss <<
"anon::ATestTag<" << e.n <<
">";
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);
283 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
286 std::string msg = std::string (reinterpret_cast<const char *>(buf),
296 CHECK (p, 1,
E (1, 0, 1000));
298 CHECK (copy, 1,
E (1, 0, 1000));
301 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
302 CHECK (copy, 1,
E (1, 0, 1000));
308 CHECK (&c0, 1,
E (1, 0, 1000));
309 CHECK (&c1, 1,
E (1, 0, 1000));
310 CHECK (copy, 1,
E (1, 0, 1000));
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));
321 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
323 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
325 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
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));
330 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
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));
339 frag0 = Create<Packet> (1000);
343 p = Create<Packet> (1000);
345 CHECK (p, 1,
E (20, 0, 1000));
347 CHECK (p, 1,
E (20, 0, 1000));
348 CHECK (frag0, 1,
E (20, 0, 90));
351 CHECK (frag0, 1,
E (20, 10, 100));
356 CHECK (tmp, 1,
E (20, 0, 100));
358 CHECK (tmp, 1,
E (20, 10, 110));
361 CHECK (tmp, 1,
E (20, 0, 100));
363 CHECK (tmp, 1,
E (20, 10, 110));
365 tmp = Create<Packet> (100);
367 CHECK (tmp, 1,
E (20, 0, 100));
369 CHECK (tmp, 1,
E (20, 0, 100));
372 CHECK (tmp, 1,
E (20, 0, 100));
374 CHECK (tmp, 1,
E (20, 0, 100));
382 CHECK (tmp, 1,
E (20, 0, 156));
384 CHECK (tmp, 1,
E (20, 0, 36));
387 CHECK (a, 1,
E (20, 0, 36));
393 CHECK (tmp, 0,
E (20, 0, 0));
398 CHECK (tmp, 1,
E (20, 0, 1000));
400 CHECK (tmp, 0,
E (0,0,0));
403 CHECK (a, 1,
E (10, 0, 10));
405 CHECK (tmp, 1,
E (10, 0, 10));
422 copy.AddPacketTag (c);
427 copy.RemovePacketTag (b);
444 CHECK (tmp, 1,
E (20, 0, 1000));
446 CHECK (tmp, 1,
E (20, 2, 1002));
448 CHECK (tmp, 1,
E (20, 1, 1001));
451 CHECK (tmp, 1,
E (20, 1, 1001));
472 const char * msg = 0);
491 int expect = t.GetData ();
492 bool found = ref.
Peek (t);
494 msg <<
": ref contains "
495 << t.GetTypeId ().GetName ());
498 msg <<
": ref " << t.GetTypeId ().GetName ()
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; \
537 const int reps = 10000;
538 std::vector< PacketTagList > ptv(reps, ref);
539 int start = clock ();
540 for (
int i = 0; i < reps; ++i) {
544 int delta = stop -
start;
546 std::cout <<
GetName () <<
"remove time: " << msg <<
": " << std::setw (8)
547 << delta <<
" ticks to remove "
557 const int reps = 100000;
560 int start = clock ();
561 for (
int i = 0; i < reps; ++i) {
566 int delta = stop -
start;
568 std::cout <<
GetName () <<
"add/remove time: " << std::setw (8)
569 << delta <<
" ticks to add+remove "
579 std::cout <<
GetName () <<
"begin" << std::endl;
593 std::cout <<
GetName () <<
"check Peek (missing tag) returns false"
600 std::cout <<
GetName () <<
"check copy and assignment" << std::endl;
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); \
620 std::cout <<
GetName () <<
"check removal of each tag" << std::endl;
631 std::cout <<
GetName () <<
"check removal doesn't disturb merge "
647 const char * msg =
"post merge, short chain";
659 std::cout <<
GetName () <<
"check replacing each tag" << std::endl;
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"); \
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) {
684 if (now < flm) flm = now;
686 std::cout <<
GetName () <<
"min add+remove time: "
687 << std::setw (8) << flm <<
" ticks"
690 std::cout <<
GetName () <<
"remove timing" << std::endl;
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) {
706 if (now < rmn[j]) rmn[j] = now;
709 for (
int j = tagLast; j > 0; --j) {
710 std::cout <<
GetName () <<
"min remove time: t"
712 << std::setw (8) << rmn[j] <<
" ticks"
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual void DoRun(void)
Implementation to actually run this TestCase.
ATestTagBase(uint8_t data)
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
TypeId AddConstructor(void)
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
void AddPacketTag(const Tag &tag) const
Add a packet tag.
List of the packet tags stored in a packet.
uint32_t GetSize(void) const
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
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)
virtual uint32_t GetSerializedSize(void) const
iterator in a Buffer instance
Callback< ObjectBase * > GetConstructor(void) const
virtual void Serialize(TagBuffer buf) const
void Add(Tag const &tag) const
Add a tag to the head of this branch.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
static TypeId GetTypeId(void)
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
uint8_t const * PeekData(void) const NS_DEPRECATED
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
void RemoveAllPacketTags(void)
Remove all packet tags.
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
void Prev(void)
go backward by one byte
virtual void Print(std::ostream &os) const
virtual uint32_t GetSerializedSize(void) const
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
void DoRun(void)
Implementation to actually run this TestCase.
virtual int GetData() const
ByteTagIterator GetByteTagIterator(void) const
static TypeId GetTypeId(void)
uint32_t GetEnd(void) const
virtual TypeId GetInstanceTypeId(void) const
virtual void Deserialize(TagBuffer buf)
Ptr< Packet > Copy(void) const
Protocol trailer serialization and deserialization.
tag a set of bytes in a packet
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Iterator over the set of byte tags in a packet.
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
std::string GetName(void) const
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
TypeId GetTypeId(void) const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
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.
uint32_t GetStart(void) const
void WriteU8(uint8_t data)
virtual ~PacketTagListTest()
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
static PacketTestSuite g_packetTestSuite
virtual uint32_t Deserialize(Buffer::Iterator iter)
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
std::string GetName(void) const
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
virtual TypeId GetInstanceTypeId(void) const
#define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)
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.
void AddHeader(const Header &header)
Add header to this packet.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
int AddRemoveTime(const bool verbose=false)