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);
129 for (uint32_t i = 0; i < N; ++i)
138 for (uint32_t i = 0; i < N; ++i)
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()
213 return (uint32_t)m_size;
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()
320 for (uint32_t i = 0; i < N; ++i)
328 for (uint32_t i = 0; i < N; ++i)
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()
425 for (uint32_t i = 0; i < N; ++i)
434 for (uint32_t i = 0; i < N; ++i)
436 uint8_t v = iter.
ReadU8();
445 void Print(std::ostream& os)
const override
471 Expected(uint32_t n_, uint32_t start_, uint32_t end_)
486 Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
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;
554 for (uint32_t
k = 0;
k < n; ++
k)
556 uint32_t N = va_arg(ap, uint32_t);
557 uint32_t
start = va_arg(ap, uint32_t);
558 uint32_t end = va_arg(ap, uint32_t);
559 expected.emplace_back(N,
start, end);
565 while (i.
HasNext() && j < expected.size())
568 Expected e = expected[j];
569 std::ostringstream oss;
570 oss <<
"anon::ATestTag<" << e.n <<
">";
588 std::vector<Expected> expected;
591 for (uint32_t
k = 0;
k < n; ++
k)
593 uint32_t N = va_arg(ap, uint32_t);
594 uint32_t
start = va_arg(ap, uint32_t);
595 uint32_t end = va_arg(ap, uint32_t);
596 int data = va_arg(ap,
int);
597 expected.emplace_back(N,
start, end,
data);
603 while (i.
HasNext() && j < expected.size())
606 Expected e = expected[j];
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);
635 auto buf =
new uint8_t[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));
687 CHECK(frag0, 3,
E(1, 0, 10),
E(2, 0, 10),
E(3, 0, 10));
702 frag0 = Create<Packet>(1000);
706 p = Create<Packet>(1000);
708 CHECK(p, 1,
E(20, 0, 1000));
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));
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));
735 CHECK(tmp, 1,
E(20, 0, 100));
737 CHECK(tmp, 1,
E(20, 0, 100));
744 CHECK(tmp, 1,
E(20, 0, 156));
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));
762 CHECK(tmp, 0,
E(0, 0, 0));
765 CHECK(a, 1,
E(10, 0, 10));
767 CHECK(tmp, 1,
E(10, 0, 10));
814 auto buffer =
new uint8_t[serializedSize + 16];
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));
849 auto buffer =
new uint8_t[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));
872 CHECK(tmp, 1,
E(20, 1, 1001));
875 CHECK (tmp, 1,
E (20, 1, 1001));
884 CHECK(tmp, 1,
E(25, 0, 100));
886 CHECK(tmp, 1,
E(25, 0, 50));
888 CHECK(tmp, 1,
E(25, 50, 100));
896 CHECK(tmp, 1,
E(25, 0, 100));
898 CHECK(tmp, 1,
E(25, 0, 50));
900 CHECK(tmp, 1,
E(25, 0, 50));
908 CHECK(tmp, 1,
E(25, 0, 100));
910 CHECK(tmp, 1,
E(25, 0, 50));
912 CHECK(tmp, 1,
E(25, 25, 75));
920 CHECK(tmp, 1,
E(25, 0, 100));
922 CHECK(tmp, 1,
E(25, 0, 50));
924 CHECK(tmp, 1,
E(25, 0, 50));
932 CHECK(tmp, 1,
E(25, 0, 100));
934 CHECK(tmp, 1,
E(25, 0, 50));
936 CHECK(tmp, 1,
E(25, 0, 50));
946 CHECK(tmp, 1,
E(25, 0, 100));
948 CHECK(tmp, 1,
E(25, 0, 50));
950 CHECK(tmp, 1,
E(25, 0, 50));
974 void DoRun()
override;
1020 int expect = t.GetData();
1021 bool found = ref.
Peek(t);
1027 msg <<
": ref " << t.GetTypeId().GetName() <<
" = " << expect);
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)
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)
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;
1115 std::cout <<
GetName() <<
"check Peek (missing tag) returns false" << std::endl;
1122 std::cout <<
GetName() <<
"check copy and assignment" << std::endl;
1141 #define RemoveCheck(n) \
1142 PacketTagList p##n = ref; \
1143 p##n.Remove(t##n); \
1144 CheckRefList(ref, "remove " #n " orig"); \
1145 CheckRefList(p##n, "remove " #n " copy", n);
1149 std::cout <<
GetName() <<
"check removal of each tag" << std::endl;
1161 std::cout <<
GetName() <<
"check removal doesn't disturb merge " << std::endl;
1176 const char* msg =
"post merge, short chain";
1188 std::cout <<
GetName() <<
"check replacing each tag" << std::endl;
1190 #define ReplaceCheck(n) \
1193 PacketTagList p##n = ref; \
1194 p##n.Replace(t##n); \
1195 CheckRefList(ref, "replace " #n " orig"); \
1196 CheckRef(p##n, t##n, "replace " #n " copy"); \
1210 std::cout <<
GetName() <<
"add+remove timing" << std::endl;
1212 const int nIterations = 100;
1213 for (
int i = 0; i < nIterations; ++i)
1221 std::cout <<
GetName() <<
"min add+remove time: " << std::setw(8) << flm <<
" ticks"
1224 std::cout <<
GetName() <<
"remove timing" << std::endl;
1227 for (
int i = 0; i < nIterations; ++i)
1229 for (
int j = 1; j <= TAG_LAST; ++j)
1263 for (
int j = TAG_LAST; j > 0; --j)
1265 std::cout <<
GetName() <<
"min remove time: t" << j <<
": " << std::setw(8) << rmn[j]
1266 <<
" 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.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
void AddHeader(const Header &header)
Add header to this packet.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Ptr< Packet > Copy() const
performs a COW copy of the packet.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
void RemoveAllPacketTags()
Remove all packet tags.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
ByteTagIterator GetByteTagIterator() const
Returns an iterator over the set of byte tags included in this packet.
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.
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.