Index: src/stats/test/scaling-collector-test-suite.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/stats/test/scaling-collector-test-suite.cc |
@@ -0,0 +1,172 @@ |
+/* -*- 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/scaling-collector.h" |
+#include "ns3/double-probe.h" |
+#include "ns3/test.h" |
+#include "ns3/core-module.h" |
+ |
+using namespace ns3; |
+ |
+class ScalingCollectorSampleEmitter : public Object |
+{ |
+public: |
+ static TypeId GetTypeId (void); |
+ ScalingCollectorSampleEmitter () |
+ { |
+ m_var = CreateObject<ExponentialRandomVariable> (); |
+ } |
+ virtual ~ScalingCollectorSampleEmitter () |
+ { |
+ } |
+ void Start () |
+ { |
+ Reschedule (); |
+ } |
+ void Reschedule () |
+ { |
+ m_time = m_var->GetValue (); |
+ Simulator::Schedule (Seconds (m_time), &ScalingCollectorSampleEmitter::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 |
+ScalingCollectorSampleEmitter::GetTypeId (void) |
+{ |
+ static TypeId tid = TypeId ("ScalingCollectorSampleEmitter") |
+ .SetParent<Object> () |
+ .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&ScalingCollectorSampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback") |
+ ; |
+ return tid; |
+} |
+ |
+ |
+// =========================================================================== |
+// Test case |
+// =========================================================================== |
+ |
+class ScalingCollectorTestCase1 : public TestCase |
+{ |
+public: |
+ ScalingCollectorTestCase1 (); |
+ virtual ~ScalingCollectorTestCase1 (); |
+ |
+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<ScalingCollector> collector; |
+ Ptr<ScalingCollectorSampleEmitter> s; |
+}; |
+ |
+ScalingCollectorTestCase1::ScalingCollectorTestCase1 () |
+ : TestCase ("ScalingCollector test case 1") |
+{ |
+} |
+ |
+ScalingCollectorTestCase1::~ScalingCollectorTestCase1 () |
+{ |
+} |
+ |
+void |
+ScalingCollectorTestCase1::DoRun (void) |
+{ |
+ collector = CreateObject<ScalingCollector> (); |
+ collector->SetAttribute ("ScalingFactor", DoubleValue (100)); |
+ s = CreateObject<ScalingCollectorSampleEmitter> (); |
+ |
+ Ptr<DoubleProbe> p = CreateObject<DoubleProbe> (); |
+ p->SetName ("ScalingCollectorSampleProbe"); |
+ |
+ Simulator::Schedule (Seconds (1), &ScalingCollectorSampleEmitter::Start, s); |
+ p->SetAttribute ("Start", TimeValue (Seconds (100.0))); |
+ p->SetAttribute ("Stop", TimeValue (Seconds (200.0))); |
+ Simulator::Stop (Seconds (300)); |
+ |
+ Names::Add ("/Names/ScalingCollectorSampleEmitter", 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 (&ScalingCollectorTestCase1::ReceiveDouble, this)); |
+ NS_UNUSED (connected); |
+ |
+ // Hook the collector to the probe |
+ connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&ScalingCollector::TraceSinkDouble, collector)); |
+ |
+ // connect the collector to the TraceSinkCallback. |
+ connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&ScalingCollectorTestCase1::TraceSinkCallback, this)); |
+ |
+ Simulator::Run (); |
+ Simulator::Destroy (); |
+} |
+ |
+void |
+ScalingCollectorTestCase1::TraceSinkCallback (std::string context, double oldValue, double newValue) |
+{ |
+ NS_TEST_ASSERT_MSG_EQ_TOL (receivedDouble * 100, newValue, 0.0001, "Value generated by the collector different than the actual value"); |
+} |
+ |
+void |
+ScalingCollectorTestCase1::ReceiveDouble (std::string context, double oldValue, double newValue) |
+{ |
+ receivedDouble = newValue; |
+} |
+ |
+ |
+class ScalingCollectorTestSuite : public TestSuite |
+{ |
+public: |
+ ScalingCollectorTestSuite (); |
+}; |
+ |
+ScalingCollectorTestSuite::ScalingCollectorTestSuite () |
+ : TestSuite ("scaling-collector", UNIT) |
+{ |
+ AddTestCase (new ScalingCollectorTestCase1, TestCase::QUICK); |
+} |
+ |
+static ScalingCollectorTestSuite ScalingCollectorTestSuite; |
+ |