20 #include "ns3/object.h"
21 #include "ns3/boolean.h"
22 #include "ns3/integer.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/config.h"
26 #include "ns3/string.h"
27 #include "ns3/random-variable-stream.h"
28 #include "ns3/double.h"
29 #include "ns3/object-vector.h"
30 #include "ns3/object-map.h"
31 #include "ns3/traced-value.h"
32 #include "ns3/callback.h"
33 #include "ns3/trace-source-accessor.h"
34 #include "ns3/pointer.h"
35 #include "ns3/object-factory.h"
88 .HideFromDocumentation ()
89 .AddAttribute (
"TestBoolName",
"help text",
92 MakeBooleanChecker ())
93 .AddAttribute (
"TestBoolA",
"help text",
97 MakeBooleanChecker ())
98 .AddAttribute (
"TestInt16",
"help text",
101 MakeIntegerChecker<int16_t> ())
102 .AddAttribute (
"TestInt16WithBounds",
"help text",
105 MakeIntegerChecker<int16_t> (-5, 10))
106 .AddAttribute (
"TestInt16SetGet",
"help text",
110 MakeIntegerChecker<int16_t> ())
111 .AddAttribute (
"TestUint8",
"help text",
114 MakeUintegerChecker<uint8_t> ())
115 .AddAttribute (
"TestEnum",
"help text",
121 .AddAttribute (
"TestRandom",
"help text",
122 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
124 MakePointerChecker <RandomVariableStream>())
125 .AddAttribute (
"TestFloat",
"help text",
128 MakeDoubleChecker<float> ())
129 .AddAttribute (
"TestVector1",
"help text",
132 MakeObjectVectorChecker<Derived> ())
133 .AddAttribute (
"TestVector2",
"help text",
137 MakeObjectVectorChecker<Derived> ())
138 .AddAttribute (
"TestMap1",
"help text",
141 MakeObjectMapChecker<Derived> ())
142 .AddAttribute (
"IntegerTraceSource1",
"help text",
145 MakeIntegerChecker<int8_t> ())
146 .AddAttribute (
"IntegerTraceSource2",
"help text",
150 MakeIntegerChecker<int8_t> ())
151 .AddAttribute (
"UIntegerTraceSource",
"help text",
154 MakeIntegerChecker<uint8_t> ())
155 .AddAttribute (
"DoubleTraceSource",
"help text",
158 MakeDoubleChecker<double> ())
159 .AddAttribute (
"BoolTraceSource",
"help text",
162 MakeBooleanChecker ())
163 .AddAttribute (
"EnumTraceSource",
"help text",
167 .AddAttribute (
"ValueClassSource",
"help text",
170 MakeValueClassTestChecker ())
171 .AddTraceSource (
"Source1",
"help test",
173 .AddTraceSource (
"Source2",
"help text",
175 .AddTraceSource (
"ValueSource",
"help text",
177 .AddAttribute (
"Pointer",
"help text",
180 MakePointerChecker<Derived> ())
181 .AddAttribute (
"PointerInitialized",
"help text",
184 MakePointerChecker<Derived> ())
185 .AddAttribute (
"PointerInitialized2",
"help text",
188 MakePointerChecker<Derived> ())
189 .AddAttribute (
"Callback",
"help text",
192 MakeCallbackChecker ())
233 std::map <uint32_t, Ptr<Derived> >
m_map1;
254 template <
typename T>
262 virtual void DoRun (
void);
267 template <
typename T>
273 template <
typename T>
278 template <
typename T>
bool
281 std::string attributeName,
282 std::string expectedString,
292 bool ok2 = stringValue.
Get () == expectedString;
298 bool ok4 = expectedValue.Get () == actualValue.Get ();
300 return ok1 && ok2 && ok3 && ok4;
312 p = CreateObject<AttributeObjectTest> ();
320 p = CreateObject<AttributeObjectTest> ();
323 ok = CheckGetCodePaths (p,
"TestBoolName",
"true",
BooleanValue (
true));
332 p = CreateObject<AttributeObjectTest> ();
335 ok = CheckGetCodePaths (p,
"TestBoolName",
"false",
BooleanValue (
false));
344 ok = CheckGetCodePaths (p,
"TestBoolName",
"true",
BooleanValue (
true));
345 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
353 ok = CheckGetCodePaths (p,
"TestBoolName",
"false",
BooleanValue (
false));
354 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
359 p = CreateObject<AttributeObjectTest> ();
371 ok = CheckGetCodePaths (p,
"TestBoolA",
"true",
BooleanValue (
true));
372 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (getter/setter) via StringValue");
380 ok = CheckGetCodePaths (p,
"TestBoolA",
"false",
BooleanValue (
false));
381 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (getter/setter) via StringValue");
390 p = CreateObject<AttributeObjectTest> ();
397 ok = CheckGetCodePaths (p,
"TestInt16",
"-2",
IntegerValue (-2));
406 ok = CheckGetCodePaths (p,
"TestInt16",
"-5",
IntegerValue (-5));
407 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
415 ok = CheckGetCodePaths (p,
"TestInt16",
"2",
IntegerValue (2));
416 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
424 ok = CheckGetCodePaths (p,
"TestInt16",
"-32768",
IntegerValue (-32768));
425 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (most negative) via StringValue");
432 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to -32769");
434 ok = CheckGetCodePaths (p,
"TestInt16",
"-32768",
IntegerValue (-32768));
443 ok = CheckGetCodePaths (p,
"TestInt16",
"32767",
IntegerValue (32767));
444 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (most positive) via StringValue");
451 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to 32768");
453 ok = CheckGetCodePaths (p,
"TestInt16",
"32767",
IntegerValue (32767));
463 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"10",
IntegerValue (10));
464 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (positive limit) via StringValue");
470 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 11");
472 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"10",
IntegerValue (10));
481 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"-5",
IntegerValue (-5));
482 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (negative limit) via StringValue");
488 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -6");
490 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"-5",
IntegerValue (-5));
500 p = CreateObject<AttributeObjectTest> ();
507 ok = CheckGetCodePaths (p,
"TestUint8",
"1",
UintegerValue (1));
516 ok = CheckGetCodePaths (p,
"TestUint8",
"0",
UintegerValue (0));
517 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
525 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
526 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (positive limit) via UintegerValue");
535 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
545 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
546 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
553 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to 256");
555 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
562 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to -1");
571 p = CreateObject<AttributeObjectTest> ();
578 ok = CheckGetCodePaths (p,
"TestFloat",
"-1.1",
DoubleValue ((
float)-1.1));
587 ok = CheckGetCodePaths (p,
"TestFloat",
"2.3",
DoubleValue ((
float)2.3));
588 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via DoubleValue");
597 p = CreateObject<AttributeObjectTest> ();
614 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via EnumValue");
623 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
663 virtual void DoRun (
void);
683 p = CreateObject<AttributeObjectTest> ();
689 ok = p->SetAttributeFailSafe (
"TestRandom",
StringValue (
"ns3::UniformRandomVariable[Min=0.,Max=1.]"));
698 ok = p->SetAttributeFailSafe (
"TestRandom",
StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"));
714 virtual void DoRun (
void);
728 p = CreateObject<AttributeObjectTest> ();
735 p->GetAttribute (
"TestVector1", vector);
747 p->GetAttribute (
"TestVector1", vector);
765 p->GetAttribute (
"TestVector1", vector);
779 virtual void DoRun (
void);
793 p = CreateObject<AttributeObjectTest> ();
800 p->GetAttribute (
"TestMap1", map);
812 p->GetAttribute (
"TestMap1", map);
830 p->GetAttribute (
"TestMap1", map);
845 virtual void DoRun (
void);
860 p = CreateObject<AttributeObjectTest> ();
867 p->GetAttribute (
"IntegerTraceSource1", iv);
873 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (5));
876 p->GetAttribute (
"IntegerTraceSource1", iv);
882 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (127));
885 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (128));
886 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 128");
888 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (-128));
891 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (-129));
892 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -129");
898 p->GetAttribute (
"IntegerTraceSource2", iv);
904 ok = p->SetAttributeFailSafe (
"IntegerTraceSource2",
IntegerValue (5));
907 p->GetAttribute (
"IntegerTraceSource2", iv);
913 ok = p->SetAttributeFailSafe (
"IntegerTraceSource2",
IntegerValue (127));
916 ok = p->SetAttributeFailSafe (
"IntegerTraceSource2",
IntegerValue (128));
917 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 128");
919 ok = p->SetAttributeFailSafe (
"IntegerTraceSource2",
IntegerValue (-128));
922 ok = p->SetAttributeFailSafe (
"IntegerTraceSource2",
IntegerValue (-129));
923 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -129");
937 virtual void DoRun (
void);
954 p = CreateObject<AttributeObjectTest> ();
963 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (-1));
973 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not TraceConnectWithoutContext() \"Source1\" to NodifySource1()");
980 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (0));
990 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not TraceConnectWithoutContext() \"Source1\" to NodifySource1()");
992 ok = p->SetAttributeFailSafe (
"IntegerTraceSource1",
IntegerValue (1));
1009 virtual void DoRun (
void);
1027 p = CreateObject<AttributeObjectTest> ();
1043 p->InvokeCb (1.0, -5, 0.0);
1057 p->InvokeCb (1.0, -5, 0.0);
1068 p->InvokeCb (-1.0, -5, 0.0);
1083 virtual void DoRun (
void);
1101 p = CreateObject<AttributeObjectTest> ();
1110 p->GetAttribute (
"Pointer", ptr);
1112 NS_TEST_ASSERT_MSG_EQ (derived, 0,
"Unexpectedly found non-null pointer in newly initialized PointerValue Attribute");
1118 derived = Create<Derived> ();
1119 ok = p->SetAttributeFailSafe (
"Pointer",
PointerValue (derived));
1120 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not SetAttributeFailSafe() a PointerValue of the correct type");
1126 p->GetAttribute (
"Pointer", ptr);
1136 p->GetAttribute (
"Pointer", ptr);
1138 NS_TEST_ASSERT_MSG_EQ (storedBase, stored,
"Retreived Ptr<Object> does not match stored Ptr<Derived>");
1144 p->GetAttribute (
"Pointer", ptr);
1146 NS_TEST_ASSERT_MSG_EQ (x, 0,
"Unexpectedly retreived unrelated Ptr<type> from stored Ptr<Derived>");
1152 p->GetAttribute (
"PointerInitialized", ptr);
1156 p2->GetAttribute (
"PointerInitialized", ptr2);
1158 NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr2,
"ptr and ptr2 both have PointerInitialized pointing to the same object");
1160 p2->GetAttribute (
"PointerInitialized", ptr3);
1162 NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr3,
"ptr and ptr3 both have PointerInitialized pointing to the same object");
1168 factory.
SetTypeId (
"ns3::AttributeObjectTest");
1169 factory.
Set (
"PointerInitialized",
StringValue (
"ns3::Derived"));
1176 aotPtr->GetAttribute (
"PointerInitialized", ptr4);
1179 aotPtr2->GetAttribute (
"PointerInitialized", ptr5);
1181 NS_TEST_ASSERT_MSG_NE (storedPtr4, storedPtr5,
"aotPtr and aotPtr2 are unique, but their Derived member is not");
1201 virtual void DoRun (
void);
1221 p = CreateObject<AttributeObjectTest> ();
1241 p->InvokeCbValue (2);
1246 ok = p->SetAttributeFailSafe (
"Callback", cbValue);
1252 p->InvokeCbValue (2);
1255 ok = p->SetAttributeFailSafe (
"Callback",
CallbackValue (MakeNullCallback<void,int8_t> ()));
1261 p->InvokeCbValue (3);