Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(601)

Unified Diff: src/stats/test/event-driven-collector-test-suite.cc

Issue 245260043: DCF collectors
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/stats/test/event-driven-collector-test-suite.cc
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/stats/test/event-driven-collector-test-suite.cc
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2015 Bucknell University
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Li Li (ll024@bucknell.edu)
+ */
+
+#include <iostream>
+#include <string>
+#include "ns3/event-driven-collector.h"
+#include "ns3/double-probe.h"
+#include "ns3/test.h"
+#include "ns3/core-module.h"
+
+using namespace ns3;
+
+class EventDrivenCollectorSampleEmitter : public Object
+{
+public:
+ static TypeId GetTypeId (void);
+ EventDrivenCollectorSampleEmitter ()
+ {
+ m_var = CreateObject<ExponentialRandomVariable> ();
+ }
+ virtual ~EventDrivenCollectorSampleEmitter ()
+ {
+ }
+ void Start ()
+ {
+ Reschedule ();
+ }
+ void Reschedule ()
+ {
+ m_time = m_var->GetValue ();
+ Simulator::Schedule (Seconds (m_time), &EventDrivenCollectorSampleEmitter::Report, this);
+ m_time += Simulator::Now ().GetSeconds ();
+ }
+ double GetTime ()
+ {
+ return m_time;
+ }
+ double GetValue ()
+ {
+ return aux;
+ }
+private:
+ void Report ()
+ {
+ aux = m_var->GetValue ();
+ m_trace = aux;
+ Reschedule ();
+ }
+ Ptr<ExponentialRandomVariable> m_var;
+ double m_time;
+ TracedValue<double> m_trace;
+ double aux;
+};
+
+
+TypeId
+EventDrivenCollectorSampleEmitter::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("EventDrivenCollectorSampleEmitter")
+ .SetParent<Object> ()
+ .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&EventDrivenCollectorSampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback")
+ ;
+ return tid;
+}
+
+
+// ===========================================================================
+// Test case
+// ===========================================================================
+
+class EventDrivenCollectorTestCase1 : public TestCase
+{
+public:
+ EventDrivenCollectorTestCase1 ();
+ virtual ~EventDrivenCollectorTestCase1 ();
+
+private:
+ virtual void DoRun (void);
+ void TraceSinkCallback (std::string context, double oldValue, double newValue);
+ void ReceiveDouble (std::string context, double oldValue, double newValue);
+ double receivedDouble;
+ Ptr<EventDrivenCollector> collector;
+ Ptr<EventDrivenCollectorSampleEmitter> s;
+};
+
+EventDrivenCollectorTestCase1::EventDrivenCollectorTestCase1 ()
+ : TestCase ("EventDrivenCollector test case 1")
+{
+ collector = CreateObject<EventDrivenCollector> ();
+ s = CreateObject<EventDrivenCollectorSampleEmitter> ();
+}
+
+EventDrivenCollectorTestCase1::~EventDrivenCollectorTestCase1 ()
+{
+}
+
+void
+EventDrivenCollectorTestCase1::DoRun (void)
+{
+ Ptr<DoubleProbe> p = CreateObject<DoubleProbe> ();
+ p->SetName ("EventDrivenCollectorSampleProbe");
+
+ Simulator::Schedule (Seconds (1), &EventDrivenCollectorSampleEmitter::Start, s);
+ p->SetAttribute ("Start", TimeValue (Seconds (100.0)));
+ p->SetAttribute ("Stop", TimeValue (Seconds (200.0)));
+ Simulator::Stop (Seconds (300));
+
+ Names::Add ("/Names/EventDrivenCollectorSampleEmitter", s);
+
+ // Hook probe to the emitter.
+ p->ConnectByObject ("Emitter", s);
+
+ // update the variable that keeps track of the data received
+ bool connected = p->TraceConnect ("Output", p->GetName (), MakeCallback (&EventDrivenCollectorTestCase1::ReceiveDouble, this));
+ NS_UNUSED (connected);
+
+ // Hook the collector to the probe
+ connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&EventDrivenCollector::TraceSinkDouble, collector));
+
+ // connect the collector to the TraceSinkCallback.
+ connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&EventDrivenCollectorTestCase1::TraceSinkCallback, this));
+
+ Simulator::Run ();
+ Simulator::Destroy ();
+}
+
+void
+EventDrivenCollectorTestCase1::TraceSinkCallback (std::string context, double oldValue, double newValue)
+{
+ NS_TEST_ASSERT_MSG_EQ_TOL (receivedDouble, newValue, 0.0001, "Value generated by the collector different than the actual value");
+}
+
+void
+EventDrivenCollectorTestCase1::ReceiveDouble (std::string context, double oldValue, double newValue)
+{
+ receivedDouble = newValue;
+}
+
+
+class EventDrivenCollectorTestSuite : public TestSuite
+{
+public:
+ EventDrivenCollectorTestSuite ();
+};
+
+EventDrivenCollectorTestSuite::EventDrivenCollectorTestSuite ()
+ : TestSuite ("event-driven-collector", UNIT)
+{
+ AddTestCase (new EventDrivenCollectorTestCase1, TestCase::QUICK);
+}
+
+static EventDrivenCollectorTestSuite EventDrivenCollectorTestSuite;
+

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b