A Discrete-Event Network Simulator
API
buffer-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
19  */
20 
21 #include "ns3/buffer.h"
22 #include "ns3/random-variable-stream.h"
23 #include "ns3/double.h"
24 #include "ns3/test.h"
25 
26 using namespace ns3;
27 
39 class BufferTest : public TestCase {
40 private:
49  void EnsureWrittenBytes (Buffer b, uint32_t n, uint8_t array[], const char *file, int line);
50 public:
51  virtual void DoRun (void);
52  BufferTest ();
53 };
54 
55 
57  : TestCase ("Buffer") {
58 }
59 
60 void
61 BufferTest::EnsureWrittenBytes (Buffer b, uint32_t n, uint8_t array[], const char *file, int line)
62 {
63  bool success = true;
64  uint8_t *expected = array;
65  uint8_t const*got;
66  got = b.PeekData ();
67  for (uint32_t j = 0; j < n; j++)
68  {
69  if (got[j] != expected[j])
70  {
71  success = false;
72  }
73  }
74  if (!success)
75  {
76  std::ostringstream failure;
77  failure << "Buffer -- ";
78  failure << "expected: n=";
79  failure << n << ", ";
80  failure.setf (std::ios::hex, std::ios::basefield);
81  for (uint32_t j = 0; j < n; j++)
82  {
83  failure << (uint16_t)expected[j] << " ";
84  }
85  failure.setf (std::ios::dec, std::ios::basefield);
86  failure << "got: ";
87  failure.setf (std::ios::hex, std::ios::basefield);
88  for (uint32_t j = 0; j < n; j++)
89  {
90  failure << (uint16_t)got[j] << " ";
91  }
92  failure << std::endl;
93  NS_TEST_ASSERT_MSG_EQ_INTERNAL (true, false, failure.str (), file, line);
94  }
95 }
96 
97 /*
98  * Works only when variadic macros are
99  * available which is the case for gcc.
100  */
101 #define ENSURE_WRITTEN_BYTES(buffer, n, ...) \
102  { \
103  uint8_t bytes[] = { __VA_ARGS__}; \
104  EnsureWrittenBytes (buffer, n, bytes, __FILE__, __LINE__); \
105  }
106 
107 void
109 {
110  Buffer buffer;
112  buffer.AddAtStart (6);
113  i = buffer.Begin ();
114  i.WriteU8 (0x66);
115  ENSURE_WRITTEN_BYTES (buffer, 1, 0x66);
116  i = buffer.Begin ();
117  i.WriteU8 (0x67);
118  ENSURE_WRITTEN_BYTES (buffer, 1, 0x67);
119  i.WriteHtonU16 (0x6568);
120  i = buffer.Begin ();
121  ENSURE_WRITTEN_BYTES (buffer, 3, 0x67, 0x65, 0x68);
122  i.WriteHtonU16 (0x6369);
123  ENSURE_WRITTEN_BYTES (buffer, 3, 0x63, 0x69, 0x68);
124  i.WriteHtonU32 (0xdeadbeaf);
125  ENSURE_WRITTEN_BYTES (buffer, 6, 0x63, 0x69, 0xde, 0xad, 0xbe, 0xaf);
126  buffer.AddAtStart (2);
127  i = buffer.Begin ();
128  i.WriteU16 (0);
129  ENSURE_WRITTEN_BYTES (buffer, 8, 0, 0, 0x63, 0x69, 0xde, 0xad, 0xbe, 0xaf);
130  buffer.AddAtEnd (2);
131  i = buffer.Begin ();
132  i.Next (8);
133  i.WriteU16 (0);
134  ENSURE_WRITTEN_BYTES (buffer, 10, 0, 0, 0x63, 0x69, 0xde, 0xad, 0xbe, 0xaf, 0, 0);
135  buffer.RemoveAtStart (3);
136  i = buffer.Begin ();
137  ENSURE_WRITTEN_BYTES (buffer, 7, 0x69, 0xde, 0xad, 0xbe, 0xaf, 0, 0);
138  buffer.RemoveAtEnd (4);
139  i = buffer.Begin ();
140  ENSURE_WRITTEN_BYTES (buffer, 3, 0x69, 0xde, 0xad);
141  buffer.AddAtStart (1);
142  i = buffer.Begin ();
143  i.WriteU8 (0xff);
144  ENSURE_WRITTEN_BYTES (buffer, 4, 0xff, 0x69, 0xde, 0xad);
145  buffer.AddAtEnd (1);
146  i = buffer.Begin ();
147  i.Next (4);
148  i.WriteU8 (0xff);
149  i.Prev (2);
150  uint16_t saved = i.ReadU16 ();
151  i.Prev (2);
152  i.WriteHtonU16 (0xff00);
153  i.Prev (2);
154  NS_TEST_ASSERT_MSG_EQ (i.ReadNtohU16 (), 0xff00, "Could not read expected value");
155  i.Prev (2);
156  i.WriteU16 (saved);
157  ENSURE_WRITTEN_BYTES (buffer, 5, 0xff, 0x69, 0xde, 0xad, 0xff);
158  Buffer o = buffer;
159  ENSURE_WRITTEN_BYTES (o, 5, 0xff, 0x69, 0xde, 0xad, 0xff);
160  o.AddAtStart (1);
161  i = o.Begin ();
162  i.WriteU8 (0xfe);
163  ENSURE_WRITTEN_BYTES (o, 6, 0xfe, 0xff, 0x69, 0xde, 0xad, 0xff);
164  buffer.AddAtStart (2);
165  i = buffer.Begin ();
166  i.WriteU8 (0xfd);
167  i.WriteU8 (0xfd);
168  ENSURE_WRITTEN_BYTES (o, 6, 0xfe, 0xff, 0x69, 0xde, 0xad, 0xff);
169  ENSURE_WRITTEN_BYTES (buffer, 7, 0xfd, 0xfd, 0xff, 0x69, 0xde, 0xad, 0xff);
170 
171  // test 64-bit read/write
172  Buffer buff64;
173  buff64.AddAtStart (8);
174  i = buff64.Begin ();
175  i.WriteU64 (0x0123456789ABCDEFllu);
176  i = buff64.Begin ();
177  NS_TEST_ASSERT_MSG_EQ (i.ReadU64 (), 0x0123456789abcdefllu, "Could not read expected value");
178  i = buff64.Begin ();
179  i.WriteHtolsbU64 (0x0123456789ABCDEFllu);
180  ENSURE_WRITTEN_BYTES (buff64, 8, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01);
181  i = buff64.Begin ();
182  NS_TEST_ASSERT_MSG_EQ (i.ReadLsbtohU64 (), 0x0123456789abcdefllu, "Could not read expected value");
183  i = buff64.Begin ();
184  i.WriteHtonU64 (0x0123456789ABCDEFllu);
185  ENSURE_WRITTEN_BYTES (buff64, 8, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef);
186  i = buff64.Begin ();
187  NS_TEST_ASSERT_MSG_EQ (i.ReadNtohU64 (), 0x0123456789abcdefllu, "could not read expected value");
188 
189  // test self-assignment
190  {
191  Buffer a = o;
192  a = a;
193  }
194 
195  // test Remove start.
196  buffer = Buffer (5);
197  ENSURE_WRITTEN_BYTES (buffer, 5, 0, 0, 0, 0, 0);
198  buffer.RemoveAtStart (1);
199  ENSURE_WRITTEN_BYTES (buffer, 4, 0, 0, 0, 0);
200  buffer.AddAtStart (1);
201  buffer.Begin ().WriteU8 (0xff);
202  ENSURE_WRITTEN_BYTES (buffer, 5, 0xff, 0, 0, 0, 0);
203  buffer.RemoveAtStart (3);
204  ENSURE_WRITTEN_BYTES (buffer, 2, 0, 0);
205  buffer.AddAtStart (4);
206  buffer.Begin ().WriteHtonU32 (0xdeadbeaf);
207  ENSURE_WRITTEN_BYTES (buffer, 6, 0xde, 0xad, 0xbe, 0xaf, 0, 0);
208  buffer.RemoveAtStart (2);
209  ENSURE_WRITTEN_BYTES (buffer, 4, 0xbe, 0xaf, 0, 0);
210  buffer.AddAtEnd (4);
211  i = buffer.Begin ();
212  i.Next (4);
213  i.WriteHtonU32 (0xdeadbeaf);
214  ENSURE_WRITTEN_BYTES (buffer, 8, 0xbe, 0xaf, 0, 0, 0xde, 0xad, 0xbe, 0xaf);
215  buffer.RemoveAtStart (5);
216  ENSURE_WRITTEN_BYTES (buffer, 3, 0xad, 0xbe, 0xaf);
217  // test Remove end
218  buffer = Buffer (5);
219  ENSURE_WRITTEN_BYTES (buffer, 5, 0, 0, 0, 0, 0);
220  buffer.RemoveAtEnd (1);
221  ENSURE_WRITTEN_BYTES (buffer, 4, 0, 0, 0, 0);
222  buffer.AddAtEnd (2);
223  i = buffer.Begin ();
224  i.Next (4);
225  i.WriteU8 (0xab);
226  i.WriteU8 (0xac);
227  ENSURE_WRITTEN_BYTES (buffer, 6, 0, 0, 0, 0, 0xab, 0xac);
228  buffer.RemoveAtEnd (1);
229  ENSURE_WRITTEN_BYTES (buffer, 5, 0, 0, 0, 0, 0xab);
230  buffer.RemoveAtEnd (3);
231  ENSURE_WRITTEN_BYTES (buffer, 2, 0, 0);
232  buffer.AddAtEnd (6);
233  i = buffer.Begin ();
234  i.Next (2);
235  i.WriteU8 (0xac);
236  i.WriteU8 (0xad);
237  i.WriteU8 (0xae);
238  i.WriteU8 (0xaf);
239  i.WriteU8 (0xba);
240  i.WriteU8 (0xbb);
241  ENSURE_WRITTEN_BYTES (buffer, 8, 0, 0, 0xac, 0xad, 0xae, 0xaf, 0xba, 0xbb);
242  buffer.AddAtStart (3);
243  i = buffer.Begin ();
244  i.WriteU8 (0x30);
245  i.WriteU8 (0x31);
246  i.WriteU8 (0x32);
247  ENSURE_WRITTEN_BYTES (buffer, 11, 0x30, 0x31, 0x32, 0, 0, 0xac, 0xad, 0xae, 0xaf, 0xba, 0xbb);
248  buffer.RemoveAtEnd (9);
249  ENSURE_WRITTEN_BYTES (buffer, 2, 0x30, 0x31);
250  buffer = Buffer (3);
251  buffer.AddAtEnd (2);
252  i = buffer.Begin ();
253  i.Next (3);
254  i.WriteHtonU16 (0xabcd);
255  buffer.AddAtStart (1);
256  buffer.Begin ().WriteU8 (0x21);
257  ENSURE_WRITTEN_BYTES (buffer, 6, 0x21, 0, 0, 0, 0xab, 0xcd);
258  buffer.RemoveAtEnd (8);
259  NS_TEST_ASSERT_MSG_EQ (buffer.GetSize (), 0, "Buffer size not zero");
260 
261  buffer = Buffer (6);
262  buffer.AddAtStart (9);
263  buffer.AddAtEnd (3);
264  i = buffer.End ();
265  i.Prev (1);
266  i.WriteU8 (1, 1);
267 
268  buffer = Buffer (6);
269  buffer.AddAtStart (3);
270  buffer.RemoveAtEnd (8);
271  buffer.AddAtEnd (4);
272  i = buffer.End ();
273  i.Prev (4);
274  i.WriteU8 (1, 4);
275 
276  buffer = Buffer (1);
277  buffer.AddAtEnd (100);
278  i = buffer.End ();
279  i.Prev (100);
280  i.WriteU8 (1, 100);
281 
284  {
285  const uint32_t actualSize = 72602;
286  const uint32_t chunkSize = 67624;
287  Ptr<UniformRandomVariable> bytesRng = CreateObject<UniformRandomVariable> ();
288  bytesRng->SetAttribute ("Min", DoubleValue (0));
289  bytesRng->SetAttribute ("Max", DoubleValue (256));
290 
291  Buffer inputBuffer;
292  Buffer outputBuffer;
293 
294  inputBuffer.AddAtEnd (actualSize);
295  {
296  Buffer::Iterator iter = inputBuffer.Begin ();
297  for (uint32_t i = 0; i < actualSize; i++)
298  iter.WriteU8 (static_cast<uint8_t> (bytesRng->GetValue ()));
299  }
300 
301  outputBuffer.AddAtEnd (chunkSize);
302  Buffer::Iterator iter = outputBuffer.End ();
303  iter.Prev (chunkSize);
304  iter.Write (inputBuffer.PeekData (), chunkSize);
305 
306  NS_TEST_EXPECT_MSG_EQ (memcmp (inputBuffer.PeekData (), outputBuffer.PeekData (), chunkSize), 0, "memcp works");
307  }
308 
309  buffer = Buffer (5);
310  buffer.AddAtEnd (2);
311  i = buffer.End ();
312  i.Prev (2);
313  i.WriteU8 (0);
314  i.WriteU8 (0x66);
315  ENSURE_WRITTEN_BYTES (buffer, 7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66);
316  Buffer frag0 = buffer.CreateFragment (0, 2);
317  ENSURE_WRITTEN_BYTES (frag0, 2, 0x00, 0x00);
318  Buffer frag1 = buffer.CreateFragment (2, 5);
319  ENSURE_WRITTEN_BYTES (frag1, 5, 0x00, 0x00, 0x00, 0x00, 0x66);
320  frag0.AddAtEnd (frag1);
321  ENSURE_WRITTEN_BYTES (buffer, 7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66);
322  ENSURE_WRITTEN_BYTES (frag0, 7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66);
323 
324  buffer = Buffer (5);
325  buffer.AddAtStart (2);
326  i = buffer.Begin ();
327  i.WriteU8 (0x1);
328  i.WriteU8 (0x2);
329  buffer.AddAtEnd (2);
330  i = buffer.End ();
331  i.Prev (2);
332  i.WriteU8 (0x3);
333  i.WriteU8 (0x4);
334  ENSURE_WRITTEN_BYTES (buffer, 9, 0x1, 0x2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3, 0x4);
335  Buffer other;
336  other.AddAtStart (9);
337  i = other.Begin ();
338  i.Write (buffer.Begin (), buffer.End ());
339  ENSURE_WRITTEN_BYTES (other, 9, 0x1, 0x2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3, 0x4);
340 
342  std::string ct ("This is the next content of the buffer.");
343  buffer = Buffer ();
344  buffer.AddAtStart (ct.size ());
345  i = buffer.Begin ();
346  i.Write ((const uint8_t*)ct.c_str (), ct.size ());
347  uint32_t sizeBuffer = buffer.GetSize ();
348  NS_TEST_ASSERT_MSG_EQ (sizeBuffer, ct.size(), "Buffer bad size");
349  uint8_t const* evilBuffer = buffer.PeekData ();
350  NS_TEST_ASSERT_MSG_NE( evilBuffer, 0, "Buffer PeekData failed");
351  uint8_t *cBuf = (uint8_t*) malloc ( sizeBuffer );
352  uint32_t copyLen = buffer.CopyData (cBuf, sizeBuffer);
353  NS_TEST_ASSERT_MSG_EQ (copyLen, sizeBuffer, "CopyData return bad size");
354  for (uint8_t i=0; i < sizeBuffer ; i++ )
355  {
356  NS_TEST_ASSERT_MSG_EQ ( cBuf [i], *(((const uint8_t*)ct.c_str ()) + i), "Bad buffer copied data");
357  NS_TEST_ASSERT_MSG_EQ ( evilBuffer [i], cBuf [i] , "Bad buffer peeked");
358  }
359  free (cBuf);
360 
362  buffer = Buffer (1);
363  buffer.AddAtEnd (2);
364  i = buffer.Begin ();
365  i.Next (1);
366  i.WriteU8 (0x77);
367  i.WriteU8 (0x66);
368  ENSURE_WRITTEN_BYTES (buffer, 3, 0x00, 0x77, 0x66);
369  i = buffer.Begin ();
370  i.ReadU8 ();
371  uint16_t val1 = i.ReadNtohU16 ();
372  i = buffer.Begin ();
373  i.ReadU8 ();
374  uint16_t val2 = 0;
375  val2 |= i.ReadU8 ();
376  val2 <<= 8;
377  val2 |= i.ReadU8 ();
378  NS_TEST_ASSERT_MSG_EQ (val1, val2, "Bad ReadNtohU16()");
379 }
380 
388 {
389 public:
390  BufferTestSuite ();
391 };
392 
394  : TestSuite ("buffer", UNIT)
395 {
396  AddTestCase (new BufferTest, TestCase::QUICK);
397 }
398 
uint16_t ReadU16(void)
Definition: buffer.h:1029
void WriteHtonU64(uint64_t data)
Definition: buffer.cc:940
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
#define ENSURE_WRITTEN_BYTES(buffer, n,...)
Definition: buffer-test.cc:101
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:483
void RemoveAtStart(uint32_t start)
Definition: buffer.cc:438
A suite of tests to run.
Definition: test.h:1342
automatically resized byte buffer
Definition: buffer.h:92
Buffer unit tests.
Definition: buffer-test.cc:39
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:285
encapsulates test code
Definition: test.h:1155
uint8_t const * PeekData(void) const
Definition: buffer.cc:705
void EnsureWrittenBytes(Buffer b, uint32_t n, uint8_t array[], const char *file, int line)
Checks the buffer content.
Definition: buffer-test.cc:61
#define NS_TEST_ASSERT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:120
iterator in a Buffer instance
Definition: buffer.h:98
void Prev(void)
go backward by one byte
Definition: buffer.h:851
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
void WriteU16(uint16_t data)
Definition: buffer.cc:870
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
void Next(void)
go forward by one byte
Definition: buffer.h:845
void WriteU64(uint64_t data)
Definition: buffer.cc:890
void AddAtEnd(uint32_t end)
Definition: buffer.cc:354
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: buffer-test.cc:108
uint64_t ReadU64(void)
Definition: buffer.cc:989
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
Buffer::Iterator End(void) const
Definition: buffer.h:1075
void WriteU8(uint8_t data)
Definition: buffer.h:869
#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...
Definition: test.h:624
void WriteHtolsbU64(uint64_t data)
Definition: buffer.cc:926
uint32_t GetSize(void) const
Definition: buffer.h:1063
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition: buffer.cc:519
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
static BufferTestSuite g_bufferTestSuite
Static variable for test initialization.
Definition: buffer-test.cc:399
Buffer TestSuite.
Definition: buffer-test.cc:387