MOAB: Mesh Oriented datABase  (version 5.4.1)
internals_test.cpp
Go to the documentation of this file.
00001 #include "Internals.hpp"
00002 #include <iostream>
00003 using namespace std;
00004 
00005 using namespace moab;
00006 
00007 HandleUtils handleUtils( 0, 1 );
00008 
00009 bool internal_assert( bool c )
00010 {
00011     return !c;
00012 }
00013 
00014 #define handle_test_assert( C )                  \
00015     if( internal_assert( C ) )                   \
00016     {                                            \
00017         cout << "Test: " #C " failed ." << endl; \
00018         return false;                            \
00019     }
00020 
00021 bool handle_test( EntityType type, EntityID id, int proc, bool should_fail )
00022 {
00023     int err             = 0;
00024     EntityHandle handle = CREATE_HANDLE( type, handleUtils.create_id( id, proc ), err );
00025     if( should_fail )
00026     {
00027         handle_test_assert( err ) return true;
00028     }
00029     handle_test_assert( !err )
00030 
00031         EntityType type_from_handle = TYPE_FROM_HANDLE( handle );
00032     handle_test_assert( type_from_handle == type )
00033 
00034         EntityID id_from_handle = handleUtils.id_from_handle( handle );
00035     handle_test_assert( id_from_handle == id )
00036 
00037         int proc_from_handle = handleUtils.rank_from_handle( handle );
00038     handle_test_assert( proc_from_handle == proc )
00039 
00040         return true;
00041 }
00042 
00043 #define tag_test_assert( C )                    \
00044     if( internal_assert( C ) )                  \
00045     {                                           \
00046         cout << "Test: " #C " failed." << endl; \
00047         return false;                           \
00048     }
00049 
00050 bool tag_test( TagId id, TagType prop )
00051 {
00052     Tag tag = TAG_HANDLE_FROM_ID( id, prop );
00053 
00054     unsigned long id_from_handle = ID_FROM_TAG_HANDLE( tag );
00055     tag_test_assert( id_from_handle == id )
00056 
00057         TagType prop_from_handle = PROP_FROM_TAG_HANDLE( tag );
00058     tag_test_assert( prop_from_handle == prop )
00059 
00060         return true;
00061 }
00062 
00063 //! Sample code on a 32-bit system.
00064 int main()
00065 {
00066     const unsigned cpus[] = { 1, 4, 16, 5, 20 };
00067     const int num_cpus    = sizeof( cpus ) / sizeof( cpus[0] );
00068     unsigned errors = 0, tests = 0;
00069     const int num_prop = MB_TAG_LAST + 1;
00070 
00071     ++tests;
00072     if( MB_TAG_LAST > num_prop )
00073     {
00074         cout << "MB_TAG_PROP_WIDTH insufficient for size of TagType" << endl;
00075         ++errors;
00076     }
00077 
00078     ++tests;
00079     if( MBMAXTYPE > 1 << MB_TYPE_WIDTH )
00080     {
00081         cout << "MB_TYPE_WIDTH insufficient for size of EntityType" << endl;
00082         ++errors;
00083     }
00084 
00085     // if any errors so far, abort because everything else will
00086     // probably fail.
00087     if( errors ) return errors;
00088 
00089     for( int num_cpu = 0; num_cpu < num_cpus; ++num_cpu )
00090     {
00091 
00092         handleUtils = HandleUtils( 0, cpus[num_cpu] );
00093 
00094         // init these after setting num_cpu, because max id depends on num cpu.
00095         const EntityID ids[] = { 0, 1, handleUtils.max_id() / 2, handleUtils.max_id() - 1, handleUtils.max_id() };
00096         const TagId tids[]   = { 0, 1, MB_TAG_PROP_MASK / 2, MB_TAG_PROP_MASK - 1, MB_TAG_PROP_MASK };
00097         const int num_ids    = sizeof( ids ) / sizeof( ids[0] );
00098         const int num_tids   = sizeof( tids ) / sizeof( tids[0] );
00099 
00100         for( unsigned cpu = 0; cpu < cpus[num_cpu]; ++cpu )
00101         {
00102             for( EntityType type = MBVERTEX; type < MBMAXTYPE; ++type )
00103                 for( int id = 0; id < num_ids; ++id )
00104                 {
00105                     ++tests;
00106                     if( !handle_test( type, ids[id], cpu, false ) )
00107                     {
00108                         cout << "Test of handle with type=" << type << ", id=" << ids[id] << ", proc=" << cpu
00109                              << ", and numproc=" << cpus[num_cpu] << endl;
00110                         ++errors;
00111                     }
00112                 }
00113 
00114             for( int prop = 0; prop < num_prop; ++prop )
00115                 for( int id = 0; id < num_tids; ++id )
00116                 {
00117                     ++tests;
00118                     if( !tag_test( tids[id], (TagType)prop ) )
00119                     {
00120                         cout << "Test of tag handle with prop=" << prop << ", id=" << tids[id] << ", proc=" << cpu
00121                              << ", and numproc=" << cpus[num_cpu] << endl;
00122                         ++errors;
00123                     }
00124                 }
00125         }
00126     }
00127 
00128     // test some stuff that should fail
00129     handleUtils = HandleUtils( 0, 16 );
00130     ++tests;
00131     if( !handle_test( MBVERTEX, MB_END_ID + 1, 0, true ) )
00132     {
00133         cout << "Failed to catch ID overflow" << endl;
00134         ++errors;
00135     }
00136     ++tests;
00137     if( !handle_test( (EntityType)( MBMAXTYPE + 1 ), 1, 0, true ) )
00138     {
00139         cout << "Failed to catch type overflow" << endl;
00140         ++errors;
00141     }
00142     //  ++tests;
00143     //  if (!handle_test( MBHEX, 1, 17, true)) {
00144     //    cout << "Failed to catch Proc# overflow" << endl;
00145     //    ++errors;
00146     //  }
00147 
00148     if( errors )
00149         cout << endl << errors << " of " << tests << " tests failed." << endl << endl;
00150     else
00151         cout << endl << tests << " tests passed." << endl << endl;
00152     return errors;
00153 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines