A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
test-string-value-formatting.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Tom Henderson
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include "ns3/core-module.h"
19
20/**
21 * \defgroup string-value-formatting StringValue parsing tests
22 * Check that StringValue parses complex values correctly.
23 */
24
25/**
26 * \file
27 * \ingroup core-examples
28 * \ingroup string-value-formatting
29 * Check that StringValue parses complex values correctly.
30 * \todo This should really be turned into a TestSuite
31 */
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE("TestStringValueFormatting");
36
37namespace
38{
39
40/**
41 * \ingroup string-value-formatting
42 *
43 * StringValue formatting example test object.
44 *
45 * We use an attribute containing a pointer to a random variable
46 * to stress StringValue.
47 */
49{
50 public:
51 /**
52 * \brief Register this type and get the TypeId.
53 * \return the object TypeId
54 */
55 static TypeId GetTypeId();
56 /** Default constructor */
58 /**
59 * Get the test variable
60 * \returns the test variable
61 */
62 Ptr<RandomVariableStream> GetTestVariable() const;
63
64 private:
66};
67
69
72{
73 static TypeId tid =
74 TypeId("ns3::FormattingTestObject")
76 .AddConstructor<FormattingTestObject>()
77 .AddAttribute("OnTime",
78 "A RandomVariableStream used to pick the duration of the 'On' state.",
79 StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
81 MakePointerChecker<RandomVariableStream>());
82 return tid;
83}
84
86{
87}
88
91{
92 return m_testVariable;
93}
94
95/**
96 * \ingroup string-value-formatting
97 *
98 * StringValue formatting example test helper class.
99 */
101{
102 public:
103 /** Default constructor */
105 /**
106 * Set an attribute by name
107 * \param name the attribute
108 * \param value the attribute value
109 */
110 void SetAttribute(std::string name, const AttributeValue& value);
111 /**
112 * Create an Object as configured by SetAttribute
113 * \returns the newly created Object
114 */
115 Ptr<Object> CreateFromFactory();
116
117 private:
118 ObjectFactory m_factory; //!< Object factory
119};
120
122{
123 m_factory.SetTypeId(FormattingTestObject::GetTypeId());
124}
125
126void
128{
129 m_factory.Set(name, value);
130}
131
134{
135 return m_factory.Create();
136}
137
138} // unnamed namespace
139
140int
141main(int argc, char* argv[])
142{
143 // CreateObject parsing
144 Ptr<FormattingTestObject> obj = CreateObject<FormattingTestObject>();
145 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable"));
146 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.]"));
147 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
148 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
149
150 Ptr<RandomVariableStream> rvStream = obj->GetTestVariable();
151 // Either GetObject () or DynamicCast may be used to get subclass pointer
152 Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable>();
153 NS_ASSERT(uniformStream);
154
155 // Check that the last setting of Min to 50 and Max to 100 worked
156 DoubleValue val;
157 uniformStream->GetAttribute("Min", val);
158 NS_ASSERT_MSG(val.Get() == 50, "Minimum not set to 50");
159 uniformStream->GetAttribute("Max", val);
160 NS_ASSERT_MSG(val.Get() == 100, "Maximum not set to 100");
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",
179 StringValue("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
180 // Attribute doesn't exist
181 // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
182 // Missing right bracket
183 // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
184 // Comma delimiter fails
185 // formattingHelper.SetAttribute ("OnTime", StringValue
186 // ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
187 // Incomplete specification
188 // formattingHelper.SetAttribute ("OnTime", StringValue
189 // ("ns3::UniformRandomVariable[Min=30.|Max=]"));
190 // Incomplete specification
191 // formattingHelper.SetAttribute ("OnTime", StringValue
192 // ("ns3::UniformRandomVariable[Min=30.|Max]"));
193
194 // verify that creation occurs correctly
195 Ptr<Object> outputObj = formattingHelper.CreateFromFactory();
196 Ptr<FormattingTestObject> fto = DynamicCast<FormattingTestObject>(outputObj);
197 NS_ASSERT_MSG(fto, "object creation failed");
198 rvStream = fto->GetTestVariable();
199 uniformStream = rvStream->GetObject<UniformRandomVariable>();
200 NS_ASSERT(uniformStream);
201 // Check that the last setting of Min to 30 and Max to 60 worked
202 uniformStream->GetAttribute("Min", val);
203 NS_ASSERT_MSG(val.Get() == 30, "Minimum not set to 30");
204 uniformStream->GetAttribute("Max", val);
205 NS_ASSERT_MSG(val.Get() == 60, "Maximum not set to 60");
206
207 return 0;
208}
void SetAttribute(std::string name, const AttributeValue &value)
Set an attribute by name.
Ptr< Object > CreateFromFactory()
Create an Object as configured by SetAttribute.
Ptr< RandomVariableStream > GetTestVariable() const
Get the test variable.
Hold a value for an Attribute.
Definition: attribute.h:70
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
double Get() const
Definition: double.cc:37
Instantiate subclasses of ns3::Object.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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:66
#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:86
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.