[cig-commits] r14653 - in cs/spatialdata-0.1/trunk: libsrc libsrc/units tests/libtests/units
brad at geodynamics.org
brad at geodynamics.org
Fri Apr 10 09:56:10 PDT 2009
Author: brad
Date: 2009-04-10 09:56:09 -0700 (Fri, 10 Apr 2009)
New Revision: 14653
Added:
cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh
cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc
cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh
Modified:
cs/spatialdata-0.1/trunk/libsrc/Makefile.am
cs/spatialdata-0.1/trunk/libsrc/units/Makefile.am
cs/spatialdata-0.1/trunk/libsrc/units/unitsfwd.hh
cs/spatialdata-0.1/trunk/tests/libtests/units/Makefile.am
Log:
Added skeleton and units tests for units parser.
Modified: cs/spatialdata-0.1/trunk/libsrc/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/Makefile.am 2009-04-10 03:16:44 UTC (rev 14652)
+++ cs/spatialdata-0.1/trunk/libsrc/Makefile.am 2009-04-10 16:56:09 UTC (rev 14653)
@@ -41,6 +41,7 @@
spatialdb/UniformDB.cc \
spatialdb/cspatialdb.cc \
units/Nondimensional.cc \
+ units/Parser.cc \
utils/LineParser.cc \
utils/PointsStream.cc
Modified: cs/spatialdata-0.1/trunk/libsrc/units/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Makefile.am 2009-04-10 03:16:44 UTC (rev 14652)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Makefile.am 2009-04-10 16:56:09 UTC (rev 14653)
@@ -16,6 +16,7 @@
subpkginclude_HEADERS = \
Nondimensional.hh \
Nondimensional.icc \
+ Parser.hh \
unitsfwd.hh
noinst_HEADERS =
Added: cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc (rev 0)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Parser.cc 2009-04-10 16:56:09 UTC (rev 14653)
@@ -0,0 +1,56 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+// Brad T. Aagaard
+// U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+#include <portinfo>
+
+#include "Parser.hh" // implementation of class methods
+
+#include <sstream> // USES std::ostringstream
+#include <stdexcept> // USES std::runtime_error
+#include <assert.h> // USES assert()
+
+// ----------------------------------------------------------------------
+// Default constructor
+spatialdata::units::Parser::Parser(void)
+{ // constructor
+} // constructor
+
+// ----------------------------------------------------------------------
+// Default destructor
+spatialdata::units::Parser::~Parser(void)
+{ // destructor
+} // destructor
+
+// ----------------------------------------------------------------------
+// Get SI scaling factor for units given by string. To get value in
+// SI units, multiple value given by units by scaling factor.
+double
+spatialdata::units::Parser::parse(const char* units)
+{ // parse
+ double scale = 1.0;
+
+ /* Replicate Python functionality given by
+ *
+ * import pyre.units
+ * p = pyre.units.parser()
+ * x = p.parse(units) [units is a string]
+ * scale = x.value
+ *
+ * Any nontrivial setup/teardown should be moved to
+ * constructor/destructor.
+ */
+
+ return scale;
+} // parser
+
+
+// End of file
Added: cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh (rev 0)
+++ cs/spatialdata-0.1/trunk/libsrc/units/Parser.hh 2009-04-10 16:56:09 UTC (rev 14653)
@@ -0,0 +1,59 @@
+// -*- C++ -*-
+//
+// ======================================================================
+//
+// Brad T. Aagaard
+// U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ======================================================================
+//
+
+/** @file libsrc/units/Parser.hh
+ *
+ * @brief C++ interface to Pyre units parser.
+ */
+
+#if !defined(spatialdata_units_parser_hh)
+#define spatialdata_units_parser_hh
+
+#include "unitsfwd.hh"
+
+/// C++ interface to Pyre units parser.
+class spatialdata::units::Parser
+{ // class Parser
+ friend class TestParser; // Unit testing
+
+public :
+ // PUBLIC METHODS /////////////////////////////////////////////////////
+
+ /* MATT- I am not sure if we want just a single static method or we
+ * want an object so that any setup/teardown is done once per
+ * instantiation rather than each call to parse.
+ */
+
+ /// Default constructor
+ Parser(void);
+
+ /// Default destructor
+ ~Parser(void);
+
+ /** Get SI scaling factor for units given by string. To get value in
+ * SI units, multiple value given by units by scaling factor.
+ *
+ * @returns Scaling factor to convert to SI units.
+ */
+ double parse(const char* units);
+
+
+private :
+ // PRIVATE MEMBERS ////////////////////////////////////////////////////
+
+
+}; // class Parser
+
+#endif // spatialdata_units_parser_hh
+
+
+// End of file
Modified: cs/spatialdata-0.1/trunk/libsrc/units/unitsfwd.hh
===================================================================
--- cs/spatialdata-0.1/trunk/libsrc/units/unitsfwd.hh 2009-04-10 03:16:44 UTC (rev 14652)
+++ cs/spatialdata-0.1/trunk/libsrc/units/unitsfwd.hh 2009-04-10 16:56:09 UTC (rev 14653)
@@ -24,6 +24,7 @@
namespace spatialdata {
namespace units {
class Nondimensional;
+ class Parser;
} // units
} // spatialdata
Modified: cs/spatialdata-0.1/trunk/tests/libtests/units/Makefile.am
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/units/Makefile.am 2009-04-10 03:16:44 UTC (rev 14652)
+++ cs/spatialdata-0.1/trunk/tests/libtests/units/Makefile.am 2009-04-10 16:56:09 UTC (rev 14653)
@@ -19,10 +19,12 @@
testunits_SOURCES = \
TestNondimensional.cc \
+ TestParser.cc \
testunits.cc
noinst_HEADERS = \
- TestNondimensional.hh
+ TestNondimensional.hh \
+ TestParser.hh
testunits_LDFLAGS =
Added: cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.cc 2009-04-10 16:56:09 UTC (rev 14653)
@@ -0,0 +1,94 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+// Brad T. Aagaard
+// U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+#include <portinfo>
+
+#include "TestParser.hh" // Implementation of class methods
+
+#include "spatialdata/units/Parser.hh" // USES Parser
+
+// ----------------------------------------------------------------------
+CPPUNIT_TEST_SUITE_REGISTRATION( spatialdata::units::TestParser );
+
+// ----------------------------------------------------------------------
+// Test constructor.
+void
+spatialdata::units::TestParser::testConstructor(void)
+{ // testConstructor
+ Parser parser;
+} // testConstructor
+
+// ----------------------------------------------------------------------
+// Test parse() with length scale.
+void
+spatialdata::units::TestParser::testLength(void)
+{ // testLength
+ Parser parser;
+
+ const double tolerance = 1.0e-06;
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(0.01, parser.parse("cm"), tolerance);
+
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0254, parser.parse("inch"), tolerance);
+} // testLength
+
+// ----------------------------------------------------------------------
+// Test parse() with time scale.
+void
+spatialdata::units::TestParser::testTime(void)
+{ // testTime
+ Parser parser;
+
+ const double tolerance = 1.0e-06;
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(60.0, parser.parse("minute"), tolerance);
+
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(0.001, parser.parse("millisecond"), tolerance);
+} // testTime
+
+// ----------------------------------------------------------------------
+// Test parse() with velocity scale.
+void
+spatialdata::units::TestParser::testVelocity(void)
+{ // testVelocity
+ Parser parser;
+
+ const double tolerance = 1.0e-06;
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(0.01, parser.parse("cm/s"), tolerance);
+
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0/3.6, parser.parse("km/hour"), tolerance);
+} // testVelocity
+
+// ----------------------------------------------------------------------
+// Test parse() with density scale.
+void
+spatialdata::units::TestParser::testDensity(void)
+{ // testDensity
+ Parser parser;
+
+ CPPUNIT_ASSERT_EQUAL(1.0, parser.parse("kg/m**3"));
+
+ CPPUNIT_ASSERT_EQUAL(1000.0, parser.parse("g/cm**3"));
+} // testDensity
+
+// ----------------------------------------------------------------------
+// Test parse() with pressure scale.
+void
+spatialdata::units::TestParser::testPressure(void)
+{ // testPressure
+ Parser parser;
+
+ CPPUNIT_ASSERT_EQUAL(1.0, parser.parse("pascal"));
+
+ CPPUNIT_ASSERT_EQUAL(1.0e+06, parser.parse("MPa"));
+} // testPressure
+
+
+// End of file
Added: cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh
===================================================================
--- cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh (rev 0)
+++ cs/spatialdata-0.1/trunk/tests/libtests/units/TestParser.hh 2009-04-10 16:56:09 UTC (rev 14653)
@@ -0,0 +1,74 @@
+// -*- C++ -*-
+//
+// ----------------------------------------------------------------------
+//
+// Brad T. Aagaard
+// U.S. Geological Survey
+//
+// {LicenseText}
+//
+// ----------------------------------------------------------------------
+//
+
+/** @file tests/libtests/units/TestParser.hh
+ *
+ * @brief C++ TestParser object
+ *
+ * C++ unit testing for Parser.
+ */
+
+#if !defined(spatialdata_units_testparser_hh)
+#define spatialdata_units_testparser_hh
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/// Namespace for spatialdata package
+namespace spatialdata {
+ namespace units {
+ class TestParser;
+ } // units
+} // spatialdata
+
+/// C++ unit testing for Parser
+class spatialdata::units::TestParser : public CppUnit::TestFixture
+{ // class TestParser
+
+ // CPPUNIT TEST SUITE /////////////////////////////////////////////////
+ CPPUNIT_TEST_SUITE( TestParser );
+
+ CPPUNIT_TEST( testConstructor );
+ CPPUNIT_TEST( testLength );
+ CPPUNIT_TEST( testTime );
+ CPPUNIT_TEST( testVelocity );
+ CPPUNIT_TEST( testDensity );
+ CPPUNIT_TEST( testPressure );
+
+ CPPUNIT_TEST_SUITE_END();
+
+ // PUBLIC METHODS /////////////////////////////////////////////////////
+public :
+
+ /// Test constructor.
+ void testConstructor(void);
+
+ /// Test parse() with length scale.
+ void testLength(void);
+
+ /// Test parse() with time scale.
+ void testTime(void);
+
+ /// Test parse() with velocity scale.
+ void testVelocity(void);
+
+ /// Test parse() with density scale.
+ void testDensity(void);
+
+ /// Test parse() with pressure scale.
+ void testPressure(void);
+
+}; // class TestParser
+
+#endif // spatialdata_units_testparser_hh
+
+
+// End of file
More information about the CIG-COMMITS
mailing list