FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
test_misc.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include <fei_macros.hpp>
10 
11 #include <test_utils/fei_test_utils.hpp>
12 
13 #include <test_utils/test_misc.hpp>
14 
15 #include <test_utils/test_Factory_helper.hpp>
16 
17 #include <fei_FieldMask.hpp>
18 #include <snl_fei_RecordCollection.hpp>
19 
20 #include <fei_VectorSpace.hpp>
21 #include <fei_Vector_Impl.hpp>
22 
23 #undef fei_file
24 #define fei_file "test_misc.cpp"
25 #include <fei_ErrMacros.hpp>
26 
27 test_misc::test_misc(MPI_Comm comm)
28  : tester(comm)
29 {
30 }
31 
32 test_misc::~test_misc()
33 {
34 }
35 
36 void test_misc_FieldMask()
37 {
38  FEI_COUT << "testing fei::FieldMask...";
39 
40  //A general test of fei::FieldMask.
41 
42  unsigned numFields = 5;
43  std::vector<int> fieldIDs(numFields);
44  std::vector<int> fieldSizes(numFields);
45  int checkNumIndices = 0;
46  for(unsigned i=0; i<numFields; ++i) {
47  fieldIDs[i] = i;
48  fieldSizes[i] = i;
49  checkNumIndices += i;
50  }
51 
52  fei::FieldMask fieldMask;
53 
54  for(int i=fieldIDs.size()-1; i>= 0; --i) {
55  fieldMask.addField(fieldIDs[i], fieldSizes[i]);
56  }
57 
58  std::vector<int>& maskFields = fieldMask.getFieldIDs();
59  std::vector<int>& maskFieldSizes = fieldMask.getFieldSizes();
60 
61  if (maskFields != fieldIDs) {
62  throw std::runtime_error("FieldMask test failed.");
63  }
64 
65  if (maskFieldSizes != fieldSizes) {
66  throw std::runtime_error("FieldMask size test failed.");
67  }
68 
69  int checkOffset = 0;
70  for(unsigned j=0; j<fieldIDs.size(); ++j) {
71  int offset = -1;
72  fieldMask.getFieldEqnOffset(fieldIDs[j], offset);
73  if (offset != checkOffset) {
74  throw std::runtime_error("FieldMask offset test failed.");
75  }
76  checkOffset += j;
77  }
78 
79  int numIndices = fieldMask.getNumIndices();
80  if (numIndices != checkNumIndices) {
81  throw std::runtime_error("FieldMask numIndices test failed.");
82  }
83 
84  bool exc_caught = false;
85  try {
86  fieldMask.addField(-1, 0);
87  }
88  catch (...) {
89  exc_caught = true;
90  }
91 
92  if (!exc_caught) {
93  throw std::runtime_error("FieldMask failed to throw on negative fieldID.");
94  }
95 
96  fieldMask.addField(2, 2);
97 
98  if (fieldMask.getNumFields() != numFields) {
99  throw std::runtime_error("FieldMask getNumFields test failed.");
100  }
101 
102  int fieldid1 = 0;
103  int fieldid2 = 1;
104  int fieldid3 = 2;
105  int fieldsize = 1;
106 
107  fei::FieldMask fm1(1, &fieldid1, &fieldsize);
108  fei::FieldMask fm2(1, &fieldid2, &fieldsize);
109  fei::FieldMask fm3(1, &fieldid3, &fieldsize);
110 
111  fei::FieldMask fm12(fm1);
112  fm12.addField(fieldid2, fieldsize);
113 
114  fei::FieldMask fm123(fm1);
115  fm123.addField(fieldid2, fieldsize);
116  fm123.addField(fieldid3, fieldsize);
117 
118  if (fm1.getMaskID() == fm2.getMaskID()) {
119  throw std::runtime_error("FieldMask getMaskID test failed.");
120  }
121 
122  if (fm2.getMaskID() == fm12.getMaskID()) {
123  throw std::runtime_error("FieldMask getMaskID2 test failed.");
124  }
125 
126  if (fm12.getMaskID() !=
127  fei::FieldMask::calculateMaskID(fm1, fieldid2)){
128  throw std::runtime_error("FieldMask getMaskID3 test failed.");
129  }
130 
131  if (fm12.getMaskID() == fm3.getMaskID()) {
132  throw std::runtime_error("FieldMask getMaskID4 test failed.");
133  }
134 
135  if (fm123.getMaskID() !=
136  fei::FieldMask::calculateMaskID(fm12, fieldid3)){
137  throw std::runtime_error("FieldMask getMaskID5 test failed.");
138  }
139 
140  if (fm3.getMaskID() == fm123.getMaskID()) {
141  throw std::runtime_error("FieldMask getMaskID6 test failed.");
142  }
143 
144  FEI_COUT << "ok"<<FEI_ENDL;
145 }
146 
147 void test_misc_RecordCollection()
148 {
149  FEI_COUT << "testing snl_fei::RecordCollection...";
150 
151  int fieldID0 = 0;
152  int fieldID1 = 1;
153  int fieldID2 = 2;
154  int fieldSize = 1;
155  int ID0 = 0;
156  int ID1 = 1;
157 
158  std::vector<fei::FieldMask*> fieldMasks;
159 
160  snl_fei::RecordCollection recColl(0);
161 
162  int* records = new int[1];
163 
164  recColl.initRecords(fieldID0, fieldSize, 1, &ID0,
165  fieldMasks, records);
166 
167  recColl.initRecords(fieldID1, fieldSize, 1, &ID0,
168  fieldMasks, records);
169 
170  recColl.initRecords(fieldID2, fieldSize, 1, &ID0,
171  fieldMasks, records);
172 
173  recColl.initRecords(fieldID0, fieldSize, 1, &ID1,
174  fieldMasks, records);
175 
176  recColl.initRecords(fieldID1, fieldSize, 1, &ID1,
177  fieldMasks, records);
178 
179  recColl.initRecords(fieldID2, fieldSize, 1, &ID1,
180  fieldMasks, records);
181 
182  if (fieldMasks.size() != 5) {
183  throw std::runtime_error("RecordCollection fieldMasks.length test failed.");
184  }
185 
186  std::vector<fei::Record<int> >& rvec = recColl.getRecords();
187 
188  std::vector<fei::Record<int> >::iterator
189  r_iter = rvec.begin(),
190  r_end = rvec.end();
191 
192  int numIndices = 0;
193  for(; r_iter != r_end; ++r_iter) {
194  numIndices += (*r_iter).getFieldMask()->getNumIndices();
195  }
196 
197  if (numIndices != 6) {
198  throw std::runtime_error("RecordCollection numIndices test failed.");
199  }
200 
201  delete [] records;
202  for(unsigned i=0; i<fieldMasks.size(); ++i) delete fieldMasks[i];
203 
204  FEI_COUT << "ok"<<FEI_ENDL;
205 }
206 
207 int test_misc::runtests()
208 {
209  if (numProcs_ < 2) {
210  test_misc_FieldMask();
211  test_misc_RecordCollection();
212 
213  CHK_ERR( serialtest1() );
214  CHK_ERR( serialtest2() );
215  CHK_ERR( serialtest3() );
216  }
217 
218  CHK_ERR( test1() );
219  CHK_ERR( test2() );
220  CHK_ERR( test3() );
221  CHK_ERR( test4() );
222  return(0);
223 }
224 
225 int test_misc::serialtest1()
226 {
227  FEI_COUT << "testing fei_test_utils::within_percentage_margin...";
228  double value1 = 65000.0;
229  double value2 = 6500.0;
230  bool result = fei_test_utils::within_percentage_margin(value1, value2, 10);
231  if (result == true) {
232  ERReturn(-1);
233  }
234 
235  value1 = 6500.0;
236  value2 = 6500.1;
237  result = fei_test_utils::within_percentage_margin(value1,value2,1);
238  if (result != true) {
239  ERReturn(-1);
240  }
241 
242  value1 = -10.0;
243  value2 = 0.0;
244  result = fei_test_utils::within_percentage_margin(value1,value2,30);
245  if (result == true) {
246  ERReturn(-1);
247  }
248 
249  value1 = -1.e-18;
250  value2 = 1.e-15;
251  result = fei_test_utils::within_percentage_margin(value1,value2,10);
252  if (result != true) {
253  ERReturn(-1);
254  }
255 
256  FEI_COUT << "ok"<<FEI_ENDL;
257  return(0);
258 }
259 
260 int test_misc::serialtest2()
261 {
262  FEI_COUT << "testing fei::lowerBound...";
263  std::vector<int> list(5);
264 
265  list[0] = 1;
266  list[1] = 4;
267  list[2] = 6;
268  list[3] = 7;
269  list[4] = 11;
270 
271  int item = 0;
272  int lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
273 
274  if (lowerbound != 0) {
275  throw std::runtime_error("failed test 1");
276  }
277 
278  item = 1;
279  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
280 
281  if (lowerbound != 0) {
282  throw std::runtime_error("failed test 2");
283  }
284 
285  item = 2;
286  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
287 
288  if (lowerbound != 1) {
289  throw std::runtime_error("failed test 3");
290  }
291 
292  item = 7;
293  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
294 
295  if (lowerbound != 3) {
296  throw std::runtime_error("failed test 4");
297  }
298 
299  item = 9;
300  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
301 
302  if (lowerbound != 4) {
303  throw std::runtime_error("failed test 5");
304  }
305 
306  item = 11;
307  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
308 
309  if (lowerbound != 4) {
310  throw std::runtime_error("failed test6");
311  }
312 
313  item = 12;
314  lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
315 
316  if (lowerbound != 5) {
317  throw std::runtime_error("failed test 7");
318  }
319 
320  lowerbound = fei::lowerBound<int>(item, (int*)0, (int)0);
321 
322  if (lowerbound != 0) {
323  throw std::runtime_error("failed test 8");
324  }
325 
326  std::vector<int> list2;
327  list2.push_back(2);
328 
329  item = 2;
330  lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
331 
332  if (lowerbound != 0) {
333  throw std::runtime_error("failed test 9");
334  }
335 
336  item = 5;
337  lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
338 
339  if (lowerbound != 1) {
340  throw std::runtime_error("failed test 10");
341  }
342 
343  FEI_COUT << "ok"<<FEI_ENDL;
344 
345  return(0);
346 }
347 
348 int test_misc::serialtest3()
349 {
350  FEI_COUT << "testing snl_fei::MapContig<fei::ctg_set<int>*>...";
351 
354 
355  static fei::ctg_set<int> dummy;
356 
357  for(int i=1; i<6; ++i) {
358  fei::ctg_set<int>* newset = pool_alloc.allocate(1);
359  pool_alloc.construct(newset,dummy);
360 
361 
362  for(int j=0; j<3; ++j) {
363  newset->insert2(j);
364  }
365 
366  std::pair<int,fei::ctg_set<int>*> newpair(i,newset);
367  mc.insert(newpair);
368  }
369 
371 
372  if (m_copy.size() != mc.size()) {
373  throw std::runtime_error("failed test 1.");
374  }
375 
377  mc_iter = mc.begin(),
378  mc_end = mc.end();
379 
381  c_iter = m_copy.begin(),
382  c_end = m_copy.end();
383 
384  for(; mc_iter != mc_end; ++mc_iter) {
385  std::pair<int,fei::ctg_set<int>*> mc_pair = *mc_iter;
386  std::pair<int,fei::ctg_set<int>*> c_pair = *c_iter;
387 
388  if (mc_pair.first != c_pair.first) {
389  throw std::runtime_error("failed test 2.");
390  }
391 
392  if (*(mc_pair.second) != *(c_pair.second)) {
393  throw std::runtime_error("failed test 3.");
394  }
395  ++c_iter;
396  }
397 
398  mc_iter = mc.begin();
399  for(; mc_iter != mc_end; ++mc_iter) {
400  pool_alloc.destroy((*mc_iter).second);
401  }
402 
403  FEI_COUT << "ok" << FEI_ENDL;
404 
405  return(0);
406 }
407 
408 int test_misc::test1()
409 {
410 
411  return(0);
412 }
413 
414 int test_misc::test2()
415 {
416 
417  return(0);
418 }
419 
420 int test_misc::test3()
421 {
422 
423  return(0);
424 }
425 
426 int test_misc::test4()
427 {
428  return(0);
429 }
int getNumIndices() const
size_t getNumFields() const
static int calculateMaskID(int numFields, const int *fieldIDs)
std::vector< int > & getFieldSizes()
void insert2(const T &item)
std::vector< int > & getFieldIDs()
void addField(int fieldID, int fieldSize)
bool within_percentage_margin(double value1, double value2, unsigned margin)
int getFieldEqnOffset(int fieldID, int &offset) const