[cig-commits] r13190 - in cs/cigma/trunk: . src tests/libcigma

luis at geodynamics.org luis at geodynamics.org
Wed Oct 29 15:12:18 PDT 2008


Author: luis
Date: 2008-10-29 15:12:17 -0700 (Wed, 29 Oct 2008)
New Revision: 13190

Added:
   cs/cigma/trunk/src/FunctionRegistry.cpp
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/src/Function.cpp
   cs/cigma/trunk/src/Function.h
   cs/cigma/trunk/src/FunctionRegistry.h
   cs/cigma/trunk/src/fn_zero.cpp
   cs/cigma/trunk/tests/libcigma/FunctionTest.cpp
   cs/cigma/trunk/tests/libcigma/FunctionTest.h
Log:
Tests for cigma::Function

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/Makefile.am	2008-10-29 22:12:17 UTC (rev 13190)
@@ -130,6 +130,10 @@
 	src/DofHandler.cpp \
 	src/Function.h \
 	src/Function.cpp \
+	src/FunctionRegistry.h \
+	src/FunctionRegistry.cpp \
+	src/fn_zero.h \
+	src/fn_zero.cpp \
 	src/io_file_reader.h \
 	src/io_file_reader.cpp \
 	src/io_null_reader.h \

Modified: cs/cigma/trunk/src/Function.cpp
===================================================================
--- cs/cigma/trunk/src/Function.cpp	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/src/Function.cpp	2008-10-29 22:12:17 UTC (rev 13190)
@@ -1,33 +1,24 @@
 #include "Function.h"
-//#include "fn_zero.h"
+#include "FunctionRegistry.h"
+#include "fn_zero.h"
 
 using namespace cigma;
 
 // ----------------------------------------------------------------------------
 
-Function::Function() {}
-Function::~Function() {}
-
-// ----------------------------------------------------------------------------
-
-
-boost::shared_ptr<Function> Function::New(const char *source)
+Function* Function::New(const char *source)
 {
+    //
+    // XXX: Change the return type to boost::shared_ptr<Function>
     // See http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/sp_techniques.html#encapsulation
     //
-    // XXX: now, depending on 'source', we should allocate the correct subclass
-    // of Function (Field, UserFunction, etc..). 
-    //
-    boost::shared_ptr<Function> pf;
+    static FunctionRegistry registry;
+    return registry.getFunction(source);
+}
 
-    /* // example
-    if (source indicates we should use ZeroFunction)
-    {
-        boost::shared_ptr<ZeroFunction> temp(new ZeroFunction(...))
-        pf = temp;
-    }*/
+// ----------------------------------------------------------------------------
 
-    return pf;
-}
+Function::Function() {}
+Function::~Function() {}
 
 // ----------------------------------------------------------------------------

Modified: cs/cigma/trunk/src/Function.h
===================================================================
--- cs/cigma/trunk/src/Function.h	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/src/Function.h	2008-10-29 22:12:17 UTC (rev 13190)
@@ -1,7 +1,7 @@
 #ifndef __CIGMA_FUNCTION_H__
 #define __CIGMA_FUNCTION_H__
 
-#include <boost/smart_ptr.hpp>
+#include <boost/shared_ptr.hpp>
 
 namespace cigma
 {
@@ -18,7 +18,7 @@
     virtual int n_rank() const = 0;
     virtual bool eval(double *point, double *value) = 0;
 
-    static boost::shared_ptr<Function> New(const char *source);
+    static Function* New(const char *source); // XXX: use boost::shared_ptr<Function>
 };
 
 #endif

Added: cs/cigma/trunk/src/FunctionRegistry.cpp
===================================================================
--- cs/cigma/trunk/src/FunctionRegistry.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/FunctionRegistry.cpp	2008-10-29 22:12:17 UTC (rev 13190)
@@ -0,0 +1,66 @@
+#include "FunctionRegistry.h"
+#include "fn_zero.h"
+#include "Exception.h"
+using namespace cigma;
+
+#include <cassert>
+using namespace std;
+
+FunctionRegistry::FunctionRegistry()
+{
+    // 
+    // Initial set of functions
+    //
+    ZeroFunction *zero = new ZeroFunction();
+    this->addFunction("zero", zero);
+
+    /*
+    UnitScalarFunction *one = new UnitScalarFunction();
+    this->addFunction("one", one);
+
+    typedef cigma::test_functions::Cube CubeBM
+    CubeBM *cube = new CubeBM();
+    this->addFunction("test.cube", cube);
+
+    typedef cigma::test_functions::Square SquareBM;
+    SquareBM *square = new SquareBM();
+    this->addFunction("test.square", square);
+    // */
+}
+
+FunctionRegistry::~FunctionRegistry()
+{
+    FunctionMap::iterator it;
+    for (it = functions.begin(); it != functions.end(); ++it)
+    {
+        Function *fn = it->second;
+        if (fn != 0)
+        {
+            delete fn;
+        }
+    }
+}
+
+void FunctionRegistry::addFunction(std::string name, Function *function)
+{
+    assert(function != 0);
+    functions[name] = function;
+}
+
+bool FunctionRegistry::hasFunction(std::string name)
+{
+    FunctionMap::iterator it = functions.find(name);
+    return (it != functions.end());
+}
+
+Function* FunctionRegistry::getFunction(std::string name)
+{
+    Function *fn = 0;
+    FunctionMap::iterator it = functions.find(name);
+    if (it != functions.end())
+    {
+        fn = it->second;
+    }
+    return fn;
+}
+

Modified: cs/cigma/trunk/src/FunctionRegistry.h
===================================================================
--- cs/cigma/trunk/src/FunctionRegistry.h	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/src/FunctionRegistry.h	2008-10-29 22:12:17 UTC (rev 13190)
@@ -7,21 +7,22 @@
 
 namespace cigma
 {
-    class FunctionRegistry
+    class FunctionRegistry;
 }
 
+/**
+ * Internal class for use in the Function::New() factory.
+ */
 class cigma::FunctionRegistry
 {
 public:
     FunctionRegistry();
     ~FunctionRegistry();
-
-    void initialize();
     
-    void addField(std::string name, Field *field);
-    Field *getField(std::string name);
+    void addFunction(std::string name, Function *field);
+    bool hasFunction(std::string name);
+    Function* getFunction(std::string name);
 
-private:
     typedef std::map<std::string,Function*> FunctionMap;
     FunctionMap functions;
 };

Modified: cs/cigma/trunk/src/fn_zero.cpp
===================================================================
--- cs/cigma/trunk/src/fn_zero.cpp	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/src/fn_zero.cpp	2008-10-29 22:12:17 UTC (rev 13190)
@@ -1,17 +1,19 @@
-#include "Zero.h"
-
+#include "fn_zero.h"
 using namespace cigma;
 
-ZeroFunction::ZeroFunction()
+ZeroFunction::ZeroFunction() : dim(1), rank(1)
 {
-    dim = 1;
-    rank = 1;
 }
 
 ZeroFunction::~ZeroFunction()
 {
 }
 
+void ZeroFunction::setShape(int dim, int rank)
+{
+    this->dim = dim;
+    this->rank = rank;
+}
 
 bool ZeroFunction::eval(double *point, double *value)
 {

Modified: cs/cigma/trunk/tests/libcigma/FunctionTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/FunctionTest.cpp	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/tests/libcigma/FunctionTest.cpp	2008-10-29 22:12:17 UTC (rev 13190)
@@ -2,9 +2,42 @@
 using namespace libcigma;
 
 #include "Function.h"
+#include "fn_zero.h"
 using namespace cigma;
 
-void FunctionTest::test_something()
+const double delta = 1e-8;
+
+void FunctionTest::test_factory()
 {
+    double x = 1;
+    double value = -1;
+    bool status;
+
+    Function *fn = Function::New("zero");
+    CPPUNIT_ASSERT(fn != 0);
+
+    status = fn->eval(&x, &value);
+    CPPUNIT_ASSERT_EQUAL(true, status);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(0, value, delta);
+
+    Function *asdf = Function::New("asdf");
+    CPPUNIT_ASSERT(asdf == 0);
 }
 
+void FunctionTest::test_zero()
+{
+    double point[3] = {0.0, 0.0, 0.0};
+    double value = -1;
+    bool status = false;
+
+    ZeroFunction zero;
+
+    zero.setShape(3,1);
+    CPPUNIT_ASSERT_EQUAL(3, zero.n_dim());
+    CPPUNIT_ASSERT_EQUAL(1, zero.n_rank());
+
+    status = zero.eval(point, &value);
+    CPPUNIT_ASSERT_EQUAL(true, status);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(0, value, delta);
+}
+

Modified: cs/cigma/trunk/tests/libcigma/FunctionTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/FunctionTest.h	2008-10-29 22:12:16 UTC (rev 13189)
+++ cs/cigma/trunk/tests/libcigma/FunctionTest.h	2008-10-29 22:12:17 UTC (rev 13190)
@@ -10,13 +10,15 @@
     class FunctionTest : public CPPUNIT_NS::TestFixture
     {
         CPPUNIT_TEST_SUITE(FunctionTest);
-        CPPUNIT_TEST(test_something);
+        CPPUNIT_TEST(test_factory);
+        CPPUNIT_TEST(test_zero);
         CPPUNIT_TEST_SUITE_END();
 
     public:
         FunctionTest() {}
         ~FunctionTest() {}
-        void test_something();
+        void test_factory();
+        void test_zero();
     };
 };
 



More information about the CIG-COMMITS mailing list