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>
86 std::map<std::string, int>
m_map;
102 static TypeId tid =
TypeId (
"ns3::AttributeContainerObject")
104 .SetGroupName(
"Test")
106 .AddAttribute (
"DoubleList",
"List of doubles",
109 MakeAttributeContainerChecker<DoubleValue> (MakeDoubleChecker<double> ()))
110 .AddAttribute (
"IntegerVector",
"Vector of integers",
114 MakeAttributeContainerChecker<IntegerValue> (MakeIntegerChecker<int> ()))
115 .AddAttribute (
"MapStringInt",
"Map of strings to ints",
120 MakePairChecker<StringValue, IntegerValue> (
MakeStringChecker (), MakeIntegerChecker<int> ())))
128 m_doublelist.reverse ();
129 std::vector<int> tmp;
130 std::copy_backward (m_intvec.begin (), m_intvec.end (), tmp.begin ());
137 os <<
"AttributeContainerObject: ";
141 if (!
first) os <<
", ";
157 template <
class A,
class B,
class C,
class D>
161 return x.first == y.first &&
x.second == y.second;
176 virtual void DoRun ();
180 :
TestCase (
"test instantiation, initialization, access")
189 std::list<double> ref = {1.0, 2.1, 3.145269};
194 auto aciter = ac.
Begin ();
195 for (
auto rend = ref.end (),
196 riter= ref.begin (); riter != rend; ++riter)
206 std::vector<int> ref = {-2, 3, 10, -1042};
211 auto aciter = ac.
Begin ();
212 for (
auto rend = ref.end (),
213 riter= ref.begin (); riter != rend; ++riter)
223 auto ref = {
"one",
"two",
"three"};
227 auto aciter = ac.
Begin ();
238 auto ref = {
"one",
"two",
"three"};
242 auto aciter = ac.
Begin ();
254 std::map<std::string, int64_t> ref = { {
"one", 1}, {
"two", 2}, {
"three", 3}};
258 auto aciter = ac.
Begin ();
282 virtual void DoRun (
void);
286 :
TestCase (
"test serialization and deserialization")
296 std::string doubles =
"1.0001, 20.53, -102.3";
300 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
301 acchecker->SetItemChecker (MakeDoubleChecker<double> ());
306 std::string canonical = doubles;
307 canonical.erase (std::remove (canonical.begin (), canonical.end (),
' '), canonical.end ());
313 std::string ints =
"1, 2, -3, -4";
317 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
318 acchecker->SetItemChecker (MakeIntegerChecker<int> ());
323 std::string canonical = ints;
324 canonical.erase (std::remove (canonical.begin (), canonical.end (),
' '), canonical.end ());
329 std::string strings =
"this is a sentence with words";
333 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
339 std::string canonical = strings;
344 std::string pairs =
"one 1,two 2,three 3";
347 auto acchecker = DynamicCast<AttributeContainerChecker> (checker);
348 acchecker->SetItemChecker (MakePairChecker <StringValue, IntegerValue> (
354 std::string canonical = pairs;
372 virtual void DoRun (
void);
376 :
TestCase (
"test attribute set and get")
386 std::ostringstream oss;
388 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: ",
"DoubleList initialized incorrectly");
391 std::list<double> doubles = {1.1, 2.22, 3.333};
394 std::ostringstream oss;
396 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: 1.1, 2.22, 3.333",
"DoubleList incorrectly set");
401 std::ostringstream oss;
403 NS_TEST_ASSERT_MSG_EQ (oss.str (),
"AttributeContainerObject: 3.333, 2.22, 1.1",
"DoubleList incorrectly reversed");
407 obj->GetAttribute (
"DoubleList", value);
412 auto iter = doubles.rbegin ();
413 for (
auto d: doublevec)
420 std::vector<int> ints = {-1, 0, 1, 2, 3};
427 obj->GetAttribute (
"IntegerVector", value);
432 auto iter = ints.begin ();
433 for (
auto d: intlist)
440 std::map<std::string, int> map = { {
"one", 1}, {
"two", 2}, {
"three", 3}};
445 obj->GetAttribute (
"MapStringInt", value);
449 std::map<std::string, int> mapstrint;
450 auto lst = value.Get ();
451 for (
auto l: lst) mapstrint[l.first] = l.second;
454 auto iter = map.begin ();
455 for (
auto v: mapstrint)
475 :
TestSuite (
"attribute-container-test-suite", UNIT)
static AttributeContainerTestSuite g_attributeContainerTestSuite
Static variable for test initialization.
Attribute container object.
std::vector< int > m_intvec
Vector of ints.
virtual ~AttributeContainerObject()
AttributeContainerObject()
std::list< double > m_doublelist
List of doubles.
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.
Attribute serialization and deserialization TestCase.
virtual ~AttributeContainerSerializationTestCase()
AttributeContainerSerializationTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
Attribute set and get TestCase.
AttributeContainerSetGetTestCase()
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual ~AttributeContainerSetGetTestCase()
Test AttributeContainer instantiation, initialization, access.
virtual ~AttributeContainerTestCase()
AttributeContainerTestCase()
virtual void DoRun()
Implementation to actually run this TestCase.
Attribute attribute container TestCase.
AttributeContainerTestSuite()
A container for one type of attribute.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Iterator End(void)
NS3-style ending of container.
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
size_type GetN(void) const
NS3-style Number of items.
size_type size(void) const
STL-style number of items in container.
Iterator Begin(void)
NS3-style beginning of container.
C< item_type > result_type
Type of container returned.
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(void)
#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)
list x
Random number samples.