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