View | Details | Raw Unified | Return to bug 2447
Collapse All | Expand All

(-)5f5bfe56c5b9 (+151 lines)
Added Link Here 
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
25
class FormattingTestObject : public Object
26
{
27
public:
28
  static TypeId GetTypeId (void);
29
  FormattingTestObject ();
30
  Ptr<RandomVariableStream> GetTestVariable (void) const;
31
private:
32
  Ptr<RandomVariableStream> m_testVariable;
33
};
34
35
NS_OBJECT_ENSURE_REGISTERED (FormattingTestObject);
36
37
TypeId
38
FormattingTestObject::GetTypeId (void)
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]"),
45
                   MakePointerAccessor (&FormattingTestObject::m_testVariable),
46
                   MakePointerChecker <RandomVariableStream>())
47
  ;
48
  return tid;
49
}
50
51
FormattingTestObject::FormattingTestObject ()
52
{
53
}
54
55
Ptr<RandomVariableStream>
56
FormattingTestObject::GetTestVariable (void) const
57
{
58
  return m_testVariable;
59
}
60
61
class FormattingTestObjectHelper
62
{
63
public:
64
  FormattingTestObjectHelper ();
65
  void SetAttribute (std::string name, const AttributeValue &value);
66
  Ptr<Object> CreateFromFactory (void);
67
private:
68
  ObjectFactory m_factory; 
69
};
70
71
FormattingTestObjectHelper::FormattingTestObjectHelper ()
72
{
73
  m_factory.SetTypeId (FormattingTestObject::GetTypeId ());
74
}
75
76
void
77
FormattingTestObjectHelper::SetAttribute (std::string name, const AttributeValue &value)
78
{
79
  m_factory.Set (name, value);
80
}
81
82
Ptr<Object>
83
FormattingTestObjectHelper::CreateFromFactory (void)
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
}
(-)a/src/core/examples/wscript (+3 lines)
 Lines 37-42    Link Here 
37
                                 ['core'])
37
                                 ['core'])
38
    obj.source = 'hash-example.cc'
38
    obj.source = 'hash-example.cc'
39
39
40
    obj = bld.create_ns3_program('test-string-value-formatting', ['core'])
41
    obj.source = 'test-string-value-formatting.cc'
42
40
    if bld.env['ENABLE_THREADING'] and bld.env["ENABLE_REAL_TIME"]:
43
    if bld.env['ENABLE_THREADING'] and bld.env["ENABLE_REAL_TIME"]:
41
        obj = bld.create_ns3_program('main-test-sync', ['network'])
44
        obj = bld.create_ns3_program('main-test-sync', ['network'])
42
        obj.source = 'main-test-sync.cc'
45
        obj.source = 'main-test-sync.cc'
(-)a/src/core/test/examples-to-run.py (+1 lines)
 Lines 14-19    Link Here 
14
    ("main-ptr", "True", "True"),
14
    ("main-ptr", "True", "True"),
15
    ("main-random-variable", "True", "False"),
15
    ("main-random-variable", "True", "False"),
16
    ("sample-random-variable", "True", "True"),
16
    ("sample-random-variable", "True", "True"),
17
    ("test-string-value-formatting", "True", "True"),
17
]
18
]
18
19
19
# A list of Python examples to run in order to ensure that they remain
20
# A list of Python examples to run in order to ensure that they remain

Return to bug 2447