20 #include "ns3/packet.h"
21 #include "ns3/packet-tag-list.h"
23 #include "ns3/unused.h"
51 .SetGroupName (
"Network")
52 .HideFromDocumentation ()
58 int result = (int)m_data;
74 std::ostringstream oss;
75 oss <<
"anon::ATestTag<" << N <<
">";
77 .SetParent<ATestTagBase> ()
88 return N +
sizeof(m_data);
92 for (uint32_t i = 0; i < N; ++i)
99 for (uint32_t i = 0; i < N; ++i)
101 uint8_t v = buf.
ReadU8 ();
108 virtual void Print (std::ostream &os)
const {
109 os << N <<
"(" << m_data <<
")";
129 .SetGroupName (
"Network")
130 .HideFromDocumentation ()
147 std::ostringstream oss;
148 oss <<
"anon::ATestHeader<" << N <<
">";
150 .SetParent<ATestHeaderBase> ()
164 for (uint32_t i = 0; i < N; ++i)
170 for (uint32_t i = 0; i < N; ++i)
172 uint8_t v = iter.
ReadU8 ();
180 virtual void Print (std::ostream &os)
const {
199 .SetGroupName (
"Network")
200 .HideFromDocumentation ()
217 std::ostringstream oss;
218 oss <<
"anon::ATestTrailer<" << N <<
">";
220 .SetParent<ATestTrailerBase> ()
235 for (uint32_t i = 0; i < N; ++i)
242 for (uint32_t i = 0; i < N; ++i)
244 uint8_t v = iter.
ReadU8 ();
252 virtual void Print (std::ostream &os)
const {
262 Expected (uint32_t n_, uint32_t start_, uint32_t end_)
263 : n (n_),
start (start_), end (end_) {}
273 #define E(a,b,c) a,b,c
275 #define CHECK(p, n, ...) \
276 DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__)
282 virtual void DoRun (
void);
295 std::vector<struct Expected> expected;
298 for (uint32_t k = 0; k < n; ++k)
300 uint32_t N = va_arg (ap, uint32_t);
301 uint32_t
start = va_arg (ap, uint32_t);
302 uint32_t end = va_arg (ap, uint32_t);
303 expected.push_back (Expected (N, start, end));
309 while (i.
HasNext () && j < expected.size ())
312 struct Expected e = expected[j];
313 std::ostringstream oss;
314 oss <<
"anon::ATestTag<" << e.n <<
">";
332 Ptr<Packet> pkt1 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
"hello"), 5);
333 Ptr<Packet> pkt2 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
" world"), 6);
340 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
343 std::string msg = std::string (reinterpret_cast<const char *>(buf),
353 CHECK (p, 1,
E (1, 0, 1000));
355 CHECK (copy, 1,
E (1, 0, 1000));
358 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
359 CHECK (copy, 1,
E (1, 0, 1000));
365 CHECK (&c0, 1,
E (1, 0, 1000));
366 CHECK (&c1, 1,
E (1, 0, 1000));
367 CHECK (copy, 1,
E (1, 0, 1000));
369 CHECK (&c0, 2,
E (1, 0, 1000),
E (10, 0, 1000));
370 CHECK (&c1, 1,
E (1, 0, 1000));
371 CHECK (copy, 1,
E (1, 0, 1000));
378 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
380 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
382 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
385 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));
387 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
390 E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10),
391 E (1, 10, 100),
E (2, 10, 100),
E (4, 10, 100),
392 E (1, 100, 1000),
E (2, 100, 1000),
E (5, 100, 1000));
396 frag0 = Create<Packet> (1000);
400 p = Create<Packet> (1000);
402 CHECK (p, 1,
E (20, 0, 1000));
404 CHECK (p, 1,
E (20, 0, 1000));
405 CHECK (frag0, 1,
E (20, 0, 90));
408 CHECK (frag0, 1,
E (20, 10, 100));
413 CHECK (tmp, 1,
E (20, 0, 100));
415 CHECK (tmp, 1,
E (20, 10, 110));
418 CHECK (tmp, 1,
E (20, 0, 100));
420 CHECK (tmp, 1,
E (20, 10, 110));
422 tmp = Create<Packet> (100);
424 CHECK (tmp, 1,
E (20, 0, 100));
426 CHECK (tmp, 1,
E (20, 0, 100));
429 CHECK (tmp, 1,
E (20, 0, 100));
431 CHECK (tmp, 1,
E (20, 0, 100));
439 CHECK (tmp, 1,
E (20, 0, 156));
441 CHECK (tmp, 1,
E (20, 0, 36));
444 CHECK (a, 1,
E (20, 0, 36));
450 CHECK (tmp, 0,
E (20, 0, 0));
455 CHECK (tmp, 1,
E (20, 0, 1000));
457 CHECK (tmp, 0,
E (0,0,0));
460 CHECK (a, 1,
E (10, 0, 10));
462 CHECK (tmp, 1,
E (10, 0, 10));
479 copy.AddPacketTag (c);
484 copy.RemovePacketTag (b);
501 CHECK (tmp, 1,
E (20, 0, 1000));
503 CHECK (tmp, 1,
E (20, 2, 1002));
505 CHECK (tmp, 1,
E (20, 1, 1001));
508 CHECK (tmp, 1,
E (20, 1, 1001));
517 CHECK (tmp, 1,
E (25, 0, 100));
519 CHECK (tmp, 1,
E (25, 0, 50));
521 CHECK (tmp, 1,
E (25, 50, 100));
529 CHECK (tmp, 1,
E (25, 0, 100));
531 CHECK (tmp, 1,
E (25, 0, 50));
533 CHECK (tmp, 1,
E (25, 0, 50));
541 CHECK (tmp, 1,
E (25, 0, 100));
543 CHECK (tmp, 1,
E (25, 0, 50));
545 CHECK (tmp, 1,
E (25, 25, 75));
553 CHECK (tmp, 1,
E (25, 0, 100));
555 CHECK (tmp, 1,
E (25, 0, 50));
557 CHECK (tmp, 1,
E (25, 0, 50));
565 CHECK (tmp, 1,
E (25, 0, 100));
567 CHECK (tmp, 1,
E (25, 0, 50));
569 CHECK (tmp, 1,
E (25, 0, 50));
579 CHECK (tmp, 1,
E (25, 0, 100));
581 CHECK (tmp, 1,
E (25, 0, 50));
583 CHECK (tmp, 1,
E (25, 0, 50));
603 const char * msg = 0);
622 int expect = t.GetData ();
623 bool found = ref.
Peek (t);
625 msg <<
": ref contains "
626 << t.GetTypeId ().GetName ());
629 msg <<
": ref " << t.GetTypeId ().GetName ()
635 #define MAKE_TEST_TAGS \
636 ATestTag<1> t1 (1); \
637 ATestTag<2> t2 (1); \
638 ATestTag<3> t3 (1); \
639 ATestTag<4> t4 (1); \
640 ATestTag<5> t5 (1); \
641 ATestTag<6> t6 (1); \
642 ATestTag<7> t7 (1); \
643 const int tagLast = 7; \
668 const int reps = 10000;
669 std::vector< PacketTagList > ptv(reps, ref);
670 int start = clock ();
671 for (
int i = 0; i < reps; ++i) {
675 int delta = stop -
start;
677 std::cout <<
GetName () <<
"remove time: " << msg <<
": " << std::setw (8)
678 << delta <<
" ticks to remove "
688 const int reps = 100000;
691 int start = clock ();
692 for (
int i = 0; i < reps; ++i) {
697 int delta = stop -
start;
699 std::cout <<
GetName () <<
"add/remove time: " << std::setw (8)
700 << delta <<
" ticks to add+remove "
710 std::cout <<
GetName () <<
"begin" << std::endl;
724 std::cout <<
GetName () <<
"check Peek (missing tag) returns false"
731 std::cout <<
GetName () <<
"check copy and assignment" << std::endl;
743 # define RemoveCheck(n) \
744 { PacketTagList p ## n = ref; \
745 p ## n .Remove ( t ## n ); \
746 CheckRefList (ref, "remove " #n " orig"); \
747 CheckRefList (p ## n, "remove " #n " copy", n); \
751 std::cout <<
GetName () <<
"check removal of each tag" << std::endl;
762 std::cout <<
GetName () <<
"check removal doesn't disturb merge "
778 const char * msg =
"post merge, short chain";
790 std::cout <<
GetName () <<
"check replacing each tag" << std::endl;
792 # define ReplaceCheck(n) \
793 t ## n .m_data = 2; \
794 { PacketTagList p ## n = ref; \
795 p ## n .Replace ( t ## n ); \
796 CheckRefList (ref, "replace " #n " orig"); \
797 CheckRef (p ## n, t ## n, "replace " #n " copy"); \
810 std::cout <<
GetName () <<
"add+remove timing" << std::endl;
812 const int nIterations = 100;
813 for (
int i = 0; i < nIterations; ++i) {
815 if (now < flm) flm = now;
817 std::cout <<
GetName () <<
"min add+remove time: "
818 << std::setw (8) << flm <<
" ticks"
821 std::cout <<
GetName () <<
"remove timing" << std::endl;
824 for (
int i = 0; i < nIterations; ++i) {
825 for (
int j = 1; j <= tagLast; ++j) {
837 if (now < rmn[j]) rmn[j] = now;
840 for (
int j = tagLast; j > 0; --j) {
841 std::cout <<
GetName () <<
"min remove time: t"
843 << 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)
Record in this TypeId the fact that the default constructor is accessible.
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.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
static TypeId GetTypeId(void)
Register this type.
static TypeId GetTypeId(void)
Register this type.
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
Get the constructor callback.
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)
Register this type.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
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 AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
void Prev(void)
go backward by one byte
virtual void Print(std::ostream &os) const
virtual uint32_t GetSerializedSize(void) const
void DoRun(void)
Implementation to actually run this TestCase.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
ByteTagIterator GetByteTagIterator(void) const
Retiurns an iterator over the set of byte tags included in this packet.
static TypeId GetTypeId(void)
Register this type.
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
TypeId SetGroupName(std::string groupName)
Set the group name.
virtual void Deserialize(TagBuffer buf)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Protocol trailer serialization and deserialization.
tag a set of bytes in a packet
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Iterator over the set of byte tags in a packet.
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
std::string GetName(void) const
Get the name.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
TypeId GetTypeId(void) const
#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.
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
The index is an offset from the start of the packet.
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
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
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.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
void AddHeader(const Header &header)
Add header to this packet.
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
int AddRemoveTime(const bool verbose=false)