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 Ptr<SampleEmitter> s = CreateObject<SampleEmitter> ();
60 
61 TypeId
63 {
64  static TypeId tid = TypeId ("SampleEmitter")
65  .SetParent<Object> ()
66  .AddTraceSource ("Emitter", "XX",
68  "ns3::TracedValue::DoubleCallback")
69  ;
70  return tid;
71 }
72 
73 class ProbeTestCase1 : public TestCase
74 {
75 public:
76  ProbeTestCase1 ();
77  virtual ~ProbeTestCase1 ();
78 
79 private:
80  virtual void DoRun (void);
81  void TraceSink (std::string context, double oldValue, double newValue);
82  uint32_t m_objectProbed;
83  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 (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  // Test that all instances of probe data are between time window specified
119  // Check also that probes can be hooked to sources by Object and by path
120 
121  Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
122  p->SetName ("testProbe");
123 
124  Simulator::Schedule (Seconds (1), &SampleEmitter::Start, s);
125  p->SetAttribute ("Start", TimeValue (Seconds (100.0)));
126  p->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
127  Simulator::Stop (Seconds (300));
128 
129  // Register our emitter object so we can fetch it by using the Config
130  // namespace
131  Names::Add ("/Names/SampleEmitter", s);
132 
133  // Hook probe to the emitter.
134  p->ConnectByObject ("Emitter", s);
135 
136  // Hook our test function to the probe trace source
137  p->TraceConnect ("Output", p->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
138 
139  // Repeat but hook the probe to the object this time using the Config
140  // name set above
141  Ptr<DoubleProbe> p2 = CreateObject<DoubleProbe> ();
142  p2->SetName ("testProbe2");
143  p2->SetAttribute ("Start", TimeValue (Seconds (100.0)));
144  p2->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
145 
146  // Hook probe to the emitter.
147  p2->ConnectByPath ("/Names/SampleEmitter/Emitter");
148 
149  // Hook our test function to the probe trace source
150  p2->TraceConnect ("Output", p2->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
151 
152  Simulator::Run ();
153 
154  // Check that each trace sink was called
155  NS_TEST_ASSERT_MSG_GT (m_objectProbed, 0, "Trace sink for object probe never called");
156  NS_TEST_ASSERT_MSG_GT (m_pathProbed, 0, "Trace sink for path probe never called");
157  Simulator::Destroy ();
158 }
159 
160 
161 class ProbeTestSuite : public TestSuite
162 {
163 public:
164  ProbeTestSuite ();
165 };
166 
168  : TestSuite ("double-probe", UNIT)
169 {
170  AddTestCase (new ProbeTestCase1, TestCase::QUICK);
171 }
172 
173 // Do not forget to allocate an instance of this TestSuite
175 
std::string GetName(void) const
Get the object's name.
A suite of tests to run.
Definition: test.h:1270
void TraceSink(std::string context, double oldValue, double newValue)
static ProbeTestSuite probeTestSuite
Ptr< ExponentialRandomVariable > m_var
encapsulates test code
Definition: test.h:1102
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
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:94
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:184
AttributeValue implementation for Time.
Definition: nstime.h:921
virtual void DoRun(void)
Implementation to actually run this TestCase.
Ptr< SampleEmitter > s
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
#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:356
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:223
TracedValue< double > m_trace
virtual bool ConnectByObject(std::string traceSource, Ptr< Object > obj)
connect to a trace source attribute provided by a given object
Definition: double-probe.cc:85
void SetName(std::string name)
Set the object's name. All spaces are replaced by underscores.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:311
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
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:954
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:190
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
#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:772