MOAB: Mesh Oriented datABase  (version 5.5.0)
internals_test.cpp
Go to the documentation of this file.
1 #include "Internals.hpp"
2 #include <iostream>
3 using namespace std;
4 
5 using namespace moab;
6 
7 HandleUtils handleUtils( 0, 1 );
8 
9 bool internal_assert( bool c )
10 {
11  return !c;
12 }
13 
14 #define handle_test_assert( C ) \
15  if( internal_assert( C ) ) \
16  { \
17  cout << "Test: " #C " failed ." << endl; \
18  return false; \
19  }
20 
21 bool handle_test( EntityType type, EntityID id, int proc, bool should_fail )
22 {
23  int err = 0;
24  EntityHandle handle = CREATE_HANDLE( type, handleUtils.create_id( id, proc ), err );
25  if( should_fail )
26  {
27  handle_test_assert( err ) return true;
28  }
29  handle_test_assert( !err )
30 
31  EntityType type_from_handle = TYPE_FROM_HANDLE( handle );
32  handle_test_assert( type_from_handle == type )
33 
34  EntityID id_from_handle = handleUtils.id_from_handle( handle );
35  handle_test_assert( id_from_handle == id )
36 
37  int proc_from_handle = handleUtils.rank_from_handle( handle );
38  handle_test_assert( proc_from_handle == proc )
39 
40  return true;
41 }
42 
43 #define tag_test_assert( C ) \
44  if( internal_assert( C ) ) \
45  { \
46  cout << "Test: " #C " failed." << endl; \
47  return false; \
48  }
49 
50 bool tag_test( TagId id, TagType prop )
51 {
52  Tag tag = TAG_HANDLE_FROM_ID( id, prop );
53 
54  unsigned long id_from_handle = ID_FROM_TAG_HANDLE( tag );
55  tag_test_assert( id_from_handle == id )
56 
57  TagType prop_from_handle = PROP_FROM_TAG_HANDLE( tag );
58  tag_test_assert( prop_from_handle == prop )
59 
60  return true;
61 }
62 
63 //! Sample code on a 32-bit system.
64 int main()
65 {
66  const unsigned cpus[] = { 1, 4, 16, 5, 20 };
67  const int num_cpus = sizeof( cpus ) / sizeof( cpus[0] );
68  unsigned errors = 0, tests = 0;
69  const int num_prop = MB_TAG_LAST + 1;
70 
71  ++tests;
72  if( MB_TAG_LAST > num_prop )
73  {
74  cout << "MB_TAG_PROP_WIDTH insufficient for size of TagType" << endl;
75  ++errors;
76  }
77 
78  ++tests;
79  if( MBMAXTYPE > 1 << MB_TYPE_WIDTH )
80  {
81  cout << "MB_TYPE_WIDTH insufficient for size of EntityType" << endl;
82  ++errors;
83  }
84 
85  // if any errors so far, abort because everything else will
86  // probably fail.
87  if( errors ) return errors;
88 
89  for( int num_cpu = 0; num_cpu < num_cpus; ++num_cpu )
90  {
91 
92  handleUtils = HandleUtils( 0, cpus[num_cpu] );
93 
94  // init these after setting num_cpu, because max id depends on num cpu.
95  const EntityID ids[] = { 0, 1, handleUtils.max_id() / 2, handleUtils.max_id() - 1, handleUtils.max_id() };
96  const TagId tids[] = { 0, 1, MB_TAG_PROP_MASK / 2, MB_TAG_PROP_MASK - 1, MB_TAG_PROP_MASK };
97  const int num_ids = sizeof( ids ) / sizeof( ids[0] );
98  const int num_tids = sizeof( tids ) / sizeof( tids[0] );
99 
100  for( unsigned cpu = 0; cpu < cpus[num_cpu]; ++cpu )
101  {
102  for( EntityType type = MBVERTEX; type < MBMAXTYPE; ++type )
103  for( int id = 0; id < num_ids; ++id )
104  {
105  ++tests;
106  if( !handle_test( type, ids[id], cpu, false ) )
107  {
108  cout << "Test of handle with type=" << type << ", id=" << ids[id] << ", proc=" << cpu
109  << ", and numproc=" << cpus[num_cpu] << endl;
110  ++errors;
111  }
112  }
113 
114  for( int prop = 0; prop < num_prop; ++prop )
115  for( int id = 0; id < num_tids; ++id )
116  {
117  ++tests;
118  if( !tag_test( tids[id], (TagType)prop ) )
119  {
120  cout << "Test of tag handle with prop=" << prop << ", id=" << tids[id] << ", proc=" << cpu
121  << ", and numproc=" << cpus[num_cpu] << endl;
122  ++errors;
123  }
124  }
125  }
126  }
127 
128  // test some stuff that should fail
129  handleUtils = HandleUtils( 0, 16 );
130  ++tests;
131  if( !handle_test( MBVERTEX, MB_END_ID + 1, 0, true ) )
132  {
133  cout << "Failed to catch ID overflow" << endl;
134  ++errors;
135  }
136  ++tests;
137  if( !handle_test( (EntityType)( MBMAXTYPE + 1 ), 1, 0, true ) )
138  {
139  cout << "Failed to catch type overflow" << endl;
140  ++errors;
141  }
142  // ++tests;
143  // if (!handle_test( MBHEX, 1, 17, true)) {
144  // cout << "Failed to catch Proc# overflow" << endl;
145  // ++errors;
146  // }
147 
148  if( errors )
149  cout << endl << errors << " of " << tests << " tests failed." << endl << endl;
150  else
151  cout << endl << tests << " tests passed." << endl << endl;
152  return errors;
153 }