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>
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>()))));
211 void DoRun()
override;
215 :
TestCase(
"test instantiation, initialization, access")
223 std::list<double> ref = {1.0, 2.1, 3.145269};
228 auto aciter = ac.
Begin();
229 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
239 std::vector<int> ref = {-2, 3, 10, -1042};
244 auto aciter = ac.
Begin();
245 for (
auto rend = ref.end(), riter = ref.begin(); riter != rend; ++riter)
255 auto ref = {
"one",
"two",
"three"};
259 auto aciter = ac.
Begin();
270 auto ref = {
"one",
"two",
"three"};
274 auto aciter = ac.Begin();
286 std::map<std::string, int64_t> ref = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
290 auto aciter = ac.
Begin();
291 for (
const auto& v : ref)
295 v.first == (*aciter)->Get().first && v.second == (*aciter)->Get().second;
318 void DoRun()
override;
322 :
TestCase(
"test serialization and deserialization")
331 std::string doubles =
"1.0001, 20.53, -102.3";
335 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
336 acchecker->SetItemChecker(MakeDoubleChecker<double>());
339 "Deserialize failed");
343 std::string canonical = doubles;
344 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
350 std::string ints =
"1, 2, -3, -4";
354 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
355 acchecker->SetItemChecker(MakeIntegerChecker<int>());
358 "Deserialize failed");
362 std::string canonical = ints;
363 canonical.erase(std::remove(canonical.begin(), canonical.end(),
' '), canonical.end());
368 std::string strings =
"this is a sentence with words";
372 auto acchecker = DynamicCast<AttributeContainerChecker>(checker);
376 "Deserialize failed");
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");
416 void DoRun()
override;
420 :
TestCase(
"test attribute set and get")
429 auto doubleList = obj->GetDoubleList();
433 const std::list<double> doubles = {1.1, 2.22, 3.333};
436 auto doubleList = obj->GetDoubleList();
439 "DoubleList incorrectly set");
442 obj->ReverseDoubleList();
444 auto doubleList = obj->GetDoubleList();
447 "DoubleList incorrectly reversed");
451 obj->GetAttribute(
"DoubleList", value);
458 "Incorrect value in doublesvec");
461 const std::vector<int> ints = {-1, 0, 1, 2, 3};
468 obj->GetAttribute(
"IntegerVector", value);
476 "Incorrect value in intvec");
479 std::string intVecPairString(
"0 1,2,3; 1 0; 2 0,1");
481 obj->SetAttribute(
"IntVecPairVec",
StringValue(intVecPairString));
488 obj->GetAttribute(
"IntVecPairVec", value);
493 auto reslistIt = reslist.
begin();
497 "Incorrect number of integer values in first pair");
502 "Incorrect number of integer values in second pair");
507 "Incorrect number of integer values in third pair");
510 std::map<std::string, int> map = {{
"one", 1}, {
"two", 2}, {
"three", 3}};
511 obj->SetAttribute(
"MapStringInt",
516 obj->GetAttribute(
"MapStringInt", value);
520 std::map<std::string, int> mapstrint;
521 auto lst = value.Get();
522 for (
const auto& l : lst)
524 mapstrint[l.first] = l.second;
528 auto iter = map.begin();
529 for (
const auto& v : mapstrint)
531 bool valCheck = v.first == (*iter).first && v.second == (*iter).second;
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.
AttributeValue implementation for Pair.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
void AddTestCase(TestCase *testCase, Duration duration=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< AttributeChecker > MakeAttributeContainerChecker()
Make uninitialized AttributeContainerChecker using explicit types.
Ptr< AttributeChecker > MakePairChecker()
Make a PairChecker without abscissa and ordinate AttributeCheckers.
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.