[MOAB-dev] r2057 - MOAB/trunk

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Mon Sep 8 11:06:16 CDT 2008


Author: kraftche
Date: 2008-09-08 11:06:16 -0500 (Mon, 08 Sep 2008)
New Revision: 2057

Modified:
   MOAB/trunk/MBCore.cpp
   MOAB/trunk/SequenceManager.cpp
   MOAB/trunk/SequenceManager.hpp
   MOAB/trunk/TagServer.cpp
   MOAB/trunk/TagTest.cpp
Log:
Old code checked for invalid handles in MBCore before setting tag data, unless
the type of the handle was MBENTITYSET.  

o Move this code from MBCore to TagServer because 
    - tag server now has a pointer to the SequenceManager and is therefore
      capable of doing the check 
    - it avoids checking the validity of handles twice for dense tag data.  
    
o Remove exception for type == MBENTITYSET.  It is nonsensical and most likely
  legacy logic from when entity sets were not stored in sequences.

o Add regression test to verify initial bug and correctness of fix.



Modified: MOAB/trunk/MBCore.cpp
===================================================================
--- MOAB/trunk/MBCore.cpp	2008-09-05 18:44:36 UTC (rev 2056)
+++ MOAB/trunk/MBCore.cpp	2008-09-08 16:06:16 UTC (rev 2057)
@@ -1445,18 +1445,6 @@
   if (NULL == entity_handles && 0 == num_entities)
     return tagServer->set_mesh_data(tag_handle, tag_data);
 
-  //verify handles
-  const EntitySequence* seq;
-  const MBEntityHandle* iter;
-  const MBEntityHandle* end = entity_handles + num_entities;
-  for(iter = entity_handles; iter != end; ++iter)
-  {
-    if (TYPE_FROM_HANDLE(*iter) == MBENTITYSET) continue;
-    
-    else if(sequenceManager->find(*iter, seq) != MB_SUCCESS)
-      return MB_ENTITY_NOT_FOUND;
-  }
-
   return tagServer->set_data(tag_handle, entity_handles, num_entities, tag_data);
 }
 
@@ -1465,10 +1453,6 @@
                                     const MBRange& entity_handles, 
                                     const void *tag_data)
 {
-  //verify handles
-  MBErrorCode result = sequence_manager()->check_valid_entities( entity_handles );
-  if (MB_SUCCESS != result)
-    return result;
   return tagServer->set_data(tag_handle, entity_handles, tag_data);
 }
 
@@ -1507,18 +1491,6 @@
   if (NULL == entity_handles && 0 == num_entities)
     return tagServer->set_mesh_data(tag_handle, tag_data[0], tag_sizes ? tag_sizes[0] : 0);
 
-  //verify handles
-  const EntitySequence* seq;
-  const MBEntityHandle* iter;
-  const MBEntityHandle* end = entity_handles + num_entities;
-  for(iter = entity_handles; iter != end; ++iter)
-  {
-    if (TYPE_FROM_HANDLE(*iter) == MBENTITYSET) continue;
-    
-    else if(sequenceManager->find(*iter, seq) != MB_SUCCESS)
-      return MB_ENTITY_NOT_FOUND;
-  }
-
   return tagServer->set_data(tag_handle, entity_handles, num_entities, tag_data, tag_sizes);
 }
 
@@ -1528,10 +1500,6 @@
                                    void const* const* tag_data,
                                    const int* tag_sizes )
 {
-  //verify handles
-  MBErrorCode result = sequence_manager()->check_valid_entities( entity_handles );
-  if (MB_SUCCESS != result)
-    return result;
   return tagServer->set_data(tag_handle, entity_handles, tag_data, tag_sizes);
 }
 

Modified: MOAB/trunk/SequenceManager.cpp
===================================================================
--- MOAB/trunk/SequenceManager.cpp	2008-09-05 18:44:36 UTC (rev 2056)
+++ MOAB/trunk/SequenceManager.cpp	2008-09-08 16:06:16 UTC (rev 2057)
@@ -136,6 +136,22 @@
   return MB_SUCCESS;
 }
 
+MBErrorCode SequenceManager::check_valid_entities( const MBEntityHandle* entities,
+                                                   size_t num_entities ) const
+{
+  MBErrorCode rval = MB_SUCCESS;
+  const EntitySequence* ptr = 0;
+  
+  const MBEntityHandle* const end = entities + num_entities;
+  for (; entities < end; ++entities) {
+    rval = find(*entities, ptr);
+    if (MB_SUCCESS != rval)
+      break;
+  }
+  
+  return rval;
+}
+
 MBErrorCode SequenceManager::delete_entity( MBEntityHandle entity )
 {
   return typeData[TYPE_FROM_HANDLE(entity)].erase( entity );

Modified: MOAB/trunk/SequenceManager.hpp
===================================================================
--- MOAB/trunk/SequenceManager.hpp	2008-09-05 18:44:36 UTC (rev 2056)
+++ MOAB/trunk/SequenceManager.hpp	2008-09-08 16:06:16 UTC (rev 2057)
@@ -62,6 +62,10 @@
       /** Check if passed entity handles are valid */
     MBErrorCode check_valid_entities( const MBRange& entities ) const;
     
+      /** Check if passed entity handles are valid */
+    MBErrorCode check_valid_entities( const MBEntityHandle entities[],
+                                      size_t num_entities ) const;
+    
       /** Delete an entity.  Deletes sequence if only contained entity. */
     MBErrorCode delete_entity( MBEntityHandle entity );
     

Modified: MOAB/trunk/TagServer.cpp
===================================================================
--- MOAB/trunk/TagServer.cpp	2008-09-05 18:44:36 UTC (rev 2056)
+++ MOAB/trunk/TagServer.cpp	2008-09-08 16:06:16 UTC (rev 2057)
@@ -280,56 +280,78 @@
                                  const int num_entities,
                                  const void* data )
 {
+  MBErrorCode rval;
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
   const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
   const TagInfo* tag_info;
   switch (tag_type) {
     case MB_TAG_DENSE:
       if (!(tag_info = get_tag_info( tag_id, tag_type )))
-        return MB_TAG_NOT_FOUND;
-      return sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
+        rval = MB_TAG_NOT_FOUND;
+      else
+        rval = sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
                                             data, tag_info->default_value() );
-  
+      break;
+        
     case MB_TAG_SPARSE:
-      return mSparseData->set_data( tag_id, entity_handles, num_entities, data );
-    
+      rval = sequenceManager->check_valid_entities( entity_handles, num_entities );
+      if (MB_SUCCESS == rval)
+        rval = mSparseData->set_data( tag_id, entity_handles, num_entities, data );
+      break;
+      
     case MB_TAG_BIT:
       if (num_entities == 1)
-        return set_bits( tag_handle, *entity_handles, *reinterpret_cast<const unsigned char*>(data) );
+        rval = sequenceManager->check_valid_entities( entity_handles, num_entities );
       else
-        return MB_FAILURE;
+        rval = MB_FAILURE;
+      if (MB_SUCCESS == rval)
+        rval = set_bits( tag_handle, *entity_handles, *reinterpret_cast<const unsigned char*>(data) );
+      break;
     
     default:
-      return MB_TAG_NOT_FOUND;
+      rval = MB_TAG_NOT_FOUND;
+      break;
   }
+  return rval;
 }
 
 MBErrorCode TagServer::set_data( const MBTag tag_handle, 
                                  const MBRange& entity_handles, 
                                  const void* data )
 {
+  MBErrorCode rval;
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
   const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
   const TagInfo* tag_info;
   switch (tag_type) {
     case MB_TAG_DENSE:
       if (!(tag_info = get_tag_info( tag_id, tag_type )))
-        return MB_TAG_NOT_FOUND;
-      return sequenceManager->set_tag_data( tag_id, entity_handles, 
+        rval = MB_TAG_NOT_FOUND;
+      else
+        rval = sequenceManager->set_tag_data( tag_id, entity_handles, 
                                             data, tag_info->default_value() );
   
     case MB_TAG_SPARSE:
-      return mSparseData->set_data( tag_id, entity_handles, data );
+      rval = sequenceManager->check_valid_entities( entity_handles );
+      if (MB_SUCCESS == rval)
+        rval = mSparseData->set_data( tag_id, entity_handles, data );
+      break;
     
     case MB_TAG_BIT:
       if (entity_handles.size() == 1)
-        return set_bits( tag_handle, entity_handles.front(), *reinterpret_cast<const unsigned char*>(data) );
+        rval = sequenceManager->check_valid_entities( entity_handles );
       else
-        return MB_FAILURE;
+        rval = MB_FAILURE;
+      if (MB_SUCCESS == rval)
+        rval = set_bits( tag_handle, entity_handles.front(), *reinterpret_cast<const unsigned char*>(data) );
+      break;
     
     default:
-      return MB_TAG_NOT_FOUND;
+      rval = MB_TAG_NOT_FOUND;
+      break;
   }
+  
+  return rval;
 }
 
 MBErrorCode TagServer::set_data( const MBTag tag_handle, 
@@ -338,6 +360,7 @@
                                  void const* const* data,
                                  const int* lengths )
 {
+  MBErrorCode rval;
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
   const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
   const TagInfo* tag_info = get_tag_info( tag_id, tag_type );
@@ -350,21 +373,31 @@
     
   switch (tag_type) {
     case MB_TAG_DENSE:
-      return sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
+      rval = sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
                    data, lengths, tag_info->default_value() );
-  
+      break;
+      
     case MB_TAG_SPARSE:
-      return mSparseData->set_data( tag_id, entity_handles, num_entities, data, lengths );
+      rval = sequenceManager->check_valid_entities( entity_handles, num_entities );
+      if (MB_SUCCESS == rval)
+        rval = mSparseData->set_data( tag_id, entity_handles, num_entities, data, lengths );
+      break;
     
     case MB_TAG_BIT:
       if (num_entities == 1)
-        return set_bits( tag_handle, *entity_handles, *reinterpret_cast<const unsigned char*>(*data) );
+        rval = sequenceManager->check_valid_entities( entity_handles, num_entities );
       else
-        return MB_FAILURE;
+        rval = MB_FAILURE;
+      if (MB_SUCCESS == rval)
+        rval = set_bits( tag_handle, *entity_handles, *reinterpret_cast<const unsigned char*>(*data) );
+      break;
     
     default:
-      return MB_TAG_NOT_FOUND;
+      rval = MB_TAG_NOT_FOUND;
+      break;
   }
+  
+  return rval;
 }
 
 MBErrorCode TagServer::set_data( const MBTag tag_handle, 
@@ -372,6 +405,7 @@
                                  void const* const* data,
                                  const int* lengths )
 {
+  MBErrorCode rval;
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
   const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
   const TagInfo* tag_info = get_tag_info( tag_id, tag_type );
@@ -384,21 +418,31 @@
     
   switch (tag_type) {
     case MB_TAG_DENSE:
-      return sequenceManager->set_tag_data( tag_id, entity_handles, 
+      rval = sequenceManager->set_tag_data( tag_id, entity_handles, 
                       data, lengths, tag_info->default_value() );
-  
+      break;
+      
     case MB_TAG_SPARSE:
-      return mSparseData->set_data( tag_id, entity_handles, data, lengths );
+      rval = sequenceManager->check_valid_entities( entity_handles );
+      if (MB_SUCCESS == rval)
+        rval = mSparseData->set_data( tag_id, entity_handles, data, lengths );
+      break;
     
     case MB_TAG_BIT:
       if (entity_handles.size() == 1)
-        return set_bits( tag_handle, entity_handles.front(), *reinterpret_cast<const unsigned char*>(*data) );
+        rval = sequenceManager->check_valid_entities( entity_handles );
       else
-        return MB_FAILURE;
+        rval = MB_FAILURE;
+      if (MB_SUCCESS == rval)
+        rval = set_bits( tag_handle, entity_handles.front(), *reinterpret_cast<const unsigned char*>(*data) );
+      break;
     
     default:
-      return MB_TAG_NOT_FOUND;
+      rval = MB_TAG_NOT_FOUND;
+      break;
   }
+  
+  return rval;
 }
 
 

Modified: MOAB/trunk/TagTest.cpp
===================================================================
--- MOAB/trunk/TagTest.cpp	2008-09-05 18:44:36 UTC (rev 2056)
+++ MOAB/trunk/TagTest.cpp	2008-09-08 16:06:16 UTC (rev 2057)
@@ -29,6 +29,7 @@
 void test_get_set_variable_length_mesh();
 
 void regression_one_entity_by_var_tag();
+void regression_tag_on_nonexistent_entity();
 
 int main()
 {
@@ -59,6 +60,7 @@
   failures += RUN_TEST( test_get_set_variable_length_dense );
   failures += RUN_TEST( test_get_set_variable_length_mesh );  
   failures += RUN_TEST( regression_one_entity_by_var_tag );
+  failures += RUN_TEST( regression_tag_on_nonexistent_entity );
   
   return failures;
 }
@@ -1661,3 +1663,86 @@
   CHECK_EQUAL( vertex, ents.front() );
 }
 
+/* Return MB_ENTITY_NOT_FOUND if asked to set a tag on an 
+   entity that doesn't exist.
+ */
+void regression_tag_on_nonexistent_entity()
+{
+  MBCore moab;
+  MBErrorCode rval;
+  const int tagval = 0xdeadbeef;
+  const void* valarr[1] = { &tagval };
+  const int numval = sizeof(int);
+  
+    // create all three types of tags
+  MBTag dense, sparse, bit;
+  rval = moab.tag_create( "test_dense", numval, MB_TAG_DENSE, MB_TYPE_INTEGER, dense, 0, false );
+  CHECK_ERR(rval);
+  rval = moab.tag_create( "test_sparse", numval, MB_TAG_SPARSE, MB_TYPE_INTEGER, sparse, 0, false );
+  CHECK_ERR(rval);
+  rval = moab.tag_create( "test_bit", numval, MB_TAG_BIT, MB_TYPE_BIT, bit, 0, false );
+  CHECK_ERR(rval);
+  
+    // for each tag type, check all four mechanisms for setting tag data
+    // (fixed and variable length given array or range).
+  MBEntityHandle handle = (MBEntityHandle)1;
+  MBRange handles;
+  handles.insert( handle );
+  
+  rval = moab.tag_set_data( dense,  &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( bit,    &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( bit,    handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  &handle, 1, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, &handle, 1, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  handles, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, handles, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+    // now add create an entity and try an adjacent handle
+  MBEntityHandle set;
+  rval = moab.create_meshset( 0, set );
+  CHECK_ERR(rval);
+  
+  handle = (MBEntityHandle)(set+1);
+  handles.clear();
+  handles.insert( handle );
+  
+  rval = moab.tag_set_data( dense,  &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( bit,    &handle, 1, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( bit,    handles, &tagval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  &handle, 1, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, &handle, 1, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  
+  rval = moab.tag_set_data( dense,  handles, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+  rval = moab.tag_set_data( sparse, handles, valarr, &numval );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, rval );
+}




More information about the moab-dev mailing list