A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/core-module.h"
7 
8 using namespace ns3;
9 
10 class SampleEmitter : public Object
11 {
12 public:
13  static TypeId GetTypeId (void);
15  {
16  }
17  virtual ~SampleEmitter ()
18  {
19  }
20  void Start ()
21  {
22  Reschedule ();
23  }
24  void Reschedule ()
25  {
26  m_time = m_var.GetValue ();
27  Simulator::Schedule (Seconds (m_time), &SampleEmitter::Report, this);
28  m_time += Simulator::Now ().GetSeconds ();
29  }
30  double GetTime ()
31  {
32  return m_time;
33  }
34  double GetValue ()
35  {
36  return aux;
37  }
38 private:
39  void Report ()
40  {
41  aux = m_var.GetValue ();
42  m_trace = aux;
43  Reschedule ();
44  }
46  double m_time;
48  double aux;
49 };
50 
51 Ptr<SampleEmitter> s = CreateObject<SampleEmitter> ();
52 
53 TypeId
55 {
56  static TypeId tid = TypeId ("SampleEmitter")
57  .SetParent<Object> ()
58  .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&SampleEmitter::m_trace))
59  ;
60  return tid;
61 }
62 
63 class ProbeTestCase1 : public TestCase
64 {
65 public:
66  ProbeTestCase1 ();
67  virtual ~ProbeTestCase1 ();
68 
69 private:
70  virtual void DoRun (void);
71  void TraceSink (std::string context, double oldValue, double newValue);
72  uint32_t m_objectProbed;
73  uint32_t m_pathProbed;
74 };
75 
77  : TestCase ("basic probe test case"),
78  m_objectProbed (0),
79  m_pathProbed (0)
80 {
81 }
82 
84 {
85 }
86 
87 void
88 ProbeTestCase1::TraceSink (std::string context, double oldValue, double newValue)
89 {
90  NS_TEST_ASSERT_MSG_GT (Simulator::Now (), Seconds (100), "Probed a value outside of the time window");
91  NS_TEST_ASSERT_MSG_LT (Simulator::Now (), Seconds (200), "Probed a value outside of the time window");
92 
93  NS_TEST_ASSERT_MSG_EQ_TOL (s->GetValue (), newValue, 0.00001, "Value probed different than value in the variable");
94 
95  if (context == "testProbe")
96  {
98  }
99  else if (context == "testProbe2")
100  {
101  m_pathProbed++;
102  }
103 }
104 
105 void
107 {
108  // Test that all instances of probe data are between time window specified
109  // Check also that probes can be hooked to sources by Object and by path
110 
111  Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
112  p->SetName ("testProbe");
113 
114  Simulator::Schedule (Seconds (1), &SampleEmitter::Start, s);
115  p->SetAttribute ("Start", TimeValue (Seconds (100.0)));
116  p->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
117  Simulator::Stop (Seconds (300));
118 
119  // Register our emitter object so we can fetch it by using the Config
120  // namespace
121  Names::Add ("/Names/SampleEmitter", s);
122 
123  // Hook probe to the emitter.
124  p->ConnectByObject ("Emitter", s);
125 
126  // Hook our test function to the probe trace source
127  p->TraceConnect ("Output", p->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
128 
129  // Repeat but hook the probe to the object this time using the Config
130  // name set above
131  Ptr<DoubleProbe> p2 = CreateObject<DoubleProbe> ();
132  p2->SetName ("testProbe2");
133  p2->SetAttribute ("Start", TimeValue (Seconds (100.0)));
134  p2->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
135 
136  // Hook probe to the emitter.
137  p2->ConnectByPath ("/Names/SampleEmitter/Emitter");
138 
139  // Hook our test function to the probe trace source
140  p2->TraceConnect ("Output", p2->GetName (), MakeCallback (&ProbeTestCase1::TraceSink, this));
141 
142  Simulator::Run ();
143 
144  // Check that each trace sink was called
145  NS_TEST_ASSERT_MSG_GT (m_objectProbed, 0, "Trace sink for object probe never called");
146  NS_TEST_ASSERT_MSG_GT (m_pathProbed, 0, "Trace sink for path probe never called");
147  Simulator::Destroy ();
148 }
149 
150 
151 class ProbeTestSuite : public TestSuite
152 {
153 public:
154  ProbeTestSuite ();
155 };
156 
158  : TestSuite ("double-probe", UNIT)
159 {
160  AddTestCase (new ProbeTestCase1, TestCase::QUICK);
161 }
162 
163 // Do not forget to allocate an instance of this TestSuite
165 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
std::string GetName(void) const
Get the object's name.
#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
A suite of tests to run.
Definition: test.h:1025
void TraceSink(std::string context, double oldValue, double newValue)
static ProbeTestSuite probeTestSuite
encapsulates test code
Definition: test.h:849
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:824
ExponentialVariable m_var
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
Definition: nstime.h:274
#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:773
hold objects of type ns3::Time
Definition: nstime.h:961
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:1238
static TypeId GetTypeId(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Exponentially Distributed random varThis class supports the creation of objects that return random nu...
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
TracedValue< double > m_trace
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
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 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:688
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:282
a base class which provides memory management and object aggregation
Definition: object.h:63
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611