20 #include "ns3/packet.h" 21 #include "ns3/packet-tag-list.h" 23 #include "ns3/unused.h" 61 .SetGroupName (
"Network")
62 .HideFromDocumentation ()
70 int result = (int)m_data;
94 std::ostringstream oss;
95 oss <<
"anon::ATestTag<" << N <<
">";
97 .SetParent<ATestTagBase> ()
108 return N +
sizeof(m_data);
112 for (uint32_t i = 0; i < N; ++i)
119 for (uint32_t i = 0; i < N; ++i)
121 uint8_t v = buf.
ReadU8 ();
128 virtual void Print (std::ostream &os)
const {
129 os << N <<
"(" << m_data <<
")";
141 #define LARGE_TAG_BUFFER_SIZE 64 159 m_data.push_back (i);
171 .SetGroupName (
"Network")
172 .HideFromDocumentation ()
181 return (uint32_t) m_size;
185 for (uint8_t i = 0; i < (m_size - 1); ++i)
192 for (uint8_t i = 0; i < (m_size - 1); ++i)
194 uint8_t v = buf.
ReadU8 ();
195 m_data.push_back (v);
198 virtual void Print (std::ostream &os)
const {
199 os <<
"(" << (uint16_t) m_size <<
")";
226 .SetGroupName (
"Network")
227 .HideFromDocumentation ()
252 std::ostringstream oss;
253 oss <<
"anon::ATestHeader<" << N <<
">";
255 .SetParent<ATestHeaderBase> ()
269 for (uint32_t i = 0; i < N; ++i)
275 for (uint32_t i = 0; i < N; ++i)
277 uint8_t v = iter.
ReadU8 ();
285 virtual void Print (std::ostream &os)
const {
312 .SetGroupName (
"Network")
313 .HideFromDocumentation ()
338 std::ostringstream oss;
339 oss <<
"anon::ATestTrailer<" << N <<
">";
341 .SetParent<ATestTrailerBase> ()
356 for (uint32_t i = 0; i < N; ++i)
363 for (uint32_t i = 0; i < N; ++i)
365 uint8_t v = iter.
ReadU8 ();
373 virtual void Print (std::ostream &os)
const {
396 Expected (uint32_t n_, uint32_t start_, uint32_t end_)
397 :
n (n_),
start (start_), end (end_) {}
407 #define E(a,b,c) a,b,c 409 #define CHECK(p, n, ...) \ 410 DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__) 422 virtual void DoRun (
void);
443 std::vector<struct Expected> expected;
446 for (uint32_t k = 0; k <
n; ++k)
448 uint32_t N = va_arg (ap, uint32_t);
449 uint32_t
start = va_arg (ap, uint32_t);
450 uint32_t end = va_arg (ap, uint32_t);
451 expected.push_back (Expected (N,
start, end));
457 while (i.
HasNext () && j < expected.size ())
460 struct Expected e = expected[j];
461 std::ostringstream oss;
462 oss <<
"anon::ATestTag<" << e.n <<
">";
480 Ptr<Packet> pkt1 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
"hello"), 5);
481 Ptr<Packet> pkt2 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
" world"), 6);
488 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
491 std::string msg = std::string (reinterpret_cast<const char *>(buf),
501 CHECK (p, 1,
E (1, 0, 1000));
503 CHECK (copy, 1,
E (1, 0, 1000));
506 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
507 CHECK (copy, 1,
E (1, 0, 1000));
513 CHECK (&c0, 1,
E (1, 0, 1000));
514 CHECK (&c1, 1,
E (1, 0, 1000));
515 CHECK (copy, 1,
E (1, 0, 1000));
517 CHECK (&c0, 2,
E (1, 0, 1000),
E (10, 0, 1000));
518 CHECK (&c1, 1,
E (1, 0, 1000));
519 CHECK (copy, 1,
E (1, 0, 1000));
526 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
528 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
530 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
533 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));
535 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
538 E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10),
539 E (1, 10, 100),
E (2, 10, 100),
E (4, 10, 100),
540 E (1, 100, 1000),
E (2, 100, 1000),
E (5, 100, 1000));
544 frag0 = Create<Packet> (1000);
548 p = Create<Packet> (1000);
550 CHECK (p, 1,
E (20, 0, 1000));
552 CHECK (p, 1,
E (20, 0, 1000));
553 CHECK (frag0, 1,
E (20, 0, 90));
556 CHECK (frag0, 1,
E (20, 10, 100));
561 CHECK (tmp, 1,
E (20, 0, 100));
563 CHECK (tmp, 1,
E (20, 10, 110));
566 CHECK (tmp, 1,
E (20, 0, 100));
568 CHECK (tmp, 1,
E (20, 10, 110));
570 tmp = Create<Packet> (100);
572 CHECK (tmp, 1,
E (20, 0, 100));
574 CHECK (tmp, 1,
E (20, 0, 100));
577 CHECK (tmp, 1,
E (20, 0, 100));
579 CHECK (tmp, 1,
E (20, 0, 100));
587 CHECK (tmp, 1,
E (20, 0, 156));
589 CHECK (tmp, 1,
E (20, 0, 36));
592 CHECK (a, 1,
E (20, 0, 36));
598 CHECK (tmp, 0,
E (20, 0, 0));
603 CHECK (tmp, 1,
E (20, 0, 1000));
605 CHECK (tmp, 0,
E (0,0,0));
608 CHECK (a, 1,
E (10, 0, 10));
610 CHECK (tmp, 1,
E (10, 0, 10));
627 copy.AddPacketTag (c);
632 copy.RemovePacketTag (b);
649 CHECK (tmp, 1,
E (20, 0, 1000));
651 CHECK (tmp, 1,
E (20, 2, 1002));
653 CHECK (tmp, 1,
E (20, 1, 1001));
656 CHECK (tmp, 1,
E (20, 1, 1001));
665 CHECK (tmp, 1,
E (25, 0, 100));
667 CHECK (tmp, 1,
E (25, 0, 50));
669 CHECK (tmp, 1,
E (25, 50, 100));
677 CHECK (tmp, 1,
E (25, 0, 100));
679 CHECK (tmp, 1,
E (25, 0, 50));
681 CHECK (tmp, 1,
E (25, 0, 50));
689 CHECK (tmp, 1,
E (25, 0, 100));
691 CHECK (tmp, 1,
E (25, 0, 50));
693 CHECK (tmp, 1,
E (25, 25, 75));
701 CHECK (tmp, 1,
E (25, 0, 100));
703 CHECK (tmp, 1,
E (25, 0, 50));
705 CHECK (tmp, 1,
E (25, 0, 50));
713 CHECK (tmp, 1,
E (25, 0, 100));
715 CHECK (tmp, 1,
E (25, 0, 50));
717 CHECK (tmp, 1,
E (25, 0, 50));
727 CHECK (tmp, 1,
E (25, 0, 100));
729 CHECK (tmp, 1,
E (25, 0, 50));
731 CHECK (tmp, 1,
E (25, 0, 50));
785 const char * msg = 0);
810 int expect = t.GetData ();
811 bool found = ref.
Peek (t);
813 msg <<
": ref contains " 814 << t.GetTypeId ().GetName ());
817 msg <<
": ref " << t.GetTypeId ().GetName ()
823 #define MAKE_TEST_TAGS \ 824 ATestTag<1> t1 (1); \ 825 ATestTag<2> t2 (1); \ 826 ATestTag<3> t3 (1); \ 827 ATestTag<4> t4 (1); \ 828 ATestTag<5> t5 (1); \ 829 ATestTag<6> t6 (1); \ 830 ATestTag<7> t7 (1); \ 831 const int tagLast = 7; \ 856 const int reps = 10000;
857 std::vector< PacketTagList > ptv(reps, ref);
858 int start = clock ();
859 for (
int i = 0; i < reps; ++i) {
863 int delta = stop -
start;
865 std::cout <<
GetName () <<
"remove time: " << msg <<
": " << std::setw (8)
866 << delta <<
" ticks to remove " 876 const int reps = 100000;
879 int start = clock ();
880 for (
int i = 0; i < reps; ++i) {
885 int delta = stop -
start;
887 std::cout <<
GetName () <<
"add/remove time: " << std::setw (8)
888 << delta <<
" ticks to add+remove " 898 std::cout <<
GetName () <<
"begin" << std::endl;
912 std::cout <<
GetName () <<
"check Peek (missing tag) returns false" 919 std::cout <<
GetName () <<
"check copy and assignment" << std::endl;
931 # define RemoveCheck(n) \ 932 { PacketTagList p ## n = ref; \ 933 p ## n .Remove ( t ## n ); \ 934 CheckRefList (ref, "remove " #n " orig"); \ 935 CheckRefList (p ## n, "remove " #n " copy", n); \ 939 std::cout <<
GetName () <<
"check removal of each tag" << std::endl;
950 std::cout <<
GetName () <<
"check removal doesn't disturb merge " 966 const char * msg =
"post merge, short chain";
978 std::cout <<
GetName () <<
"check replacing each tag" << std::endl;
980 # define ReplaceCheck(n) \ 981 t ## n .m_data = 2; \ 982 { PacketTagList p ## n = ref; \ 983 p ## n .Replace ( t ## n ); \ 984 CheckRefList (ref, "replace " #n " orig"); \ 985 CheckRef (p ## n, t ## n, "replace " #n " copy"); \ 998 std::cout <<
GetName () <<
"add+remove timing" << std::endl;
1000 const int nIterations = 100;
1001 for (
int i = 0; i < nIterations; ++i) {
1003 if (now < flm) flm = now;
1005 std::cout <<
GetName () <<
"min add+remove time: " 1006 << std::setw (8) << flm <<
" ticks" 1009 std::cout <<
GetName () <<
"remove timing" << std::endl;
1012 for (
int i = 0; i < nIterations; ++i) {
1013 for (
int j = 1; j <= tagLast; ++j) {
1025 if (now < rmn[j]) rmn[j] = now;
1028 for (
int j = tagLast; j > 0; --j) {
1029 std::cout <<
GetName () <<
"min remove time: t" 1031 << std::setw (8) << rmn[j] <<
" ticks"
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual void Serialize(TagBuffer buf) const
std::vector< uint8_t > m_data
Tag data.
virtual void DoRun(void)
Implementation to actually run this TestCase.
ATestTagBase(uint8_t data)
Constructor.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
TypeId GetTypeId(void) const
std::string GetName(void) const
Get the name.
Packet Tag list unit tests.
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)
Checks against a reference PacketTagList.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
bool m_error
Error in the Tag.
virtual uint32_t GetSerializedSize(void) const
static TypeId GetTypeId(void)
Register this type.
virtual void Serialize(TagBuffer buf) const
virtual uint32_t GetSerializedSize(void) const
virtual uint32_t GetSerializedSize(void) const
uint8_t m_size
Packet size.
Template class for Test trailers.
List of the packet tags stored in a packet.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
virtual void Print(std::ostream &os) const
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
virtual void Print(std::ostream &os) const
static TypeId GetTypeId(void)
Register this type.
static TypeId GetTypeId(void)
Register this type.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.
TAG_BUFFER_INLINE uint8_t ReadU8(void)
iterator in a Buffer instance
static TypeId GetTypeId(void)
Register this type.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
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.
Base class for Test trailers.
void Prev(void)
go backward by one byte
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
void DoRun(void)
Implementation to actually run this TestCase.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
static TypeId GetTypeId(void)
Register this type.
TypeId SetGroupName(std::string groupName)
Set the group name.
virtual void Deserialize(TagBuffer buf)
Protocol trailer serialization and deserialization.
tag a set of bytes in a packet
int GetData() const
Get the tag data.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
Prints the remove time.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Ptr< Packet > Copy(void) const
performs a COW copy of the 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.
bool Peek(Tag &tag) const
Find a tag and return its value.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Base class for Test tags.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
#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 Add(Tag const &tag) const
Add a tag to the head of this branch.
#define LARGE_TAG_BUFFER_SIZE
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
virtual void Deserialize(TagBuffer buf)
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
ATestTag(uint8_t data)
Constructor.
std::string GetName(void) const
void WriteU8(uint8_t data)
void AddPacketTag(const Tag &tag) const
Add a packet tag.
virtual ~PacketTagListTest()
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
virtual void Serialize(Buffer::Iterator iter) const
virtual uint32_t Deserialize(Buffer::Iterator iter)
virtual void Print(std::ostream &os) const
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Template class for Test tags.
Template class for Large Test tags.
void DoCheck(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
Checks the packet.
Struct to hold the expected data in the packet.
a unique identifier for an interface.
bool m_error
Error in the Trailer.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
uint32_t n
Number of elements.
void AddHeader(const Header &header)
Add header to this packet.
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
int AddRemoveTime(const bool verbose=false)
Prints the remove time.