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)