MOAB: Mesh Oriented datABase  (version 5.4.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) [email protected]
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,
00105                                    const size_t* vertex_map,
00106                                    const size_t* element_map,
00107                                    MsqError& err );
00108 
00109     std::vector< size_t > vertexMap, elementMap;
00110 };
00111 
00112 void TestExtraData::notify_patch_destroyed()
00113 {
00114     lastEvent = DESTROYED;
00115 }
00116 void TestExtraData::notify_new_patch()
00117 {
00118     lastEvent = NEW;
00119 }
00120 void TestExtraData::notify_sub_patch( PatchData& p, const size_t* vertex_map, const size_t* element_map, MsqError& )
00121 {
00122     lastEvent   = SUB;
00123     subPatchPtr = &p;
00124     vertexMap.resize( p.num_nodes() );
00125     elementMap.resize( p.num_elements() );
00126     std::copy( vertex_map, vertex_map + p.num_nodes(), vertexMap.begin() );
00127     std::copy( element_map, element_map + p.num_elements(), elementMap.begin() );
00128 }
00129 
00130 void ExtraDataTest::test_initialize()
00131 {
00132     PatchData patch;
00133     TestExtraData data1( patch );
00134     TestExtraData data2( patch );
00135     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), &patch );
00136     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), &patch );
00137     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NONE );
00138     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NONE );
00139 }
00140 
00141 void ExtraDataTest::test_finalize()
00142 {
00143     PatchData patch;
00144 
00145     // PatchData doesn't expose any public method to
00146     // verify that the ExtraData object was removed when it
00147     // was destroyed!.  Do some funky stuff to be able to
00148     // call the ExtraData destructor w/out invalidating the
00149     // memory of the actual object.
00150     unsigned char* mem  = new unsigned char[sizeof( TestExtraData )];
00151     TestExtraData* data = new( mem ) TestExtraData( patch );
00152     data->~TestExtraData();
00153     CPPUNIT_ASSERT_EQUAL( data->get_patch_data(), (PatchData*)0 );
00154     delete[] mem;
00155 }
00156 
00157 void ExtraDataTest::test_notify_destroyed()
00158 {
00159     PatchData* patch = new PatchData;
00160     TestExtraData data1( *patch );
00161     TestExtraData data2( *patch );
00162 
00163     delete patch;
00164     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::DESTROYED );
00165     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::DESTROYED );
00166     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), (PatchData*)0 );
00167     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), (PatchData*)0 );
00168 }
00169 
00170 void ExtraDataTest::test_notify_subpatch()
00171 {
00172     MsqPrintError err( std::cerr );
00173     PatchData patch, subpatch;
00174     create_four_quads_patch( patch, err );
00175     CPPUNIT_ASSERT( !err );
00176 
00177     TestExtraData data1( patch );
00178     TestExtraData data2( patch );
00179     patch.get_subpatch( 0, 1, subpatch, err );
00180     CPPUNIT_ASSERT( !err );
00181 
00182     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::SUB );
00183     CPPUNIT_ASSERT_EQUAL( data1.get_sub_patch_ptr(), &subpatch );
00184     CPPUNIT_ASSERT_EQUAL( data1.get_patch_data(), &patch );
00185 
00186     size_t i;
00187     for( i = 0; i < subpatch.num_nodes(); ++i )
00188         CPPUNIT_ASSERT_EQUAL( subpatch.get_vertex_handles_array()[i],
00189                               patch.get_vertex_handles_array()[data1.vertexMap[i]] );
00190     for( i = 0; i < subpatch.num_elements(); ++i )
00191         CPPUNIT_ASSERT_EQUAL( subpatch.get_element_handles_array()[i],
00192                               patch.get_element_handles_array()[data1.elementMap[i]] );
00193 
00194     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::SUB );
00195     CPPUNIT_ASSERT_EQUAL( data2.get_sub_patch_ptr(), &subpatch );
00196     CPPUNIT_ASSERT_EQUAL( data2.get_patch_data(), &patch );
00197 
00198     for( i = 0; i < subpatch.num_nodes(); ++i )
00199         CPPUNIT_ASSERT_EQUAL( subpatch.get_vertex_handles_array()[i],
00200                               patch.get_vertex_handles_array()[data2.vertexMap[i]] );
00201     for( i = 0; i < subpatch.num_elements(); ++i )
00202         CPPUNIT_ASSERT_EQUAL( subpatch.get_element_handles_array()[i],
00203                               patch.get_element_handles_array()[data2.elementMap[i]] );
00204 }
00205 
00206 void ExtraDataTest::test_notify_new_patch_fill()
00207 {
00208     MsqPrintError err( std::cerr );
00209     PatchData patch;
00210     TestExtraData data1( patch );
00211     TestExtraData data2( patch );
00212 
00213     create_four_quads_patch( patch, err );
00214     CPPUNIT_ASSERT( !err );
00215     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00216     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00217 
00218     data1.clear();
00219     data2.clear();
00220 
00221     create_four_quads_patch( patch, err );
00222     CPPUNIT_ASSERT( !err );
00223     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00224     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00225 }
00226 
00227 void ExtraDataTest::test_notify_new_patch_sub()
00228 {
00229     MsqPrintError err( std::cerr );
00230     PatchData patch, subpatch;
00231     TestExtraData data1( subpatch );
00232     TestExtraData data2( subpatch );
00233 
00234     create_four_quads_patch( patch, err );
00235     CPPUNIT_ASSERT( !err );
00236     patch.get_subpatch( 0, 1, subpatch, err );
00237     CPPUNIT_ASSERT( !err );
00238     CPPUNIT_ASSERT_EQUAL( data1.get_last_event(), TestExtraData::NEW );
00239     CPPUNIT_ASSERT_EQUAL( data2.get_last_event(), TestExtraData::NEW );
00240 }
00241 
00242 void ExtraDataTest::test_multiple_data()
00243 {
00244     PatchData* patch;
00245     TestExtraData *data1, *data2, *data3, *data4;
00246 
00247     patch = new PatchData;
00248     data1 = new TestExtraData( *patch );
00249     data2 = new TestExtraData( *patch );
00250     data3 = new TestExtraData( *patch );
00251     data4 = new TestExtraData( *patch );
00252 
00253     delete data2;
00254     delete data4;
00255     CPPUNIT_ASSERT_EQUAL( data1->get_patch_data(), patch );
00256     CPPUNIT_ASSERT_EQUAL( data3->get_patch_data(), patch );
00257 
00258     delete patch;
00259     CPPUNIT_ASSERT_EQUAL( data1->get_last_event(), TestExtraData::DESTROYED );
00260     CPPUNIT_ASSERT_EQUAL( data3->get_last_event(), TestExtraData::DESTROYED );
00261     CPPUNIT_ASSERT_EQUAL( data1->get_patch_data(), (PatchData*)0 );
00262     CPPUNIT_ASSERT_EQUAL( data3->get_patch_data(), (PatchData*)0 );
00263 
00264     delete data1;
00265     delete data3;
00266 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines