Bug 976
authorMitch Watrous <watrous@u.washington.edu>
Wed, 03 Oct 2012 10:06:52 -0700
changeset 9099 8009389af158
parent 9098 e507321e9a0d
child 9100 c807bfccdce9
Bug 976
src/mobility/examples/mobility-trace-example.cc
src/mobility/examples/wscript
src/mobility/test/mobility-trace-example.mob
src/mobility/test/mobility-trace-test-suite.cc
src/mobility/wscript
src/network/utils/ascii-file.cc
src/network/utils/ascii-file.h
src/network/utils/ascii-test.h
src/network/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mobility/examples/mobility-trace-example.cc	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of Washington
+ *
+ * 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
+ */
+
+// Test that mobility tracing works
+
+#include "ns3/core-module.h"
+#include "ns3/mobility-module.h"
+#include "ns3/network-module.h"
+
+using namespace ns3;
+
+int main (int argc, char *argv[])
+{
+  NodeContainer sta;
+  sta.Create (4);
+  MobilityHelper mobility;
+  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
+                                 "MinX", DoubleValue (1.0),
+                                 "MinY", DoubleValue (1.0),
+                                 "DeltaX", DoubleValue (5.0),
+                                 "DeltaY", DoubleValue (5.0),
+                                 "GridWidth", UintegerValue (3),
+                                 "LayoutType", StringValue ("RowFirst"));
+  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
+                             "Mode", StringValue ("Time"),
+                             "Time", StringValue ("2s"),
+                             "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
+                             "Bounds", RectangleValue (Rectangle (0.0, 20.0, 0.0, 20.0)));
+  mobility.Install (sta);
+  // Set mobility random number streams to fixed values
+  mobility.AssignStreams (sta, 0);
+
+  AsciiTraceHelper ascii;
+  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream ("mobility-trace-example.mob"));
+
+  Simulator::Stop (Seconds (5.0));
+  Simulator::Run ();
+  Simulator::Destroy ();
+}
--- a/src/mobility/examples/wscript	Fri Sep 28 14:33:15 2012 -0700
+++ b/src/mobility/examples/wscript	Wed Oct 03 10:06:52 2012 -0700
@@ -16,6 +16,10 @@
                                  ['core', 'mobility'])
     obj.source = 'main-random-walk.cc'
 
+    obj = bld.create_ns3_program('mobility-trace-example',
+                                 ['core', 'mobility', 'network'])
+    obj.source = 'mobility-trace-example.cc'
+
     obj = bld.create_ns3_program('ns2-mobility-trace', 
                                  ['core', 'mobility'])
     obj.source = 'ns2-mobility-trace.cc'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mobility/test/mobility-trace-example.mob	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,18 @@
+now=+0.0ns node=0 pos=1.000:1.000:0.000 vel=-0.293:-0.956:0.000
+now=+0.0ns node=1 pos=6.000:1.000:0.000 vel=0.844:-0.537:0.000
+now=+0.0ns node=2 pos=11.000:1.000:0.000 vel=0.875:-0.485:0.000
+now=+0.0ns node=3 pos=1.000:6.000:0.000 vel=0.574:-0.819:0.000
+now=+1046036669.0ns node=0 pos=0.693:0.000:0.000 vel=-0.293:0.956:0.000
+now=+1863851861.0ns node=1 pos=7.573:0.000:0.000 vel=0.844:0.537:0.000
+now=+2000000000.0ns node=2 pos=12.750:0.031:0.000 vel=-0.952:-0.305:0.000
+now=+2000000000.0ns node=3 pos=2.148:4.362:0.000 vel=-0.932:-0.362:0.000
+now=+2000000000.0ns node=0 pos=0.413:0.912:0.000 vel=-0.320:-0.947:0.000
+now=+2000000000.0ns node=1 pos=7.688:0.073:0.000 vel=-1.000:0.013:0.000
+now=+2101230083.0ns node=2 pos=12.653:0.000:0.000 vel=-0.952:0.305:0.000
+now=+2962748145.0ns node=0 pos=0.105:0.000:0.000 vel=-0.320:0.947:0.000
+now=+3289440024.0ns node=0 pos=0.000:0.309:0.000 vel=0.320:0.947:0.000
+now=+4000000000.0ns node=3 pos=0.284:3.638:0.000 vel=0.891:-0.455:0.000
+now=+4000000000.0ns node=1 pos=5.688:0.099:0.000 vel=0.923:0.384:0.000
+now=+4000000000.0ns node=2 pos=10.845:0.580:0.000 vel=0.256:0.967:0.000
+now=+4000000000.0ns node=0 pos=0.228:0.983:0.000 vel=-0.724:-0.690:0.000
+now=+4314385619.0ns node=0 pos=0.000:0.766:0.000 vel=0.724:-0.690:0.000
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mobility/test/mobility-trace-test-suite.cc	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,129 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of Washington
+ *
+ * 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: Mitch Watrous (watrous@u.washington.edu)
+ */
+
+#include <string>
+
+#include "ns3/ascii-test.h"
+#include "ns3/trace-helper.h"
+#include "ns3/double.h"
+#include "ns3/uinteger.h"
+#include "ns3/string.h"
+#include "ns3/simulator.h"
+#include "ns3/rectangle.h"
+#include "ns3/mobility-model.h"
+#include "ns3/mobility-helper.h"
+#include "ns3/node-container.h"
+#include "ns3/test.h"
+
+using namespace ns3;
+
+
+//===========================================================================
+// Class: MobilityTraceTestCase
+//
+//
+// Test case.
+//===========================================================================
+
+class MobilityTraceTestCase : public TestCase
+{
+public:
+  MobilityTraceTestCase ();
+  virtual ~MobilityTraceTestCase ();
+
+private:
+  virtual void DoRun (void);
+};
+
+MobilityTraceTestCase::MobilityTraceTestCase ()
+  :
+  TestCase ("Mobility Trace Test Case")
+{
+}
+
+MobilityTraceTestCase::~MobilityTraceTestCase ()
+{
+}
+
+void
+MobilityTraceTestCase::DoRun (void)
+{
+  //***************************************************************************
+  // Create the new mobility trace.
+  //***************************************************************************
+
+  NodeContainer sta;
+  sta.Create (4);
+  MobilityHelper mobility;
+  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
+                                 "MinX", DoubleValue (1.0),
+                                 "MinY", DoubleValue (1.0),
+                                 "DeltaX", DoubleValue (5.0),
+                                 "DeltaY", DoubleValue (5.0),
+                                 "GridWidth", UintegerValue (3),
+                                 "LayoutType", StringValue ("RowFirst"));
+  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
+                             "Mode", StringValue ("Time"),
+                             "Time", StringValue ("2s"),
+                             "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
+                             "Bounds", RectangleValue (Rectangle (0.0, 20.0, 0.0, 20.0)));
+  mobility.Install (sta);
+  // Set mobility random number streams to fixed values
+  mobility.AssignStreams (sta, 0);
+
+  SetDataDir (NS_TEST_SOURCEDIR);
+  std::string referenceMobilityFilePath = CreateDataDirFilename ("mobility-trace-example.mob");
+  std::string testMobilityFilePath      = CreateTempDirFilename ("mobility-trace-test.mob");
+
+  AsciiTraceHelper ascii;
+  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream (testMobilityFilePath));
+  Simulator::Stop (Seconds (5.0));
+  Simulator::Run ();
+  Simulator::Destroy ();
+
+
+  //***************************************************************************
+  // Test the new mobility trace against the reference mobility trace.
+  //***************************************************************************
+
+  NS_ASCII_TEST_EXPECT_EQ (testMobilityFilePath, referenceMobilityFilePath);
+}
+
+
+//===========================================================================
+// Class: MobilityTraceTestSuite
+//
+//
+// Test suite.
+//===========================================================================
+
+class MobilityTraceTestSuite : public TestSuite
+{
+public:
+  MobilityTraceTestSuite ();
+};
+
+MobilityTraceTestSuite::MobilityTraceTestSuite ()
+  : TestSuite ("mobility-trace", UNIT)
+{
+  AddTestCase (new MobilityTraceTestCase);
+}
+
+static MobilityTraceTestSuite mobilityTraceTestSuite;
--- a/src/mobility/wscript	Fri Sep 28 14:33:15 2012 -0700
+++ b/src/mobility/wscript	Wed Oct 03 10:06:52 2012 -0700
@@ -25,6 +25,7 @@
 
     mobility_test = bld.create_ns3_module_test_library('mobility')
     mobility_test.source = [
+        'test/mobility-trace-test-suite.cc',
         'test/ns2-mobility-helper-test-suite.cc',
         'test/steady-state-random-waypoint-mobility-model-test.cc',
         'test/waypoint-mobility-model-test.cc',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network/utils/ascii-file.cc	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,129 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of Washington
+ *
+ * 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: Mitch Watrous (watrous@u.washington.edu)
+ *
+ * This file is based on pcap-file.cc by Craig Dowell (craigdo@ee.washington.edu)
+ */
+
+#include <iostream>
+#include <string>
+#include "ns3/assert.h"
+#include "ns3/fatal-error.h"
+#include "ns3/fatal-impl.h"
+#include "ascii-file.h"
+//
+// This file is used as part of the ns-3 test framework, so please refrain from 
+// adding any ns-3 specific constructs such as Packet to this file.
+//
+namespace ns3 {
+
+AsciiFile::AsciiFile ()
+  : m_file ()
+{
+  FatalImpl::RegisterStream (&m_file);
+}
+
+AsciiFile::~AsciiFile ()
+{
+  FatalImpl::UnregisterStream (&m_file);
+  Close ();
+}
+
+bool 
+AsciiFile::Fail (void) const
+{
+  return m_file.fail ();
+}
+bool 
+AsciiFile::Eof (void) const
+{
+  return m_file.eof ();
+}
+
+void
+AsciiFile::Close (void)
+{
+  m_file.close ();
+}
+
+void
+AsciiFile::Open (std::string const &filename, std::ios::openmode mode)
+{
+  NS_ASSERT ((mode & std::ios::app) == 0);
+  NS_ASSERT (!m_file.fail ());
+
+  m_file.open (filename.c_str (), mode);
+}
+
+void
+AsciiFile::Read (std::string& line)
+{
+  NS_ASSERT (m_file.good ());
+
+  // Read the next line.
+  getline (m_file, line);
+}
+
+bool
+AsciiFile:: Diff (std::string const & f1,
+		  std::string const & f2, 
+		  uint64_t & lineNumber)
+{
+  AsciiFile ascii1, ascii2;
+  ascii1.Open (f1, std::ios::in);
+  ascii2.Open (f2, std::ios::in);
+  bool bad = ascii1.Fail () || ascii2.Fail ();
+  if (bad)
+    {
+      return true;
+    }
+
+  std::string line1;
+  std::string line2;
+  lineNumber = 0;
+  bool diff = false;
+
+  while (!ascii1.Eof () && !ascii2.Eof ())
+    {
+      ascii1.Read (line1);
+      ascii2.Read (line2);
+
+      lineNumber++;
+
+      bool same = ascii1.Fail () == ascii2.Fail ();
+      if (!same)
+        {
+          diff = true;
+          break;
+        }
+      if (ascii1.Eof ())
+        {
+          break;
+        }
+
+      if (line1 != line2)
+        {
+          diff = true; // Lines do not match
+          break;
+        }
+    }
+
+  return diff;
+}
+
+} // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network/utils/ascii-file.h	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of Washington
+ *
+ * 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: Mitch Watrous (watrous@u.washington.edu)
+ *
+ * This file is based on pcap-file.h by Craig Dowell (craigdo@ee.washington.edu)
+ */
+
+#ifndef ASCII_FILE_H
+#define ASCII_FILE_H
+
+#include <string>
+#include <fstream>
+#include <stdint.h>
+
+namespace ns3 {
+
+
+/*
+ * A class representing an ascii file.
+ */
+
+class AsciiFile
+{
+public:
+  AsciiFile ();
+  ~AsciiFile ();
+
+  /**
+   * \return true if the 'fail' bit is set in the underlying iostream, false otherwise.
+   */
+  bool Fail (void) const;
+  /**
+   * \return true if the 'eof' bit is set in the underlying iostream, false otherwise.
+   */
+  bool Eof (void) const;
+  /**
+   * Clear all state bits of the underlying iostream.
+   */
+  /**
+   * Create a new ascii file or open an existing ascii file.
+   *
+   * \param filename String containing the name of the file.
+   * \param mode the access mode for the file.
+   */
+  void Open (std::string const &filename, std::ios::openmode mode);
+
+  /**
+   * Close the underlying file.
+   */
+  void Close (void);
+
+  /**
+   * \brief Read next line from file
+   * 
+   * \param line    [out] line from file
+   * 
+   */
+  void Read (std::string& line);
+
+  /**
+   * \brief Compare two ASCII files line-by-line
+   * 
+   * \return true if files are different, false otherwise
+   * 
+   * \param  f1         First ASCII file name
+   * \param  f2         Second ASCII file name
+   * \param  lineNumbe   [out] Line number of first different line.
+   */
+  static bool Diff (std::string const & f1,
+		    std::string const & f2, 
+                    uint64_t & lineNumber);
+
+private:
+  std::string    m_filename;
+  std::fstream   m_file;
+};
+
+} // namespace ns3
+
+#endif /* ASCII_FILE_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/network/utils/ascii-test.h	Wed Oct 03 10:06:52 2012 -0700
@@ -0,0 +1,49 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of Washington
+ *
+ * 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: Mitch Watrous (watrous@u.washington.edu)
+ *
+ * This file is based on pcap-test.h by Craig Dowell (craigdo@ee.washington.edu)
+ */
+
+#ifndef ASCII_TEST_H
+#define ASCII_TEST_H
+
+#include <stdint.h>
+#include "ascii-file.h"
+#include "ns3/test.h"
+
+/**
+ * \brief Test that a pair of new/reference ascii files are equal
+ *
+ * \param gotFilename The name of the new file to read in including
+ * its path
+ * \param expectedFilename The name of the reference file to read in
+ * including its path
+ */
+#define NS_ASCII_TEST_EXPECT_EQ(gotFilename, expectedFilename)		\
+  do {                                                                  \
+    uint64_t line(0);                                                   \
+    bool diff = AsciiFile::Diff (gotFilename, expectedFilename, line);  \
+    NS_TEST_EXPECT_MSG_EQ (diff, false,                                 \
+                           "ASCII traces " << gotFilename <<            \
+                           " and " << expectedFilename <<               \
+                           " differ starting from line " << line);      \
+  } while (false)
+
+
+#endif /* ASCII_TEST_H */
--- a/src/network/wscript	Fri Sep 28 14:33:15 2012 -0700
+++ b/src/network/wscript	Wed Oct 03 10:06:52 2012 -0700
@@ -23,7 +23,8 @@
         'model/tag.cc',
         'model/tag-buffer.cc',
         'model/trailer.cc',
-	'utils/address-utils.cc',
+        'utils/address-utils.cc',
+        'utils/ascii-file.cc',
         'utils/data-rate.cc',
         'utils/drop-tail-queue.cc',
         'utils/error-model.cc',
@@ -93,7 +94,9 @@
         'model/tag.h',
         'model/tag-buffer.h',
         'model/trailer.h',
-      	'utils/address-utils.h',
+        'utils/address-utils.h',
+        'utils/ascii-file.h',
+        'utils/ascii-test.h',
         'utils/data-rate.h',
         'utils/drop-tail-queue.h',
         'utils/error-model.h',