[cig-commits] r15103 - in cs/spatialdata-0.1/trunk: libsrc/spatialdb tests/libtests/spatialdb tests/libtests/spatialdb/data

brad at geodynamics.org brad at geodynamics.org
Tue Jun 2 11:21:13 PDT 2009


Author: brad
Date: 2009-06-02 11:21:13 -0700 (Tue, 02 Jun 2009)
New Revision: 15103

Added:
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.hh
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory_comments.dat
Modified:
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am
Log:
Fixed bugs in TimeHistoryIO. Added C++ unit test.

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc	2009-06-02 16:21:57 UTC (rev 15102)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc	2009-06-02 18:21:13 UTC (rev 15103)
@@ -15,6 +15,7 @@
 #include "TimeHistoryIO.hh" // Implementation of class methods
 
 #include "spatialdata/utils/LineParser.hh" // USES LineParser
+#include "spatialdata/units/Parser.hh" // USES Parser
 
 #include <fstream> // USES std::ifstream, std::ofstream
 #include <iomanip> // USES setw(), setiosflags(), resetiosflags()
@@ -117,6 +118,9 @@
     } // if
     if (!ok)
       throw std::runtime_error(msg.str());
+
+    units::Parser uparser;
+    const double scale = uparser.parse(timeUnits.c_str());
     
     const int size = *npts;
     delete[] *time; *time = (size > 0) ? new double[size] : 0;
@@ -125,8 +129,9 @@
     for (int i=0; i < size; ++i) {
       buffer.str(parser.next());
       buffer.clear();
-      buffer >> (*time[i]);
-      buffer >> (*amplitude[i]);
+      buffer >> (*time)[i];
+      buffer >> (*amplitude)[i];
+      (*amplitude)[i] *= scale;
     } // for
     
     if (!filein.good())
@@ -182,7 +187,7 @@
       << HEADER << "\n"
       << "TimeHistory {\n"
       << "  num-points = " << std::setw(6) << npts << "\n"
-      << "  time-units =" << timeUnits << "\n"
+      << "  time-units = " << timeUnits << "\n"
       << "}\n";
     if (!fileout.good())
       throw std::runtime_error("I/O error while writing TimeHistory "

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am	2009-06-02 16:21:57 UTC (rev 15102)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am	2009-06-02 18:21:13 UTC (rev 15103)
@@ -33,6 +33,7 @@
 	TestSimpleDBVolume3D.cc \
 	TestSimpleIOAscii.cc \
 	TestSpatialDB.cc \
+	TestTimeHistoryIO.cc \
 	TestUniformDB.cc \
 	testcquery.c \
 	testspatial.cc
@@ -57,7 +58,7 @@
 
 testspatial_LDFLAGS =
 
-INCLUDES +=
+AM_CPPFLAGS = $(PYTHON_EGG_CPPFLAGS) -I$(PYTHON_INCDIR) 
 
 testspatial_LDADD = \
 	-lcppunit -ldl \

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc	2009-06-02 18:21:13 UTC (rev 15103)
@@ -0,0 +1,78 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+#include <portinfo>
+
+#include "TestTimeHistoryIO.hh" // Implementation of class methods
+
+#include "spatialdata/spatialdb/TimeHistoryIO.hh" // USES TimeHistoryIO
+#include "spatialdata/units/Parser.hh" // USES Parser
+
+#include <string.h> // USES strcmp()
+#include <sstream> // USES std::ostringstream
+
+// ----------------------------------------------------------------------
+CPPUNIT_TEST_SUITE_REGISTRATION( spatialdata::spatialdb::TestTimeHistoryIO );
+
+// ----------------------------------------------------------------------
+// Test write(), read().
+void
+spatialdata::spatialdb::TestTimeHistoryIO::testIO(void)
+{ // testIO
+  const int npts = 6;
+  const double time[npts] = { 0.0, 0.2, 0.8, 1.0, 2.0, 10.0 };
+  const double amplitude[npts] = { 0.0, 0.4, 1.6, 2.0, 4.0, 0.0 };
+  const char* timeUnits = "minute";
+
+  const char* filename = "data/timehistory.dat";
+  TimeHistoryIO::write(time, npts, amplitude, npts, timeUnits, filename);
+
+  int nptsIn = 0;
+  double* timeIn = 0;
+  double* amplitudeIn = 0;
+  TimeHistoryIO::read(&timeIn, &amplitudeIn, &nptsIn, filename);
+
+  units::Parser parser;
+  const double scale = parser.parse(timeUnits);
+  CPPUNIT_ASSERT(scale > 0.0);
+
+  CPPUNIT_ASSERT_EQUAL(npts, nptsIn);
+  const double tolerance = 1.0e-06;
+  for (int i=0; i < npts; ++i)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitude[i], amplitudeIn[i]/scale,
+				 tolerance);
+} // testIO
+
+// ----------------------------------------------------------------------
+// Test read() with time history file that contains comments.
+void
+spatialdata::spatialdb::TestTimeHistoryIO::testReadComments(void)
+{ // testReadComments
+  const int npts = 6;
+  const double time[npts] = { 0.0, 0.2, 0.8, 1.0, 2.0, 10.0 };
+  const double amplitude[npts] = { 0.0, 0.4, 1.6, 2.0, 4.0, 0.0 };
+  const char* filename = "data/timehistory_comments.dat";
+
+  int nptsIn = 0;
+  double* timeIn = 0;
+  double* amplitudeIn = 0;
+  TimeHistoryIO::read(&timeIn, &amplitudeIn, &nptsIn, filename);
+
+  CPPUNIT_ASSERT_EQUAL(npts, nptsIn);
+  const double tolerance = 1.0e-06;
+  for (int i=0; i < npts; ++i)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitude[i], amplitudeIn[i],
+				 tolerance);
+} // testReadComments
+
+
+// End of file 

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.hh
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.hh	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.hh	2009-06-02 18:21:13 UTC (rev 15103)
@@ -0,0 +1,57 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+/** @file tests/libtests/spatialdb/TestTimeHistoryIO.hh
+ *
+ * @brief C++ TestTimeHistoryIO object
+ *
+ * C++ unit testing for TimeHistoryIO.
+ */
+
+#if !defined(spatialdata_spatialdb_testtimehistoryio_hh)
+#define spatialdata_spatialdb_testtimehistoryio_hh
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/// Namespace for spatialdata package
+namespace spatialdata {
+  namespace spatialdb {
+    class TestTimeHistoryIO;
+    class TimeHistoryIO; // USES TimeHistoryIO
+  } // spatialdb
+} // spatialdata
+
+/// C++ unit testing for TimeHistoryIO
+class spatialdata::spatialdb::TestTimeHistoryIO : public CppUnit::TestFixture
+{ // class TestTimeHistoryIO
+
+  // CPPUNIT TEST SUITE /////////////////////////////////////////////////
+  CPPUNIT_TEST_SUITE( TestTimeHistoryIO );
+  CPPUNIT_TEST( testIO );
+  CPPUNIT_TEST( testReadComments );
+  CPPUNIT_TEST_SUITE_END();
+
+  // PUBLIC METHODS /////////////////////////////////////////////////////
+public :
+
+  /// Test read(), write().
+  void testIO(void);
+
+  /// Test read() with time history file that contains comments.
+  void testReadComments(void);
+
+}; // class TestTimeHistoryIO
+
+#endif // spatialdata_spatialdb_testtimehistoryio_hh
+
+
+// End of file 

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am	2009-06-02 16:21:57 UTC (rev 15102)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am	2009-06-02 18:21:13 UTC (rev 15103)
@@ -12,10 +12,12 @@
 
 dist_noinst_DATA = \
 	spatialdb.dat \
-	spatial_comments.dat
+	spatial_comments.dat \
+	timehistory_comments.dat
 
 noinst_TMP = \
-	spatial.dat
+	spatial.dat \
+	timehistory.dat
 
 noinst_HEADERS = \
 	header.hh

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory_comments.dat
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory_comments.dat	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory_comments.dat	2009-06-02 18:21:13 UTC (rev 15103)
@@ -0,0 +1,13 @@
+#TIME HISTORY ascii
+TimeHistory {
+  num-points =      6 // This is a comment
+  time-units = second
+// Just another comment
+}
+// Columns are time and amplitude.
+  0.000000e+00  0.000000e+00
+  2.000000e-01  4.000000e-01 // Add a comment here
+  8.000000e-01  1.600000e+00
+  1.000000e+00  2.000000e+00
+  2.000000e+00  4.000000e+00
+  1.000000e+01  0.000000e+00



More information about the CIG-COMMITS mailing list