A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
double-probe-test-suite.cc
Go to the documentation of this file.
1// Include a header file from your module to test.
2#include "ns3/double-probe.h"
3#include "ns3/names.h"
4#include "ns3/nstime.h"
5#include "ns3/object.h"
6#include "ns3/random-variable-stream.h"
7#include "ns3/simulator.h"
8#include "ns3/test.h"
9#include "ns3/trace-source-accessor.h"
10#include "ns3/traced-value.h"
11#include "ns3/type-id.h"
12
13using namespace ns3;
14
15/**
16 * @ingroup stats-tests
17 *
18 * @brief Simple data emitter to check that a probe receives data.
19 */
20class SampleEmitter : public Object
21{
22 public:
23 /**
24 * @brief Get the type ID.
25 * @return The object TypeId.
26 */
27 static TypeId GetTypeId();
28
33
34 ~SampleEmitter() override
35 {
36 }
37
38 /// Start emission of data.
39 void Start()
40 {
41 Reschedule();
42 }
43
44 /// Reschedule a report \sa Report
51
52 /// @return the time delta of the next report.
53 double GetTime() const
54 {
55 return m_time;
56 }
57
58 /// @return a random variable, different for each reschedule.
59 double GetValue() const
60 {
61 return aux;
62 }
63
64 private:
65 /// Reports a new value and reschedules \sa Reschedule
66 void Report()
67 {
68 aux = m_var->GetValue();
69 m_trace = aux;
70 Reschedule();
71 }
72
73 Ptr<ExponentialRandomVariable> m_var; //!< Random value generator.
74 double m_time; //!< Delta time between reschedules.
76 double aux; //!< Emitted value.
77};
78
81{
82 static TypeId tid = TypeId("SampleEmitter")
84 .AddTraceSource("Emitter",
85 "XX",
87 "ns3::TracedValueCallback::Double");
88 return tid;
89}
90
91/**
92 * @ingroup stats-tests
93 *
94 * @brief DoubleProbe class - Test case for connecting and receiving data.
95 */
97{
98 public:
100 ~ProbeTestCase1() override;
101
102 private:
103 void DoRun() override;
104
105 /**
106 * Trace sink.
107 * @param context Trace context
108 * @param oldValue Old value
109 * @param newValue New value
110 */
111 void TraceSink(std::string context, double oldValue, double newValue);
112 uint32_t m_objectProbed; //!< Number of probes by Object
113 uint32_t m_pathProbed; //!< Number of probed by Path
114 Ptr<SampleEmitter> m_s; //!< Sample emitter pointer
115};
116
118 : TestCase("basic probe test case"),
120 m_pathProbed(0)
121{
122}
123
127
128void
129ProbeTestCase1::TraceSink(std::string context, double oldValue, double newValue)
130{
132 Seconds(100),
133 "Probed a value outside of the time window");
135 Seconds(200),
136 "Probed a value outside of the time window");
137
138 NS_TEST_ASSERT_MSG_EQ_TOL(m_s->GetValue(),
139 newValue,
140 0.00001,
141 "Value probed different than value in the variable");
142
143 if (context == "testProbe")
144 {
146 }
147 else if (context == "testProbe2")
148 {
149 m_pathProbed++;
150 }
151}
152
153void
155{
156 // Defer creation of this until here because it is a random variable
158 // Test that all instances of probe data are between time window specified
159 // Check also that probes can be hooked to sources by Object and by path
160
162 p->SetName("testProbe");
163
165 p->SetAttribute("Start", TimeValue(Seconds(100)));
166 p->SetAttribute("Stop", TimeValue(Seconds(200)));
168
169 // Register our emitter object so we can fetch it by using the Config
170 // namespace
171 Names::Add("/Names/SampleEmitter", m_s);
172
173 // Hook probe to the emitter.
174 p->ConnectByObject("Emitter", m_s);
175
176 // Hook our test function to the probe trace source
177 p->TraceConnect("Output", p->GetName(), MakeCallback(&ProbeTestCase1::TraceSink, this));
178
179 // Repeat but hook the probe to the object this time using the Config
180 // name set above
182 p2->SetName("testProbe2");
183 p2->SetAttribute("Start", TimeValue(Seconds(100)));
184 p2->SetAttribute("Stop", TimeValue(Seconds(200)));
185
186 // Hook probe to the emitter.
187 p2->ConnectByPath("/Names/SampleEmitter/Emitter");
188
189 // Hook our test function to the probe trace source
190 p2->TraceConnect("Output", p2->GetName(), MakeCallback(&ProbeTestCase1::TraceSink, this));
191
193
194 // Check that each trace sink was called
195 NS_TEST_ASSERT_MSG_GT(m_objectProbed, 0, "Trace sink for object probe never called");
196 NS_TEST_ASSERT_MSG_GT(m_pathProbed, 0, "Trace sink for path probe never called");
198}
199
200/**
201 * @ingroup stats-tests
202 *
203 * @brief DoubleProbe class TestSuite
204 */
206{
207 public:
209};
210
216
217/// 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.
double aux
Emitted value.
static TypeId GetTypeId()
Get the type ID.
void Reschedule()
Reschedule a report.
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.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
Object()
Constructor.
Definition object.cc:96
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
@ QUICK
Fast test.
Definition test.h:1054
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1257
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1259
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:394
AttributeValue implementation for Time.
Definition nstime.h:1456
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
static ProbeTestSuite probeTestSuite
Static variable for test initialization.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:698
#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:863
#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:326
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
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:684