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 
15 using namespace ns3;
16 
17 class SampleEmitter : public Object
18 {
19 public:
20  static TypeId GetTypeId (void);
22  {
23  m_var = CreateObject<ExponentialRandomVariable> ();
24  }
25  virtual ~SampleEmitter ()
26  {
27  }
28  void Start ()
29  {
30  Reschedule ();
31  }
32  void Reschedule ()
33  {
34  m_time = m_var->GetValue ();
36  m_time += Simulator::Now ().GetSeconds ();
37  }
38  double GetTime ()
39  {
40  return m_time;
41  }
42  double GetValue ()
43  {
44  return aux;
45  }
46 private:
47  void Report ()
48  {
49  aux = m_var->GetValue ();
50  m_trace = aux;
51  Reschedule ();
52  }
54  double m_time;
56  double aux;
57 };
58 
59 
60 TypeId
62 {
63  static TypeId tid = TypeId ("SampleEmitter")
64  .SetParent<Object> ()
65  .AddTraceSource ("Emitter", "XX",
67  "ns3::TracedValueCallback::Double")
68  ;
69  return tid;
70 }
71 
72 class ProbeTestCase1 : public TestCase
73 {
74 public:
75  ProbeTestCase1 ();
76  virtual ~ProbeTestCase1 ();
77 
78 private:
79  virtual void DoRun (void);
80  void TraceSink (std::string context, double oldValue, double newValue);
81  uint32_t m_objectProbed;
82  uint32_t m_pathProbed;
84 };
85 
87  : TestCase ("basic probe test case"),
88  m_objectProbed (0),
89  m_pathProbed (0)
90 {
91 }
92 
94 {
95 }
96 
97 void
98 ProbeTestCase1::TraceSink (std::string context, double oldValue, double newValue)
99 {
100  NS_TEST_ASSERT_MSG_GT (Simulator::Now (), Seconds (100), "Probed a value outside of the time window");
101  NS_TEST_ASSERT_MSG_LT (Simulator::Now (), Seconds (200), "Probed a value outside of the time window");
102 
103  NS_TEST_ASSERT_MSG_EQ_TOL (m_s->GetValue (), newValue, 0.00001, "Value probed different than value in the variable");
104 
105  if (context == "testProbe")
106  {
107  m_objectProbed++;
108  }
109  else if (context == "testProbe2")
110  {
111  m_pathProbed++;
112  }
113 }
114 
115 void
117 {
118  // Defer creation of this until here because it is a random variable
119  m_s = CreateObject<SampleEmitter> ();
120  // Test that all instances of probe data are between time window specified
121  // Check also that probes can be hooked to sources by Object and by path
122 
123  Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
124  p->SetName ("testProbe");
125 
126  Simulator::Schedule (Seconds (1), &SampleEmitter::Start, m_s);
127  p->SetAttribute ("Start", TimeValue (Seconds (100.0)));
128  p->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
129  Simulator::Stop (Seconds (300));
130 
131  // Register our emitter object so we can fetch it by using the Config
132  // namespace
133  Names::Add ("/Names/SampleEmitter", m_s);
134 
135  // Hook probe to the emitter.
136  p->ConnectByObject ("Emitter", m_s);
137 
138  // Hook our test function to the probe trace source
139  p->TraceConnect ("Output", p->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
140 
141  // Repeat but hook the probe to the object this time using the Config
142  // name set above
143  Ptr<DoubleProbe> p2 = CreateObject<DoubleProbe> ();
144  p2->SetName ("testProbe2");
145  p2->SetAttribute ("Start", TimeValue (Seconds (100.0)));
146  p2->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
147 
148  // Hook probe to the emitter.
149  p2->ConnectByPath ("/Names/SampleEmitter/Emitter");
150 
151  // Hook our test function to the probe trace source
152  p2->TraceConnect ("Output", p2->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
153 
154  Simulator::Run ();
155 
156  // Check that each trace sink was called
157  NS_TEST_ASSERT_MSG_GT (m_objectProbed, 0, "Trace sink for object probe never called");
158  NS_TEST_ASSERT_MSG_GT (m_pathProbed, 0, "Trace sink for path probe never called");
159  Simulator::Destroy ();
160 }
161 
162 
163 class ProbeTestSuite : public TestSuite
164 {
165 public:
166  ProbeTestSuite ();
167 };
168 
170  : TestSuite ("double-probe", UNIT)
171 {
172  AddTestCase (new ProbeTestCase1, TestCase::QUICK);
173 }
174 
175 // Do not forget to allocate an instance of this TestSuite
177 
A suite of tests to run.
Definition: test.h:1343
void TraceSink(std::string context, double oldValue, double newValue)
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:361
static ProbeTestSuite probeTestSuite
Ptr< ExponentialRandomVariable > m_var
encapsulates test code
Definition: test.h:1153
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void ConnectByPath(std::string path)
connect to a trace source provided by a config path
Definition: double-probe.cc:95
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
AttributeValue implementation for Time.
Definition: nstime.h:1132
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ptr< SampleEmitter > m_s
virtual void DoRun(void)
Implementation to actually run this TestCase.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1610
#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:378
static TypeId GetTypeId(void)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
TracedValue< double > m_trace
void SetName(std::string name)
Set the object&#39;s name. All spaces are replaced by underscores.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:308
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
A base class which provides memory management and object aggregation.
Definition: object.h:87
#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:995
std::string GetName(void) const
Get the object&#39;s name.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:187
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
#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:809