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 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/core-module.h"
8
9/**
10 * \defgroup string-value-formatting StringValue parsing tests
11 * Check that StringValue parses complex values correctly.
12 */
13
14/**
15 * \file
16 * \ingroup core-examples
17 * \ingroup string-value-formatting
18 * Check that StringValue parses complex values correctly.
19 * \todo This should really be turned into a TestSuite
20 */
21
22using namespace ns3;
23
24NS_LOG_COMPONENT_DEFINE("TestStringValueFormatting");
25
26namespace
27{
28
29/**
30 * \ingroup string-value-formatting
31 *
32 * StringValue formatting example test object.
33 *
34 * We use an attribute containing a pointer to a random variable
35 * to stress StringValue.
36 */
38{
39 public:
40 /**
41 * \brief Register this type and get the TypeId.
42 * \return the object TypeId
43 */
44 static TypeId GetTypeId();
45 /** Default constructor */
47 /**
48 * Get the test variable
49 * \returns the test variable
50 */
51 Ptr<RandomVariableStream> GetTestVariable() const;
52
53 private:
55};
56
58
61{
62 static TypeId tid =
63 TypeId("ns3::FormattingTestObject")
65 .AddConstructor<FormattingTestObject>()
66 .AddAttribute("OnTime",
67 "A RandomVariableStream used to pick the duration of the 'On' state.",
68 StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
71 return tid;
72}
73
77
80{
81 return m_testVariable;
82}
83
84/**
85 * \ingroup string-value-formatting
86 *
87 * StringValue formatting example test helper class.
88 */
90{
91 public:
92 /** Default constructor */
94 /**
95 * Set an attribute by name
96 * \param name the attribute
97 * \param value the attribute value
98 */
99 void SetAttribute(std::string name, const AttributeValue& value);
100 /**
101 * Create an Object as configured by SetAttribute
102 * \returns the newly created Object
103 */
104 Ptr<Object> CreateFromFactory();
105
106 private:
107 ObjectFactory m_factory; //!< Object factory
108};
109
114
115void
117{
118 m_factory.Set(name, value);
119}
120
123{
124 return m_factory.Create();
125}
126
127} // unnamed namespace
128
129int
130main(int argc, char* argv[])
131{
132 // CreateObject parsing
134 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable"));
135 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.]"));
136 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
137 obj->SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
138
139 Ptr<RandomVariableStream> rvStream = obj->GetTestVariable();
140 // Either GetObject () or DynamicCast may be used to get subclass pointer
141 Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable>();
142 NS_ASSERT(uniformStream);
143
144 // Check that the last setting of Min to 50 and Max to 100 worked
145 DoubleValue val;
146 uniformStream->GetAttribute("Min", val);
147 NS_ASSERT_MSG(val.Get() == 50, "Minimum not set to 50");
148 uniformStream->GetAttribute("Max", val);
149 NS_ASSERT_MSG(val.Get() == 100, "Maximum not set to 100");
150
151 // The following malformed values should result in an error exit
152 // if uncommented
153
154 // Attribute doesn't exist
155 // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
156 // Missing right bracket
157 // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0."));
158 // Comma delimiter fails
159 // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
160 // Incomplete specification
161 // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=]"));
162 // Incomplete specification
163 // obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max]"));
164
165 // ObjectFactory parsing
166 FormattingTestObjectHelper formattingHelper;
167 formattingHelper.SetAttribute("OnTime",
168 StringValue("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
169 // Attribute doesn't exist
170 // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
171 // Missing right bracket
172 // formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
173 // Comma delimiter fails
174 // formattingHelper.SetAttribute ("OnTime", StringValue
175 // ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
176 // Incomplete specification
177 // formattingHelper.SetAttribute ("OnTime", StringValue
178 // ("ns3::UniformRandomVariable[Min=30.|Max=]"));
179 // Incomplete specification
180 // formattingHelper.SetAttribute ("OnTime", StringValue
181 // ("ns3::UniformRandomVariable[Min=30.|Max]"));
182
183 // verify that creation occurs correctly
184 Ptr<Object> outputObj = formattingHelper.CreateFromFactory();
186 NS_ASSERT_MSG(fto, "object creation failed");
187 rvStream = fto->GetTestVariable();
188 uniformStream = rvStream->GetObject<UniformRandomVariable>();
189 NS_ASSERT(uniformStream);
190 // Check that the last setting of Min to 30 and Max to 60 worked
191 uniformStream->GetAttribute("Min", val);
192 NS_ASSERT_MSG(val.Get() == 30, "Minimum not set to 30");
193 uniformStream->GetAttribute("Max", val);
194 NS_ASSERT_MSG(val.Get() == 60, "Maximum not set to 60");
195
196 return 0;
197}
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:59
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
double Get() const
Definition double.cc:26
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:78
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:55
#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:75
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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580