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

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


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

Modified:
   cs/cigma/trunk/src/io_file_reader.cpp
   cs/cigma/trunk/src/io_file_reader.h
   cs/cigma/trunk/tests/libcigma/MiscTest.cpp
   cs/cigma/trunk/tests/libcigma/MiscTest.h
   cs/cigma/trunk/tests/libcigma/ReaderTest.cpp
   cs/cigma/trunk/tests/libcigma/ReaderTest.h
Log:
Tests for the FileReader::New() factory

Modified: cs/cigma/trunk/src/io_file_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_file_reader.cpp	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/src/io_file_reader.cpp	2008-10-29 22:12:21 UTC (rev 13191)
@@ -1,66 +1,81 @@
 #include "io_file_reader.h"
-#include "Filesystem.h"
+#include "Exception.h"
 
 // std
 #include <string>
 #include <cstdlib>
 #include <cassert>
+using namespace std;
 
+// boost
+#include "Filesystem.h"
+namespace fs = boost::filesystem;
+using boost::shared_ptr;
+
 // readers
 #include "io_null_reader.h"
-//#include "io_text_reader.h"
+#include "io_text_reader.h"
 //#include "io_hdf5_reader.h"
 //#include "io_vtk_reader.h"
-
-
-using namespace std;
 using namespace cigma;
-namespace fs = boost::filesystem;
 
 
-// ----------------------------------------------------------------------------
-
-FileReader::FileReader()
+shared_ptr<FileReader> FileReader::New(std::string filename, std::string mode)
 {
-}
+    int status = -1;
+    shared_ptr<FileReader> tmp(new NullReader());
 
-FileReader::~FileReader()
-{
-}
-
-// ----------------------------------------------------------------------------
-
-FileReader* FileReader::New(std::string filename, std::string mode)
-{
-    // XXX: return a boost::shared_ptr 
-
-    assert(filename != "");
-
     fs::path p(filename);
 
-    if (!fs::exists(p))
+    if (fs::exists(p))
     {
-        return new NullReader();
-    }
+        string ext = fs::extension(p);
 
-    /*
-    string ext = fs::extension(p);
-
-    if (HDF5_Extension(ext.c_str()))
-    {
-        return new HDF5_Reader();
+        if (is_hdf5_extension(ext.c_str()))
+        {
+            /*
+            tmp.reset(new HDF5_Reader());
+            status = tmp->open(filename.c_str())
+            if (status < 0)
+            {
+                throw cigma::Exception("FileReader::New", string("Could not open HDF5 file ") + filename);
+            } // */
+            throw cigma::Exception("FileReader::New", "Need HDF5_Reader");
+        }
+        else if (is_vtk_extension(ext.c_str()))
+        {
+            /*
+            tmp.reset(new VtkReader());
+            status = tmp->open(filename.c_str());
+            if (status < 0)
+            {
+                throw cigma::Exception("FileReader::New", string("Could not open VTK file ") + filename);
+            } // */
+            throw cigma::Exception("FileReader::New", "Need VtkReader");
+        }
+        else if (is_text_extension(ext.c_str()))
+        {
+            tmp.reset(new TextReader());
+            status = tmp->open(filename.c_str());
+            if (status < 0)
+            {
+                throw cigma::Exception("FileReader::New", string("Could not open text file ") + filename);
+            }
+        }
     }
-    else if (VtkExtension(ext.c_str()))
+    else
     {
-        return new VtkReader();
+        throw cigma::Exception("FileReader::New", string("Could not find file ") + filename);
     }
-    else if (TextExtension(ext.c_str()))
-    {
-        return new TextReader();
-    }
-    */
 
-    return new NullReader();
+    return tmp;
 }
 
-// ----------------------------------------------------------------------------
+FileReader::FileReader()
+{
+}
+
+FileReader::~FileReader()
+{
+}
+

Modified: cs/cigma/trunk/src/io_file_reader.h
===================================================================
--- cs/cigma/trunk/src/io_file_reader.h	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/src/io_file_reader.h	2008-10-29 22:12:21 UTC (rev 13191)
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <boost/noncopyable.hpp>
+#include <boost/shared_ptr.hpp>
 #include "Filesystem.h"
 
 namespace cigma
@@ -27,7 +28,7 @@
     virtual int getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd) = 0;
     virtual int getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs) = 0;
 
-    static FileReader *New(std::string filename, std::string mode);
+    static boost::shared_ptr<FileReader> New(std::string filename, std::string mode);
 
 public:
     typedef enum {

Modified: cs/cigma/trunk/tests/libcigma/MiscTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/MiscTest.cpp	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/tests/libcigma/MiscTest.cpp	2008-10-29 22:12:21 UTC (rev 13191)
@@ -1,11 +1,23 @@
 #include "MiscTest.h"
+using namespace libcigma;
+
+#include <iostream>
 #include <cstdlib>
 #include <cstdio>
+using namespace std;
 
-using namespace libcigma;
+#include <boost/filesystem.hpp>
+namespace fs = boost::filesystem;
 
-MiscTest::MiscTest() {}
-MiscTest::~MiscTest() {}
+/*
+void ReaderTest::test_something()
+{
+    using namespace std;
+    namespace fs = boost::filesystem;
+    fs::path full_path(fs::current_path());
+    cout << endl << full_path.string() << endl;
+}
+*/
 
 void MiscTest::test_000()
 {

Modified: cs/cigma/trunk/tests/libcigma/MiscTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/MiscTest.h	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/tests/libcigma/MiscTest.h	2008-10-29 22:12:21 UTC (rev 13191)
@@ -18,8 +18,8 @@
         CPPUNIT_TEST_SUITE_END();
 
     public:
-        MiscTest();
-        virtual ~MiscTest();
+        MiscTest() {}
+        ~MiscTest() {}
 
         void test_000();
         void test_001();

Modified: cs/cigma/trunk/tests/libcigma/ReaderTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/ReaderTest.cpp	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/tests/libcigma/ReaderTest.cpp	2008-10-29 22:12:21 UTC (rev 13191)
@@ -6,33 +6,38 @@
 #include "io_text_reader.h"
 using namespace cigma;
 
-//#include <boost/filesystem.hpp>
-//#include <iostream>
+#include <boost/shared_ptr.hpp>
+using boost::shared_ptr;
 
+// ----------------------------------------------------------------------------
 
-void ReaderTest::test_something()
+void ReaderTest::test_factory()
 {
-    /*
-    using namespace std;
-    namespace fs = boost::filesystem;
-    fs::path full_path(fs::current_path());
-    cout << endl << full_path.string() << endl;
-    */
+    shared_ptr<FileReader> reader;
+
+    reader = FileReader::New("tests/data/brick1/brick1_connect.dat", "r");
+    CPPUNIT_ASSERT_EQUAL(FileReader::TEXT_FILE_READER, reader->getReaderType());
+
+    //reader = FileReader::New("tests/data/brick1/brick1.h5", "r");
+    //CPPUNIT_ASSERT_EQUAL(FileReader::TEXT_FILE_READER, reader->getReaderType());
+
+    //reader = FileReader::New("tests/data/brick1/brick1.vtk", "r");
+    //CPPUNIT_ASSERT_EQUAL(FileReader::TEXT_FILE_READER, reader->getReaderType());
 }
 
 void ReaderTest::test_null_reader()
 {
     NullReader reader;
-    CPPUNIT_ASSERT_EQUAL(reader.getReaderType(), FileReader::NULL_FILE_READER);
+    CPPUNIT_ASSERT_EQUAL(FileReader::NULL_FILE_READER, reader.getReaderType());
 }
 
 void ReaderTest::test_text_reader()
 {
     int status;
+
     TextReader reader;
+    CPPUNIT_ASSERT_EQUAL(FileReader::TEXT_FILE_READER, reader.getReaderType());
 
-    CPPUNIT_ASSERT_EQUAL(reader.getReaderType(), FileReader::TEXT_FILE_READER);
-
     status = reader.open("tests/data/brick1/brick1_connect.dat");
     CPPUNIT_ASSERT_EQUAL(0, status);
 

Modified: cs/cigma/trunk/tests/libcigma/ReaderTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/ReaderTest.h	2008-10-29 22:12:17 UTC (rev 13190)
+++ cs/cigma/trunk/tests/libcigma/ReaderTest.h	2008-10-29 22:12:21 UTC (rev 13191)
@@ -10,7 +10,7 @@
     class ReaderTest : public CPPUNIT_NS::TestFixture
     {
         CPPUNIT_TEST_SUITE(ReaderTest);
-        CPPUNIT_TEST(test_something);
+        CPPUNIT_TEST(test_factory);
         CPPUNIT_TEST(test_null_reader);
         CPPUNIT_TEST(test_text_reader);
         CPPUNIT_TEST_SUITE_END();
@@ -19,7 +19,7 @@
         ReaderTest() {}
         ~ReaderTest() {}
 
-        void test_something();
+        void test_factory();
         void test_null_reader();
         void test_text_reader();
     };



More information about the CIG-COMMITS mailing list