20 #include <ns3/attribute-container.h>
21 #include <ns3/double.h>
22 #include <ns3/integer.h>
24 #include <ns3/object.h>
27 #include <ns3/string.h>
29 #include <ns3/type-id.h>
61 void ReverseDoubleList();
74 void SetDoubleList(
const std::list<double>& doubleList);
80 std::list<double> GetDoubleList()
const;
87 void SetIntVec(std::vector<int> vec);
93 std::vector<int> GetIntVec()
const;
99 std::map<std::string, int>
m_map;
117 TypeId(
"ns3::AttributeContainerObject")
119 .SetGroupName(
"Test")
121 .AddAttribute(
"DoubleList",
124 MakeAttributeContainerAccessor<DoubleValue>(
126 MakeAttributeContainerChecker<DoubleValue>(MakeDoubleChecker<double>()))
129 "Vector of integers",
133 MakeAttributeContainerAccessor<IntegerValue, ';', std::list>(
136 MakeAttributeContainerChecker<IntegerValue, ';'>(MakeIntegerChecker<int>()))
139 "Map of strings to ints",
142 MakeAttributeContainerAccessor<PairValue<StringValue, IntegerValue>>(
146 MakeIntegerChecker<int>())))
149 "An example of complex attribute that is defined by a vector of pairs consisting "
150 "of an integer value and a vector of integers. In case a string is used to set "
151 "this attribute, the string shall contain the pairs separated by a semicolon (;); "
152 "in every pair, the integer value and the vector of integers are separated by a "
153 "blank space, and the elements of the vectors are separated by a comma (,) "
154 "without spaces. E.g. \"0 1,2,3; 1 0; 2 0,1\" consists of three pairs containing "
155 "vectors of 3, 1 and 2 elements, respectively.",
157 MakeAttributeContainerAccessor<IntVecMapValue, ';'>(
159 MakeAttributeContainerChecker<IntVecMapValue, ';'>(
161 MakeIntegerChecker<int>(),
162 MakeAttributeContainerChecker<IntegerValue>(MakeIntegerChecker<int>()))));
169 m_doublelist.reverse();
175 m_doublelist = doubleList;
205 template <
class A,
class B,
class C,
class D>
209 return x.first == y.first &&
x.second == y.second;
227 void DoRun()
override;
231 :
TestCase(
"test instantiation, initialization, access")
239 std::list<double> ref = {1.0, 2.1, 3.145269};
244 auto aciter = ac.
Begin();
245 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
255 std::vector<int> ref = {-2, 3, 10, -1042};
260 auto aciter = ac.
Begin();
261 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
271 auto ref = {
"one",
"two",
"three"};
275 auto aciter = ac.
Begin();
286 auto ref = {
"one",
"two",
"three"};
290 auto aciter = ac.
Begin();
302 std::map<std::string, int64_t> ref = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
306 auto aciter = ac.
Begin();
307 for (
const auto& v : ref)
332 void DoRun()
override;
336 :
TestCase(
"test serialization and deserialization")
345 std::string doubles =
"1.0001, 20.53, -102.3";
349 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
350 acchecker->SetItemChecker(MakeDoubleChecker<double>());
353 "Deserialize failed");
357 std::string canonical = doubles;
358 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
364 std::string ints =
"1, 2, -3, -4";
368 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
369 acchecker->SetItemChecker(MakeIntegerChecker<int>());
372 "Deserialize failed");
376 std::string canonical = ints;
377 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
382 std::string strings =
"this is a sentence with words";
386 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
390 "Deserialize failed");
398 std::string pairs =
"one 1,two 2,three 3";
401 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
402 acchecker->SetItemChecker(
404 MakeIntegerChecker<int>()));
407 "Deserialization failed");
430 void DoRun()
override;
434 :
TestCase(
"test attribute set and get")
443 auto doubleList = obj->GetDoubleList();
447 const std::list<double> doubles = {1.1, 2.22, 3.333};
450 auto doubleList = obj->GetDoubleList();
453 "DoubleList incorrectly set");
456 obj->ReverseDoubleList();
458 auto doubleList = obj->GetDoubleList();
461 "DoubleList incorrectly reversed");
465 obj->GetAttribute(
"DoubleList",
value);
472 "Incorrect value in doublesvec");
475 const std::vector<int> ints = {-1, 0, 1, 2, 3};
482 obj->GetAttribute(
"IntegerVector",
value);
490 "Incorrect value in intvec");
493 std::string intVecPairString(
"0 1,2,3; 1 0; 2 0,1");
495 obj->SetAttribute(
"IntVecPairVec",
StringValue(intVecPairString));
502 obj->GetAttribute(
"IntVecPairVec",
value);
507 auto reslistIt = reslist.
begin();
511 "Incorrect number of integer values in first pair");
516 "Incorrect number of integer values in second pair");
521 "Incorrect number of integer values in third pair");
524 std::map<std::string, int> map = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
525 obj->SetAttribute(
"MapStringInt",
530 obj->GetAttribute(
"MapStringInt",
value);
534 std::map<std::string, int> mapstrint;
535 auto lst =
value.Get();
536 for (
const auto& l : lst)
538 mapstrint[l.first] = l.second;
542 auto iter = map.begin();
543 for (
const auto& v : mapstrint)
563 :
TestSuite(
"attribute-container-test-suite", UNIT)
static AttributeContainerTestSuite g_attributeContainerTestSuite
Static variable for test initialization.
Attribute container object.
std::vector< int > GetIntVec() const
Get the vector of ints.
std::vector< int > m_intvec
Vector of ints.
AttributeContainerObject()
void ReverseDoubleList()
Reverses the list of doubles.
std::list< double > m_doublelist
List of doubles.
~AttributeContainerObject() override
std::list< double > GetDoubleList() const
Get the list of doubles.
std::map< std::string, int > m_map
Map of <std::string, int>.
static TypeId GetTypeId()
Get the type ID.
void SetIntVec(std::vector< int > vec)
Set the vector of ints to the given vector.
void SetDoubleList(const std::list< double > &doubleList)
Set the list of doubles to the given list.
std::map< int64_t, std::list< int64_t > > m_intVecIntMapping
Mapping integers to vectors.
Attribute serialization and deserialization TestCase.
void DoRun() override
Implementation to actually run this TestCase.
AttributeContainerSerializationTestCase()
~AttributeContainerSerializationTestCase() override
Attribute set and get TestCase.
AttributeContainerSetGetTestCase()
~AttributeContainerSetGetTestCase() override
void DoRun() override
Implementation to actually run this TestCase.
Test AttributeContainer instantiation, initialization, access.
void DoRun() override
Implementation to actually run this TestCase.
AttributeContainerTestCase()
~AttributeContainerTestCase() override
Attribute attribute container TestCase.
AttributeContainerTestSuite()
A container for one type of attribute.
Iterator End()
NS3-style ending of container.
size_type GetN() const
NS3-style Number of items.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
C< item_type > result_type
Type of container returned.
Iterator Begin()
NS3-style beginning of container.
size_type size() const
STL-style number of items in container.
iterator begin()
STL-style beginning of container.
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Hold a signed integer type.
A base class which provides memory management and object aggregation.
Hold objects of type std::pair<A, B>.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#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.
#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.
bool operator==(const EventId &a, const EventId &b)
Ptr< AttributeChecker > MakeAttributeContainerChecker(const AttributeContainerValue< A, Sep, C > &value)
Make AttributeContainerChecker from AttributeContainerValue.
Ptr< const AttributeChecker > MakeStringChecker()
Ptr< AttributeChecker > MakePairChecker(const PairValue< A, B > &value)
Make a PairChecker from a PairValue.