diff --git a/src/core/test/attribute-test-suite.cc b/src/core/test/attribute-test-suite.cc index e0f7e37c8..a5e45c1aa 100644 --- a/src/core/test/attribute-test-suite.cc +++ b/src/core/test/attribute-test-suite.cc @@ -31,6 +31,7 @@ #include "ns3/callback.h" #include "ns3/trace-source-accessor.h" #include "ns3/pointer.h" +#include "ns3/object-factory.h" namespace ns3 { @@ -61,12 +62,16 @@ class Derived : public Object public: static TypeId GetTypeId (void) { static TypeId tid = TypeId ("ns3::Derived") + .AddConstructor () .SetParent () ; return tid; } + Derived () {} }; +NS_OBJECT_ENSURE_REGISTERED (Derived); + class AttributeObjectTest : public Object { public: @@ -77,6 +82,7 @@ public: }; static TypeId GetTypeId (void) { static TypeId tid = TypeId ("ns3::AttributeObjectTest") + .AddConstructor () .SetParent () .HideFromDocumentation () .AddAttribute ("TestBoolName", "help text", @@ -167,6 +173,14 @@ public: PointerValue (), MakePointerAccessor (&AttributeObjectTest::m_ptr), MakePointerChecker ()) + .AddAttribute ("PointerInitialized", "help text", + StringValue("ns3::Derived"), + MakePointerAccessor (&AttributeObjectTest::m_ptrInitialized), + MakePointerChecker ()) + .AddAttribute ("PointerInitialized2", "help text", + StringValue("ns3::Derived[]"), + MakePointerAccessor (&AttributeObjectTest::m_ptrInitialized2), + MakePointerChecker ()) .AddAttribute ("Callback", "help text", CallbackValue (), MakeCallbackAccessor (&AttributeObjectTest::m_cbValue), @@ -214,6 +228,8 @@ private: TracedCallback m_cb; TracedValue m_valueSrc; Ptr m_ptr; + Ptr m_ptrInitialized; + Ptr m_ptrInitialized2; TracedValue m_uintSrc; TracedValue m_enumSrc; TracedValue m_doubleSrc; @@ -1019,6 +1035,41 @@ PointerAttributeTestCase::DoRun (void) p->GetAttribute ("Pointer", ptr); Ptr x = ptr.Get (); NS_TEST_ASSERT_MSG_EQ (x, 0, "Unexpectedly retreived unrelated Ptr from stored Ptr"); + + // + // Test whether the initialized pointers from two different objects + // point to different Derived objects + // + p->GetAttribute ("PointerInitialized", ptr); + Ptr storedPtr = ptr.Get (); + Ptr p2 = CreateObject (); + PointerValue ptr2; + p2->GetAttribute ("PointerInitialized", ptr2); + Ptr storedPtr2 = ptr2.Get (); + NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr2, "ptr and ptr2 both have PointerInitialized pointing to the same object"); + PointerValue ptr3; + p2->GetAttribute ("PointerInitialized", ptr3); + Ptr storedPtr3 = ptr3.Get (); + NS_TEST_ASSERT_MSG_NE (storedPtr, storedPtr3, "ptr and ptr3 both have PointerInitialized pointing to the same object"); + + // + // Test whether object factory creates the objects properly + // + ObjectFactory factory; + factory.SetTypeId ("ns3::AttributeObjectTest"); + factory.Set ("PointerInitialized", StringValue ("ns3::Derived")); + Ptr aotPtr = factory.Create ()->GetObject (); + NS_TEST_ASSERT_MSG_NE (aotPtr, 0, "Unable to factory.Create() a AttributeObjectTest"); + Ptr aotPtr2 = factory.Create ()->GetObject (); + NS_TEST_ASSERT_MSG_NE (aotPtr2, 0, "Unable to factory.Create() a AttributeObjectTest"); + NS_TEST_ASSERT_MSG_NE (aotPtr, aotPtr2, "factory object not creating unique objects"); + PointerValue ptr4; + aotPtr->GetAttribute ("PointerInitialized", ptr4); + Ptr storedPtr4 = ptr4.Get (); + PointerValue ptr5; + aotPtr2->GetAttribute ("PointerInitialized", ptr5); + Ptr storedPtr5 = ptr5.Get (); + NS_TEST_ASSERT_MSG_NE (storedPtr4, storedPtr5, "aotPtr and aotPtr2 are unique, but their Derived member is not"); } // ===========================================================================