MOAB: Mesh Oriented datABase  (version 5.4.1)
range_test.cpp
Go to the documentation of this file.
00001 #include "moab/Range.hpp"
00002 #include "TestUtil.hpp"
00003 
00004 using namespace moab;
00005 
00006 void insert_hint_test();
00007 void intersect_test();
00008 void merge_test();
00009 void const_pair_iterator_test();
00010 void swap_test();
00011 void subtract_test();
00012 void subset_by_dimension_test();
00013 void erase_test();
00014 void contains_test();
00015 
00016 int main()
00017 {
00018     int rval = 0;
00019     rval += RUN_TEST( insert_hint_test );
00020     rval += RUN_TEST( intersect_test );
00021     rval += RUN_TEST( merge_test );
00022     rval += RUN_TEST( const_pair_iterator_test );
00023     rval += RUN_TEST( swap_test );
00024     rval += RUN_TEST( subtract_test );
00025     rval += RUN_TEST( subset_by_dimension_test );
00026     rval += RUN_TEST( erase_test );
00027     rval += RUN_TEST( contains_test );
00028     return rval;
00029 }
00030 
00031 void insert_hint_test()
00032 {
00033     const EntityHandle pairs[][2] = { { 4980, 4981 }, { 4985, 4986 }, { 4990, 4990 }, { 5886, 5886 }, { 5888, 5890 },
00034                                       { 5890, 5890 }, { 5886, 5888 }, { 5890, 5890 }, { 5894, 5894 }, { 5899, 5899 } };
00035     const int num_pairs           = sizeof( pairs ) / sizeof( pairs[0] );
00036     const EntityHandle exp_pairs[][2] = { { 4980, 4981 }, { 4985, 4986 }, { 4990, 4990 },
00037                                           { 5886, 5890 }, { 5894, 5894 }, { 5899, 5899 } };
00038     const int num_exp                 = sizeof( exp_pairs ) / sizeof( exp_pairs[0] );
00039 
00040     Range range;
00041     Range::iterator it = range.begin();
00042     for( int i = 0; i < num_pairs; ++i )
00043         it = range.insert( it, pairs[i][0], pairs[i][1] );
00044 
00045     Range::const_pair_iterator pit = range.const_pair_begin();
00046     for( int i = 0; i < num_exp; ++i )
00047     {
00048         CHECK( pit != range.const_pair_end() );
00049         CHECK_EQUAL( exp_pairs[i][0], pit->first );
00050         CHECK_EQUAL( exp_pairs[i][1], pit->second );
00051         ++pit;
00052     }
00053     CHECK( pit == range.const_pair_end() );
00054 }
00055 
00056 // common constants used for a bunch of tests below
00057 const EntityHandle h1  = CREATE_HANDLE( MBVERTEX, 1 );
00058 const EntityHandle h4  = CREATE_HANDLE( MBVERTEX, 4 );
00059 const EntityHandle h5  = CREATE_HANDLE( MBVERTEX, 5 );
00060 const EntityHandle h9  = CREATE_HANDLE( MBVERTEX, 9 );
00061 const EntityHandle h10 = CREATE_HANDLE( MBVERTEX, 10 );
00062 const EntityHandle h15 = CREATE_HANDLE( MBVERTEX, 15 );
00063 const EntityHandle h16 = CREATE_HANDLE( MBVERTEX, 16 );
00064 const EntityHandle h20 = CREATE_HANDLE( MBVERTEX, 20 );
00065 const EntityHandle hh1 = CREATE_HANDLE( MBHEX, 1 );
00066 const EntityHandle hh3 = CREATE_HANDLE( MBHEX, 3 );
00067 
00068 void intersect_test()
00069 {
00070     Range r1, r2, rhs;
00071 
00072     // equal start/end
00073     r1.insert( h1, h5 );
00074     r1.insert( h10, h15 );
00075     r2.insert( h5, h10 );
00076     r2.insert( h15, h20 );
00077     rhs = intersect( r1, r2 );
00078     CHECK_EQUAL( (size_t)3, rhs.size() );
00079 
00080     // identical ranges test
00081     r1.clear();
00082     r2.clear();
00083     rhs.clear();
00084     r1.insert( h1, h5 );
00085     r1.insert( h10, h20 );
00086     r2.insert( h1, h5 );
00087     r2.insert( h10, h20 );
00088     rhs = intersect( r1, r2 );
00089     CHECK_EQUAL( r1.size(), rhs.size() );
00090     CHECK_EQUAL( r2.size(), rhs.size() );
00091 
00092     // off by one test
00093     r1.clear();
00094     r2.clear();
00095     rhs.clear();
00096     r1.insert( h1, h4 );
00097     r1.insert( h10, h15 );
00098     r2.insert( h5, h9 );
00099     r2.insert( h16, h20 );
00100     rhs = intersect( r1, r2 );
00101     CHECK( rhs.empty() );
00102 
00103     // interior test
00104     r1.clear();
00105     r2.clear();
00106     rhs.clear();
00107     r1.insert( h1, h20 );
00108     r2.insert( h5, h10 );
00109     rhs = intersect( r1, r2 );
00110     CHECK_EQUAL( r2.size(), rhs.size() );
00111 
00112     // half-above test
00113     r1.clear();
00114     r2.clear();
00115     rhs.clear();
00116     r1.insert( h1, h10 );
00117     r2.insert( h5, h20 );
00118     rhs = intersect( r1, r2 );
00119     CHECK_EQUAL( (size_t)( h10 - h5 + 1 ), rhs.size() );
00120 
00121     // half-below test
00122     r1.clear();
00123     r2.clear();
00124     rhs.clear();
00125     r1.insert( h5, h20 );
00126     r2.insert( h1, h10 );
00127     rhs = intersect( r1, r2 );
00128     CHECK_EQUAL( (size_t)( h10 - h5 + 1 ), rhs.size() );
00129 }
00130 
00131 void merge_test()
00132 {
00133     Range r1, r2;
00134 
00135     // merge all test
00136     r1.clear();
00137     r2.clear();
00138     r1.insert( h1, h5 );
00139     r1.insert( h10, h15 );
00140     r2 = r1;
00141     r1.merge( r2 );
00142     CHECK_EQUAL( r2.size(), r1.size() );
00143 
00144     // merge subset test
00145     r1.clear();
00146     r2.clear();
00147     r1.insert( h1, h5 );
00148     r1.insert( h10, h15 );
00149     Range::const_iterator i1 = r1.begin();
00150     Range::const_iterator i2 = r1.end();
00151     i1 += 2;
00152     i2 -= 2;
00153     r2.merge( i1, i2 );
00154     CHECK_EQUAL( r1.size() - 4, r2.size() );
00155 }
00156 
00157 void const_pair_iterator_test()
00158 {
00159     // const_pair_iterator test
00160     Range r1;
00161     r1.insert( h1 );
00162     r1.insert( h4 );
00163     r1.insert( h5 );
00164     Range::const_pair_iterator pair_iter = r1.const_pair_begin();
00165     EntityHandle cpi_h1                  = ( *pair_iter ).first;
00166     EntityHandle cpi_h2                  = ( *pair_iter ).second;
00167     ++pair_iter;
00168     EntityHandle cpi_h3 = ( *pair_iter ).first;
00169     EntityHandle cpi_h4 = ( *pair_iter ).second;
00170 
00171     ++pair_iter;
00172     CHECK_EQUAL( cpi_h1, h1 );
00173     CHECK_EQUAL( cpi_h2, h1 );
00174     CHECK_EQUAL( cpi_h3, h4 );
00175     CHECK_EQUAL( cpi_h4, h5 );
00176 }
00177 
00178 void swap_test()
00179 {
00180     Range r1, r2;
00181 
00182     // both non-empty
00183     r1.clear();
00184     r2.clear();
00185     r1.insert( h1 );
00186     r1.insert( h4 );
00187     r1.insert( h5 );
00188     r2.insert( h9 );
00189     r1.swap( r2 );
00190     CHECK_EQUAL( (size_t)1, r1.size() );
00191     CHECK_EQUAL( (size_t)3, r2.size() );
00192     CHECK( r1.find( h9 ) != r1.end() );
00193     CHECK( r2.find( h1 ) != r2.end() );
00194     CHECK( r2.find( h4 ) != r2.end() );
00195     CHECK( r2.find( h5 ) != r2.end() );
00196 
00197     // destination empty
00198     r1.clear();
00199     r2.clear();
00200     r2.insert( h1 );
00201     r2.insert( h4 );
00202     r2.insert( h5 );
00203     r1.swap( r2 );
00204     CHECK_EQUAL( (size_t)3, r1.size() );
00205     CHECK_EQUAL( (size_t)0, r2.size() );
00206     CHECK( r1.find( h1 ) != r1.end() );
00207     CHECK( r1.find( h4 ) != r1.end() );
00208     CHECK( r1.find( h5 ) != r1.end() );
00209 
00210     // source empty
00211     r1.clear();
00212     r2.clear();
00213     r1.insert( h1 );
00214     r1.insert( h4 );
00215     r1.insert( h5 );
00216     r1.swap( r2 );
00217     CHECK_EQUAL( (size_t)0, r1.size() );
00218     CHECK_EQUAL( (size_t)3, r2.size() );
00219     CHECK( r2.find( h1 ) != r2.end() );
00220     CHECK( r2.find( h4 ) != r2.end() );
00221     CHECK( r2.find( h5 ) != r2.end() );
00222 
00223     // both empty
00224     r1.clear();
00225     r2.clear();
00226     r1.swap( r2 );
00227     CHECK( r1.empty() );
00228     CHECK( r2.empty() );
00229 }
00230 
00231 void subtract_test()
00232 {
00233     Range r1, r2, r3;
00234 
00235     r1.clear();
00236     r2.clear();
00237     r1.insert( h1 );
00238     r1.insert( h4 );
00239     r1.insert( h5 );
00240     r2.insert( h4 );
00241     r3 = subtract( r1, r2 );
00242     CHECK_EQUAL( (size_t)2, r3.size() );
00243     CHECK( r3.find( h1 ) != r3.end() );
00244     CHECK( r3.find( h5 ) != r3.end() );
00245     CHECK( r3.find( h4 ) == r3.end() );
00246 
00247     // destination empty
00248     r1.clear();
00249     r2.clear();
00250     r1.insert( h1 );
00251     r1.insert( h4 );
00252     r1.insert( h5 );
00253     r3 = subtract( r1, r2 );
00254     CHECK_EQUAL( (size_t)3, r3.size() );
00255     CHECK( r3.find( h1 ) != r3.end() );
00256     CHECK( r3.find( h4 ) != r3.end() );
00257     CHECK( r3.find( h5 ) != r3.end() );
00258 }
00259 
00260 void subset_by_dimension_test()
00261 {
00262     Range r1, r2;
00263 
00264     // subset_by_dimension test
00265     r1.insert( h1 );
00266     r1.insert( h4 );
00267     r1.insert( hh1 );
00268     r1.insert( hh3 );
00269     r2 = r1.subset_by_dimension( 3 );
00270     CHECK_EQUAL( (size_t)2, r2.size() );
00271     CHECK_EQUAL( hh1, r2.front() );
00272     CHECK_EQUAL( hh3, r2.back() );
00273 }
00274 
00275 void erase_test()
00276 {
00277     Range range;
00278     Range::iterator result;
00279 
00280     // test erase from first node
00281     range.clear();
00282     range.insert( 5, 10 );
00283     range.insert( 12, 20 );
00284     result = range.erase( range.begin(), range.begin() + 2 );
00285     CHECK_EQUAL( (EntityHandle)7, range.front() );
00286     CHECK_EQUAL( (EntityHandle)20, range.back() );
00287     CHECK_EQUAL( (size_t)13, range.size() );
00288     CHECK_EQUAL( (EntityHandle)7, *result );
00289 
00290     // test erase first node
00291     range.clear();
00292     range.insert( 5, 10 );
00293     range.insert( 12, 20 );
00294     result = range.erase( range.begin(), range.begin() + 6 );
00295     CHECK_EQUAL( (EntityHandle)12, range.front() );
00296     CHECK_EQUAL( (EntityHandle)20, range.back() );
00297     CHECK_EQUAL( (size_t)9, range.size() );
00298     CHECK_EQUAL( (EntityHandle)12, *result );
00299 
00300     // test erase from back of first node
00301     range.clear();
00302     range.insert( 5, 10 );
00303     range.insert( 12, 20 );
00304     result = range.erase( range.begin() + 2, range.begin() + 6 );
00305     CHECK_EQUAL( (EntityHandle)5, range.front() );
00306     CHECK_EQUAL( (EntityHandle)20, range.back() );
00307     CHECK_EQUAL( (size_t)11, range.size() );
00308     CHECK_EQUAL( (EntityHandle)12, *result );
00309 
00310     // test erase from middle of first node
00311     range.clear();
00312     range.insert( 5, 10 );
00313     range.insert( 12, 20 );
00314     result = range.erase( range.begin() + 2, range.begin() + 5 );
00315     CHECK_EQUAL( (EntityHandle)5, range.front() );
00316     CHECK_EQUAL( (EntityHandle)20, range.back() );
00317     CHECK_EQUAL( (size_t)12, range.size() );
00318     CHECK_EQUAL( (EntityHandle)10, *result );
00319 
00320     // test erase spanning two nodes
00321     range.clear();
00322     range.insert( 5, 10 );
00323     range.insert( 12, 20 );
00324     result = range.erase( range.begin() + 3, range.begin() + 7 );
00325     CHECK_EQUAL( (EntityHandle)5, range.front() );
00326     CHECK_EQUAL( (EntityHandle)20, range.back() );
00327     CHECK_EQUAL( (size_t)11, range.size() );
00328     CHECK_EQUAL( (EntityHandle)13, *result );
00329 
00330     // test erase of first node and part of second
00331     range.clear();
00332     range.insert( 5, 10 );
00333     range.insert( 12, 20 );
00334     result = range.erase( range.begin(), range.begin() + 7 );
00335     CHECK_EQUAL( (EntityHandle)13, range.front() );
00336     CHECK_EQUAL( (EntityHandle)20, range.back() );
00337     CHECK_EQUAL( (size_t)8, range.size() );
00338     CHECK_EQUAL( (EntityHandle)13, *result );
00339 
00340     // test erase spanning three nodes
00341     range.clear();
00342     range.insert( 5, 10 );
00343     range.insert( 12, 20 );
00344     range.insert( 100, 101 );
00345     result = range.erase( range.begin() + 3, range.begin() + 16 );
00346     CHECK_EQUAL( (EntityHandle)5, range.front() );
00347     CHECK_EQUAL( (EntityHandle)101, range.back() );
00348     CHECK_EQUAL( (size_t)4, range.size() );
00349     CHECK_EQUAL( (EntityHandle)101, *result );
00350 
00351     // test erase from start of second node
00352     range.clear();
00353     range.insert( 5, 10 );
00354     range.insert( 12, 20 );
00355     result = range.erase( range.begin() + 6, range.begin() + 8 );
00356     CHECK_EQUAL( (EntityHandle)5, range.front() );
00357     CHECK_EQUAL( (EntityHandle)20, range.back() );
00358     CHECK_EQUAL( (size_t)13, range.size() );
00359     CHECK_EQUAL( (EntityHandle)14, *result );
00360 
00361     // test erase from back of last node
00362     range.clear();
00363     range.insert( 5, 10 );
00364     range.insert( 12, 20 );
00365     result = range.erase( range.begin() + 13, range.end() );
00366     CHECK_EQUAL( (EntityHandle)5, range.front() );
00367     CHECK_EQUAL( (EntityHandle)18, range.back() );
00368     CHECK_EQUAL( (size_t)13, range.size() );
00369     CHECK( result == range.end() );
00370 
00371     // test erase part of first node through end
00372     range.clear();
00373     range.insert( 5, 10 );
00374     range.insert( 12, 20 );
00375     result = range.erase( range.begin() + 4, range.end() );
00376     CHECK_EQUAL( (EntityHandle)5, range.front() );
00377     CHECK_EQUAL( (EntityHandle)8, range.back() );
00378     CHECK_EQUAL( (size_t)4, range.size() );
00379     CHECK( result == range.end() );
00380 
00381     // test erase of single node
00382     range.clear();
00383     range.insert( 5, 10 );
00384     result = range.erase( range.begin(), range.end() );
00385     CHECK_EQUAL( (size_t)0, range.size() );
00386     CHECK( result == range.end() );
00387 
00388     // test erase of multi-node range
00389     range.clear();
00390     range.insert( 5, 10 );
00391     range.insert( 12, 20 );
00392     range.insert( 100, 101 );
00393     result = range.erase( range.begin(), range.end() );
00394     CHECK_EQUAL( (size_t)0, range.size() );
00395     CHECK( result == range.end() );
00396 
00397     // test erase nothing
00398     range.clear();
00399     range.insert( 5, 10 );
00400     range.insert( 12, 20 );
00401     result = range.erase( range.begin() + 3, range.begin() + 3 );
00402     CHECK_EQUAL( (EntityHandle)5, range.front() );
00403     CHECK_EQUAL( (EntityHandle)20, range.back() );
00404     CHECK_EQUAL( (size_t)15, range.size() );
00405     CHECK_EQUAL( (EntityHandle)8, *result );
00406 
00407     // test iterators before erase remain valid
00408     Range::iterator a, b, c;
00409     range.clear();
00410     range.insert( 5, 10 );
00411     range.insert( 12, 20 );
00412     a      = range.begin();
00413     b      = range.begin() + 6;
00414     c      = range.begin() + 8;
00415     result = range.erase( range.begin() + 9, range.end() );
00416     CHECK( a == range.begin() );
00417     CHECK( b == range.begin() + 6 );
00418     CHECK( c == range.begin() + 8 );
00419     CHECK( result == range.end() );
00420 
00421     // test iterators before erase remain valid, single value case
00422     range.clear();
00423     range.insert( 5, 10 );
00424     range.insert( 12, 20 );
00425     a      = range.begin();
00426     b      = range.begin() + 6;
00427     c      = range.begin() + 8;
00428     result = range.erase( range.begin() + 9 );
00429     CHECK_EQUAL( (EntityHandle)5, range.front() );
00430     CHECK_EQUAL( (EntityHandle)20, range.back() );
00431     CHECK_EQUAL( (size_t)14, range.size() );
00432     CHECK( a == range.begin() );
00433     CHECK( b == range.begin() + 6 );
00434     CHECK( c == range.begin() + 8 );
00435     CHECK_EQUAL( (EntityHandle)16, *result );
00436 }
00437 
00438 void contains_test()
00439 {
00440     Range r1, r2;
00441 
00442     // simple test cases: one range each
00443 
00444     r1.clear();
00445     r2.clear();
00446     r1.insert( 1, 20 );
00447     r2.insert( 1, 21 );
00448     CHECK( !r1.contains( r2 ) );
00449     CHECK( r2.contains( r1 ) );
00450 
00451     r1.clear();
00452     r2.clear();
00453     r1.insert( 2, 20 );
00454     r2.insert( 1, 20 );
00455     CHECK( !r1.contains( r2 ) );
00456     CHECK( r2.contains( r1 ) );
00457 
00458     r1.clear();
00459     r2.clear();
00460     r1.insert( 5 );
00461     r2.insert( 1, 6 );
00462     CHECK( !r1.contains( r2 ) );
00463     CHECK( r2.contains( r1 ) );
00464 
00465     r1.clear();
00466     r2.clear();
00467     r1.insert( 5 );
00468     r2.insert( 6 );
00469     CHECK( !r1.contains( r2 ) );
00470     CHECK( !r2.contains( r1 ) );
00471 
00472     r1.clear();
00473     r2.clear();
00474     r1.insert( 18 );
00475     r2.insert( 18 );
00476     CHECK( r1.contains( r2 ) );
00477     CHECK( r2.contains( r1 ) );
00478 
00479     // empty range test cases
00480 
00481     r1.clear();
00482     r2.clear();
00483     CHECK( r1.contains( r2 ) );
00484     CHECK( r2.contains( r1 ) );
00485 
00486     r1.clear();
00487     r2.clear();
00488     r1.insert( 18 );
00489     CHECK( r1.contains( r2 ) );
00490     CHECK( !r2.contains( r1 ) );
00491 
00492     // slightly more complex tests: one range in one container
00493     // and multiple in the other
00494 
00495     r1.clear();
00496     r1.insert( 10, 100 );
00497     r2.clear();
00498     r2.insert( 20, 30 );
00499     r2.insert( 40, 50 );
00500     CHECK( r1.contains( r2 ) );
00501     CHECK( !r2.contains( r1 ) );
00502 
00503     r2.insert( 10, 12 );
00504     CHECK( r1.contains( r2 ) );
00505     CHECK( !r2.contains( r1 ) );
00506 
00507     r2.insert( 90, 100 );
00508     CHECK( r1.contains( r2 ) );
00509     CHECK( !r2.contains( r1 ) );
00510 
00511     r2.insert( 9 );
00512     CHECK( !r1.contains( r2 ) );
00513     CHECK( !r2.contains( r1 ) );
00514 
00515     r2.erase( 9 );
00516     r2.insert( 101 );
00517     CHECK( !r1.contains( r2 ) );
00518     CHECK( !r2.contains( r1 ) );
00519 
00520     r2.erase( 101 );
00521     r2.insert( 103, 110 );
00522     CHECK( !r1.contains( r2 ) );
00523     CHECK( !r2.contains( r1 ) );
00524 
00525     r2.insert( 1, 5 );
00526     CHECK( !r1.contains( r2 ) );
00527     CHECK( !r2.contains( r1 ) );
00528 
00529     // most complex case: both containers have several ranges
00530 
00531     r1.clear();
00532     r1.insert( 10, 30 );
00533     r1.insert( 40, 50 );
00534     r1.insert( 90, 100 );
00535     r2.clear();
00536     r2.insert( 20, 30 );
00537     r2.insert( 40, 50 );
00538     CHECK( r1.contains( r2 ) );
00539     CHECK( !r2.contains( r1 ) );
00540 
00541     r2.insert( 10, 12 );
00542     CHECK( r1.contains( r2 ) );
00543     CHECK( !r2.contains( r1 ) );
00544 
00545     r2.insert( 90, 100 );
00546     CHECK( r1.contains( r2 ) );
00547     CHECK( !r2.contains( r1 ) );
00548 
00549     r2.insert( 9 );
00550     CHECK( !r1.contains( r2 ) );
00551     CHECK( !r2.contains( r1 ) );
00552 
00553     r2.erase( 9 );
00554     r2.insert( 101 );
00555     CHECK( !r1.contains( r2 ) );
00556     CHECK( !r2.contains( r1 ) );
00557 
00558     r2.erase( 101 );
00559     r2.insert( 103, 110 );
00560     CHECK( !r1.contains( r2 ) );
00561     CHECK( !r2.contains( r1 ) );
00562 
00563     r2.insert( 1, 5 );
00564     CHECK( !r1.contains( r2 ) );
00565     CHECK( !r2.contains( r1 ) );
00566 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines