A Discrete-Event Network Simulator
API
test-string-value-formatting.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Tom Henderson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/core-module.h"
20 
34 using namespace ns3;
35 
36 NS_LOG_COMPONENT_DEFINE ("TestStringValueFormatting");
37 
38 namespace {
39 
49 {
50 public:
55  static TypeId GetTypeId (void);
62  Ptr<RandomVariableStream> GetTestVariable (void) const;
63 private:
65 };
66 
68 
69 TypeId
70 FormattingTestObject::GetTypeId (void)
71 {
72  static TypeId tid = TypeId ("ns3::FormattingTestObject")
73  .SetParent<Object> ()
74  .AddConstructor<FormattingTestObject> ()
75  .AddAttribute ("OnTime", "A RandomVariableStream used to pick the duration of the 'On' state.",
76  StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
77  MakePointerAccessor (&FormattingTestObject::m_testVariable),
78  MakePointerChecker <RandomVariableStream>())
79  ;
80  return tid;
81 }
82 
83 FormattingTestObject::FormattingTestObject ()
84 {
85 }
86 
88 FormattingTestObject::GetTestVariable (void) const
89 {
90  return m_testVariable;
91 }
92 
99 {
100 public:
108  void SetAttribute (std::string name, const AttributeValue &value);
113  Ptr<Object> CreateFromFactory (void);
114 private:
116 };
117 
118 FormattingTestObjectHelper::FormattingTestObjectHelper ()
119 {
120  m_factory.SetTypeId (FormattingTestObject::GetTypeId ());
121 }
122 
123 void
124 FormattingTestObjectHelper::SetAttribute (std::string name, const AttributeValue &value)
125 {
126  m_factory.Set (name, value);
127 }
128 
130 FormattingTestObjectHelper::CreateFromFactory (void)
131 {
132  return m_factory.Create ();
133 }
134 
135 } // unnamed namespace
136 
137 
138 int
139 main (int argc, char *argv[])
140 {
141  // CreateObject parsing
142  Ptr<FormattingTestObject> obj = CreateObject<FormattingTestObject> ();
143  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable"));
144  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.]"));
145  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
146  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
147 
148  Ptr<RandomVariableStream> rvStream = obj->GetTestVariable ();
149  // Either GetObject () or DynamicCast may be used to get subclass pointer
150  Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable> ();
151  NS_ASSERT (uniformStream);
152 
153  // Check that the last setting of Min to 50 and Max to 100 worked
154  DoubleValue val;
155  uniformStream->GetAttribute ("Min", val);
156  NS_ASSERT_MSG (val.Get () == 50, "Minimum not set to 50");
157  uniformStream->GetAttribute ("Max", val);
158  NS_ASSERT_MSG (val.Get () == 100, "Maximum not set to 100");
159 
160 
161  // The following malformed values should result in an error exit
162  // if uncommented
163 
164  // Attribute doesn't exist
165  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
166  // Missing right bracket
167  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0."));
168  // Comma delimiter fails
169  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
170  // Incomplete specification
171  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=]"));
172  // Incomplete specification
173  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max]"));
174 
175  // ObjectFactory parsing
176  FormattingTestObjectHelper formattingHelper;
177  formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
178  // Attribute doesn't exist
179  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
180  // Missing right bracket
181  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
182  // Comma delimiter fails
183  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
184  // Incomplete specification
185  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=]"));
186  // Incomplete specification
187  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max]"));
188 
189  // verify that creation occurs correctly
190  Ptr<Object> outputObj = formattingHelper.CreateFromFactory ();
191  Ptr<FormattingTestObject> fto = DynamicCast<FormattingTestObject> (outputObj);
192  NS_ASSERT_MSG (fto, "object creation failed");
193  rvStream = fto->GetTestVariable ();
194  uniformStream = rvStream->GetObject<UniformRandomVariable> ();
195  NS_ASSERT (uniformStream);
196  // Check that the last setting of Min to 30 and Max to 60 worked
197  uniformStream->GetAttribute ("Min", val);
198  NS_ASSERT_MSG (val.Get () == 30, "Minimum not set to 30");
199  uniformStream->GetAttribute ("Max", val);
200  NS_ASSERT_MSG (val.Get () == 60, "Maximum not set to 60");
201 }
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
Definition: string.h:41
Hold a value for an Attribute.
Definition: attribute.h:68
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
The uniform distribution Random Number Generator (RNG).
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
double Get(void) const
Definition: double.cc:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Instantiate subclasses of ns3::Object.
A base class which provides memory management and object aggregation.
Definition: object.h:87
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915