A Discrete-Event Network Simulator
API
double-probe-test-suite.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
3// Include a header file from your module to test.
4#include "ns3/double-probe.h"
5#include "ns3/test.h"
6#include "ns3/random-variable-stream.h"
7#include "ns3/trace-source-accessor.h"
8#include "ns3/traced-value.h"
9#include "ns3/nstime.h"
10#include "ns3/simulator.h"
11#include "ns3/object.h"
12#include "ns3/type-id.h"
13#include "ns3/names.h"
14
15using namespace ns3;
16
22class SampleEmitter : public Object
23{
24public:
29 static TypeId GetTypeId (void);
31 {
32 m_var = CreateObject<ExponentialRandomVariable> ();
33 }
34 virtual ~SampleEmitter ()
35 {
36 }
38 void Start ()
39 {
40 Reschedule ();
41 }
43 void Reschedule ()
44 {
45 m_time = m_var->GetValue ();
46 Simulator::Schedule (Seconds (m_time), &SampleEmitter::Report, this);
48 }
50 double GetTime ()
51 {
52 return m_time;
53 }
55 double GetValue ()
56 {
57 return aux;
58 }
59private:
61 void Report ()
62 {
63 aux = m_var->GetValue ();
64 m_trace = aux;
65 Reschedule ();
66 }
68 double m_time;
70 double aux;
71};
72
73
76{
77 static TypeId tid = TypeId ("SampleEmitter")
78 .SetParent<Object> ()
79 .AddTraceSource ("Emitter", "XX",
81 "ns3::TracedValueCallback::Double")
82 ;
83 return tid;
84}
85
86
93{
94public:
96 virtual ~ProbeTestCase1 ();
97
98private:
99 virtual void DoRun (void);
100
107 void TraceSink (std::string context, double oldValue, double newValue);
111};
112
114 : TestCase ("basic probe test case"),
115 m_objectProbed (0),
116 m_pathProbed (0)
117{
118}
119
121{
122}
123
124void
125ProbeTestCase1::TraceSink (std::string context, double oldValue, double newValue)
126{
127 NS_TEST_ASSERT_MSG_GT (Simulator::Now (), Seconds (100), "Probed a value outside of the time window");
128 NS_TEST_ASSERT_MSG_LT (Simulator::Now (), Seconds (200), "Probed a value outside of the time window");
129
130 NS_TEST_ASSERT_MSG_EQ_TOL (m_s->GetValue (), newValue, 0.00001, "Value probed different than value in the variable");
131
132 if (context == "testProbe")
133 {
135 }
136 else if (context == "testProbe2")
137 {
138 m_pathProbed++;
139 }
140}
141
142void
144{
145 // Defer creation of this until here because it is a random variable
146 m_s = CreateObject<SampleEmitter> ();
147 // Test that all instances of probe data are between time window specified
148 // Check also that probes can be hooked to sources by Object and by path
149
150 Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
151 p->SetName ("testProbe");
152
153 Simulator::Schedule (Seconds (1), &SampleEmitter::Start, m_s);
154 p->SetAttribute ("Start", TimeValue (Seconds (100.0)));
155 p->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
156 Simulator::Stop (Seconds (300));
157
158 // Register our emitter object so we can fetch it by using the Config
159 // namespace
160 Names::Add ("/Names/SampleEmitter", m_s);
161
162 // Hook probe to the emitter.
163 p->ConnectByObject ("Emitter", m_s);
164
165 // Hook our test function to the probe trace source
166 p->TraceConnect ("Output", p->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
167
168 // Repeat but hook the probe to the object this time using the Config
169 // name set above
170 Ptr<DoubleProbe> p2 = CreateObject<DoubleProbe> ();
171 p2->SetName ("testProbe2");
172 p2->SetAttribute ("Start", TimeValue (Seconds (100.0)));
173 p2->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
174
175 // Hook probe to the emitter.
176 p2->ConnectByPath ("/Names/SampleEmitter/Emitter");
177
178 // Hook our test function to the probe trace source
179 p2->TraceConnect ("Output", p2->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
180
181 Simulator::Run ();
182
183 // Check that each trace sink was called
184 NS_TEST_ASSERT_MSG_GT (m_objectProbed, 0, "Trace sink for object probe never called");
185 NS_TEST_ASSERT_MSG_GT (m_pathProbed, 0, "Trace sink for path probe never called");
186 Simulator::Destroy ();
187}
188
189
196{
197public:
199};
200
202 : TestSuite ("double-probe", UNIT)
203{
204 AddTestCase (new ProbeTestCase1, TestCase::QUICK);
205}
206
209
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Ptr< SampleEmitter > m_s
Sample emitter pointer.
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.
void Reschedule()
Reschedule a report.
static TypeId GetTypeId(void)
Get the type ID.
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, as a double from the exponential distribution with the specified mean and ...
A base class which provides memory management and object aggregation.
Definition: object.h:88
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
static ProbeTestSuite probeTestSuite
Static variable for test initialization.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
#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:675
#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:825
#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:323
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648