[MOAB-dev] r3696 - MOAB/trunk/test

kraftche at cae.wisc.edu kraftche at cae.wisc.edu
Tue Mar 23 10:55:31 CDT 2010


Author: kraftche
Date: 2010-03-23 10:55:31 -0500 (Tue, 23 Mar 2010)
New Revision: 3696

Modified:
   MOAB/trunk/test/kd_tree_test.cpp
Log:
clean up kd_tree_test

o use TestUtil.hpp to run tests and verify results
o split one large test function into individual tests
o don't test save/restore if no HDF5 support


Modified: MOAB/trunk/test/kd_tree_test.cpp
===================================================================
--- MOAB/trunk/test/kd_tree_test.cpp	2010-03-23 01:57:56 UTC (rev 3695)
+++ MOAB/trunk/test/kd_tree_test.cpp	2010-03-23 15:55:31 UTC (rev 3696)
@@ -1,156 +1,192 @@
 #include "moab/Core.hpp"
 #include "moab/AdaptiveKDTree.hpp"
 #include "moab/Range.hpp"
+#include "moab/CartVect.hpp"
 
 #include <math.h>
 #include <assert.h>
 #include <float.h>
 #include <cstdio>
 
-#include "moab/CartVect.hpp"
+#include "TestUtil.hpp"
 
-#include <stdlib.h>
-#ifdef NDEBUG
-#  undef assert
-#  define assert(A) do { if (!(A)) abort(); } while(false)
-#endif
-
 using namespace moab;
 
 const unsigned INTERVALS = 4;
 const unsigned DEPTH = 7; // 3*log2(INTERVALS)+1
 const char* TAG_NAME = "TEST_DATA";
 
-void test_iterator_back( AdaptiveKDTree& tool, EntityHandle root );
-void test_point_search( AdaptiveKDTree& tool, EntityHandle root );
+EntityHandle create_tree( AdaptiveKDTree& tool, unsigned depth, int intervals, Tag* tag_handle = 0 );
+void validate_tree( AdaptiveKDTree& tool, EntityHandle root, int depth, double intervals );
 
+void test_tree_create();
+void test_leaf_merge();
+void test_tree_readwrite();
+void test_tree_delete();
+void test_iterator_back();
+void test_point_search();
+
 int main()
 {
-  // Initialize MOAB & create tree tool
-  Core moab;
-  AdaptiveKDTree tool(&moab);
+  int err = RUN_TEST(test_tree_create);
+  if (err)  // can't run other tests if can't create tree
+    return 1;
+  err += RUN_TEST(test_leaf_merge);
+#ifdef HDF5_FILE
+  err += RUN_TEST(test_tree_readwrite);
+#endif
+  err += RUN_TEST(test_tree_delete);
+  err += RUN_TEST(test_iterator_back);
+  err += RUN_TEST(test_point_search);
   
+  return err;
+}
+
+EntityHandle create_tree( AdaptiveKDTree& tool, unsigned depth, int intervals, Tag* tag_handle )
+{
   // Create tree root
   ErrorCode err;
   EntityHandle root, leaf;
   const double tree_box_min_corner[] = { 0, 0, 0 };  
     // Make each leaf box be 1x1x1.
-  const double tree_box_max_corner[] = { INTERVALS, INTERVALS, INTERVALS };
+  const double tree_box_max_corner[] = { intervals, intervals, intervals };
   err = tool.create_tree( tree_box_min_corner, tree_box_max_corner, root );
   assert(!err);
   
   // Use iterator to create tree to fixed depth of DEPTH
   AdaptiveKDTreeIter iter;
   err = tool.get_tree_iterator( root, iter );
-  assert(!err);
+  CHECK_ERR(err);
   while(err == MB_SUCCESS) { 
-    if (iter.depth() < DEPTH) {
+    if (iter.depth() < depth) {
         // bisect leaves along alternating axes
       AdaptiveKDTree::Plane split;
       split.norm = iter.depth() % 3;  // alternate split axes;
       split.coord = 0.5 * (iter.box_min()[split.norm] + iter.box_max()[split.norm]);
       err = tool.split_leaf( iter, split ); // advances iter to first new leaf
-      assert(!err);
+      CHECK_ERR(err);
     }
       // if current leaf is at desired depth, advance to next one
     else {
       err = iter.step();
     }
   }
-  assert(MB_ENTITY_NOT_FOUND == err);
+  CHECK(MB_ENTITY_NOT_FOUND == err);
   
+  if (!tag_handle)
+    return root;
+  
   // define a tag to use to store integer values on tree leaves
-  Tag data;
-  err = moab.tag_create( TAG_NAME, sizeof(int), MB_TAG_DENSE, MB_TYPE_INTEGER, data, 0, false );
-  assert(!err);
+  err = tool.moab()->tag_create( TAG_NAME, sizeof(int), MB_TAG_DENSE, MB_TYPE_INTEGER, *tag_handle, 0, false );
+  CHECK_ERR(err);
+
+  // iterate over tree setting data
+  int counter = 0;
+  for (err = tool.get_tree_iterator( root, iter ); !err; err = iter.step()) {
+    // store integer value on leaf
+    ++counter;
+    leaf = iter.handle();
+    err = tool.moab()->tag_set_data( *tag_handle, &leaf, 1, &counter );
+    CHECK_ERR(err);
+  }
   
-  // iterate over tree, verifying leaves and setting data
+  return root;
+}
+
+void validate_tree( AdaptiveKDTree& tool, EntityHandle root, unsigned depth, int intervals, Tag data )
+{
+  ErrorCode err;
+  const double VOL = 1.0; // all leaves should be 1x1x1 boxes
+  int val;
+
+  // iterate over tree, verifying leaves 
+  AdaptiveKDTreeIter iter;
   int counter = 0;
   for (err = tool.get_tree_iterator( root, iter ); !err; err = iter.step()) {
     // store integer value on leaf
     ++counter;
-    leaf = iter.handle();
-    err = moab.tag_set_data( data, &leaf, 1, &counter );
-    assert(!err);
-      
+    EntityHandle leaf = iter.handle();
+    CHECK(leaf != 0);
+    CHECK_EQUAL(MBENTITYSET, TYPE_FROM_HANDLE(leaf));
+     
     // check size of leaf
     const double* min = iter.box_min();
     const double* max = iter.box_max();
     double dims[] = { max[0] - min[0], max[1] - min[1], max[2] - min[2] };
     double volume = dims[0] * dims[1] * dims[2];
-    assert( fabs(volume - 1.0) <= DBL_EPSILON );  
+    CHECK_REAL_EQUAL( VOL, volume, DBL_EPSILON );  
    
     // check depth of leaf
-    assert( iter.depth() == DEPTH );
+    CHECK_EQUAL( depth, iter.depth() );
+    
+    // check tag value on leaf
+    err = tool.moab()->tag_get_data( data, &leaf, 1, &val );
+    CHECK_ERR(err);
+    CHECK_EQUAL(counter, val);
   }
     // check number of leaves
-  const int num_leaves = INTERVALS*INTERVALS*INTERVALS;
-  assert( num_leaves == counter );
+  const int num_leaves = intervals*intervals*intervals;
+  CHECK_EQUAL( num_leaves, counter );
+}
+
+void test_tree_create()
+{
+  Tag tag;
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  const EntityHandle root = create_tree( tool, DEPTH, INTERVALS, &tag );
+  validate_tree( tool, root, DEPTH, INTERVALS, tag );
+}
+
+void test_leaf_merge()
+{
+  ErrorCode err;
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  Tag data;
+  const EntityHandle root = create_tree( tool, DEPTH, INTERVALS, &data );
   
-  test_iterator_back( tool, root );
-  test_point_search( tool, root );
-  
   // reduce tree depth to DEPTH-1 by merging adjacent leaf pairs, 
   // make new "leaf" have smaller of two data values on original pair
+  AdaptiveKDTreeIter iter;
   for (err = tool.get_tree_iterator( root, iter ); !err; err = iter.step()) {
     // get data for first leaf
     int data1;
-    leaf = iter.handle();
-    err = moab.tag_get_data( data, &leaf, 1, &data1 );
-    assert(!err);
+    EntityHandle leaf = iter.handle();
+    err = mb.tag_get_data( data, &leaf, 1, &data1 );
+    CHECK_ERR(err);
     // tree traversal is always such that two leaves with same parent are consective
     err = iter.step();
-    assert(!err);
+    CHECK_ERR(err);
     // get data for sibling
     int data2;
     leaf = iter.handle();
-    err = moab.tag_get_data( data, &leaf, 1, &data2 );
-    assert(!err);
+    err = mb.tag_get_data( data, &leaf, 1, &data2 );
+    CHECK_ERR(err);
     // as we stored increasing values, these had better be increasing
-    assert( data2 - data1 == 1 );
+    CHECK_EQUAL( 1, data2 - data1 );
     // merge leaf pair (iter can be at either one)
     err = tool.merge_leaf( iter );  // changes iter to be new "merged" leaf
-    assert(!err);
+    CHECK_ERR(err);
     // store smaller of two values on new leaf
     leaf = iter.handle();
-    err = moab.tag_set_data( data, &leaf, 1, &data1 );
-    assert(!err);
+    err = mb.tag_set_data( data, &leaf, 1, &data1 );
+    CHECK_ERR(err);
   }
-  
-  // write to file
-  err = moab.write_file( "tree.h5m" );
-  assert(!err);
 
-  // clear everything
-  moab.delete_mesh();
   
-  // read tree from file
-  err = moab.load_file( "tree.h5m" );
-  assert(!err);
- 
-  // get tag handle by name, because the handle may have changed
-  err = moab.tag_get_handle( TAG_NAME, data );
-  assert(!err);
-
-  // get root handle for tree
-  Range range;
-  err = tool.find_all_trees( range );
-  assert(!err);
-  assert(range.size() == 1);
-  root = range.front(); // first (only) handle
-  
   // Iterate over tree, verifying leaves and checking data
   // Initial leaves had volume of 1 : merged pairs of leaves so volume should now be 2.
   // Initial leaves were enumerated in order : merged pairs so new leaves should
   //   have data incrementing in steps of 2.
-  counter = 1;
+  int counter = 1;
   for (err = tool.get_tree_iterator( root, iter ); !err; err = iter.step()) {
     // store integer value on leaf
     int data1;
-    leaf = iter.handle();
-    err = moab.tag_get_data( data, &leaf, 1, &data1 );
-    assert(!err);
-    assert( counter == data1 );
+    EntityHandle leaf = iter.handle();
+    err = mb.tag_get_data( data, &leaf, 1, &data1 );
+    CHECK_ERR(err);
+    CHECK_EQUAL( counter, data1 );
     counter += 2;
       
     // check size of leaf
@@ -158,48 +194,73 @@
     const double* max = iter.box_max();
     double dims[] = { max[0] - min[0], max[1] - min[1], max[2] - min[2] };
     double volume = dims[0] * dims[1] * dims[2];
-    assert( fabs(volume - 2.0) <= DBL_EPSILON );  
+    CHECK_REAL_EQUAL( 2.0, volume, DBL_EPSILON );  
     
     // check depth of leaf
-    assert( iter.depth() == DEPTH-1 );
+    CHECK_EQUAL( DEPTH-1, iter.depth() );
   }
-    // check number of leaves
-    // (num_leaves is original number of leaves, twice current number,
-    //  but counter is incremented by 2 for each iteration, so just
-    //  compare them.)
-  assert( counter-1 == num_leaves );
-  
-  
-    // Delete data from tree
-  err = moab.tag_delete( data );
-  assert( !err );
+}
 
+void test_tree_readwrite()
+{
+  ErrorCode err;
+  Tag tag;
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  EntityHandle root = create_tree( tool, DEPTH, INTERVALS, &tag );
   
   // write to file
-  err = moab.write_file( "tree.h5m" );
-  assert(!err);
+  err = mb.write_file( "tree.h5m" );
+  CHECK_ERR(err);
 
   // clear everything
-  moab.delete_mesh();
+  mb.delete_mesh();
   
   // read tree from file
-  err = moab.load_file( "tree.h5m" );
+  err = mb.load_file( "tree.h5m" );
+  remove("tree.h5m");
+  CHECK_ERR(err);
+ 
+  // get tag handle by name, because the handle may have changed
+  err = mb.tag_get_handle( TAG_NAME, tag );
+  CHECK_ERR(err);
+
+  // get root handle for tree
+  Range range;
+  err = tool.find_all_trees( range );
   assert(!err);
+  assert(range.size() == 1);
+  root = range.front(); // first (only) handle
   
-  // check that tag doesn't exist
-  err = moab.tag_get_handle( TAG_NAME, data );
-  assert( MB_TAG_NOT_FOUND == err );
+  validate_tree( tool, root, DEPTH, INTERVALS, tag );
+}
 
-  remove( "tree.h5m" );
-  return 0;
+void test_tree_delete()
+{
+  ErrorCode err;
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  Tag data;
+  const EntityHandle root = create_tree( tool, DEPTH, INTERVALS, &data );
+  
+  err = tool.delete_tree( root );
+  CHECK_ERR(err);
+  
+  Range ents;
+  err = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &data, 0, 1, ents );
+  CHECK_ERR(err);
+  CHECK(ents.empty());
 }
 
-
-void test_iterator_back( AdaptiveKDTree& tool, EntityHandle root )
+void test_iterator_back( )
 {
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  const EntityHandle root = create_tree( tool, DEPTH, INTERVALS );
+  
   AdaptiveKDTreeIter iter;
   ErrorCode rval = tool.get_tree_iterator( root, iter );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   
   CartVect min( iter.box_min() );
   CartVect max( iter.box_max() );
@@ -207,14 +268,18 @@
   
     // going back from first location should fail.
   rval = iter.back();
-  assert( MB_ENTITY_NOT_FOUND == rval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
   rval = tool.get_tree_iterator( root, iter );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   
     // make sure iterator is valid
-  assert( iter.box_min()[0] == min[0] && iter.box_min()[1] == min[1] && iter.box_min()[2] == min[2] );
-  assert( iter.box_max()[0] == max[0] && iter.box_max()[1] == max[1] && iter.box_max()[2] == max[2] );
-  assert( iter.handle() == leaf );
+  CHECK_REAL_EQUAL( min[0], iter.box_min()[0], DBL_EPSILON );
+  CHECK_REAL_EQUAL( min[1], iter.box_min()[1], DBL_EPSILON );
+  CHECK_REAL_EQUAL( min[2], iter.box_min()[2], DBL_EPSILON );
+  CHECK_REAL_EQUAL( max[0], iter.box_max()[0], DBL_EPSILON );
+  CHECK_REAL_EQUAL( max[1], iter.box_max()[1], DBL_EPSILON );
+  CHECK_REAL_EQUAL( max[2], iter.box_max()[2], DBL_EPSILON );
+  CHECK_EQUAL( leaf, iter.handle() );
   
   while (MB_SUCCESS == iter.step()) {
       // Get values at current iterator location
@@ -224,21 +289,29 @@
   
       // step back to previous location
     rval = iter.back();
-    assert( MB_SUCCESS == rval );
+    CHECK_ERR(rval);
     
       // check expected values for previous location
-    assert( iter.box_min()[0] == min[0] && iter.box_min()[1] == min[1] && iter.box_min()[2] == min[2] );
-    assert( iter.box_max()[0] == max[0] && iter.box_max()[1] == max[1] && iter.box_max()[2] == max[2] );
-    assert( iter.handle() == leaf );
+    CHECK_REAL_EQUAL( min[0], iter.box_min()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( min[1], iter.box_min()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( min[2], iter.box_min()[2], DBL_EPSILON );
+    CHECK_REAL_EQUAL( max[0], iter.box_max()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( max[1], iter.box_max()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( max[2], iter.box_max()[2], DBL_EPSILON );
+    CHECK_EQUAL( leaf, iter.handle() );
     
       // advance iterator to 'current' location
     rval = iter.step();
-    assert( MB_SUCCESS == rval );
+    CHECK_ERR(rval);
     
       // check that iterator values are correct
-    assert( iter.box_min()[0] == next_min[0] && iter.box_min()[1] == next_min[1] && iter.box_min()[2] == next_min[2] );
-    assert( iter.box_max()[0] == next_max[0] && iter.box_max()[1] == next_max[1] && iter.box_max()[2] == next_max[2] );
-    assert( iter.handle() == next_leaf );
+    CHECK_REAL_EQUAL( next_min[0], iter.box_min()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( next_min[1], iter.box_min()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( next_min[2], iter.box_min()[2], DBL_EPSILON );
+    CHECK_REAL_EQUAL( next_max[0], iter.box_max()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( next_max[1], iter.box_max()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( next_max[2], iter.box_max()[2], DBL_EPSILON );
+    CHECK_EQUAL( next_leaf, iter.handle() );
    
       // store values for next iteration
     min = next_min;
@@ -247,8 +320,12 @@
   }
 }
 
-void test_point_search( AdaptiveKDTree& tool, EntityHandle root )
+void test_point_search()
 {
+  Core mb;
+  AdaptiveKDTree tool(&mb);
+  const EntityHandle root = create_tree( tool, DEPTH, INTERVALS );
+  
   ErrorCode rval;
   EntityHandle leaf;
   AdaptiveKDTreeIter iter, iter2;
@@ -259,56 +336,58 @@
  
     // compare leaf search to iterator search
   rval = tool.leaf_containing_point( root, left.array(), leaf );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   rval = tool.leaf_containing_point( root, left.array(), iter );
-  assert( MB_SUCCESS == rval );
-  assert( iter.handle() == leaf );
+  CHECK_ERR(rval);
+  CHECK_EQUAL( leaf, iter.handle() );
   
     // iterator should be at 'first' leaf 
   rval = tool.get_tree_iterator( root, iter2 );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   for (;;) {
-    assert( iter.handle() == iter2.handle() );
-    assert( iter.depth() == iter2.depth() );
-    assert( iter.box_min()[0] == iter2.box_min()[0] );
-    assert( iter.box_min()[1] == iter2.box_min()[1] );
-    assert( iter.box_min()[2] == iter2.box_min()[2] );
-    assert( iter.box_max()[0] == iter2.box_max()[0] );
-    assert( iter.box_max()[1] == iter2.box_max()[1] );
-    assert( iter.box_max()[2] == iter2.box_max()[2] );
+    CHECK_EQUAL( iter.handle(), iter2.handle() );
+    CHECK_EQUAL( iter.depth(), iter2.depth() );
+    CHECK_REAL_EQUAL( iter.box_min()[0], iter2.box_min()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_min()[1], iter2.box_min()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_min()[2], iter2.box_min()[2], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[0], iter2.box_max()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[1], iter2.box_max()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[2], iter2.box_max()[2], DBL_EPSILON );
     
     rval = iter2.step();
-    if (MB_SUCCESS != rval)
+    if (MB_ENTITY_NOT_FOUND == rval)
       break;
+    CHECK_ERR(rval);
     rval = iter.step();
-    assert( MB_SUCCESS == rval );
+    CHECK_ERR(rval);
   }
   
     // compare leaf search to iterator search
   rval = tool.leaf_containing_point( root, right.array(), leaf );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   rval = tool.leaf_containing_point( root, right.array(), iter );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   assert( iter.handle() == leaf );
   
     // iterator should be at 'last' leaf 
   rval = tool.get_last_iterator( root, iter2 );
-  assert( MB_SUCCESS == rval );
+  CHECK_ERR(rval);
   for (;;) {
-    assert( iter.handle() == iter2.handle() );
-    assert( iter.depth() == iter2.depth() );
-    assert( iter.box_min()[0] == iter2.box_min()[0] );
-    assert( iter.box_min()[1] == iter2.box_min()[1] );
-    assert( iter.box_min()[2] == iter2.box_min()[2] );
-    assert( iter.box_max()[0] == iter2.box_max()[0] );
-    assert( iter.box_max()[1] == iter2.box_max()[1] );
-    assert( iter.box_max()[2] == iter2.box_max()[2] );
+    CHECK_EQUAL( iter.handle(), iter2.handle() );
+    CHECK_EQUAL( iter.depth(), iter2.depth() );
+    CHECK_REAL_EQUAL( iter.box_min()[0], iter2.box_min()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_min()[1], iter2.box_min()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_min()[2], iter2.box_min()[2], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[0], iter2.box_max()[0], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[1], iter2.box_max()[1], DBL_EPSILON );
+    CHECK_REAL_EQUAL( iter.box_max()[2], iter2.box_max()[2], DBL_EPSILON );
     
     rval = iter2.back();
-    if (MB_SUCCESS != rval)
+    if (MB_ENTITY_NOT_FOUND == rval)
       break;
+    CHECK_ERR(rval);
     rval = iter.back();
-    assert( MB_SUCCESS == rval );
+    CHECK_ERR(rval);
   }
 }
 



More information about the moab-dev mailing list