8#include "ns3/packet-tag-list.h"
60 TypeId(
"ATestTagBase").
SetParent<
Tag>().SetGroupName(
"Network").HideFromDocumentation()
96 std::ostringstream oss;
97 oss <<
"anon::ATestTag<" << N <<
">";
100 .SetGroupName(
"Network")
101 .HideFromDocumentation()
113 return N +
sizeof(
m_data);
138 void Print(std::ostream& os)
const override
140 os << N <<
"(" <<
m_data <<
")";
158#define LARGE_TAG_BUFFER_SIZE 64
190 .SetGroupName(
"Network")
191 .HideFromDocumentation()
209 for (uint8_t i = 0; i < (
m_size - 1); ++i)
218 for (uint8_t i = 0; i < (
m_size - 1); ++i)
225 void Print(std::ostream& os)
const override
227 os <<
"(" << (uint16_t)
m_size <<
")";
260 .SetGroupName(
"Network")
261 .HideFromDocumentation()
288 std::ostringstream oss;
289 oss <<
"anon::ATestHeader<" << N <<
">";
292 .SetGroupName(
"Network")
293 .HideFromDocumentation()
329 void Print(std::ostream& os)
const override
364 .SetGroupName(
"Network")
365 .HideFromDocumentation()
392 std::ostringstream oss;
393 oss <<
"anon::ATestTrailer<" << N <<
">";
396 .SetGroupName(
"Network")
397 .HideFromDocumentation()
435 void Print(std::ostream& os)
const override
493#define E(name, start, end) name, start, end
496#define E_DATA(name, start, end, data) name, start, end, data
499#define CHECK(p, n, ...) DoCheck(p, n, __VA_ARGS__)
502#define CHECK_DATA(p, n, ...) DoCheckData(p, n, __VA_ARGS__)
514 void DoRun()
override;
541 std::vector<Expected> expected;
549 expected.emplace_back(N, start, end);
555 while (i.
HasNext() && j < expected.size())
559 std::ostringstream oss;
560 oss <<
"anon::ATestTag<" << e.
n <<
">";
578 std::vector<Expected> expected;
586 int data = va_arg(ap,
int);
587 expected.emplace_back(N, start, end,
data);
593 while (i.
HasNext() && j < expected.size())
597 std::ostringstream oss;
598 oss <<
"anon::ATestTag<" << e.
n <<
">";
620 packet->AddAtEnd(pkt1);
621 packet->AddAtEnd(pkt2);
625 auto buf =
new uint8_t[packet->GetSize()];
626 packet->CopyData(buf, packet->GetSize());
628 std::string msg = std::string(
reinterpret_cast<const char*
>(buf), packet->GetSize());
636 CHECK(p, 1,
E(1, 0, 1000));
638 CHECK(copy, 1,
E(1, 0, 1000));
641 CHECK(p, 2,
E(1, 0, 1000),
E(2, 0, 1000));
642 CHECK(copy, 1,
E(1, 0, 1000));
648 CHECK(&c0, 1,
E(1, 0, 1000));
649 CHECK(&c1, 1,
E(1, 0, 1000));
650 CHECK(copy, 1,
E(1, 0, 1000));
652 CHECK(&c0, 2,
E(1, 0, 1000),
E(10, 0, 1000));
653 CHECK(&c1, 1,
E(1, 0, 1000));
654 CHECK(copy, 1,
E(1, 0, 1000));
661 CHECK(frag0, 3,
E(1, 0, 10),
E(2, 0, 10),
E(3, 0, 10));
663 CHECK(frag1, 3,
E(1, 0, 90),
E(2, 0, 90),
E(4, 0, 90));
665 CHECK(frag2, 3,
E(1, 0, 900),
E(2, 0, 900),
E(5, 0, 900));
667 frag1->AddAtEnd(frag2);
677 CHECK(frag0, 3,
E(1, 0, 10),
E(2, 0, 10),
E(3, 0, 10));
678 frag0->AddAtEnd(frag1);
698 CHECK(p, 1,
E(20, 0, 1000));
699 frag0 = p->CreateFragment(10, 90);
700 CHECK(p, 1,
E(20, 0, 1000));
701 CHECK(frag0, 1,
E(20, 0, 90));
704 CHECK(frag0, 1,
E(20, 10, 100));
709 CHECK(tmp, 1,
E(20, 0, 100));
711 CHECK(tmp, 1,
E(20, 10, 110));
713 tmp->RemoveHeader(h);
714 CHECK(tmp, 1,
E(20, 0, 100));
716 CHECK(tmp, 1,
E(20, 10, 110));
720 CHECK(tmp, 1,
E(20, 0, 100));
722 CHECK(tmp, 1,
E(20, 0, 100));
724 tmp->RemoveTrailer(t);
725 CHECK(tmp, 1,
E(20, 0, 100));
727 CHECK(tmp, 1,
E(20, 0, 100));
734 CHECK(tmp, 1,
E(20, 0, 156));
735 tmp->RemoveAtStart(120);
736 CHECK(tmp, 1,
E(20, 0, 36));
739 CHECK(a, 1,
E(20, 0, 36));
745 CHECK(tmp, 0,
E(20, 0, 0));
750 CHECK(tmp, 1,
E(20, 0, 1000));
751 tmp->RemoveAtStart(1000);
752 CHECK(tmp, 0,
E(0, 0, 0));
755 CHECK(a, 1,
E(10, 0, 10));
757 CHECK(tmp, 1,
E(10, 0, 10));
799 p1->AddPacketTag(a1);
800 p1->AddPacketTag(b1);
801 p1->AddPacketTag(c1);
803 uint32_t serializedSize = p1->GetSerializedSize();
804 auto buffer =
new uint8_t[serializedSize + 16];
805 p1->Serialize(buffer, serializedSize);
827 nix1->AddNeighborIndex(3, 2);
828 nix1->AddNeighborIndex(5, 3);
829 p1->SetNixVector(nix1);
831 uint32_t serializedSize = p1->GetSerializedSize();
832 std::vector<uint8_t> buffer(serializedSize);
835 "serialized size mismatch");
859 CHECK(p1, 3,
E(10, 0, 1000),
E(11, 0, 1000),
E(12, 0, 1000));
861 uint32_t serializedSize = p1->GetSerializedSize();
862 auto buffer =
new uint8_t[serializedSize];
863 p1->Serialize(buffer, serializedSize);
881 CHECK(tmp, 1,
E(20, 0, 1000));
883 CHECK(tmp, 1,
E(20, 2, 1002));
884 tmp->RemoveAtStart(1);
885 CHECK(tmp, 1,
E(20, 1, 1001));
888 CHECK (tmp, 1,
E (20, 1, 1001));
897 CHECK(tmp, 1,
E(25, 0, 100));
898 tmp->RemoveAtStart(50);
899 CHECK(tmp, 1,
E(25, 0, 50));
901 CHECK(tmp, 1,
E(25, 50, 100));
909 CHECK(tmp, 1,
E(25, 0, 100));
910 tmp->RemoveAtEnd(50);
911 CHECK(tmp, 1,
E(25, 0, 50));
913 CHECK(tmp, 1,
E(25, 0, 50));
921 CHECK(tmp, 1,
E(25, 0, 100));
922 tmp->RemoveAtStart(50);
923 CHECK(tmp, 1,
E(25, 0, 50));
925 CHECK(tmp, 1,
E(25, 25, 75));
933 CHECK(tmp, 1,
E(25, 0, 100));
934 tmp->RemoveAtEnd(50);
935 CHECK(tmp, 1,
E(25, 0, 50));
937 CHECK(tmp, 1,
E(25, 0, 50));
945 CHECK(tmp, 1,
E(25, 0, 100));
946 tmp->RemoveAtEnd(50);
947 CHECK(tmp, 1,
E(25, 0, 50));
948 tmp->AddPaddingAtEnd(50);
949 CHECK(tmp, 1,
E(25, 0, 50));
959 CHECK(tmp, 1,
E(25, 0, 100));
960 tmp->RemoveAtEnd(50);
961 CHECK(tmp, 1,
E(25, 0, 50));
962 tmp->AddPaddingAtEnd(50);
963 CHECK(tmp, 1,
E(25, 0, 50));
970 tmp->AddPacketTag(a);
987 void DoRun()
override;
1034 bool found = ref.
Peek(t);
1045#define MAKE_TEST_TAGS \
1046 ATestTag<1> t1(1); \
1047 ATestTag<2> t2(1); \
1048 ATestTag<3> t3(1); \
1049 ATestTag<4> t4(1); \
1050 ATestTag<5> t5(1); \
1051 ATestTag<6> t6(1); \
1052 ATestTag<7> t7(1); \
1053 constexpr int TAG_LAST [[maybe_unused]] = 7;
1071 const int reps = 10000;
1072 std::vector<PacketTagList> ptv(reps, ref);
1073 int start = clock();
1074 for (
int i = 0; i < reps; ++i)
1079 int delta = stop - start;
1082 std::cout <<
GetName() <<
"remove time: " << msg <<
": " << std::setw(8) << delta
1083 <<
" ticks to remove " << reps <<
" times" << std::endl;
1091 const int reps = 100000;
1094 int start = clock();
1095 for (
int i = 0; i < reps; ++i)
1101 int delta = stop - start;
1104 std::cout <<
GetName() <<
"add/remove time: " << std::setw(8) << delta
1105 <<
" ticks to add+remove " << reps <<
" times" << std::endl;
1113 std::cout <<
GetName() <<
"begin" << std::endl;
1128 std::cout <<
GetName() <<
"check Peek (missing tag) returns false" << std::endl;
1135 std::cout <<
GetName() <<
"check copy and assignment" << std::endl;
1154#define RemoveCheck(n) \
1155 PacketTagList p##n = ref; \
1156 p##n.Remove(t##n); \
1157 CheckRefList(ref, "remove " #n " orig"); \
1158 CheckRefList(p##n, "remove " #n " copy", n);
1162 std::cout <<
GetName() <<
"check removal of each tag" << std::endl;
1174 std::cout <<
GetName() <<
"check removal doesn't disturb merge " << std::endl;
1189 const char* msg =
"post merge, short chain";
1201 std::cout <<
GetName() <<
"check replacing each tag" << std::endl;
1203#define ReplaceCheck(n) \
1206 PacketTagList p##n = ref; \
1207 p##n.Replace(t##n); \
1208 CheckRefList(ref, "replace " #n " orig"); \
1209 CheckRef(p##n, t##n, "replace " #n " copy"); \
1223 std::cout <<
GetName() <<
"add+remove timing" << std::endl;
1224 int flm = std::numeric_limits<int>::max();
1225 const int nIterations = 100;
1226 for (
int i = 0; i < nIterations; ++i)
1234 std::cout <<
GetName() <<
"min add+remove time: " << std::setw(8) << flm <<
" ticks"
1237 std::cout <<
GetName() <<
"remove timing" << std::endl;
1239 std::vector<int> rmn(TAG_LAST + 1, std::numeric_limits<int>::max());
1240 for (
int i = 0; i < nIterations; ++i)
1242 for (
int j = 1; j <= TAG_LAST; ++j)
1276 for (
int j = TAG_LAST; j > 0; --j)
1278 std::cout <<
GetName() <<
"min remove time: t" << j <<
": " << std::setw(8) << rmn[j]
1279 <<
" 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
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, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
std::string GetName() const
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Protocol trailer serialization and deserialization.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
std::string GetName() const
Get the name.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
#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.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort 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.