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>
75 void SetIntVec(std::vector<int> vec);
81 std::vector<int> GetIntVec()
const;
96 std::map<std::string, int>
m_map;
111 TypeId(
"ns3::AttributeContainerObject")
113 .SetGroupName(
"Test")
115 .AddAttribute(
"DoubleList",
118 MakeAttributeContainerAccessor<DoubleValue>(
120 MakeAttributeContainerChecker<DoubleValue>(MakeDoubleChecker<double>()))
121 .AddAttribute(
"IntegerVector",
122 "Vector of integers",
126 MakeAttributeContainerAccessor<IntegerValue, std::list>(
129 MakeAttributeContainerChecker<IntegerValue>(MakeIntegerChecker<int>()))
132 "Map of strings to ints",
135 MakeAttributeContainerAccessor<PairValue<StringValue, IntegerValue>>(
139 MakeIntegerChecker<int>())));
146 m_doublelist.reverse();
147 std::vector<int> tmp;
148 std::copy_backward(m_intvec.begin(), m_intvec.end(), tmp.begin());
167 os <<
"AttributeContainerObject: ";
190 template <
class A,
class B,
class C,
class D>
194 return x.first == y.first &&
x.second == y.second;
212 void DoRun()
override;
216 :
TestCase(
"test instantiation, initialization, access")
224 std::list<double> ref = {1.0, 2.1, 3.145269};
229 auto aciter = ac.
Begin();
230 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
240 std::vector<int> ref = {-2, 3, 10, -1042};
245 auto aciter = ac.
Begin();
246 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
256 auto ref = {
"one",
"two",
"three"};
260 auto aciter = ac.
Begin();
271 auto ref = {
"one",
"two",
"three"};
275 auto aciter = ac.
Begin();
287 std::map<std::string, int64_t> ref = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
291 auto aciter = ac.
Begin();
292 for (
const auto& v : ref)
317 void DoRun()
override;
321 :
TestCase(
"test serialization and deserialization")
330 std::string doubles =
"1.0001, 20.53, -102.3";
334 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
335 acchecker->SetItemChecker(MakeDoubleChecker<double>());
338 "Deserialize failed");
342 std::string canonical = doubles;
343 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
349 std::string ints =
"1, 2, -3, -4";
353 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
354 acchecker->SetItemChecker(MakeIntegerChecker<int>());
357 "Deserialize failed");
361 std::string canonical = ints;
362 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
367 std::string strings =
"this is a sentence with words";
371 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
375 "Deserialize failed");
379 std::string canonical = strings;
384 std::string pairs =
"one 1,two 2,three 3";
387 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
388 acchecker->SetItemChecker(
390 MakeIntegerChecker<int>()));
393 "Deserialization failed");
397 std::string canonical = pairs;
417 void DoRun()
override;
421 :
TestCase(
"test attribute set and get")
430 std::ostringstream oss;
433 "AttributeContainerObject: ",
434 "DoubleList initialized incorrectly");
437 std::list<double> doubles = {1.1, 2.22, 3.333};
440 std::ostringstream oss;
443 "AttributeContainerObject: 1.1, 2.22, 3.333",
444 "DoubleList incorrectly set");
449 std::ostringstream oss;
452 "AttributeContainerObject: 3.333, 2.22, 1.1",
453 "DoubleList incorrectly reversed");
457 obj->GetAttribute(
"DoubleList",
value);
462 auto iter = doubles.rbegin();
463 for (
auto d : doublevec)
470 std::vector<int> ints = {-1, 0, 1, 2, 3};
477 obj->GetAttribute(
"IntegerVector",
value);
482 auto iter = ints.begin();
483 for (
auto d : intlist)
490 std::map<std::string, int> map = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
491 obj->SetAttribute(
"MapStringInt",
496 obj->GetAttribute(
"MapStringInt",
value);
500 std::map<std::string, int> mapstrint;
501 auto lst =
value.Get();
502 for (
const auto& l : lst)
504 mapstrint[l.first] = l.second;
508 auto iter = map.begin();
509 for (
const auto& v : mapstrint)
529 :
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()
std::list< double > m_doublelist
List of doubles.
~AttributeContainerObject() override
std::map< std::string, int > m_map
Map of <std::string, int>.
void ReverseList()
Reverses the list of doubles.
static TypeId GetTypeId()
Get the type ID.
void SetIntVec(std::vector< int > vec)
Set the vector of ints to the given vector.
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 Begin()
NS3-style beginning of container.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
C< item_type > result_type
Type of container returned.
size_type GetN() const
NS3-style Number of items.
Iterator End()
NS3-style ending of container.
size_type size() const
STL-style number of items in container.
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.
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.
Ptr< const AttributeChecker > MakeStringChecker()
#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, C > &value)
Make AttributeContainerChecker from AttributeContainerValue.
std::ostream & operator<<(std::ostream &os, const Angles &a)