23 #include <ns3/attribute-container.h> 24 #include <ns3/attribute-container-accessor-helper.h> 26 #include <ns3/double.h> 27 #include <ns3/integer.h> 28 #include <ns3/string.h> 30 #include <ns3/object.h> 31 #include <ns3/type-id.h> 62 std::map<std::string, int>
m_map;
78 static TypeId tid =
TypeId (
"ns3::AttributeContainerObject")
82 .AddAttribute (
"DoubleList",
"List of doubles",
85 MakeAttributeContainerChecker<DoubleValue> (MakeDoubleChecker<double> ()))
86 .AddAttribute (
"IntegerVector",
"Vector of integers",
90 MakeAttributeContainerChecker<IntegerValue> (MakeIntegerChecker<int> ()))
91 .AddAttribute (
"MapStringInt",
"Map of strings to ints",
96 MakePairChecker<StringValue, IntegerValue> (
MakeStringChecker (), MakeIntegerChecker<int> ())))
104 m_doublelist.reverse ();
105 std::vector<int> tmp;
106 std::copy_backward (m_intvec.begin (), m_intvec.end (), tmp.begin ());
113 os <<
"AttributeContainerObject: ";
117 if (!
first) os <<
", ";
126 template <
class A,
class B,
class C,
class D>
130 return x.first == y.first &&
x.second == y.second;
141 virtual void DoRun ();
145 :
TestCase (
"test instantiation, initialization, access")
154 std::list<double> ref = {1.0, 2.1, 3.145269};
159 auto aciter = ac.
Begin ();
160 for (
auto rend = ref.end (),
161 riter= ref.begin (); riter != rend; ++riter)
171 std::vector<int> ref = {-2, 3, 10, -1042};
176 auto aciter = ac.Begin ();
177 for (
auto rend = ref.end (),
178 riter= ref.begin (); riter != rend; ++riter)
188 auto ref = {
"one",
"two",
"three"};
192 auto aciter = ac.Begin ();
203 auto ref = {
"one",
"two",
"three"};
207 auto aciter = ac.
Begin ();
219 std::map<std::string, int64_t> ref = { {
"one", 1}, {
"two", 2}, {
"three", 3}};
223 auto aciter = ac.
Begin ();
243 virtual void DoRun (
void);
247 :
TestCase (
"test serialization and deserialization")
257 std::string doubles =
"1.0001, 20.53, -102.3";
261 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
262 acchecker->SetItemChecker (MakeDoubleChecker<double> ());
267 std::string canonical = doubles;
268 canonical.erase (std::remove (canonical.begin (), canonical.end (),
' '), canonical.end ());
274 std::string ints =
"1, 2, -3, -4";
278 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
279 acchecker->SetItemChecker (MakeIntegerChecker<int> ());
284 std::string canonical = ints;
285 canonical.erase (std::remove (canonical.begin (), canonical.end (),
' '), canonical.end ());
290 std::string strings =
"this is a sentence with words";
294 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
300 std::string canonical = strings;
305 std::string pairs =
"one 1,two 2,three 3";
308 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
309 acchecker->SetItemChecker (MakePairChecker <StringValue, IntegerValue> (
315 std::string canonical = pairs;
328 virtual void DoRun (
void);
332 :
TestCase (
"test attribute set and get")
342 std::ostringstream oss;
344 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: ",
"DoubleList initialized incorrectly");
347 std::list<double> doubles = {1.1, 2.22, 3.333};
350 std::ostringstream oss;
352 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: 1.1, 2.22, 3.333",
"DoubleList incorrectly set");
357 std::ostringstream oss;
359 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: 3.333, 2.22, 1.1",
"DoubleList incorrectly reversed");
363 obj->GetAttribute (
"DoubleList", value);
368 auto iter = doubles.rbegin ();
369 for (
auto d: doublevec)
376 std::vector<int> ints = {-1, 0, 1, 2, 3};
388 auto iter = ints.begin ();
389 for (
auto d: intlist)
396 std::map<std::string, int> map = { {
"one", 1}, {
"two", 2}, {
"three", 3}};
405 std::map<std::string, int> mapstrint;
406 auto lst = value.
Get ();
407 for (
auto l: lst) mapstrint[l.first] = l.second;
410 auto iter = map.begin ();
411 for (
auto v: mapstrint)
426 :
TestSuite (
"attribute-container-test-suite", UNIT)
Iterator Begin(void)
NS3-style beginning of container.
AttributeContainerTestSuite()
Smart pointer class similar to boost::intrusive_ptr.
result_type Get(void) const
Return a container of items.
Ptr< const AttributeAccessor > MakeAttributeContainerAccessor(T1 a1)
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
size_type GetN(void) const
NS3-style Number of items.
AttributeContainerSerializationTestCase()
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
AttributeContainerTestCase()
static TypeId GetTypeId()
Ptr< const AttributeChecker > MakeStringChecker(void)
AttributeContainerObject()
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
#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.
virtual void DoRun()
Implementation to actually run this TestCase.
A container for one type of attribute.
std::map< std::string, int > m_map
virtual ~AttributeContainerTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::list< double > m_doublelist
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
std::vector< int > m_intvec
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< AttributeChecker > MakeAttributeContainerChecker(const AttributeContainerValue< A, C > &value)
Make AttributeContainerChecker from AttributeContainerValue.
Iterator End(void)
NS3-style ending of container.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const
virtual ~AttributeContainerSetGetTestCase()
#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...
C< item_type > result_type
Type of container returned.
AttributeContainerSetGetTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool operator==(const EventId &a, const EventId &b)
virtual ~AttributeContainerObject()
A base class which provides memory management and object aggregation.
Hold objects of type std::pair<A, B>.
static AttributeContainerTestSuite AttributeContainerTestSuite
Static variable for test initialization.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
a unique identifier for an interface.
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
virtual ~AttributeContainerSerializationTestCase()