19#include "ns3/packet-tag-list.h"
20#include "ns3/packet.h"
70 TypeId(
"ATestTagBase").
SetParent<
Tag>().SetGroupName(
"Network").HideFromDocumentation()
80 int result = (int)m_data;
106 std::ostringstream oss;
107 oss <<
"anon::ATestTag<" << N <<
">";
110 .SetGroupName(
"Network")
111 .HideFromDocumentation()
123 return N +
sizeof(m_data);
148 void Print(std::ostream& os)
const override
150 os << N <<
"(" << m_data <<
")";
168#define LARGE_TAG_BUFFER_SIZE 64
200 .SetGroupName(
"Network")
201 .HideFromDocumentation()
219 for (uint8_t i = 0; i < (m_size - 1); ++i)
228 for (uint8_t i = 0; i < (m_size - 1); ++i)
235 void Print(std::ostream& os)
const override
237 os <<
"(" << (uint16_t)m_size <<
")";
270 .SetGroupName(
"Network")
271 .HideFromDocumentation()
298 std::ostringstream oss;
299 oss <<
"anon::ATestHeader<" << N <<
">";
302 .SetGroupName(
"Network")
303 .HideFromDocumentation()
330 uint8_t v = iter.
ReadU8();
339 void Print(std::ostream& os)
const override
374 .SetGroupName(
"Network")
375 .HideFromDocumentation()
402 std::ostringstream oss;
403 oss <<
"anon::ATestTrailer<" << N <<
">";
406 .SetGroupName(
"Network")
407 .HideFromDocumentation()
436 uint8_t v = iter.
ReadU8();
445 void Print(std::ostream& os)
const override
503#define E(name, start, end) name, start, end
506#define E_DATA(name, start, end, data) name, start, end, data
509#define CHECK(p, n, ...) DoCheck(p, n, __VA_ARGS__)
512#define CHECK_DATA(p, n, ...) DoCheckData(p, n, __VA_ARGS__)
524 void DoRun()
override;
551 std::vector<Expected> expected;
559 expected.emplace_back(N, start, end);
565 while (i.
HasNext() && j < expected.size())
569 std::ostringstream oss;
570 oss <<
"anon::ATestTag<" << e.
n <<
">";
588 std::vector<Expected> expected;
596 int data = va_arg(ap,
int);
597 expected.emplace_back(N, start, end,
data);
603 while (i.
HasNext() && j < expected.size())
607 std::ostringstream oss;
608 oss <<
"anon::ATestTag<" << e.
n <<
">";
627 Ptr<Packet> pkt1 = Create<Packet>(
reinterpret_cast<const uint8_t*
>(
"hello"), 5);
628 Ptr<Packet> pkt2 = Create<Packet>(
reinterpret_cast<const uint8_t*
>(
" world"), 6);
630 packet->AddAtEnd(pkt1);
631 packet->AddAtEnd(pkt2);
635 uint8_t* buf =
new uint8_t[packet->GetSize()];
636 packet->CopyData(buf, packet->GetSize());
638 std::string msg = std::string(
reinterpret_cast<const char*
>(buf), packet->GetSize());
646 CHECK(p, 1,
E(1, 0, 1000));
648 CHECK(copy, 1,
E(1, 0, 1000));
651 CHECK(p, 2,
E(1, 0, 1000),
E(2, 0, 1000));
652 CHECK(copy, 1,
E(1, 0, 1000));
658 CHECK(&c0, 1,
E(1, 0, 1000));
659 CHECK(&c1, 1,
E(1, 0, 1000));
660 CHECK(copy, 1,
E(1, 0, 1000));
662 CHECK(&c0, 2,
E(1, 0, 1000),
E(10, 0, 1000));
663 CHECK(&c1, 1,
E(1, 0, 1000));
664 CHECK(copy, 1,
E(1, 0, 1000));
671 CHECK(frag0, 3,
E(1, 0, 10),
E(2, 0, 10),
E(3, 0, 10));
673 CHECK(frag1, 3,
E(1, 0, 90),
E(2, 0, 90),
E(4, 0, 90));
675 CHECK(frag2, 3,
E(1, 0, 900),
E(2, 0, 900),
E(5, 0, 900));
677 frag1->AddAtEnd(frag2);
687 CHECK(frag0, 3,
E(1, 0, 10),
E(2, 0, 10),
E(3, 0, 10));
688 frag0->AddAtEnd(frag1);
702 frag0 = Create<Packet>(1000);
706 p = Create<Packet>(1000);
708 CHECK(p, 1,
E(20, 0, 1000));
709 frag0 = p->CreateFragment(10, 90);
710 CHECK(p, 1,
E(20, 0, 1000));
711 CHECK(frag0, 1,
E(20, 0, 90));
714 CHECK(frag0, 1,
E(20, 10, 100));
719 CHECK(tmp, 1,
E(20, 0, 100));
721 CHECK(tmp, 1,
E(20, 10, 110));
723 tmp->RemoveHeader(h);
724 CHECK(tmp, 1,
E(20, 0, 100));
726 CHECK(tmp, 1,
E(20, 10, 110));
728 tmp = Create<Packet>(100);
730 CHECK(tmp, 1,
E(20, 0, 100));
732 CHECK(tmp, 1,
E(20, 0, 100));
734 tmp->RemoveTrailer(t);
735 CHECK(tmp, 1,
E(20, 0, 100));
737 CHECK(tmp, 1,
E(20, 0, 100));
744 CHECK(tmp, 1,
E(20, 0, 156));
745 tmp->RemoveAtStart(120);
746 CHECK(tmp, 1,
E(20, 0, 36));
749 CHECK(a, 1,
E(20, 0, 36));
755 CHECK(tmp, 0,
E(20, 0, 0));
760 CHECK(tmp, 1,
E(20, 0, 1000));
761 tmp->RemoveAtStart(1000);
762 CHECK(tmp, 0,
E(0, 0, 0));
765 CHECK(a, 1,
E(10, 0, 10));
767 CHECK(tmp, 1,
E(10, 0, 10));
809 p1->AddPacketTag(a1);
810 p1->AddPacketTag(b1);
811 p1->AddPacketTag(c1);
813 uint32_t serializedSize = p1->GetSerializedSize();
814 uint8_t* buffer =
new uint8_t[serializedSize + 16];
815 p1->Serialize(buffer, serializedSize);
817 Ptr<Packet> p2 = Create<Packet>(buffer, serializedSize,
true);
846 CHECK(p1, 3,
E(10, 0, 1000),
E(11, 0, 1000),
E(12, 0, 1000));
848 uint32_t serializedSize = p1->GetSerializedSize();
849 uint8_t* buffer =
new uint8_t[serializedSize];
850 p1->Serialize(buffer, serializedSize);
852 Ptr<Packet> p2 = Create<Packet>(buffer, serializedSize,
true);
868 CHECK(tmp, 1,
E(20, 0, 1000));
870 CHECK(tmp, 1,
E(20, 2, 1002));
871 tmp->RemoveAtStart(1);
872 CHECK(tmp, 1,
E(20, 1, 1001));
875 CHECK (tmp, 1,
E (20, 1, 1001));
884 CHECK(tmp, 1,
E(25, 0, 100));
885 tmp->RemoveAtStart(50);
886 CHECK(tmp, 1,
E(25, 0, 50));
888 CHECK(tmp, 1,
E(25, 50, 100));
896 CHECK(tmp, 1,
E(25, 0, 100));
897 tmp->RemoveAtEnd(50);
898 CHECK(tmp, 1,
E(25, 0, 50));
900 CHECK(tmp, 1,
E(25, 0, 50));
908 CHECK(tmp, 1,
E(25, 0, 100));
909 tmp->RemoveAtStart(50);
910 CHECK(tmp, 1,
E(25, 0, 50));
912 CHECK(tmp, 1,
E(25, 25, 75));
920 CHECK(tmp, 1,
E(25, 0, 100));
921 tmp->RemoveAtEnd(50);
922 CHECK(tmp, 1,
E(25, 0, 50));
924 CHECK(tmp, 1,
E(25, 0, 50));
932 CHECK(tmp, 1,
E(25, 0, 100));
933 tmp->RemoveAtEnd(50);
934 CHECK(tmp, 1,
E(25, 0, 50));
935 tmp->AddPaddingAtEnd(50);
936 CHECK(tmp, 1,
E(25, 0, 50));
946 CHECK(tmp, 1,
E(25, 0, 100));
947 tmp->RemoveAtEnd(50);
948 CHECK(tmp, 1,
E(25, 0, 50));
949 tmp->AddPaddingAtEnd(50);
950 CHECK(tmp, 1,
E(25, 0, 50));
957 tmp->AddPacketTag(a);
974 void DoRun()
override;
1021 bool found = ref.
Peek(t);
1032#define MAKE_TEST_TAGS \
1033 ATestTag<1> t1(1); \
1034 ATestTag<2> t2(1); \
1035 ATestTag<3> t3(1); \
1036 ATestTag<4> t4(1); \
1037 ATestTag<5> t5(1); \
1038 ATestTag<6> t6(1); \
1039 ATestTag<7> t7(1); \
1040 constexpr int TAG_LAST [[maybe_unused]] = 7;
1058 const int reps = 10000;
1059 std::vector<PacketTagList> ptv(reps, ref);
1060 int start = clock();
1061 for (
int i = 0; i < reps; ++i)
1066 int delta = stop - start;
1069 std::cout <<
GetName() <<
"remove time: " << msg <<
": " << std::setw(8) << delta
1070 <<
" ticks to remove " << reps <<
" times" << std::endl;
1078 const int reps = 100000;
1081 int start = clock();
1082 for (
int i = 0; i < reps; ++i)
1088 int delta = stop - start;
1091 std::cout <<
GetName() <<
"add/remove time: " << std::setw(8) << delta
1092 <<
" ticks to add+remove " << reps <<
" times" << std::endl;
1100 std::cout <<
GetName() <<
"begin" << std::endl;
1114 std::cout <<
GetName() <<
"check Peek (missing tag) returns false" << std::endl;
1120 std::cout <<
GetName() <<
"check copy and assignment" << std::endl;
1138#define RemoveCheck(n) \
1140 PacketTagList p##n = ref; \
1141 p##n.Remove(t##n); \
1142 CheckRefList(ref, "remove " #n " orig"); \
1143 CheckRefList(p##n, "remove " #n " copy", n); \
1147 std::cout <<
GetName() <<
"check removal of each tag" << std::endl;
1158 std::cout <<
GetName() <<
"check removal doesn't disturb merge " << std::endl;
1173 const char* msg =
"post merge, short chain";
1185 std::cout <<
GetName() <<
"check replacing each tag" << std::endl;
1187#define ReplaceCheck(n) \
1190 PacketTagList p##n = ref; \
1191 p##n.Replace(t##n); \
1192 CheckRefList(ref, "replace " #n " orig"); \
1193 CheckRef(p##n, t##n, "replace " #n " copy"); \
1206 std::cout <<
GetName() <<
"add+remove timing" << std::endl;
1207 int flm = std::numeric_limits<int>::max();
1208 const int nIterations = 100;
1209 for (
int i = 0; i < nIterations; ++i)
1217 std::cout <<
GetName() <<
"min add+remove time: " << std::setw(8) << flm <<
" ticks"
1220 std::cout <<
GetName() <<
"remove timing" << std::endl;
1222 std::vector<int> rmn(TAG_LAST + 1, std::numeric_limits<int>::max());
1223 for (
int i = 0; i < nIterations; ++i)
1225 for (
int j = 1; j <= TAG_LAST; ++j)
1259 for (
int j = TAG_LAST; j > 0; --j)
1261 std::cout <<
GetName() <<
"min remove time: t" << j <<
": " << std::setw(8) << rmn[j]
1262 <<
" ticks" << std::endl;
Packet Tag list unit tests.
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
Checks against a reference PacketTagList.
~PacketTagListTest() override
int AddRemoveTime(const bool verbose=false)
Prints the remove time.
void DoRun() override
Implementation to actually run this TestCase.
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=nullptr)
Prints the remove time.
void DoCheckData(Ptr< const Packet > p, uint32_t n,...)
Checks the packet and its data.
void DoRun() override
Implementation to actually run this TestCase.
void DoCheck(Ptr< const Packet > p, uint32_t n,...)
Checks the packet.
Template class for Large Test tags.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Register this type.
uint8_t m_size
Packet size.
void Serialize(TagBuffer buf) const override
void Print(std::ostream &os) const override
uint32_t GetSerializedSize() const override
std::vector< uint8_t > m_data
Tag data.
void Deserialize(TagBuffer buf) override
Base class for Test tags.
bool m_error
Error in the Tag.
static TypeId GetTypeId()
Register this type.
int GetData() const
Get the tag data.
ATestTagBase(uint8_t data)
Constructor.
Template class for Test tags.
ATestTag(uint8_t data)
Constructor.
void Serialize(TagBuffer buf) const override
static TypeId GetTypeId()
Register this type.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Deserialize(TagBuffer buf) override
uint32_t GetSerializedSize() const override
Base class for Test trailers.
bool m_error
Error in the Trailer.
static TypeId GetTypeId()
Register this type.
Template class for Test trailers.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Print(std::ostream &os) const override
uint32_t GetSerializedSize() const override
static TypeId GetTypeId()
Register this type.
uint32_t Deserialize(Buffer::Iterator iter) override
void Serialize(Buffer::Iterator iter) const override
iterator in a Buffer instance
void WriteU8(uint8_t data)
void Prev()
go backward by one byte
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
uint32_t GetEnd() const
The index is an offset from the start of the packet.
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
uint32_t GetStart() const
The index is an offset from the start of the packet.
Iterator over the set of byte tags in a packet.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
void RemoveAllPacketTags()
Remove all packet tags.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
void Add(const Tag &tag) const
Add a tag to the head of this branch.
bool Peek(Tag &tag) const
Find a tag and return its value.
Smart pointer class similar to boost::intrusive_ptr.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
TAG_BUFFER_INLINE uint8_t ReadU8()
tag a set of bytes in a packet
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
std::string GetName() const
Protocol trailer serialization and deserialization.
a unique identifier for an interface.
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
std::string GetName() const
Get the name.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define LARGE_TAG_BUFFER_SIZE
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
#define E_DATA(name, start, end, data)
#define E(name, start, end)
#define CHECK_DATA(p, n,...)
Struct to hold the expected data in the packet.
uint8_t data
Optional data.
Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
Constructor.
uint32_t n
Number of elements.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.