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"
36 #include "ns3/nstime.h"
89 .HideFromDocumentation ()
90 .AddAttribute (
"TestBoolName",
"help text",
93 MakeBooleanChecker ())
94 .AddAttribute (
"TestBoolA",
"help text",
98 MakeBooleanChecker ())
99 .AddAttribute (
"TestInt16",
"help text",
102 MakeIntegerChecker<int16_t> ())
103 .AddAttribute (
"TestInt16WithBounds",
"help text",
106 MakeIntegerChecker<int16_t> (-5, 10))
107 .AddAttribute (
"TestInt16SetGet",
"help text",
111 MakeIntegerChecker<int16_t> ())
112 .AddAttribute (
"TestUint8",
"help text",
115 MakeUintegerChecker<uint8_t> ())
116 .AddAttribute (
"TestEnum",
"help text",
122 .AddAttribute (
"TestRandom",
"help text",
123 StringValue (
"ns3::ConstantRandomVariable[Constant=1.0]"),
125 MakePointerChecker <RandomVariableStream>())
126 .AddAttribute (
"TestFloat",
"help text",
129 MakeDoubleChecker<float> ())
130 .AddAttribute (
"TestVector1",
"help text",
133 MakeObjectVectorChecker<Derived> ())
134 .AddAttribute (
"TestVector2",
"help text",
138 MakeObjectVectorChecker<Derived> ())
139 .AddAttribute (
"TestMap1",
"help text",
142 MakeObjectMapChecker<Derived> ())
143 .AddAttribute (
"IntegerTraceSource1",
"help text",
146 MakeIntegerChecker<int8_t> ())
147 .AddAttribute (
"IntegerTraceSource2",
"help text",
151 MakeIntegerChecker<int8_t> ())
152 .AddAttribute (
"UIntegerTraceSource",
"help text",
155 MakeIntegerChecker<uint8_t> ())
156 .AddAttribute (
"DoubleTraceSource",
"help text",
159 MakeDoubleChecker<double> ())
160 .AddAttribute (
"BoolTraceSource",
"help text",
163 MakeBooleanChecker ())
164 .AddAttribute (
"EnumTraceSource",
"help text",
168 .AddAttribute (
"ValueClassSource",
"help text",
171 MakeValueClassTestChecker ())
172 .AddTraceSource (
"Source1",
"help test",
174 .AddTraceSource (
"Source2",
"help text",
176 .AddTraceSource (
"ValueSource",
"help text",
178 .AddAttribute (
"Pointer",
"help text",
181 MakePointerChecker<Derived> ())
182 .AddAttribute (
"PointerInitialized",
"help text",
185 MakePointerChecker<Derived> ())
186 .AddAttribute (
"PointerInitialized2",
"help text",
189 MakePointerChecker<Derived> ())
190 .AddAttribute (
"Callback",
"help text",
193 MakeCallbackChecker ())
194 .AddAttribute (
"TestTimeWithBounds",
"help text",
203 void AddToVector1 (
void) { m_vector1.push_back (CreateObject<Derived> ()); }
204 void AddToVector2 (
void) { m_vector2.push_back (CreateObject<Derived> ()); }
208 void InvokeCb (
double a,
int b,
float c) { m_cb (a,b,c); }
210 void InvokeCbValue (int8_t a)
212 if (!m_cbValue.IsNull ()) {
238 std::map <uint32_t, Ptr<Derived> >
m_map1;
260 template <
typename T>
268 virtual void DoRun (
void);
270 bool CheckGetCodePaths (
Ptr<Object> p, std::string attributeName, std::string expectedString, T expectedValue);
273 template <
typename T>
279 template <
typename T>
284 template <
typename T>
bool
287 std::string attributeName,
288 std::string expectedString,
298 bool ok2 = stringValue.
Get () == expectedString;
304 bool ok4 = expectedValue.Get () == actualValue.Get ();
306 return ok1 && ok2 && ok3 && ok4;
318 p = CreateObject<AttributeObjectTest> ();
326 p = CreateObject<AttributeObjectTest> ();
329 ok = CheckGetCodePaths (p,
"TestBoolName",
"true",
BooleanValue (
true));
338 p = CreateObject<AttributeObjectTest> ();
341 ok = CheckGetCodePaths (p,
"TestBoolName",
"false",
BooleanValue (
false));
350 ok = CheckGetCodePaths (p,
"TestBoolName",
"true",
BooleanValue (
true));
351 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
359 ok = CheckGetCodePaths (p,
"TestBoolName",
"false",
BooleanValue (
false));
360 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
365 p = CreateObject<AttributeObjectTest> ();
377 ok = CheckGetCodePaths (p,
"TestBoolA",
"true",
BooleanValue (
true));
378 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (getter/setter) via StringValue");
386 ok = CheckGetCodePaths (p,
"TestBoolA",
"false",
BooleanValue (
false));
387 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (getter/setter) via StringValue");
396 p = CreateObject<AttributeObjectTest> ();
403 ok = CheckGetCodePaths (p,
"TestInt16",
"-2",
IntegerValue (-2));
412 ok = CheckGetCodePaths (p,
"TestInt16",
"-5",
IntegerValue (-5));
413 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
421 ok = CheckGetCodePaths (p,
"TestInt16",
"2",
IntegerValue (2));
422 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
430 ok = CheckGetCodePaths (p,
"TestInt16",
"-32768",
IntegerValue (-32768));
431 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (most negative) via StringValue");
438 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to -32769");
440 ok = CheckGetCodePaths (p,
"TestInt16",
"-32768",
IntegerValue (-32768));
449 ok = CheckGetCodePaths (p,
"TestInt16",
"32767",
IntegerValue (32767));
450 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (most positive) via StringValue");
457 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to 32768");
459 ok = CheckGetCodePaths (p,
"TestInt16",
"32767",
IntegerValue (32767));
469 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"10",
IntegerValue (10));
470 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (positive limit) via StringValue");
476 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 11");
478 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"10",
IntegerValue (10));
487 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"-5",
IntegerValue (-5));
488 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (negative limit) via StringValue");
494 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -6");
496 ok = CheckGetCodePaths (p,
"TestInt16WithBounds",
"-5",
IntegerValue (-5));
506 p = CreateObject<AttributeObjectTest> ();
513 ok = CheckGetCodePaths (p,
"TestUint8",
"1",
UintegerValue (1));
522 ok = CheckGetCodePaths (p,
"TestUint8",
"0",
UintegerValue (0));
523 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
531 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
532 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (positive limit) via UintegerValue");
541 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
551 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
552 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
559 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to 256");
561 ok = CheckGetCodePaths (p,
"TestUint8",
"255",
UintegerValue (255));
568 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via StringValue to -1");
577 p = CreateObject<AttributeObjectTest> ();
584 ok = CheckGetCodePaths (p,
"TestFloat",
"-1.1",
DoubleValue ((
float)-1.1));
593 ok = CheckGetCodePaths (p,
"TestFloat",
"2.3",
DoubleValue ((
float)2.3));
594 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via DoubleValue");
603 p = CreateObject<AttributeObjectTest> ();
620 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via EnumValue");
629 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() via StringValue");
658 p = CreateObject<AttributeObjectTest> ();
667 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"+5000000000.0ns",
TimeValue (
Seconds (5)));
668 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (5s) via TimeValue");
673 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"+3000000000.0ns",
TimeValue (
Seconds (3)));
674 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (3s) via StringValue");
688 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"+10000000000.0ns",
TimeValue (
Seconds (10)));
689 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (positive limit) via StringValue");
697 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"+10000000000.0ns",
TimeValue (
Seconds (10)));
706 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"-5000000000.0ns",
TimeValue (
Seconds (-5)));
707 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Attribute not set properly by SetAttributeFailSafe() (negative limit) via StringValue");
715 ok = CheckGetCodePaths (p,
"TestTimeWithBounds",
"-5000000000.0ns",
TimeValue (
Seconds (-5)));
736 virtual void DoRun (
void);
756 p = CreateObject<AttributeObjectTest> ();
787 virtual void DoRun (
void);
801 p = CreateObject<AttributeObjectTest> ();
852 virtual void DoRun (
void);
866 p = CreateObject<AttributeObjectTest> ();
918 virtual void DoRun (
void);
933 p = CreateObject<AttributeObjectTest> ();
959 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 128");
965 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -129");
990 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to 128");
996 NS_TEST_ASSERT_MSG_EQ (ok,
false,
"Unexpectedly could SetAttributeFailSafe() via IntegerValue to -129");
1010 virtual void DoRun (
void);
1027 p = CreateObject<AttributeObjectTest> ();
1046 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not TraceConnectWithoutContext() \"Source1\" to NodifySource1()");
1063 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not TraceConnectWithoutContext() \"Source1\" to NodifySource1()");
1082 virtual void DoRun (
void);
1100 p = CreateObject<AttributeObjectTest> ();
1156 virtual void DoRun (
void);
1174 p = CreateObject<AttributeObjectTest> ();
1185 NS_TEST_ASSERT_MSG_EQ (derived, 0,
"Unexpectedly found non-null pointer in newly initialized PointerValue Attribute");
1191 derived = Create<Derived> ();
1193 NS_TEST_ASSERT_MSG_EQ (ok,
true,
"Could not SetAttributeFailSafe() a PointerValue of the correct type");
1211 NS_TEST_ASSERT_MSG_EQ (storedBase, stored,
"Retreived Ptr<Object> does not match stored Ptr<Derived>");
1219 NS_TEST_ASSERT_MSG_EQ (x, 0,
"Unexpectedly retreived unrelated Ptr<type> from stored Ptr<Derived>");
1229 p2->GetAttribute (
"PointerInitialized", ptr2);
1231 NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr2,
"ptr and ptr2 both have PointerInitialized pointing to the same object");
1233 p2->GetAttribute (
"PointerInitialized", ptr3);
1235 NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr3,
"ptr and ptr3 both have PointerInitialized pointing to the same object");
1241 factory.
SetTypeId (
"ns3::AttributeObjectTest");
1242 factory.
Set (
"PointerInitialized",
StringValue (
"ns3::Derived"));
1254 NS_TEST_ASSERT_MSG_NE (storedPtr4, storedPtr5,
"aotPtr and aotPtr2 are unique, but their Derived member is not");
1274 virtual void DoRun (
void);
1294 p = CreateObject<AttributeObjectTest> ();