MOAB: Mesh Oriented datABase  (version 5.2.1)
ExtraDataTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file ExtraDataTest.cpp
00028  *  \brief Test the ExtraData functionality of the PatchData class
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "PatchData.hpp"
00034 #include "PatchDataInstances.hpp"
00035 #include "ExtraData.hpp"
00036 
00037 using namespace MBMesquite;
00038 
00039 #include <iostream>
00040 
00041 class ExtraDataTest : public CppUnit::TestFixture
00042 {
00043   private:
00044     CPPUNIT_TEST_SUITE( ExtraDataTest );
00045     CPPUNIT_TEST( test_initialize );
00046     CPPUNIT_TEST( test_finalize );
00047     CPPUNIT_TEST( test_notify_destroyed );
00048     CPPUNIT_TEST( test_notify_subpatch );
00049     CPPUNIT_TEST( test_notify_new_patch_fill );
00050     CPPUNIT_TEST( test_notify_new_patch_sub );
00051     CPPUNIT_TEST( test_multiple_data );
00052     CPPUNIT_TEST_SUITE_END();
00053 
00054   public:
00055     void test_initialize();
00056     void test_finalize();
00057     void test_notify_destroyed();
00058     void test_notify_subpatch();
00059     void test_notify_new_patch_fill();
00060     void test_notify_new_patch_sub();
00061     void test_multiple_data();
00062 };
00063 
00064 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExtraDataTest, "ExtraDataTest" );
00065 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExtraDataTest, "Unit" );
00066 
00067 class TestExtraData : public MBMesquite::ExtraData
00068 {
00069   public:
00070     enum EventType
00071     {
00072         NONE = 0,
00073         DESTROYED,
00074         NEW,
00075         SUB
00076     };
00077 
00078   private:
00079     EventType lastEvent;
00080     PatchData* subPatchPtr;
00081 
00082   public:
00083     TestExtraData( PatchData& patch ) : ExtraData( patch )
00084     {
00085         clear();
00086     }
00087 
00088     void clear()
00089     {
00090         lastEvent   = NONE;
00091         subPatchPtr = 0;
00092     }
00093     EventType get_last_event() const
00094     {
00095         return lastEvent;
00096     }
00097     PatchData* get_sub_patch_ptr() const
00098     {
00099         return subPatchPtr;
00100     }
00101 
00102     virtual void notify_patch_destroyed();
00103     virtual void notify_new_patch();
00104     virtual void notify_sub_patch( PatchData& sub_patch, const size_t* vertex_map, const size_t* element_map,
00105                                    MsqError& err );
00106 
00107     std::vector< size_t > vertexMap, elementMap;
00108 };
00109 
00110 void TestExtraData::notify_patch_destroyed()
00111 {
00112     lastEvent = DESTROYED;
00113 }
00114 void TestExtraData::notify_new_patch()
00115 {
00116     lastEvent = NEW;
00117 }
00118 void TestExtraData::notify_sub_patch( PatchData& p, const size_t* vertex_map, const size_t* element_map, MsqError& )
00119 {
00120     lastEvent   = SUB;
00121     subPatchPtr = &p;
00122     vertexMap.resize( p.num_nodes() );
00123     elementMap.resize( p.num_elements() );
00124     std::copy( vertex_map, vertex_map + p.num_nodes(), vertexMap.begin() );
00125     std::copy( element_map, element_map + p.num_elements(), elementMap.begin() );
00126 }
00127 
00128 void ExtraDataTest::test_initialize()
00129 {
00130     PatchData patch;
00131     TestExtraData data1( patch );
00132     TestExtraData data2( patch );
00133     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), &patch );
00134     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), &patch );
00135     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NONE );
00136     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NONE );
00137 }
00138 
00139 void ExtraDataTest::test_finalize()
00140 {
00141     PatchData patch;
00142 
00143     // PatchData doesn't expose any public method to
00144     // verify that the ExtraData object was removed when it
00145     // was destroyed!.  Do some funky stuff to be able to
00146     // call the ExtraData destructor w/out invalidating the
00147     // memory of the actual object.
00148     unsigned char* mem  = new unsigned char[sizeof( TestExtraData )];
00149     TestExtraData* data = new( mem ) TestExtraData( patch );
00150     data->~TestExtraData();
00151     CPPUNIT_ASSERT_EQUAL( data->get_patch_data(), (PatchData*)0 );
00152     delete[] mem;
00153 }
00154 
00155 void ExtraDataTest::test_notify_destroyed()
00156 {
00157     PatchData* patch = new PatchData;
00158     TestExtraData data1( *patch );
00159     TestExtraData data2( *patch );
00160 
00161     delete patch;
00162     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::DESTROYED );
00163     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::DESTROYED );
00164     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), (PatchData*)0 );
00165     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), (PatchData*)0 );
00166 }
00167 
00168 void ExtraDataTest::test_notify_subpatch()
00169 {
00170     MsqPrintError err( std::cerr );
00171     PatchData patch, subpatch;
00172     create_four_quads_patch( patch, err );
00173     CPPUNIT_ASSERT( !err );
00174 
00175     TestExtraData data1( patch );
00176     TestExtraData data2( patch );
00177     patch.get_subpatch( 0, 1, subpatch, err );
00178     CPPUNIT_ASSERT( !err );
00179 
00180     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::SUB );
00181     CPPUNIT_ASSERT_EQUAL( data1.get_sub_patch_ptr(), &subpatch );
00182     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), &patch );
00183 
00184     size_t i;
00185     for( i = 0; i < subpatch.num_nodes(); ++i )
00186         CPPUNIT_ASSERT_EQUAL( subpatch.get_vertex_handles_array()[i],
00187                               patch.get_vertex_handles_array()[data1.vertexMap[i]] );
00188     for( i = 0; i < subpatch.num_elements(); ++i )
00189         CPPUNIT_ASSERT_EQUAL( subpatch.get_element_handles_array()[i],
00190                               patch.get_element_handles_array()[data1.elementMap[i]] );
00191 
00192     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::SUB );
00193     CPPUNIT_ASSERT_EQUAL( data2.get_sub_patch_ptr(), &subpatch );
00194     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), &patch );
00195 
00196     for( i = 0; i < subpatch.num_nodes(); ++i )
00197         CPPUNIT_ASSERT_EQUAL( subpatch.get_vertex_handles_array()[i],
00198                               patch.get_vertex_handles_array()[data2.vertexMap[i]] );
00199     for( i = 0; i < subpatch.num_elements(); ++i )
00200         CPPUNIT_ASSERT_EQUAL( subpatch.get_element_handles_array()[i],
00201                               patch.get_element_handles_array()[data2.elementMap[i]] );
00202 }
00203 
00204 void ExtraDataTest::test_notify_new_patch_fill()
00205 {
00206     MsqPrintError err( std::cerr );
00207     PatchData patch;
00208     TestExtraData data1( patch );
00209     TestExtraData data2( patch );
00210 
00211     create_four_quads_patch( patch, err );
00212     CPPUNIT_ASSERT( !err );
00213     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00214     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00215 
00216     data1.clear();
00217     data2.clear();
00218 
00219     create_four_quads_patch( patch, err );
00220     CPPUNIT_ASSERT( !err );
00221     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00222     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00223 }
00224 
00225 void ExtraDataTest::test_notify_new_patch_sub()
00226 {
00227     MsqPrintError err( std::cerr );
00228     PatchData patch, subpatch;
00229     TestExtraData data1( subpatch );
00230     TestExtraData data2( subpatch );
00231 
00232     create_four_quads_patch( patch, err );
00233     CPPUNIT_ASSERT( !err );
00234     patch.get_subpatch( 0, 1, subpatch, err );
00235     CPPUNIT_ASSERT( !err );
00236     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00237     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00238 }
00239 
00240 void ExtraDataTest::test_multiple_data()
00241 {
00242     PatchData* patch;
00243     TestExtraData *data1, *data2, *data3, *data4;
00244 
00245     patch = new PatchData;
00246     data1 = new TestExtraData( *patch );
00247     data2 = new TestExtraData( *patch );
00248     data3 = new TestExtraData( *patch );
00249     data4 = new TestExtraData( *patch );
00250 
00251     delete data2;
00252     delete data4;
00253     CPPUNIT_ASSERT_EQUAL( data1->get_patch_data(), patch );
00254     CPPUNIT_ASSERT_EQUAL( data3->get_patch_data(), patch );
00255 
00256     delete patch;
00257     CPPUNIT_ASSERT_EQUAL( data1->get_last_event(), TestExtraData::DESTROYED );
00258     CPPUNIT_ASSERT_EQUAL( data3->get_last_event(), TestExtraData::DESTROYED );
00259     CPPUNIT_ASSERT_EQUAL( data1->get_patch_data(), (PatchData*)0 );
00260     CPPUNIT_ASSERT_EQUAL( data3->get_patch_data(), (PatchData*)0 );
00261 
00262     delete data1;
00263     delete data3;
00264 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines