FEI Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
test_utils
test_Utils.cpp
Go to the documentation of this file.
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/test_Utils.hpp
>
12
13
#include <
fei_ArrayUtils.hpp
>
14
#include <
fei_utils.hpp
>
15
#include <
fei_CommUtils.hpp
>
16
#include <
snl_fei_Utils.hpp
>
17
#include <
fei_Param.hpp
>
18
#include <
fei_ParameterSet.hpp
>
19
#include <
fei_SharedPtr.hpp
>
20
#include <cmath>
21
22
#undef fei_file
23
#define fei_file "test_Utils.cpp"
24
#include <
fei_ErrMacros.hpp
>
25
26
test_Utils::test_Utils
(
MPI_Comm
comm)
27
:
tester
(comm)
28
{
29
}
30
31
test_Utils::~test_Utils
()
32
{
33
}
34
35
void
test_Utils_binarySearch
()
36
{
37
std::vector<int> intarray;
38
intarray.push_back(1);
39
intarray.push_back(2);
40
intarray.push_back(5);
41
intarray.push_back(6);
42
intarray.push_back(9);
43
44
int
offset = 0;
45
int
insertPoint = -1;
46
47
FEI_COUT
<<
"testing correctness of fei::binarySearch(int,int*,int,int)..."
;
48
49
offset =
fei::binarySearch
(0, &intarray[0], intarray.size(),
50
insertPoint);
51
if
(offset != -1 || insertPoint != 0) {
52
throw
std::runtime_error(
"fei::binarySearch test failed 1."
);
53
}
54
55
offset =
fei::binarySearch
(2, &intarray[0], intarray.size(),
56
insertPoint);
57
if
(offset != 1) {
58
throw
std::runtime_error(
"fei::binarySearch test failed 2."
);
59
}
60
61
offset =
fei::binarySearch
(3, &intarray[0], intarray.size(),
62
insertPoint);
63
if
(offset != -1 || insertPoint != 2) {
64
throw
std::runtime_error(
"fei::binarySearch test failed 3."
);
65
}
66
67
offset =
fei::binarySearch
(4, &intarray[0], intarray.size(),
68
insertPoint);
69
if
(offset != -1 || insertPoint != 2) {
70
throw
std::runtime_error(
"fei::binarySearch test failed 4."
);
71
}
72
73
offset =
fei::binarySearch
(9, &intarray[0], intarray.size(),
74
insertPoint);
75
if
(offset != 4) {
76
throw
std::runtime_error(
"fei::binarySearch test failed 5."
);
77
}
78
79
offset =
fei::binarySearch
(8, &intarray[0], intarray.size(),
80
insertPoint);
81
if
(offset != -1 || insertPoint != 4) {
82
throw
std::runtime_error(
"fei::binarySearch test failed 6."
);
83
}
84
85
offset =
fei::binarySearch
(10, &intarray[0], intarray.size(),
86
insertPoint);
87
if
(offset != -1 || insertPoint != 5) {
88
throw
std::runtime_error(
"fei::binarySearch test failed 7."
);
89
}
90
91
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
92
}
93
94
int
test_Utils::runtests
()
95
{
96
if
(
numProcs_
< 2) {
97
test_Utils_binarySearch
();
98
99
CHK_ERR
(
serialtest1
() );
100
CHK_ERR
(
serialtest2
() );
101
CHK_ERR
(
serialtest3
() );
102
}
103
104
CHK_ERR
(
test1
() );
105
CHK_ERR
(
test2
() );
106
CHK_ERR
(
test3
() );
107
CHK_ERR
(
test4
() );
108
return
(0);
109
}
110
111
int
test_Utils::serialtest1
()
112
{
113
FEI_COUT
<<
"testing snl_fei::leading_substring_length..."
;
114
115
static
char
string1[] =
"test "
;
116
string1[4] =
'\0'
;
117
if
(
snl_fei::leading_substring_length
(string1) != 4) {
118
ERReturn
(-1);
119
}
120
121
static
char
string2[] =
"second test"
;
122
if
(
snl_fei::leading_substring_length
(string2) != 6) {
123
ERReturn
(-1);
124
}
125
126
static
char
string3[] =
"third test"
;
127
string3[5] =
'\t'
;
128
if
(
snl_fei::leading_substring_length
(string3) != 5) {
129
ERReturn
(-1);
130
}
131
132
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
133
134
return
(0);
135
}
136
137
int
test_Utils::serialtest2
()
138
{
139
FEI_COUT
<<
"testing snl_fei::getDoubleParamValue..."
;
140
141
static
char
string1[] =
"DOUBLE1 1.0"
;
142
static
char
string2[] =
"DOUBLE2 1.0e+0"
;
143
static
char
string3[] =
"DOUBLE3 1.0E+0"
;
144
static
char
string4[] =
"DOUBLE4 1"
;
145
146
std::vector<char*> params;
147
params.push_back(string1);
148
params.push_back(string2);
149
params.push_back(string3);
150
params.push_back(string4);
151
152
double
d1,d2,d3,d4;
153
154
CHK_ERR
(
snl_fei::getDoubleParamValue
(
"DOUBLE1"
,
155
params.size(), ¶ms[0],d1));
156
CHK_ERR
(
snl_fei::getDoubleParamValue
(
"DOUBLE2"
,
157
params.size(), ¶ms[0],d2));
158
CHK_ERR
(
snl_fei::getDoubleParamValue
(
"DOUBLE3"
,
159
params.size(), ¶ms[0],d3));
160
CHK_ERR
(
snl_fei::getDoubleParamValue
(
"DOUBLE4"
,
161
params.size(), ¶ms[0],d4));
162
163
if
(std::abs(d1 - 1.0) > 1.e-49 || std::abs(d2 - 1.0) > 1.e-49 ||
164
std::abs(d3 - 1.0) > 1.e-49 || std::abs(d4 - 1.0) > 1.e-49) {
165
ERReturn
(-1);
166
}
167
168
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
169
170
return
(0);
171
}
172
173
int
test_Utils::serialtest3
()
174
{
175
FEI_COUT
<<
"testing fei::Param and fei::ParameterSet..."
;
176
177
fei::Param
param1(
"string-param"
,
"garbage value"
);
178
fei::Param
param2(
"double-param"
, 2.5);
179
fei::Param
param3(
"int-param"
, 1);
180
181
if
(param1.
getType
() !=
fei::Param::STRING
) {
182
ERReturn
(-1);
183
}
184
185
if
(param2.
getType
() !=
fei::Param::DOUBLE
) {
186
ERReturn
(-1);
187
}
188
189
if
(param3.
getType
() !=
fei::Param::INT
) {
190
ERReturn
(-1);
191
}
192
193
fei::ParameterSet
paramset;
194
paramset.
add
(
fei::Param
(
"string-param"
,
"garbage value"
));
195
paramset.
add
(param2);
196
paramset.
add
(param3);
197
198
if
(paramset.
size
() != 3) {
199
ERReturn
(-1);
200
}
201
202
fei::ParameterSet::const_iterator
203
iter = paramset.
begin
(),
204
iter_end = paramset.
end
();
205
206
int
i=0;
207
for
(; iter != iter_end; ++iter) {
208
if
(i==3) {
209
ERReturn
(-1);
210
}
211
++i;
212
}
213
214
if
(paramset.
get
(
"int-param"
) == NULL) {
215
ERReturn
(-1);
216
}
217
218
int
dummy;
219
int
err = paramset.
getIntParamValue
(
"int-param"
, dummy);
220
if
(err != 0) {
221
ERReturn
(-1);
222
}
223
224
if
(dummy != 1) {
225
ERReturn
(-1);
226
}
227
228
std::string dummychars;
229
err = paramset.
getStringParamValue
(
"string-param"
, dummychars);
230
if
(err != 0) {
231
ERReturn
(-1);
232
}
233
234
if
(
"garbage value"
!= dummychars) {
235
ERReturn
(-1);
236
}
237
238
//if (!snl_fei::leadingSubstring("garbage-value", "garbage")) {
239
// ERReturn(-1);
240
//}
241
242
//if (snl_fei::leadingSubstring("garb-value", "garbage")) {
243
// ERReturn(-1);
244
//}
245
246
std::vector<std::string> stdstrings;
247
std::string tempstr;
248
249
tempstr =
"string-param garbage value"
;
250
stdstrings.push_back(tempstr);
251
252
tempstr =
"int-param 58"
;
253
stdstrings.push_back(tempstr);
254
255
tempstr =
"real-param 45.e-2"
;
256
stdstrings.push_back(tempstr);
257
258
fei::ParameterSet
pset;
259
fei::utils::parse_strings
(stdstrings,
" "
, pset);
260
261
err = pset.
getStringParamValue
(
"string-param"
, dummychars);
262
if
(
"garbage value"
!= dummychars) {
263
ERReturn
(-1);
264
}
265
266
err = pset.
getIntParamValue
(
"int-param"
, dummy);
267
if
(dummy != 58) {
268
ERReturn
(-1);
269
}
270
271
double
ddummy;
272
err = pset.
getDoubleParamValue
(
"real-param"
, ddummy);
273
if
(std::abs(ddummy - 45.e-2) > 1.e-49) {
274
ERReturn
(-1);
275
}
276
277
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
278
279
return
(0);
280
}
281
282
void
test_Utils_function_that_throws
()
283
{
284
throw
std::runtime_error(
"testing..."
);
285
}
286
287
int
test_Utils::test1
()
288
{
289
FEI_COUT
<<
"testing std::runtime_error..."
;
290
291
bool
exc_thrown_and_caught =
false
;
292
293
try
{
294
test_Utils_function_that_throws
();
295
}
296
catch
(std::runtime_error& exc) {
297
std::string str(exc.what());
298
if
(str ==
"testing..."
) {
299
exc_thrown_and_caught =
true
;
300
}
301
}
302
303
if
(!exc_thrown_and_caught) {
304
ERReturn
(-1);
305
}
306
307
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
308
return
(0);
309
}
310
311
bool
test_Utils_dummy_destroyed
=
true
;
312
313
class
test_Utils_dummy
{
314
public
:
315
test_Utils_dummy
() {
test_Utils_dummy_destroyed
=
false
;}
316
~test_Utils_dummy
()
317
{
318
test_Utils_dummy_destroyed
=
true
;
319
}
320
};
321
322
int
test_Utils_test_SharedPtr
()
323
{
324
//In this function, make sure the global bool is set to true, then create
325
//the fei::SharedPtr and make sure that the global bool has been set to false.
326
//If so, return 0, otherwise return -1.
327
//When we return, the SharedPtr goes out of scope which should destroy the
328
//test-dummy and cause the global bool to get set back to true. The code
329
//that's calling this function will verify that.
330
331
test_Utils_dummy_destroyed
=
true
;
332
fei::SharedPtr<test_Utils_dummy>
ptr(
new
test_Utils_dummy
);
333
if
(
test_Utils_dummy_destroyed
==
true
)
return
(-1);
334
else
return
(0);
335
}
336
337
int
test_Utils::test2
()
338
{
339
FEI_COUT
<<
"testing fei::SharedPtr..."
;
340
int
err =
test_Utils_test_SharedPtr
();
341
if
(err != 0) {
342
ERReturn
(-1);
343
}
344
345
if
(
test_Utils_dummy_destroyed
!=
true
) {
346
ERReturn
(-1);
347
}
348
349
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
350
return
(0);
351
}
352
353
int
test_Utils::test3
()
354
{
355
FEI_COUT
<<
"testing snl_fei::copy2DToColumnContig..."
;
356
357
int
numrows1 = 3;
358
int
numcols1 = 4;
359
int
numrows2 = 4;
360
int
numcols2 = 3;
361
362
int
i, j;
363
int
len1 = numrows1*numcols1;
364
int
len2 = numrows2*numcols2;
365
366
double
** table2d_1 =
new
double
*[numrows1];
367
for
(i=0; i<numrows1; ++i) {
368
table2d_1[i] =
new
double
[numcols1];
369
for
(j=0; j<numcols1; ++j) {
370
table2d_1[i][j] = j*numrows1+i;
371
}
372
}
373
374
double
** table2d_2 =
new
double
*[numcols2];
375
for
(j=0; j<numcols2; ++j) {
376
table2d_2[j] =
new
double
[numrows2];
377
for
(i=0; i<numrows2; ++i) {
378
table2d_2[j][i] = j*numrows2+i;
379
}
380
}
381
382
double
* cc1 =
new
double
[len1];
383
double
* cc2 =
new
double
[len2];
384
385
snl_fei::copy2DToColumnContig
(numrows1, numcols1, table2d_1,
386
FEI_DENSE_ROW
, cc1);
387
388
snl_fei::copy2DToColumnContig
(numrows2, numcols2, table2d_2,
389
FEI_DENSE_COL
, cc2);
390
391
for
(i=0; i<len1; ++i) {
392
if
(std::abs(cc1[i] - cc2[i]) > 1.e-49) {
393
throw
std::runtime_error(
"column-contig arrays not equal."
);
394
}
395
}
396
397
for
(j=0; j<numrows1; ++j)
delete
[] table2d_1[j];
398
delete
[] table2d_1;
399
delete
[] cc1;
400
delete
[] cc2;
401
402
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
403
404
FEI_COUT
<<
"testing snl_fei::copy2DBlockDiagToColumnContig..."
;
405
406
numrows1 = 12;
407
int
numBlocks = 3;
408
int
* blockSizes =
new
int
[numBlocks];
409
for
(i=0; i<numBlocks; ++i) {
410
blockSizes[i] = 4;
411
}
412
413
table2d_1 =
new
double
*[numrows1];
414
for
(i=0; i<numrows1; ++i) {
415
table2d_1[i] =
new
double
[4];
416
for
(j=0; j<4; ++j) {
417
table2d_1[i][j] = 1.0*i*4+j;
418
}
419
}
420
421
len1 = numrows1*4;
422
cc1 =
new
double
[len1];
423
424
snl_fei::copy2DBlockDiagToColumnContig
(numBlocks, blockSizes, table2d_1,
425
FEI_BLOCK_DIAGONAL_ROW
, cc1);
426
427
for
(i=0; i<len1; ++i) {
428
if
(std::abs(1.0*i - cc1[i]) > 1.e-49) {
429
throw
std::runtime_error(
"copy2DBlockDiagToColumnContig row test failed."
);
430
}
431
}
432
433
for
(j=0; j<numrows1; ++j)
delete
[] table2d_1[j];
434
delete
[] table2d_1;
435
for
(j=0; j<numcols2; ++j)
delete
[] table2d_2[j];
436
delete
[] table2d_2;
437
438
delete
[] cc1;
439
delete
[] blockSizes;
440
441
FEI_COUT
<<
"ok"
<<
FEI_ENDL
;
442
return
(0);
443
}
444
445
int
test_Utils::test4
()
446
{
447
return
(0);
448
}
fei::Param
Definition
fei_Param.hpp:23
fei::Param::DOUBLE
@ DOUBLE
Definition
fei_Param.hpp:28
fei::Param::INT
@ INT
Definition
fei_Param.hpp:29
fei::Param::STRING
@ STRING
Definition
fei_Param.hpp:27
fei::Param::getType
ParamType getType() const
Definition
fei_Param.hpp:98
fei::ParameterSet::const_iterator
Definition
fei_ParameterSet.hpp:82
fei::ParameterSet
Definition
fei_ParameterSet.hpp:46
fei::ParameterSet::size
int size() const
Definition
fei_ParameterSet.hpp:254
fei::ParameterSet::getIntParamValue
int getIntParamValue(const char *name, int ¶mValue) const
Definition
fei_ParameterSet.cpp:28
fei::ParameterSet::add
void add(const Param ¶m, bool maintain_unique_keys=true)
Definition
fei_ParameterSet.hpp:235
fei::ParameterSet::get
const Param * get(const char *name) const
Definition
fei_ParameterSet.hpp:260
fei::ParameterSet::end
const_iterator end() const
Definition
fei_ParameterSet.hpp:229
fei::ParameterSet::begin
const_iterator begin() const
Definition
fei_ParameterSet.hpp:223
fei::ParameterSet::getStringParamValue
int getStringParamValue(const char *name, std::string ¶mValue) const
Definition
fei_ParameterSet.cpp:57
fei::ParameterSet::getDoubleParamValue
int getDoubleParamValue(const char *name, double ¶mValue) const
Definition
fei_ParameterSet.cpp:40
fei::SharedPtr
Definition
fei_SharedPtr.hpp:65
test_Utils_dummy
Definition
test_Utils.cpp:313
test_Utils_dummy::~test_Utils_dummy
~test_Utils_dummy()
Definition
test_Utils.cpp:316
test_Utils_dummy::test_Utils_dummy
test_Utils_dummy()
Definition
test_Utils.cpp:315
test_Utils::test3
int test3()
Definition
test_Utils.cpp:353
test_Utils::serialtest3
int serialtest3()
Definition
test_Utils.cpp:173
test_Utils::serialtest2
int serialtest2()
Definition
test_Utils.cpp:137
test_Utils::~test_Utils
virtual ~test_Utils()
Definition
test_Utils.cpp:31
test_Utils::serialtest1
int serialtest1()
Definition
test_Utils.cpp:111
test_Utils::test2
int test2()
Definition
test_Utils.cpp:337
test_Utils::test1
int test1()
Definition
test_Utils.cpp:287
test_Utils::runtests
int runtests()
Definition
test_Utils.cpp:94
test_Utils::test_Utils
test_Utils(MPI_Comm comm)
Definition
test_Utils.cpp:26
test_Utils::test4
int test4()
Definition
test_Utils.cpp:445
tester::tester
tester(MPI_Comm comm)
Definition
tester.cpp:17
tester::numProcs_
int numProcs_
Definition
tester.hpp:38
fei_ArrayUtils.hpp
fei_CommUtils.hpp
fei_ErrMacros.hpp
ERReturn
#define ERReturn(a)
Definition
fei_ErrMacros.hpp:37
CHK_ERR
#define CHK_ERR(a)
Definition
fei_ErrMacros.hpp:26
fei_Param.hpp
fei_ParameterSet.hpp
fei_SharedPtr.hpp
FEI_DENSE_ROW
#define FEI_DENSE_ROW
Definition
fei_defs.h:77
FEI_DENSE_COL
#define FEI_DENSE_COL
Definition
fei_defs.h:80
FEI_BLOCK_DIAGONAL_ROW
#define FEI_BLOCK_DIAGONAL_ROW
Definition
fei_defs.h:84
FEI_ENDL
#define FEI_ENDL
Definition
fei_iostream.hpp:34
FEI_COUT
#define FEI_COUT
Definition
fei_iostream.hpp:33
fei_macros.hpp
MPI_Comm
#define MPI_Comm
Definition
fei_mpi.h:56
fei_utils.hpp
fei::utils::parse_strings
void parse_strings(std::vector< std::string > &stdstrings, const char *separator_string, fei::ParameterSet ¶mset)
Definition
fei_utils.cpp:191
fei::binarySearch
int binarySearch(const T &item, const T *list, int len)
Definition
fei_ArrayUtils.hpp:68
snl_fei::leading_substring_length
unsigned leading_substring_length(const char *string)
Definition
snl_fei_Utils.cpp:266
snl_fei::getDoubleParamValue
int getDoubleParamValue(const char *key, int numParams, const char *const *params, double ¶mValue)
Definition
snl_fei_Utils.cpp:79
snl_fei::copy2DBlockDiagToColumnContig
void copy2DBlockDiagToColumnContig(int numBlocks, const int *blockSizes, const double *const *values2d, int format, double *colcontigvalues)
Definition
snl_fei_Utils.cpp:540
snl_fei::copy2DToColumnContig
void copy2DToColumnContig(int numrows, int numcols, const double *const *values2d, int format, double *colcontigvalues)
Definition
snl_fei_Utils.cpp:571
snl_fei_Utils.hpp
test_Utils_dummy_destroyed
bool test_Utils_dummy_destroyed
Definition
test_Utils.cpp:311
test_Utils_binarySearch
void test_Utils_binarySearch()
Definition
test_Utils.cpp:35
test_Utils_function_that_throws
void test_Utils_function_that_throws()
Definition
test_Utils.cpp:282
test_Utils_test_SharedPtr
int test_Utils_test_SharedPtr()
Definition
test_Utils.cpp:322
test_Utils.hpp
Generated by
1.17.0