[cig-commits] r15106 - 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 15:07:06 PDT 2009


Author: brad
Date: 2009-06-02 15:07:05 -0700 (Tue, 02 Jun 2009)
New Revision: 15106

Added:
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.cc
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.hh
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory.timedb
Modified:
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc
   cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.hh
   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/TestTimeHistoryIO.cc
   cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am
Log:
Fixed query bugs. Added testing C++ of TimeHistory.

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc	2009-06-02 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.cc	2009-06-02 22:07:05 UTC (rev 15106)
@@ -70,6 +70,7 @@
   _npts = 0;
 } // close
 
+#include <iostream>
 // ----------------------------------------------------------------------
 // Query the database.
 int
@@ -80,19 +81,20 @@
   
   if (_npts > 1) {
     if (t < _time[_ilower]) {
-      const int imax = _npts-1;
-      while(_ilower < imax) {
+      while (_ilower > 0) {
 	if (t >= _time[_ilower])
 	  break;
-	++_ilower;
+	--_ilower;
       } // while
     } else if (t > _time[_ilower+1]) {
-      while (_ilower > 0) {
+      const int imax = _npts-1;
+      while(_ilower < imax) {
 	if (t <= _time[_ilower+1])
 	  break;
-	--_ilower;
+	++_ilower;
       } // while
     } // if/else
+
     if (t >= _time[_ilower] && t <= _time[_ilower+1]) {
       const double tL = _time[_ilower];
       const double tU = _time[_ilower+1];

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.hh	2009-06-02 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistory.hh	2009-06-02 22:07:05 UTC (rev 15106)
@@ -26,6 +26,7 @@
 /// C++ object for time dependence in spatial databases.
 class spatialdata::spatialdb::TimeHistory
 { // class TimeHistory
+  friend class TestTimeHistory;
 
  public :
   // PUBLIC METHODS /////////////////////////////////////////////////////

Modified: cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc	2009-06-02 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/libsrc/spatialdb/TimeHistoryIO.cc	2009-06-02 22:07:05 UTC (rev 15106)
@@ -131,7 +131,7 @@
       buffer.clear();
       buffer >> (*time)[i];
       buffer >> (*amplitude)[i];
-      (*amplitude)[i] *= scale;
+      (*time)[i] *= scale;
     } // for
     
     if (!filein.good())

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am	2009-06-02 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/Makefile.am	2009-06-02 22:07:05 UTC (rev 15106)
@@ -33,6 +33,7 @@
 	TestSimpleDBVolume3D.cc \
 	TestSimpleIOAscii.cc \
 	TestSpatialDB.cc \
+	TestTimeHistory.cc \
 	TestTimeHistoryIO.cc \
 	TestUniformDB.cc \
 	testcquery.c \
@@ -51,6 +52,8 @@
 	TestSimpleDBArea3D.hh \
 	TestSimpleDBVolume3D.hh \
 	TestSimpleIOAscii.hh \
+	TestTimeHistory.hh \
+	TestTimeHistoryIO.hh \
 	TestUniformDB.hh \
 	testcquery.h \
 	TestSpatialDB.hh

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.cc	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.cc	2009-06-02 22:07:05 UTC (rev 15106)
@@ -0,0 +1,118 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+#include <portinfo>
+
+#include "TestTimeHistory.hh" // implementation of class methods
+
+#include "spatialdata/spatialdb/TimeHistory.hh" // USES TimeHistory
+#include "spatialdata/spatialdb/TimeHistoryIO.hh" // USES TimeHistoryIO
+#include "spatialdata/units/Parser.hh" // USES Parser
+
+// ----------------------------------------------------------------------
+CPPUNIT_TEST_SUITE_REGISTRATION( spatialdata::spatialdb::TestTimeHistory );
+
+// ----------------------------------------------------------------------
+// Test label().
+void
+spatialdata::spatialdb::TestTimeHistory::testLabel(void)
+{ // testLabel
+  const std::string& label = "time history A";
+
+  TimeHistory th;
+  th.label(label.c_str());
+  CPPUNIT_ASSERT_EQUAL(label, std::string(th.label()));
+} // testLabel
+
+// ----------------------------------------------------------------------
+// Test filename().
+void
+spatialdata::spatialdb::TestTimeHistory::testFilename(void)
+{ // testFilename
+  const std::string& filename = "file.th";
+
+  TimeHistory th;
+  th.filename(filename.c_str());
+  CPPUNIT_ASSERT_EQUAL(filename, std::string(th.filename()));
+} // testFilename
+
+// ----------------------------------------------------------------------
+// Test open() and close()
+void
+spatialdata::spatialdb::TestTimeHistory::testOpenClose(void)
+{ // testOpenClose
+  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.data";
+  TimeHistoryIO::write(time, npts, amplitude, npts, timeUnits, filename);
+
+  TimeHistory th;
+  th.filename(filename);
+  th.open();
+
+  units::Parser parser;
+  const double scale = parser.parse(timeUnits);
+  CPPUNIT_ASSERT(scale > 0.0);
+
+  CPPUNIT_ASSERT_EQUAL(0, th._ilower);
+  CPPUNIT_ASSERT_EQUAL(npts, th._npts);
+  CPPUNIT_ASSERT(0 != th._time);
+  CPPUNIT_ASSERT(0 != th._amplitude);
+  const double tolerance = 1.0e-06;
+  for (int i=0; i < npts; ++i) {
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(time[i], th._time[i]/scale, tolerance);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitude[i], th._amplitude[i],
+				 tolerance);
+  } // for
+
+  th.close();
+  CPPUNIT_ASSERT_EQUAL(0, th._npts);
+  CPPUNIT_ASSERT(0 == th._time);
+  CPPUNIT_ASSERT(0 == th._amplitude);
+} // testOpenClose
+
+// ----------------------------------------------------------------------
+// Test query().
+void
+spatialdata::spatialdb::TestTimeHistory::testQuery(void)
+{ // testQuery
+  const char* filename = "data/timehistory.timedb";
+  const int nqueries = 7;
+  const double timeQ[nqueries] =
+    { 0.5, 0.0, 0.6, 2.0, 5.0, 20.0, 8.0 };
+  const double amplitudeE[nqueries] =
+    { 1.0, 0.0, 1.2, 4.0, 2.5, 0.0, 1.0 };
+  const int errQ[nqueries] =
+    { 0, 0, 0, 0, 0, 1, 0 };
+
+  TimeHistory th;
+  th.filename(filename);
+
+  th.open();
+  
+  const double tolerance = 1.0e-06;
+  double amplitude = 0.0;
+  for (int i=0; i < nqueries; ++i) {
+    int err = th.query(&amplitude, timeQ[i]);
+    CPPUNIT_ASSERT_EQUAL(errQ[i], err);
+    if (0 == errQ[i])
+      CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitudeE[i], amplitude, tolerance);
+  } // for
+
+  th.close();
+} // testQuery
+
+
+// End of file 

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.hh
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.hh	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistory.hh	2009-06-02 22:07:05 UTC (rev 15106)
@@ -0,0 +1,67 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+//                           Brad T. Aagaard
+//                        U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+/** @file tests/libtests/spatialdb/TestTimeHistory.hh
+ *
+ * @brief C++ TestTimeHistory object
+ *
+ * C++ unit testing for TimeHistory.
+ */
+
+#if !defined(spatialdata_spatialdb_testtimehistory_hh)
+#define spatialdata_spatialdb_testtimehistory_hh
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/// Namespace for spatial package
+namespace spatialdata {
+  namespace spatialdb {
+    class TestTimeHistory;
+    class TimeHistory; // USES TimeHistory
+  } // spatialdb
+} // spatialdata
+
+/// C++ unit testing for SimpleIO
+class spatialdata::spatialdb::TestTimeHistory : public CppUnit::TestFixture
+{ // class TestTimeHistory
+
+  // CPPUNIT TEST SUITE /////////////////////////////////////////////////
+  CPPUNIT_TEST_SUITE( TestTimeHistory );
+
+  CPPUNIT_TEST( testLabel );
+  CPPUNIT_TEST( testFilename );
+  CPPUNIT_TEST( testOpenClose );
+  CPPUNIT_TEST( testQuery );
+
+  CPPUNIT_TEST_SUITE_END();
+
+  // PUBLIC METHODS /////////////////////////////////////////////////////
+public :
+
+  /// Test label().
+  void testLabel(void);
+
+  /// Test filename().
+  void testFilename(void);
+
+  /// Test open() and close()
+  void testOpenClose(void);
+
+  /// Test query().
+  void testQuery(void);
+
+}; // class TestTimeHistory
+
+#endif // spatialdata_spatialdb_testtimehistory_hh
+
+
+// End of file 

Modified: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc	2009-06-02 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/TestTimeHistoryIO.cc	2009-06-02 22:07:05 UTC (rev 15106)
@@ -47,9 +47,11 @@
 
   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,
+  for (int i=0; i < npts; ++i) {
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(time[i], timeIn[i]/scale, tolerance);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitude[i], amplitudeIn[i],
 				 tolerance);
+  } // for
 } // testIO
 
 // ----------------------------------------------------------------------
@@ -69,9 +71,11 @@
 
   CPPUNIT_ASSERT_EQUAL(npts, nptsIn);
   const double tolerance = 1.0e-06;
-  for (int i=0; i < npts; ++i)
+  for (int i=0; i < npts; ++i) {
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(time[i], timeIn[i], tolerance);
     CPPUNIT_ASSERT_DOUBLES_EQUAL(amplitude[i], amplitudeIn[i],
 				 tolerance);
+  } // for
 } // testReadComments
 
 

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 21:58:40 UTC (rev 15105)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/Makefile.am	2009-06-02 22:07:05 UTC (rev 15106)
@@ -13,7 +13,8 @@
 dist_noinst_DATA = \
 	spatialdb.dat \
 	spatial_comments.dat \
-	timehistory_comments.dat
+	timehistory_comments.dat \
+	timehistory.timedb
 
 noinst_TMP = \
 	spatial.dat \

Added: cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory.timedb
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory.timedb	                        (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/spatialdb/data/timehistory.timedb	2009-06-02 22:07:05 UTC (rev 15106)
@@ -0,0 +1,11 @@
+#TIME HISTORY ascii
+TimeHistory {
+  num-points = 6
+  time-units = second
+}
+  0.000000e+00  0.000000e+00
+  2.000000e-01  4.000000e-01
+  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