|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2006,2007 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@sophia.inria.fr> |
| 19 |
*/ |
| 20 |
#include <cstdarg> |
| 21 |
#include <iostream> |
| 22 |
#include <sstream> |
| 23 |
#include "ns3/test.h" |
| 24 |
#include "ns3/bit-serializer.h" |
| 25 |
#include "ns3/bit-deserializer.h" |
| 26 |
|
| 27 |
using namespace ns3; |
| 28 |
|
| 29 |
/** |
| 30 |
* \ingroup network-test |
| 31 |
* \ingroup tests |
| 32 |
* |
| 33 |
* \brief Bit serialization test |
| 34 |
*/ |
| 35 |
class BitSerializerTest : public TestCase { |
| 36 |
public: |
| 37 |
virtual void DoRun (void); |
| 38 |
BitSerializerTest (); |
| 39 |
}; |
| 40 |
|
| 41 |
BitSerializerTest::BitSerializerTest () |
| 42 |
: TestCase ("BitSerializer") { |
| 43 |
} |
| 44 |
|
| 45 |
void BitSerializerTest::DoRun () |
| 46 |
{ |
| 47 |
BitSerializer testBitSerializer1; |
| 48 |
|
| 49 |
testBitSerializer1.PushBits (0x55, 7); |
| 50 |
testBitSerializer1.PushBits (0x7, 3); |
| 51 |
testBitSerializer1.PushBits (0x0, 2); |
| 52 |
|
| 53 |
std::vector<uint8_t> result = testBitSerializer1.GetBytes (); |
| 54 |
NS_TEST_EXPECT_MSG_EQ ((result[0] == 0xab) && (result[1] == 0xc0), true, |
| 55 |
"Incorrect serialization " << result[0] << result[1] << " instead of " << int(0xab) << " " << int(0xc0)); |
| 56 |
|
| 57 |
BitSerializer testBitSerializer2; |
| 58 |
|
| 59 |
testBitSerializer2.PushBits (0x55, 7); |
| 60 |
testBitSerializer2.PushBits (0x7, 3); |
| 61 |
testBitSerializer2.PushBits (0x0, 2); |
| 62 |
|
| 63 |
testBitSerializer2.InsertPaddingAtEnd (false); |
| 64 |
|
| 65 |
result = testBitSerializer2.GetBytes (); |
| 66 |
NS_TEST_EXPECT_MSG_EQ ((result[0] == 0x0a) && (result[1] == 0xbc), true, |
| 67 |
"Incorrect serialization " << result[0] << result[1] << " instead of " << int(0x0a) << " " << int(0xbc)); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* \ingroup network-test |
| 72 |
* \ingroup tests |
| 73 |
* |
| 74 |
* \brief Bit deserialization test |
| 75 |
*/ |
| 76 |
class BitDeserializerTest : public TestCase { |
| 77 |
public: |
| 78 |
virtual void DoRun (void); |
| 79 |
BitDeserializerTest (); |
| 80 |
}; |
| 81 |
|
| 82 |
BitDeserializerTest::BitDeserializerTest () |
| 83 |
: TestCase ("BitDeserializer") { |
| 84 |
} |
| 85 |
|
| 86 |
void BitDeserializerTest::DoRun () |
| 87 |
{ |
| 88 |
BitDeserializer testBitDeserializer; |
| 89 |
uint8_t test[2]; |
| 90 |
test[0] = 0xab; |
| 91 |
test[1] = 0xc0; |
| 92 |
|
| 93 |
testBitDeserializer.PushBytes (test,2); |
| 94 |
uint16_t nibble1 = testBitDeserializer.GetBits (7); |
| 95 |
uint16_t nibble2 = testBitDeserializer.GetBits (3); |
| 96 |
uint16_t nibble3 = testBitDeserializer.GetBits (2); |
| 97 |
|
| 98 |
bool result = (nibble1 == 0x55) && (nibble2 == 0x7) && (nibble3 == 0x0); |
| 99 |
NS_TEST_EXPECT_MSG_EQ (result, true, |
| 100 |
"Incorrect deserialization " << nibble1 << " " << nibble2 << " " << nibble3 << |
| 101 |
" << instead of " << " " << int(0x55) << " " << int(0x7) << " " << int(0x0)); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* \ingroup network-test |
| 106 |
* \ingroup tests |
| 107 |
* |
| 108 |
* \brief Packet Metadata TestSuite |
| 109 |
*/ |
| 110 |
class BitSerializerTestSuite : public TestSuite |
| 111 |
{ |
| 112 |
public: |
| 113 |
BitSerializerTestSuite (); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
BitSerializerTestSuite::BitSerializerTestSuite () |
| 118 |
: TestSuite ("bit-serializer", UNIT) |
| 119 |
{ |
| 120 |
AddTestCase (new BitSerializerTest, TestCase::QUICK); |
| 121 |
AddTestCase (new BitDeserializerTest, TestCase::QUICK); |
| 122 |
} |
| 123 |
|
| 124 |
static BitSerializerTestSuite g_bitSerializerTest; //!< Static variable for test initialization |