Index: src/stats/test/time-series-collector-test-suite.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/stats/test/time-series-collector-test-suite.cc |
@@ -0,0 +1,168 @@ |
+/* -*- 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/time-series-collector.h" |
+#include "ns3/double-probe.h" |
+#include "ns3/test.h" |
+#include "ns3/core-module.h" |
+ |
+using namespace ns3; |
+ |
+class TimeSeriesCollectorSampleEmitter : public Object |
+{ |
+public: |
+ static TypeId GetTypeId (void); |
+ TimeSeriesCollectorSampleEmitter () |
+ { |
+ m_var = CreateObject<ExponentialRandomVariable> (); |
+ } |
+ virtual ~TimeSeriesCollectorSampleEmitter () |
+ { |
+ } |
+ void Start () |
+ { |
+ Reschedule (); |
+ } |
+ void Reschedule () |
+ { |
+ m_time = m_var->GetValue (); |
+ Simulator::Schedule (Seconds (m_time), &TimeSeriesCollectorSampleEmitter::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 |
+TimeSeriesCollectorSampleEmitter::GetTypeId (void) |
+{ |
+ static TypeId tid = TypeId ("TimeSeriesCollectorSampleEmitter") |
+ .SetParent<Object> () |
+ .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&TimeSeriesCollectorSampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback") |
+ ; |
+ return tid; |
+} |
+ |
+ |
+// =========================================================================== |
+// Test case |
+// =========================================================================== |
+ |
+class TimeSeriesCollectorTestCase1 : public TestCase |
+{ |
+public: |
+ TimeSeriesCollectorTestCase1 (); |
+ virtual ~TimeSeriesCollectorTestCase1 (); |
+ |
+private: |
+ double currentTime; |
+ virtual void DoRun (void); |
+ void TraceSinkCallback (std::string context, double oldValue, double newValue); |
+ Ptr<TimeSeriesCollector> collector; |
+ Ptr<TimeSeriesCollectorSampleEmitter> s; |
+}; |
+ |
+TimeSeriesCollectorTestCase1::TimeSeriesCollectorTestCase1 () |
+ : TestCase ("TimeSeriesCollector test case 1") |
+{ |
+} |
+ |
+TimeSeriesCollectorTestCase1::~TimeSeriesCollectorTestCase1 () |
+{ |
+} |
+ |
+void |
+TimeSeriesCollectorTestCase1::DoRun (void) |
+{ |
+ // let the collector generate outputs every 1 second |
+ collector = CreateObject<TimeSeriesCollector> (); |
+ collector->SetPeriod (Seconds (1)); |
+ collector->Enable (); |
+ s = CreateObject<TimeSeriesCollectorSampleEmitter> (); |
+ currentTime = 1; |
+ |
+ Ptr<DoubleProbe> p = CreateObject<DoubleProbe> (); |
+ p->SetName ("TimeSeriesCollectorSampleProbe"); |
+ |
+ Simulator::Schedule (Seconds (0), &TimeSeriesCollectorSampleEmitter::Start, s); |
+ p->SetAttribute ("Start", TimeValue (Seconds (0.0))); |
+ p->SetAttribute ("Stop", TimeValue (Seconds (100.0))); |
+ Simulator::Stop (Seconds (200)); |
+ |
+ Names::Add ("/Names/TimeSeriesCollectorSampleEmitter", s); |
+ |
+ // Hook probe to the emitter. |
+ p->ConnectByObject ("Emitter", s); |
+ |
+ // Hook the collector to the probe |
+ bool connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&TimeSeriesCollector::TraceSinkDouble, collector)); |
+ NS_ASSERT (connected == true); |
+ |
+ // connect the collector to the TraceSinkCallback. |
+ connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&TimeSeriesCollectorTestCase1::TraceSinkCallback, this)); |
+ NS_ASSERT (connected == true); |
+ NS_UNUSED (connected); |
+ |
+ Simulator::Run (); |
+ Simulator::Destroy (); |
+} |
+ |
+void |
+TimeSeriesCollectorTestCase1::TraceSinkCallback (std::string context, double time, double newValue) |
+{ |
+ // check whether the collector is generating outputs every 1 second. |
+ NS_TEST_ASSERT_MSG_EQ_TOL (currentTime, time, 0.0001, "Value generated by the collector different than the actual value"); |
+ currentTime += 1; |
+} |
+ |
+class TimeSeriesCollectorTestSuite : public TestSuite |
+{ |
+public: |
+ TimeSeriesCollectorTestSuite (); |
+}; |
+ |
+TimeSeriesCollectorTestSuite::TimeSeriesCollectorTestSuite () |
+ : TestSuite ("time-series-collector", UNIT) |
+{ |
+ AddTestCase (new TimeSeriesCollectorTestCase1, TestCase::QUICK); |
+} |
+ |
+static TimeSeriesCollectorTestSuite TimeSeriesCollectorTestSuite; |
+ |