MOAB: Mesh Oriented datABase  (version 5.2.1)
NodeSetTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retian certain rights to 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 NodeSetTest.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "NodeSet.hpp"
00034 #include "UnitUtil.hpp"
00035 
00036 using namespace MBMesquite;
00037 using namespace std;
00038 
00039 class NodeSetTest : public CppUnit::TestFixture
00040 {
00041   private:
00042     CPPUNIT_TEST_SUITE( NodeSetTest );
00043     CPPUNIT_TEST( test_init );
00044     CPPUNIT_TEST( test_clear );
00045     CPPUNIT_TEST( test_set_get_clear_simple );
00046     CPPUNIT_TEST( test_set_get_clear_dim );
00047     CPPUNIT_TEST( test_set_node );
00048     CPPUNIT_TEST( test_clear_node );
00049     CPPUNIT_TEST( test_num_nodes );
00050     CPPUNIT_TEST( test_have_any );
00051     CPPUNIT_TEST( test_set_all );
00052     CPPUNIT_TEST( test_clear_all );
00053     CPPUNIT_TEST( test_num_before );
00054     CPPUNIT_TEST_SUITE_END();
00055 
00056   public:
00057     void test_init();
00058     void test_clear();
00059     void test_set_get_clear_simple();
00060     void test_set_get_clear_dim();
00061     void test_set_node();
00062     void test_clear_node();
00063     void test_num_nodes();
00064     void test_have_any();
00065     void test_set_all();
00066     void test_clear_all();
00067     void test_num_before();
00068 };
00069 
00070 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NodeSetTest, "NodeSetTest" );
00071 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NodeSetTest, "Unit" );
00072 
00073 void NodeSetTest::test_init()
00074 {
00075     NodeSet set;
00076     CPPUNIT_ASSERT( !set.get_bits() );
00077 }
00078 
00079 void NodeSetTest::test_clear()
00080 {
00081     NodeSet set;
00082     set.set_corner_node( 1 );
00083     set.clear();
00084     CPPUNIT_ASSERT( !set.get_bits() );
00085     set.set_mid_edge_node( 2 );
00086     set.clear();
00087     CPPUNIT_ASSERT( !set.get_bits() );
00088     set.set_mid_region_node();
00089     set.clear();
00090     CPPUNIT_ASSERT( !set.get_bits() );
00091 }
00092 
00093 void NodeSetTest::test_set_get_clear_simple()
00094 {
00095     NodeSet set;
00096     for( unsigned i = 0; i < NodeSet::NUM_CORNER_BITS; ++i )
00097     {
00098         CPPUNIT_ASSERT( !set.corner_node( i ) );
00099         set.set_corner_node( i );
00100         CPPUNIT_ASSERT( set.corner_node( i ) );
00101         set.clear_corner_node( i );
00102         CPPUNIT_ASSERT( !set.corner_node( i ) );
00103     }
00104     for( unsigned i = 0; i < NodeSet::NUM_EDGE_BITS; ++i )
00105     {
00106         CPPUNIT_ASSERT( !set.mid_edge_node( i ) );
00107         set.set_mid_edge_node( i );
00108         CPPUNIT_ASSERT( set.mid_edge_node( i ) );
00109         set.clear_mid_edge_node( i );
00110         CPPUNIT_ASSERT( !set.mid_edge_node( i ) );
00111     }
00112     for( unsigned i = 0; i < NodeSet::NUM_FACE_BITS; ++i )
00113     {
00114         CPPUNIT_ASSERT( !set.mid_face_node( i ) );
00115         set.set_mid_face_node( i );
00116         CPPUNIT_ASSERT( set.mid_face_node( i ) );
00117         set.clear_mid_face_node( i );
00118         CPPUNIT_ASSERT( !set.mid_face_node( i ) );
00119     }
00120     for( unsigned i = 0; i < NodeSet::NUM_REGION_BITS; ++i )
00121     {
00122         CPPUNIT_ASSERT( !set.mid_region_node( i ) );
00123         set.set_mid_region_node( i );
00124         CPPUNIT_ASSERT( set.mid_region_node( i ) );
00125         set.clear_mid_region_node( i );
00126         CPPUNIT_ASSERT( !set.mid_region_node( i ) );
00127     }
00128 }
00129 
00130 void NodeSetTest::test_set_get_clear_dim()
00131 {
00132     NodeSet set;
00133     for( unsigned i = 0; i < NodeSet::NUM_CORNER_BITS; ++i )
00134     {
00135         CPPUNIT_ASSERT( !set.node( Sample( 0, i ) ) );
00136         set.set_node( Sample( 0, i ) );
00137         CPPUNIT_ASSERT( set.node( Sample( 0, i ) ) );
00138         set.clear_node( Sample( 0, i ) );
00139         CPPUNIT_ASSERT( !set.node( Sample( 0, i ) ) );
00140     }
00141     for( unsigned i = 0; i < NodeSet::NUM_EDGE_BITS; ++i )
00142     {
00143         CPPUNIT_ASSERT( !set.node( Sample( 1, i ) ) );
00144         set.set_node( Sample( 1, i ) );
00145         CPPUNIT_ASSERT( set.node( Sample( 1, i ) ) );
00146         set.clear_node( Sample( 1, i ) );
00147         CPPUNIT_ASSERT( !set.node( Sample( 1, i ) ) );
00148     }
00149     for( unsigned i = 0; i < NodeSet::NUM_FACE_BITS; ++i )
00150     {
00151         CPPUNIT_ASSERT( !set.node( Sample( 2, i ) ) );
00152         set.set_node( Sample( 2, i ) );
00153         CPPUNIT_ASSERT( set.node( Sample( 2, i ) ) );
00154         set.clear_node( Sample( 2, i ) );
00155         CPPUNIT_ASSERT( !set.node( Sample( 2, i ) ) );
00156     }
00157     for( unsigned i = 0; i < NodeSet::NUM_REGION_BITS; ++i )
00158     {
00159         CPPUNIT_ASSERT( !set.node( Sample( 3, i ) ) );
00160         set.set_node( Sample( 3, i ) );
00161         CPPUNIT_ASSERT( set.node( Sample( 3, i ) ) );
00162         set.clear_node( Sample( 3, i ) );
00163         CPPUNIT_ASSERT( !set.node( Sample( 3, i ) ) );
00164     }
00165 }
00166 
00167 void NodeSetTest::test_set_node()
00168 {
00169     NodeSet set;
00170     for( unsigned i = 0; i < NodeSet::NUM_CORNER_BITS; ++i )
00171     {
00172         set.set_corner_node( i );
00173         CPPUNIT_ASSERT_EQUAL( 1u << ( NodeSet::CORNER_OFFSET + i ), set.get_bits() );
00174         set.clear();
00175     }
00176     for( unsigned i = 0; i < NodeSet::NUM_EDGE_BITS; ++i )
00177     {
00178         set.set_mid_edge_node( i );
00179         CPPUNIT_ASSERT_EQUAL( 1u << ( NodeSet::EDGE_OFFSET + i ), set.get_bits() );
00180         set.clear();
00181     }
00182     for( unsigned i = 0; i < NodeSet::NUM_FACE_BITS; ++i )
00183     {
00184         set.set_mid_face_node( i );
00185         CPPUNIT_ASSERT_EQUAL( 1u << ( NodeSet::FACE_OFFSET + i ), set.get_bits() );
00186         set.clear();
00187     }
00188     for( unsigned i = 0; i < NodeSet::NUM_REGION_BITS; ++i )
00189     {
00190         set.set_mid_region_node( i );
00191         CPPUNIT_ASSERT_EQUAL( 1u << ( NodeSet::REGION_OFFSET + i ), set.get_bits() );
00192         set.clear();
00193     }
00194 }
00195 
00196 void NodeSetTest::test_clear_node()
00197 {
00198     const EntityTopology type = HEXAHEDRON;
00199     NodeSet set;
00200     NodeSet::BitSet expected;
00201     for( unsigned i = 0; i < TopologyInfo::corners( type ); ++i )
00202     {
00203         set.set_all_nodes( type );
00204         expected = set.get_bits() & ~( 1u << ( NodeSet::CORNER_OFFSET + i ) );
00205         set.clear_corner_node( i );
00206         CPPUNIT_ASSERT_EQUAL( expected, set.get_bits() );
00207     }
00208     for( unsigned i = 0; i < TopologyInfo::edges( type ); ++i )
00209     {
00210         set.set_all_nodes( type );
00211         expected = set.get_bits() & ~( 1u << ( NodeSet::EDGE_OFFSET + i ) );
00212         set.clear_mid_edge_node( i );
00213         CPPUNIT_ASSERT_EQUAL( expected, set.get_bits() );
00214     }
00215     for( unsigned i = 0; i < TopologyInfo::faces( type ); ++i )
00216     {
00217         set.set_all_nodes( type );
00218         expected = set.get_bits() & ~( 1u << ( NodeSet::FACE_OFFSET + i ) );
00219         set.clear_mid_face_node( i );
00220         CPPUNIT_ASSERT_EQUAL( expected, set.get_bits() );
00221     }
00222 
00223     set.set_all_nodes( type );
00224     expected = set.get_bits() & ~( 1u << ( NodeSet::REGION_OFFSET ) );
00225     set.clear_mid_region_node( 0 );
00226     CPPUNIT_ASSERT_EQUAL( expected, set.get_bits() );
00227 }
00228 
00229 void NodeSetTest::test_num_nodes()
00230 {
00231     NodeSet set;
00232     set.clear();
00233     set.set_corner_node( 1 );
00234     CPPUNIT_ASSERT_EQUAL( 1u, set.num_nodes() );
00235     set.set_corner_node( 3 );
00236     CPPUNIT_ASSERT_EQUAL( 2u, set.num_nodes() );
00237     set.set_mid_region_node( 0 );
00238     CPPUNIT_ASSERT_EQUAL( 3u, set.num_nodes() );
00239     set.set_mid_edge_node( 1 );
00240     set.set_mid_edge_node( 2 );
00241     CPPUNIT_ASSERT_EQUAL( 5u, set.num_nodes() );
00242     set.set_all_nodes( HEXAHEDRON );
00243     CPPUNIT_ASSERT_EQUAL( 27u, set.num_nodes() );
00244     set.clear();
00245     set.set_all_nodes( TETRAHEDRON );
00246     CPPUNIT_ASSERT_EQUAL( 15u, set.num_nodes() );
00247     set.clear();
00248     set.set_all_nodes( TRIANGLE );
00249     CPPUNIT_ASSERT_EQUAL( 7u, set.num_nodes() );
00250     set.clear();
00251     set.set_all_nodes( QUADRILATERAL );
00252     CPPUNIT_ASSERT_EQUAL( 9u, set.num_nodes() );
00253     set.clear();
00254     set.set_all_nodes( PYRAMID );
00255     CPPUNIT_ASSERT_EQUAL( 19u, set.num_nodes() );
00256     set.clear();
00257     set.set_all_nodes( PRISM );
00258     CPPUNIT_ASSERT_EQUAL( 21u, set.num_nodes() );
00259 }
00260 
00261 void NodeSetTest::test_have_any()
00262 {
00263     NodeSet set;
00264     set.clear();
00265 
00266     CPPUNIT_ASSERT( !set.have_any_corner_node() );
00267     set.set_corner_node( 0 );
00268     CPPUNIT_ASSERT( set.have_any_corner_node() );
00269     set.clear_corner_node( 0 );
00270     CPPUNIT_ASSERT( !set.have_any_corner_node() );
00271     set.set_corner_node( NodeSet::NUM_CORNER_BITS - 1 );
00272     CPPUNIT_ASSERT( set.have_any_corner_node() );
00273     CPPUNIT_ASSERT( !set.have_any_mid_node() );
00274 
00275     CPPUNIT_ASSERT( !set.have_any_mid_edge_node() );
00276     set.set_mid_edge_node( 0 );
00277     CPPUNIT_ASSERT( set.have_any_mid_edge_node() );
00278     CPPUNIT_ASSERT( set.have_any_mid_node() );
00279     set.clear_mid_edge_node( 0 );
00280     CPPUNIT_ASSERT( !set.have_any_mid_edge_node() );
00281     CPPUNIT_ASSERT( !set.have_any_mid_node() );
00282     set.set_mid_edge_node( NodeSet::NUM_EDGE_BITS - 1 );
00283     CPPUNIT_ASSERT( set.have_any_mid_edge_node() );
00284     CPPUNIT_ASSERT( set.have_any_mid_node() );
00285     set.clear();
00286 
00287     CPPUNIT_ASSERT( !set.have_any_mid_face_node() );
00288     set.set_mid_face_node( 0 );
00289     CPPUNIT_ASSERT( set.have_any_mid_face_node() );
00290     CPPUNIT_ASSERT( set.have_any_mid_node() );
00291     set.clear_mid_face_node( 0 );
00292     CPPUNIT_ASSERT( !set.have_any_mid_face_node() );
00293     CPPUNIT_ASSERT( !set.have_any_mid_node() );
00294     set.set_mid_face_node( NodeSet::NUM_FACE_BITS - 1 );
00295     CPPUNIT_ASSERT( set.have_any_mid_face_node() );
00296     CPPUNIT_ASSERT( set.have_any_mid_node() );
00297     set.clear();
00298 
00299     CPPUNIT_ASSERT( !set.have_any_mid_region_node() );
00300     set.set_mid_region_node( 0 );
00301     CPPUNIT_ASSERT( set.have_any_mid_region_node() );
00302     CPPUNIT_ASSERT( set.have_any_mid_node() );
00303     set.clear_mid_region_node( 0 );
00304     CPPUNIT_ASSERT( !set.have_any_mid_region_node() );
00305     CPPUNIT_ASSERT( !set.have_any_mid_node() );
00306     set.set_mid_region_node( NodeSet::NUM_REGION_BITS - 1 );
00307     CPPUNIT_ASSERT( set.have_any_mid_region_node() );
00308     CPPUNIT_ASSERT( set.have_any_mid_node() );
00309     set.clear();
00310 }
00311 
00312 static bool check_all( EntityTopology type, NodeSet set, unsigned dim, bool value )
00313 {
00314     unsigned count = TopologyInfo::adjacent( type, dim );
00315     for( unsigned i = 0; i < count; ++i )
00316         if( set.node( Sample( dim, i ) ) != value ) return false;
00317     return true;
00318 }
00319 
00320 static bool check_range_set( EntityTopology type, NodeSet set, unsigned dim, bool value = true )
00321 {
00322     const unsigned max_count[] = { NodeSet::NUM_CORNER_BITS, NodeSet::NUM_EDGE_BITS, NodeSet::NUM_FACE_BITS,
00323                                    NodeSet::NUM_REGION_BITS };
00324 
00325     // test that any bits corresponding to some other dimension are not set.
00326     for( unsigned d = 0; d <= 3; ++d )
00327     {
00328         if( d == dim ) continue;
00329 
00330         for( unsigned i = 0; i < max_count[d]; ++i )
00331             if( !set.node( Sample( d, i ) ) != value ) return false;
00332     }
00333 
00334     // test that any bits for this dimension beyond the number for this
00335     // type are not set
00336     for( unsigned i = TopologyInfo::adjacent( type, dim ); i < max_count[dim]; ++i )
00337         if( !set.node( Sample( dim, i ) ) != value ) return false;
00338 
00339     // test that any bits for the type and dimension are set
00340     for( unsigned i = 0; i < TopologyInfo::adjacent( type, dim ); ++i )
00341         if( set.node( Sample( dim, i ) ) == value ) return false;
00342 
00343     return true;
00344 }
00345 
00346 void NodeSetTest::test_set_all()
00347 {
00348     const EntityTopology types[] = { TRIANGLE, QUADRILATERAL, TETRAHEDRON, HEXAHEDRON, PRISM, PYRAMID };
00349     const int num_types          = sizeof( types ) / sizeof( types[0] );
00350 
00351     for( int i = 0; i < num_types; ++i )
00352     {
00353         NodeSet set;
00354         set.set_all_corner_nodes( types[i] );
00355         check_range_set( types[i], set, 0 );
00356         set.clear();
00357         set.set_all_mid_edge_nodes( types[i] );
00358         check_range_set( types[i], set, 1 );
00359         set.clear();
00360         set.set_all_mid_face_nodes( types[i] );
00361         check_range_set( types[i], set, 2 );
00362         set.clear();
00363         set.set_all_mid_region_nodes( types[i] );
00364         check_range_set( types[i], set, 3 );
00365     }
00366 }
00367 
00368 void NodeSetTest::test_clear_all()
00369 {
00370     const EntityTopology type = HEXAHEDRON;
00371     NodeSet set;
00372     set.set_all_nodes( type );
00373     set.clear_all_corner_nodes();
00374     CPPUNIT_ASSERT( check_all( type, set, 0, false ) );
00375     CPPUNIT_ASSERT( check_all( type, set, 1, true ) );
00376     CPPUNIT_ASSERT( check_all( type, set, 2, true ) );
00377     CPPUNIT_ASSERT( check_all( type, set, 3, true ) );
00378     set.set_all_nodes( type );
00379     set.clear_all_mid_edge_nodes();
00380     CPPUNIT_ASSERT( check_all( type, set, 0, true ) );
00381     CPPUNIT_ASSERT( check_all( type, set, 1, false ) );
00382     CPPUNIT_ASSERT( check_all( type, set, 2, true ) );
00383     CPPUNIT_ASSERT( check_all( type, set, 3, true ) );
00384     set.set_all_nodes( type );
00385     set.clear_all_mid_face_nodes();
00386     CPPUNIT_ASSERT( check_all( type, set, 0, true ) );
00387     CPPUNIT_ASSERT( check_all( type, set, 1, true ) );
00388     CPPUNIT_ASSERT( check_all( type, set, 2, false ) );
00389     CPPUNIT_ASSERT( check_all( type, set, 3, true ) );
00390     set.set_all_nodes( type );
00391     set.clear_all_mid_region_nodes();
00392     CPPUNIT_ASSERT( check_all( type, set, 0, true ) );
00393     CPPUNIT_ASSERT( check_all( type, set, 1, true ) );
00394     CPPUNIT_ASSERT( check_all( type, set, 2, true ) );
00395     CPPUNIT_ASSERT( check_all( type, set, 3, false ) );
00396 }
00397 
00398 void NodeSetTest::test_num_before()
00399 {
00400     const EntityTopology type = HEXAHEDRON;
00401     NodeSet set;
00402     set.clear();
00403     set.set_mid_face_node( 2 );
00404     CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_mid_face( 2 ) );
00405     CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_mid_face( 3 ) );
00406     set.set_corner_node( 0 );
00407     CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_mid_face( 2 ) );
00408     CPPUNIT_ASSERT_EQUAL( 2u, set.num_before_mid_face( 3 ) );
00409     CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_corner( 0 ) );
00410     CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_corner( 1 ) );
00411     set.set_all_corner_nodes( type );
00412     CPPUNIT_ASSERT_EQUAL( TopologyInfo::corners( type ), set.num_before_mid_edge( 0 ) );
00413     CPPUNIT_ASSERT_EQUAL( TopologyInfo::corners( type ) - 1,
00414                           set.num_before_corner( TopologyInfo::corners( type ) - 1 ) );
00415     CPPUNIT_ASSERT_EQUAL( 0u, set.num_before_corner( 0 ) );
00416     CPPUNIT_ASSERT_EQUAL( 1u, set.num_before_corner( 1 ) );
00417     CPPUNIT_ASSERT_EQUAL( 2u, set.num_before_corner( 2 ) );
00418     CPPUNIT_ASSERT_EQUAL( 3u, set.num_before_corner( 3 ) );
00419     CPPUNIT_ASSERT_EQUAL( 4u, set.num_before_corner( 4 ) );
00420 
00421     const unsigned total_nodes = 27;
00422     set.set_all_nodes( type );
00423     CPPUNIT_ASSERT_EQUAL( total_nodes - 1, set.num_before_mid_region( 0 ) );
00424     set.clear_mid_edge_node( 0 );
00425     CPPUNIT_ASSERT_EQUAL( total_nodes - 2, set.num_before_mid_region( 0 ) );
00426     set.clear_mid_edge_node( 1 );
00427     CPPUNIT_ASSERT_EQUAL( total_nodes - 3, set.num_before_mid_region( 0 ) );
00428 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines