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 
21 using namespace ns3;
22 
23 NS_LOG_COMPONENT_DEFINE ("TestStringValueFormatting");
24 
26 {
27 public:
28  static TypeId GetTypeId (void);
30  Ptr<RandomVariableStream> GetTestVariable (void) const;
31 private:
33 };
34 
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::FormattingTestObject")
41  .SetParent<Object> ()
42  .AddConstructor<FormattingTestObject> ()
43  .AddAttribute ("OnTime", "A RandomVariableStream used to pick the duration of the 'On' state.",
44  StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
46  MakePointerChecker <RandomVariableStream>())
47  ;
48  return tid;
49 }
50 
52 {
53 }
54 
57 {
58  return m_testVariable;
59 }
60 
62 {
63 public:
65  void SetAttribute (std::string name, const AttributeValue &value);
66  Ptr<Object> CreateFromFactory (void);
67 private:
69 };
70 
72 {
73  m_factory.SetTypeId (FormattingTestObject::GetTypeId ());
74 }
75 
76 void
78 {
79  m_factory.Set (name, value);
80 }
81 
84 {
85  return m_factory.Create ();
86 }
87 
88 int
89 main (int argc, char *argv[])
90 {
91  // CreateObject parsing
92  Ptr<FormattingTestObject> obj = CreateObject<FormattingTestObject> ();
93  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable"));
94  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.]"));
95  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
96  obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=50.|Max=100.]"));
97 
98  Ptr<RandomVariableStream> rvStream = obj->GetTestVariable ();
99  // Either GetObject () or DynamicCast may be used to get subclass pointer
100  Ptr<UniformRandomVariable> uniformStream = rvStream->GetObject<UniformRandomVariable> ();
101  NS_ASSERT (uniformStream);
102 
103  // Check that the last setting of Min to 50 and Max to 100 worked
104  DoubleValue val;
105  uniformStream->GetAttribute ("Min", val);
106  NS_ASSERT_MSG (val.Get () == 50, "Minimum not set to 50");
107  uniformStream->GetAttribute ("Max", val);
108  NS_ASSERT_MSG (val.Get () == 100, "Maximum not set to 100");
109 
110 
111  // The following malformed values should result in an error exit
112  // if uncommented
113 
114  // Attribute doesn't exist
115  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
116  // Missing right bracket
117  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0."));
118  // Comma delimiter fails
119  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
120  // Incomplete specification
121  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=]"));
122  // Incomplete specification
123  //obj->SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max]"));
124 
125  // ObjectFactory parsing
126  FormattingTestObjectHelper formattingHelper;
127  formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=60.0]"));
128  // Attribute doesn't exist
129  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[A=0.]"));
130  // Missing right bracket
131  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30."));
132  // Comma delimiter fails
133  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.,Max=60.]"));
134  // Incomplete specification
135  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max=]"));
136  // Incomplete specification
137  //formattingHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=30.|Max]"));
138 
139  // verify that creation occurs correctly
140  Ptr<Object> outputObj = formattingHelper.CreateFromFactory ();
141  Ptr<FormattingTestObject> fto = DynamicCast<FormattingTestObject> (outputObj);
142  NS_ASSERT_MSG (fto, "object creation failed");
143  rvStream = fto->GetTestVariable ();
144  uniformStream = rvStream->GetObject<UniformRandomVariable> ();
145  NS_ASSERT (uniformStream);
146  // Check that the last setting of Min to 30 and Max to 60 worked
147  uniformStream->GetAttribute ("Min", val);
148  NS_ASSERT_MSG (val.Get () == 30, "Minimum not set to 30");
149  uniformStream->GetAttribute ("Max", val);
150  NS_ASSERT_MSG (val.Get () == 60, "Maximum not set to 60");
151 }
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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:201
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< RandomVariableStream > m_testVariable
double Get(void) const
Definition: double.cc:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetAttribute(std::string name, const AttributeValue &value)
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
#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:90
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 'double' or 'float'...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
Ptr< RandomVariableStream > GetTestVariable(void) const