|
A Discrete-Event Network Simulator
|
API
|
Go to the documentation of this file.
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 <<
">";
98 .SetGroupName (
"Network")
99 .HideFromDocumentation ()
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 <<
">";
256 .SetGroupName (
"Network")
257 .HideFromDocumentation ()
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 <<
">";
342 .SetGroupName (
"Network")
343 .HideFromDocumentation ()
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_),
data(0) {}
406 Expected (uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
407 :
n (n_),
start (start_), end (end_),
data(data_) {}
418 #define E(name,start,end) name,start,end
421 #define E_DATA(name,start,end,data) name,start,end,data
424 #define CHECK(p, n, ...) \
425 DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__)
428 #define CHECK_DATA(p, n, ...) \
429 DoCheckData (p, __FILE__, __LINE__, n, __VA_ARGS__)
441 virtual void DoRun (
void);
471 std::vector<struct Expected> expected;
474 for (uint32_t
k = 0;
k <
n; ++
k)
476 uint32_t N = va_arg (ap, uint32_t);
477 uint32_t
start = va_arg (ap, uint32_t);
478 uint32_t end = va_arg (ap, uint32_t);
479 expected.push_back (Expected (N,
start, end));
485 while (i.
HasNext () && j < expected.size ())
488 struct Expected e = expected[j];
489 std::ostringstream oss;
490 oss <<
"anon::ATestTag<" << e.n <<
">";
508 std::vector<struct Expected> expected;
511 for (uint32_t
k = 0;
k <
n; ++
k)
513 uint32_t N = va_arg (ap, uint32_t);
514 uint32_t
start = va_arg (ap, uint32_t);
515 uint32_t end = va_arg (ap, uint32_t);
516 int data = va_arg (ap,
int);
517 expected.push_back (Expected (N,
start, end,
data));
523 while (i.
HasNext () && j < expected.size ())
526 struct Expected e = expected[j];
527 std::ostringstream oss;
528 oss <<
"anon::ATestTag<" << e.n <<
">";
547 Ptr<Packet> pkt1 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
"hello"), 5);
548 Ptr<Packet> pkt2 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
" world"), 6);
555 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
558 std::string msg = std::string (
reinterpret_cast<const char *
>(buf),
568 CHECK (p, 1,
E (1, 0, 1000));
570 CHECK (copy, 1,
E (1, 0, 1000));
573 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
574 CHECK (copy, 1,
E (1, 0, 1000));
580 CHECK (&c0, 1,
E (1, 0, 1000));
581 CHECK (&c1, 1,
E (1, 0, 1000));
582 CHECK (copy, 1,
E (1, 0, 1000));
584 CHECK (&c0, 2,
E (1, 0, 1000),
E (10, 0, 1000));
585 CHECK (&c1, 1,
E (1, 0, 1000));
586 CHECK (copy, 1,
E (1, 0, 1000));
593 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
595 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
597 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
600 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));
602 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
605 E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10),
606 E (1, 10, 100),
E (2, 10, 100),
E (4, 10, 100),
607 E (1, 100, 1000),
E (2, 100, 1000),
E (5, 100, 1000));
611 frag0 = Create<Packet> (1000);
615 p = Create<Packet> (1000);
617 CHECK (p, 1,
E (20, 0, 1000));
619 CHECK (p, 1,
E (20, 0, 1000));
620 CHECK (frag0, 1,
E (20, 0, 90));
623 CHECK (frag0, 1,
E (20, 10, 100));
628 CHECK (tmp, 1,
E (20, 0, 100));
630 CHECK (tmp, 1,
E (20, 10, 110));
633 CHECK (tmp, 1,
E (20, 0, 100));
635 CHECK (tmp, 1,
E (20, 10, 110));
637 tmp = Create<Packet> (100);
639 CHECK (tmp, 1,
E (20, 0, 100));
641 CHECK (tmp, 1,
E (20, 0, 100));
644 CHECK (tmp, 1,
E (20, 0, 100));
646 CHECK (tmp, 1,
E (20, 0, 100));
654 CHECK (tmp, 1,
E (20, 0, 156));
656 CHECK (tmp, 1,
E (20, 0, 36));
659 CHECK (a, 1,
E (20, 0, 36));
665 CHECK (tmp, 0,
E (20, 0, 0));
670 CHECK (tmp, 1,
E (20, 0, 1000));
672 CHECK (tmp, 0,
E (0,0,0));
675 CHECK (a, 1,
E (10, 0, 10));
677 CHECK (tmp, 1,
E (10, 0, 10));
723 uint8_t* buffer =
new uint8_t[serializedSize + 16];
726 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
754 CHECK (p1, 3,
E (10, 0, 1000),
E (11, 0, 1000),
E (12, 0, 1000));
757 uint8_t* buffer =
new uint8_t[serializedSize];
760 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
764 CHECK_DATA (p2, 3,
E_DATA (10, 0, 1000, 65),
E_DATA (11, 0, 1000, 66),
E_DATA (12, 0, 1000, 67));
772 CHECK (tmp, 1,
E (20, 0, 1000));
774 CHECK (tmp, 1,
E (20, 2, 1002));
776 CHECK (tmp, 1,
E (20, 1, 1001));
779 CHECK (tmp, 1,
E (20, 1, 1001));
788 CHECK (tmp, 1,
E (25, 0, 100));
790 CHECK (tmp, 1,
E (25, 0, 50));
792 CHECK (tmp, 1,
E (25, 50, 100));
800 CHECK (tmp, 1,
E (25, 0, 100));
802 CHECK (tmp, 1,
E (25, 0, 50));
804 CHECK (tmp, 1,
E (25, 0, 50));
812 CHECK (tmp, 1,
E (25, 0, 100));
814 CHECK (tmp, 1,
E (25, 0, 50));
816 CHECK (tmp, 1,
E (25, 25, 75));
824 CHECK (tmp, 1,
E (25, 0, 100));
826 CHECK (tmp, 1,
E (25, 0, 50));
828 CHECK (tmp, 1,
E (25, 0, 50));
836 CHECK (tmp, 1,
E (25, 0, 100));
838 CHECK (tmp, 1,
E (25, 0, 50));
840 CHECK (tmp, 1,
E (25, 0, 50));
850 CHECK (tmp, 1,
E (25, 0, 100));
852 CHECK (tmp, 1,
E (25, 0, 50));
854 CHECK (tmp, 1,
E (25, 0, 50));
908 const char * msg = 0);
933 int expect = t.GetData ();
934 bool found = ref.
Peek (t);
936 msg <<
": ref contains "
937 << t.GetTypeId ().GetName ());
940 msg <<
": ref " << t.GetTypeId ().GetName ()
946 #define MAKE_TEST_TAGS \
947 ATestTag<1> t1 (1); \
948 ATestTag<2> t2 (1); \
949 ATestTag<3> t3 (1); \
950 ATestTag<4> t4 (1); \
951 ATestTag<5> t5 (1); \
952 ATestTag<6> t6 (1); \
953 ATestTag<7> t7 (1); \
954 const int tagLast = 7; \
979 const int reps = 10000;
980 std::vector< PacketTagList > ptv(reps, ref);
981 int start = clock ();
982 for (
int i = 0; i < reps; ++i) {
986 int delta = stop -
start;
988 std::cout <<
GetName () <<
"remove time: " << msg <<
": " << std::setw (8)
989 << delta <<
" ticks to remove "
999 const int reps = 100000;
1002 int start = clock ();
1003 for (
int i = 0; i < reps; ++i) {
1007 int stop = clock ();
1008 int delta = stop -
start;
1010 std::cout <<
GetName () <<
"add/remove time: " << std::setw (8)
1011 << delta <<
" ticks to add+remove "
1021 std::cout <<
GetName () <<
"begin" << std::endl;
1035 std::cout <<
GetName () <<
"check Peek (missing tag) returns false"
1042 std::cout <<
GetName () <<
"check copy and assignment" << std::endl;
1054 # define RemoveCheck(n) \
1055 { PacketTagList p ## n = ref; \
1056 p ## n .Remove ( t ## n ); \
1057 CheckRefList (ref, "remove " #n " orig"); \
1058 CheckRefList (p ## n, "remove " #n " copy", n); \
1062 std::cout <<
GetName () <<
"check removal of each tag" << std::endl;
1073 std::cout <<
GetName () <<
"check removal doesn't disturb merge "
1089 const char * msg =
"post merge, short chain";
1101 std::cout <<
GetName () <<
"check replacing each tag" << std::endl;
1103 # define ReplaceCheck(n) \
1104 t ## n .m_data = 2; \
1105 { PacketTagList p ## n = ref; \
1106 p ## n .Replace ( t ## n ); \
1107 CheckRefList (ref, "replace " #n " orig"); \
1108 CheckRef (p ## n, t ## n, "replace " #n " copy"); \
1121 std::cout <<
GetName () <<
"add+remove timing" << std::endl;
1123 const int nIterations = 100;
1124 for (
int i = 0; i < nIterations; ++i) {
1126 if (now < flm) flm = now;
1128 std::cout <<
GetName () <<
"min add+remove time: "
1129 << std::setw (8) << flm <<
" ticks"
1132 std::cout <<
GetName () <<
"remove timing" << std::endl;
1135 for (
int i = 0; i < nIterations; ++i) {
1136 for (
int j = 1; j <= tagLast; ++j) {
1148 if (now < rmn[j]) rmn[j] = now;
1151 for (
int j = tagLast; j > 0; --j) {
1152 std::cout <<
GetName () <<
"min remove time: t"
1154 << std::setw (8) << rmn[j] <<
" ticks"
a unique identifier for an interface.
uint8_t data
Optional data.
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
int AddRemoveTime(const bool verbose=false)
Prints the remove time.
TypeId GetTypeId(void) const
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
Prints the remove time.
Template class for Test trailers.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
void AddHeader(const Header &header)
Add header to this packet.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Deserialize(TagBuffer buf)
void DoRun(void)
Implementation to actually run this TestCase.
uint32_t n
Number of elements.
ATestTagBase(uint8_t data)
Constructor.
static TypeId GetTypeId(void)
Register this type.
#define CHECK_DATA(p, n,...)
std::string GetName(void) const
Struct to hold the expected data in the packet.
virtual uint32_t GetSerializedSize(void) const
Packet Tag list unit tests.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
virtual uint32_t GetSerializedSize(void) const
void WriteU8(uint8_t data)
Base class for Test trailers.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.
bool Peek(Tag &tag) const
Find a tag and return its value.
Base class for Test tags.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
virtual void Serialize(TagBuffer buf) const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define E_DATA(name, start, end, data)
virtual void Serialize(TagBuffer buf) const
Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
Constructor.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Template class for Large Test tags.
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
Checks against a reference PacketTagList.
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.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
void DoCheck(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
Checks the packet.
ATestTag(uint8_t data)
Constructor.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
virtual uint32_t Deserialize(Buffer::Iterator iter)
tag a set of bytes in a packet
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
TAG_BUFFER_INLINE uint8_t ReadU8(void)
virtual void Serialize(Buffer::Iterator iter) const
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual void Print(std::ostream &os) const
Template class for Test tags.
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
uint32_t GetEnd(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.
void Add(Tag const &tag) const
Add a tag to the head of this branch.
virtual uint32_t GetSerializedSize(void) const
static TypeId GetTypeId(void)
Register this type.
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.
std::vector< uint8_t > m_data
Tag data.
#define LARGE_TAG_BUFFER_SIZE
void AddPacketTag(const Tag &tag) const
Add a packet tag.
iterator in a Buffer instance
static TypeId GetTypeId(void)
Register this type.
std::string GetName(void) const
Get the name.
int GetData() const
Get the tag data.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Ptr< Packet > Copy(void) 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.
virtual void Deserialize(TagBuffer buf)
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
#define E(name, start, end)
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
bool m_error
Error in the Trailer.
void RemoveAllPacketTags(void)
Remove all packet tags.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
bool m_error
Error in the Tag.
virtual ~PacketTagListTest()
void DoCheckData(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
Checks the packet and its data.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Iterator over the set of byte tags in a packet.
virtual void Print(std::ostream &os) const
List of the packet tags stored in a packet.
void Prev(void)
go backward by one byte
virtual void Print(std::ostream &os) const
virtual void DoRun(void)
Implementation to actually run this TestCase.
static TypeId GetTypeId(void)
Register this type.
uint8_t m_size
Packet size.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
static TypeId GetTypeId(void)
Register this type.
Protocol trailer serialization and deserialization.