A Discrete-Event Network Simulator
API
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 
14 using namespace ns3;
15 
21 class SampleEmitter : public Object
22 {
23  public:
28  static TypeId GetTypeId();
29 
31  {
32  m_var = CreateObject<ExponentialRandomVariable>();
33  }
34 
35  ~SampleEmitter() override
36  {
37  }
38 
40  void Start()
41  {
42  Reschedule();
43  }
44 
46  void Reschedule()
47  {
48  m_time = m_var->GetValue();
51  }
52 
54  double GetTime() const
55  {
56  return m_time;
57  }
58 
60  double GetValue() const
61  {
62  return aux;
63  }
64 
65  private:
67  void Report()
68  {
69  aux = m_var->GetValue();
70  m_trace = aux;
71  Reschedule();
72  }
73 
75  double m_time;
77  double aux;
78 };
79 
80 TypeId
82 {
83  static TypeId tid = TypeId("SampleEmitter")
84  .SetParent<Object>()
85  .AddTraceSource("Emitter",
86  "XX",
88  "ns3::TracedValueCallback::Double");
89  return tid;
90 }
91 
97 class ProbeTestCase1 : public TestCase
98 {
99  public:
100  ProbeTestCase1();
101  ~ProbeTestCase1() override;
102 
103  private:
104  void DoRun() override;
105 
112  void TraceSink(std::string context, double oldValue, double newValue);
113  uint32_t m_objectProbed;
114  uint32_t m_pathProbed;
116 };
117 
119  : TestCase("basic probe test case"),
120  m_objectProbed(0),
121  m_pathProbed(0)
122 {
123 }
124 
126 {
127 }
128 
129 void
130 ProbeTestCase1::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  {
146  m_objectProbed++;
147  }
148  else if (context == "testProbe2")
149  {
150  m_pathProbed++;
151  }
152 }
153 
154 void
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 
165  Simulator::Schedule(Seconds(1), &SampleEmitter::Start, m_s);
166  p->SetAttribute("Start", TimeValue(Seconds(100.0)));
167  p->SetAttribute("Stop", TimeValue(Seconds(200.0)));
168  Simulator::Stop(Seconds(300));
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 
193  Simulator::Run();
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");
198  Simulator::Destroy();
199 }
200 
206 class ProbeTestSuite : public TestSuite
207 {
208  public:
209  ProbeTestSuite();
210 };
211 
213  : TestSuite("double-probe", UNIT)
214 {
215  AddTestCase(new ProbeTestCase1, TestCase::QUICK);
216 }
217 
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.
A base class which provides memory management and object aggregation.
Definition: object.h:89
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
static ProbeTestSuite probeTestSuite
Static variable for test initialization.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:709
#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:874
#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:337
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
double m_time
Definition: qkd_etsi_014.cc:62