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