A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
double-probe-test-suite.cc
Go to the documentation of this file.
1
2// Include a header file from your module to test.
3#include "ns3/double-probe.h"
4#include "ns3/names.h"
5#include "ns3/nstime.h"
6#include "ns3/object.h"
7#include "ns3/random-variable-stream.h"
8#include "ns3/simulator.h"
9#include "ns3/test.h"
10#include "ns3/trace-source-accessor.h"
11#include "ns3/traced-value.h"
12#include "ns3/type-id.h"
13
14using namespace ns3;
15
16/**
17 * \ingroup stats-tests
18 *
19 * \brief Simple data emitter to check that a probe receives data.
20 */
21class SampleEmitter : public Object
22{
23 public:
24 /**
25 * \brief Get the type ID.
26 * \return The object TypeId.
27 */
28 static TypeId GetTypeId();
29
31 {
32 m_var = CreateObject<ExponentialRandomVariable>();
33 }
34
35 ~SampleEmitter() override
36 {
37 }
38
39 /// Start emission of data.
40 void Start()
41 {
42 Reschedule();
43 }
44
45 /// Reschedule a report \sa Report
47 {
51 }
52
53 /// \return the time delta of the next report.
54 double GetTime() const
55 {
56 return m_time;
57 }
58
59 /// \return a random variable, different for each reschedule.
60 double GetValue() const
61 {
62 return aux;
63 }
64
65 private:
66 /// Reports a new value and reschedules \sa Reschedule
67 void Report()
68 {
69 aux = m_var->GetValue();
70 m_trace = aux;
71 Reschedule();
72 }
73
74 Ptr<ExponentialRandomVariable> m_var; //!< Random value generator.
75 double m_time; //!< Delta time between reschedules.
77 double aux; //!< Emitted value.
78};
79
82{
83 static TypeId tid = TypeId("SampleEmitter")
85 .AddTraceSource("Emitter",
86 "XX",
88 "ns3::TracedValueCallback::Double");
89 return tid;
90}
91
92/**
93 * \ingroup stats-tests
94 *
95 * \brief DoubleProbe class - Test case for connecting and receiving data.
96 */
98{
99 public:
101 ~ProbeTestCase1() override;
102
103 private:
104 void DoRun() override;
105
106 /**
107 * Trace sink.
108 * \param context Trace context
109 * \param oldValue Old value
110 * \param newValue New value
111 */
112 void TraceSink(std::string context, double oldValue, double newValue);
113 uint32_t m_objectProbed; //!< Number of probes by Object
114 uint32_t m_pathProbed; //!< Number of probed by Path
115 Ptr<SampleEmitter> m_s; //!< Sample emitter pointer
116};
117
119 : TestCase("basic probe test case"),
120 m_objectProbed(0),
121 m_pathProbed(0)
122{
123}
124
126{
127}
128
129void
130ProbeTestCase1::TraceSink(std::string context, double oldValue, double newValue)
131{
133 Seconds(100),
134 "Probed a value outside of the time window");
136 Seconds(200),
137 "Probed a value outside of the time window");
138
140 newValue,
141 0.00001,
142 "Value probed different than value in the variable");
143
144 if (context == "testProbe")
145 {
147 }
148 else if (context == "testProbe2")
149 {
150 m_pathProbed++;
151 }
152}
153
154void
156{
157 // Defer creation of this until here because it is a random variable
158 m_s = CreateObject<SampleEmitter>();
159 // Test that all instances of probe data are between time window specified
160 // Check also that probes can be hooked to sources by Object and by path
161
162 Ptr<DoubleProbe> p = CreateObject<DoubleProbe>();
163 p->SetName("testProbe");
164
166 p->SetAttribute("Start", TimeValue(Seconds(100.0)));
167 p->SetAttribute("Stop", TimeValue(Seconds(200.0)));
169
170 // Register our emitter object so we can fetch it by using the Config
171 // namespace
172 Names::Add("/Names/SampleEmitter", m_s);
173
174 // Hook probe to the emitter.
175 p->ConnectByObject("Emitter", m_s);
176
177 // Hook our test function to the probe trace source
178 p->TraceConnect("Output", p->GetName(), MakeCallback(&ProbeTestCase1::TraceSink, this));
179
180 // Repeat but hook the probe to the object this time using the Config
181 // name set above
182 Ptr<DoubleProbe> p2 = CreateObject<DoubleProbe>();
183 p2->SetName("testProbe2");
184 p2->SetAttribute("Start", TimeValue(Seconds(100.0)));
185 p2->SetAttribute("Stop", TimeValue(Seconds(200.0)));
186
187 // Hook probe to the emitter.
188 p2->ConnectByPath("/Names/SampleEmitter/Emitter");
189
190 // Hook our test function to the probe trace source
191 p2->TraceConnect("Output", p2->GetName(), MakeCallback(&ProbeTestCase1::TraceSink, this));
192
194
195 // Check that each trace sink was called
196 NS_TEST_ASSERT_MSG_GT(m_objectProbed, 0, "Trace sink for object probe never called");
197 NS_TEST_ASSERT_MSG_GT(m_pathProbed, 0, "Trace sink for path probe never called");
199}
200
201/**
202 * \ingroup stats-tests
203 *
204 * \brief DoubleProbe class TestSuite
205 */
207{
208 public:
210};
211
213 : TestSuite("double-probe", Type::UNIT)
214{
215 AddTestCase(new ProbeTestCase1, TestCase::Duration::QUICK);
216}
217
218/// Static variable for test initialization
DoubleProbe class - Test case for connecting and receiving data.
void TraceSink(std::string context, double oldValue, double newValue)
Trace sink.
uint32_t m_pathProbed
Number of probed by Path.
Ptr< SampleEmitter > m_s
Sample emitter pointer.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_objectProbed
Number of probes by Object.
DoubleProbe class TestSuite.
Simple data emitter to check that a probe receives data.
double aux
Emitted value.
double GetValue() const
static TypeId GetTypeId()
Get the type ID.
void Reschedule()
Reschedule a report.
double GetTime() const
Ptr< ExponentialRandomVariable > m_var
Random value generator.
void Report()
Reports a new value and reschedules.
double m_time
Delta time between reschedules.
void Start()
Start emission of data.
TracedValue< double > m_trace
Trace.
double GetValue(double mean, double bound)
Get the next random value drawn from the distribution.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:775
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
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
AttributeValue implementation for Time.
Definition: nstime.h:1413
Trace classes with value semantics.
Definition: traced-value.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
static ProbeTestSuite probeTestSuite
Static variable for test initialization.
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition: test.h:710
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition: test.h:875
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:338
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704