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
34using namespace ns3;
35
36NS_LOG_COMPONENT_DEFINE ("TestStringValueFormatting");
37
38namespace {
39
49{
50public:
55 static TypeId GetTypeId (void);
62 Ptr<RandomVariableStream> GetTestVariable (void) const;
63
64private:
66};
67
69
71FormattingTestObject::GetTypeId (void)
72{
73 static TypeId tid = TypeId ("ns3::FormattingTestObject")
74 .SetParent<Object> ()
75 .AddConstructor<FormattingTestObject> ()
76 .AddAttribute ("OnTime", "A RandomVariableStream used to pick the duration of the 'On' state.",
77 StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
78 MakePointerAccessor (&FormattingTestObject::m_testVariable),
79 MakePointerChecker <RandomVariableStream>())
80 ;
81 return tid;
82}
83
84FormattingTestObject::FormattingTestObject ()
85{}
86
88FormattingTestObject::GetTestVariable (void) const
89{
90 return m_testVariable;
91}
92
99{
100public:
108 void SetAttribute (std::string name, const AttributeValue &value);
113 Ptr<Object> CreateFromFactory (void);
114
115private:
117};
118
119FormattingTestObjectHelper::FormattingTestObjectHelper ()
120{
121 m_factory.SetTypeId (FormattingTestObject::GetTypeId ());
122}
123
124void
125FormattingTestObjectHelper::SetAttribute (std::string name, const AttributeValue &value)
126{
127 m_factory.Set (name, value);
128}
129
131FormattingTestObjectHelper::CreateFromFactory (void)
132{
133 return m_factory.Create ();
134}
135
136} // unnamed namespace
137
138
139int
140main (int argc, char *argv[])
141{
142 // CreateObject parsing
143 Ptr<FormattingTestObject> obj = CreateObject<FormattingTestObject> ();
144 obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable"));
145 obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.]"));
146 obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
147 obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
148
149 Ptr<RandomVariableStream> rvStream = obj->GetTestVariable ();
150 // Either GetObject () or DynamicCast may be used to get subclass pointer
151 Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable> ();
152 NS_ASSERT (uniformStream);
153
154 // Check that the last setting of Min to 50 and Max to 100 worked
155 DoubleValue val;
156 uniformStream->GetAttribute ("Min", val);
157 NS_ASSERT_MSG (val.Get () == 50, "Minimum not set to 50");
158 uniformStream->GetAttribute ("Max", val);
159 NS_ASSERT_MSG (val.Get () == 100, "Maximum not set to 100");
160
161
162 // The following malformed values should result in an error exit
163 // if uncommented
164
165 // Attribute doesn't exist
166 //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
167 // Missing right bracket
168 //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0."));
169 // Comma delimiter fails
170 //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
171 // Incomplete specification
172 //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=]"));
173 // Incomplete specification
174 //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max]"));
175
176 // ObjectFactory parsing
177 FormattingTestObjectHelper formattingHelper;
178 formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
179 // Attribute doesn't exist
180 //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
181 // Missing right bracket
182 //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
183 // Comma delimiter fails
184 //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
185 // Incomplete specification
186 //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=]"));
187 // Incomplete specification
188 //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max]"));
189
190 // verify that creation occurs correctly
191 Ptr<Object> outputObj = formattingHelper.CreateFromFactory ();
192 Ptr<FormattingTestObject> fto = DynamicCast<FormattingTestObject> (outputObj);
193 NS_ASSERT_MSG (fto, "object creation failed");
194 rvStream = fto->GetTestVariable ();
195 uniformStream = rvStream->GetObject<UniformRandomVariable> ();
196 NS_ASSERT (uniformStream);
197 // Check that the last setting of Min to 30 and Max to 60 worked
198 uniformStream->GetAttribute ("Min", val);
199 NS_ASSERT_MSG (val.Get () == 30, "Minimum not set to 30");
200 uniformStream->GetAttribute ("Max", val);
201 NS_ASSERT_MSG (val.Get () == 60, "Maximum not set to 60");
202}
Hold a value for an Attribute.
Definition: attribute.h:69
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
double Get(void) const
Definition: double.cc:35
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:294
Instantiate subclasses of ns3::Object.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
A base class which provides memory management and object aggregation.
Definition: object.h:88
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Hold variables of type string.
Definition: string.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
The uniform distribution Random Number Generator (RNG).
#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_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.