Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Validator_UnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 
16 // 2010/07/30: rabartl: Here I just added all the unit tests to the Teuchos
17 // namespace to remove some clutter.
18 
19 
20 namespace Teuchos {
21 
22 
26 TEUCHOS_UNIT_TEST(Teuchos_Validators, numberValidators)
27 {
28  /*
29  * Testing Int Validator.
30  */
31  RCP<ParameterList> intList =
32  rcp(new ParameterList("Int List"));
34  rcp(new EnhancedNumberValidator<int>(0,10,4));
35  TEST_ASSERT(intVali->getMin() == 0);
36  TEST_ASSERT(intVali->getMax() == 10);
37  TEST_ASSERT(intVali->getStep() == 4);
38  TEST_ASSERT(intVali->hasMin());
39  TEST_ASSERT(intVali->hasMax());
42  TEST_ASSERT(!intVali2->hasMin());
43  TEST_ASSERT(!intVali2->hasMax());
44  TEST_ASSERT(intVali2->getMin() == std::numeric_limits<int>::min());
45  TEST_ASSERT(intVali2->getMax() == std::numeric_limits<int>::max());
46  TEST_ASSERT(intVali2->getStep() == EnhancedNumberTraits<int>::defaultStep());
47  intList->set("Int Parameter", 5, "int parameter", intVali);
48  TEST_NOTHROW(intList->validateParameters(*intList));
49  TEST_THROW(intList->set("Int Parameter", 11),
51  TEST_THROW(intList->set("Double Parameter", 5.0, "double parameter", intVali),
53 
54  // Test String Conversions with int
55  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
56  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
57  TEST_NOTHROW(validList->set("Int Parameter", 4, "int parameter",
58  intVali));
59 #ifdef HAVE_TEUCHOSCORE_CXX11
60  TEST_NOTHROW(userList->set("Int Parameter", "x4"));
61  TEST_THROW(userList->validateParameters(*validList),
63  TEST_NOTHROW(userList->set("Int Parameter", "4x"));
64  TEST_THROW(userList->validateParameters(*validList),
66  TEST_NOTHROW(userList->set("Int Parameter", "12")); // ok string bad range
67  TEST_THROW(userList->validateParameters(*validList),
69 #endif
70  userList = rcp(new ParameterList("User List"));
71  TEST_NOTHROW(userList->set("Int Parameter", 4));
72  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
73  TEST_NOTHROW(userList->set("Int Parameter", "8"));
74  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
75  int readInt = userList->getEntry("Int Parameter").getValue<int>(&readInt);
76  TEST_ASSERT(readInt == 8);
77 
78  // check string can generate out of range
79  userList = rcp(new ParameterList("User List"));
80  TEST_NOTHROW(userList->set("Int Parameter", "20"));
81  TEST_THROW(userList->validateParameters(*validList),
83 
84  /*
85  * Testing Short Validator.
86  */
87  RCP<ParameterList> shortList =
88  rcp(new ParameterList("Short List"));
91  TEST_ASSERT(shortVali->getMin() == 0);
92  TEST_ASSERT(shortVali->getMax() == 10);
93  TEST_ASSERT(shortVali->getStep() == 4);
94  TEST_ASSERT(shortVali->hasMin());
95  TEST_ASSERT(shortVali->hasMax());
98  TEST_ASSERT(!shortVali2->hasMin());
99  TEST_ASSERT(!shortVali2->hasMax());
100  TEST_ASSERT(shortVali2->getMin() == std::numeric_limits<short>::min());
101  TEST_ASSERT(shortVali2->getMax() == std::numeric_limits<short>::max());
102  TEST_ASSERT(shortVali2->getStep() == EnhancedNumberTraits<short>::defaultStep());
103  shortList->set("Short Parameter", (short)5, "short parameter", shortVali);
104  TEST_NOTHROW(shortList->validateParameters(*shortList));
105  TEST_THROW(shortList->set("Short Parameter", (short)11),
107  TEST_THROW(shortList->set("Double Parameter", 5.0, "double parameter", shortVali),
109 
110  // Test String Conversions with short
111  validList = rcp(new ParameterList("Valid List"));
112  userList = rcp(new ParameterList("User List"));
113  TEST_NOTHROW(validList->set("Short Parameter", (short)4, "short parameter",
114  shortVali));
115 #ifdef HAVE_TEUCHOSCORE_CXX11
116  TEST_NOTHROW(userList->set("Short Parameter", "x4"));
117  TEST_THROW(userList->validateParameters(*validList),
119  TEST_NOTHROW(userList->set("Short Parameter", "4x"));
120  TEST_THROW(userList->validateParameters(*validList),
122  TEST_NOTHROW(userList->set("Short Parameter", "12")); // ok string bad range
123  TEST_THROW(userList->validateParameters(*validList),
125 #endif
126  userList = rcp(new ParameterList("User List"));
127  TEST_NOTHROW(userList->set("Short Parameter", (short)4));
128  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
129  TEST_NOTHROW(userList->set("Short Parameter", "8"));
130  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
131  short readShort = userList->getEntry("Short Parameter").getValue<short>(&readShort);
132  TEST_ASSERT(readShort == 8);
133 
134  // check string can generate out of range
135  userList = rcp(new ParameterList("User List"));
136  TEST_NOTHROW(userList->set("Short Parameter", "20"));
137  TEST_THROW(userList->validateParameters(*validList),
139 
140  /*
141  * Testing Float Validator.
142  */
143  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
145  rcp(new EnhancedNumberValidator<float>(0,10.0,4.0,6));
146  TEST_ASSERT(floatVali->getMin() == 0.0);
147  TEST_ASSERT(floatVali->getMax() == 10.0);
148  TEST_ASSERT(floatVali->getStep() == 4.0);
149  TEST_ASSERT(floatVali->getPrecision() == 6);
150  TEST_ASSERT(floatVali->hasMin());
151  TEST_ASSERT(floatVali->hasMax());
154  TEST_ASSERT(!floatVali2->hasMin());
155  TEST_ASSERT(!floatVali2->hasMax());
156  TEST_ASSERT(floatVali2->getMin() == EnhancedNumberTraits<float>::min());
157  TEST_ASSERT(floatVali2->getMax() == EnhancedNumberTraits<float>::max());
158  TEST_ASSERT(floatVali2->getStep() == EnhancedNumberTraits<float>::defaultStep());
159  TEST_ASSERT(floatVali2->getPrecision() == EnhancedNumberTraits<float>::defaultPrecision());
160  floatList->set("Float Parameter", (float)5.0, "float parameter", floatVali);
161  TEST_NOTHROW(floatList->validateParameters(*floatList));
162  TEST_THROW(floatList->set("Float Parameter", (float)11.0),
164  TEST_THROW(floatList->set("Int Parameter", 5, "int parameter", floatVali),
166 
167  // Test String Conversions with float
168  validList = rcp(new ParameterList("Valid List"));
169  userList = rcp(new ParameterList("User List"));
170  TEST_NOTHROW(validList->set("Float Parameter", (float)4.0, "float parameter",
171  floatVali));
172 #ifdef HAVE_TEUCHOSCORE_CXX11
173  TEST_NOTHROW(userList->set("Float Parameter", "x4.0"));
174  TEST_THROW(userList->validateParameters(*validList),
176  TEST_NOTHROW(userList->set("Float Parameter", "4.0x"));
177  TEST_THROW(userList->validateParameters(*validList),
179  TEST_NOTHROW(userList->set("Float Parameter", "12.0")); // ok string bad range
180  TEST_THROW(userList->validateParameters(*validList),
182 #endif
183  userList = rcp(new ParameterList("User List"));
184  TEST_NOTHROW(userList->set("Float Parameter", (float)8.0));
185  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
186  TEST_NOTHROW(userList->set("Float Parameter", "8.0"));
187  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
188  float readFloat = userList->getEntry("Float Parameter").getValue<float>(&readFloat);
189  TEST_ASSERT(readFloat == 8.0);
190 
191  // check string can generate out of range
192  userList = rcp(new ParameterList("User List"));
193  TEST_NOTHROW(userList->set("Float Parameter", "20.0"));
194  TEST_THROW(userList->validateParameters(*validList),
196 
197  /*
198  * Testing Double Validator.
199  */
200  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
202  rcp(new EnhancedNumberValidator<double>(0,10.0,4.0,6));
203  TEST_ASSERT(doubleVali->getMin() == 0.0);
204  TEST_ASSERT(doubleVali->getMax() == 10.0);
205  TEST_ASSERT(doubleVali->getStep() == 4.0);
206  TEST_ASSERT(doubleVali->getPrecision() == 6);
207  TEST_ASSERT(doubleVali->hasMin());
208  TEST_ASSERT(doubleVali->hasMax());
211  TEST_ASSERT(!doubleVali2->hasMin());
212  TEST_ASSERT(!doubleVali2->hasMax());
213  TEST_ASSERT(doubleVali2->getMin() == EnhancedNumberTraits<double>::min());
214  TEST_ASSERT(doubleVali2->getMax() == EnhancedNumberTraits<double>::max());
215  TEST_ASSERT(doubleVali2->getStep() == EnhancedNumberTraits<double>::defaultStep());
216  TEST_ASSERT(doubleVali2->getPrecision() == EnhancedNumberTraits<double>::defaultPrecision());
217  doubleList->set("Double Parameter", (double)5.0, "double parameter", doubleVali);
218  TEST_NOTHROW(doubleList->validateParameters(*doubleList));
219  TEST_THROW(doubleList->set("Double Parameter", (double)11.0),
221  TEST_THROW(doubleList->set("Int Parameter", 5, "int parameter", doubleVali),
223 
224  // Test String Conversions with double
225  validList = rcp(new ParameterList("Valid List"));
226  userList = rcp(new ParameterList("User List"));
227  TEST_NOTHROW(validList->set("Double Parameter", 4.0, "double parameter",
228  doubleVali));
229 #ifdef HAVE_TEUCHOSCORE_CXX11
230  TEST_NOTHROW(userList->set("Double Parameter", "x4.0"));
231  TEST_THROW(userList->validateParameters(*validList),
233  TEST_NOTHROW(userList->set("Double Parameter", "4.0x"));
234  TEST_THROW(userList->validateParameters(*validList),
236  TEST_NOTHROW(userList->set("Double Parameter", "12.0"));
237  TEST_THROW(userList->validateParameters(*validList), // bad range
239 #endif
240  userList = rcp(new ParameterList("Valid List"));
241  TEST_NOTHROW(userList->set("Double Parameter", 8.0));
242  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
243  TEST_NOTHROW(userList->set("Double Parameter", "8.0"));
244  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
245  double readDouble = userList->getEntry("Double Parameter").getValue<double>(&readDouble);
246  TEST_ASSERT(readDouble == 8.0);
247 
248  // check string can generate out of range
249  userList = rcp(new ParameterList("User List"));
250  TEST_NOTHROW(userList->set("Double Parameter", "20.0"));
251  TEST_THROW(userList->validateParameters(*validList),
253  }
254 
255 TEUCHOS_UNIT_TEST(Teuchos_Validators, anyNumberValidator)
256 {
257  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
258  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
259 
261  intDoubleTypes.allowString(false);
263  intStringTypes.allowDouble(false);
265  intTypes.allowDouble(false);
266  intTypes.allowString(false);
267 
268  // set up validators to test
269  // default prefers double and allows string and int
281 
282  // first check the 'good' setups which do not throw
283  TEST_NOTHROW(validList->set( "allParameter", "1.1", "documentation",
284  allValidator));
285  TEST_NOTHROW(validList->set( "allParameter", 1.1, "documentation",
286  allValidator));
287  TEST_NOTHROW(validList->set( "allParameter", "1", "documentation",
288  allValidator));
289  TEST_NOTHROW(validList->set( "allParameter", 1, "documentation",
290  allValidator));
291  TEST_NOTHROW(validList->set( "intDoubleParameter", 1.1, "documentation",
292  intDoubleValidator));
293  TEST_NOTHROW(validList->set( "intDoubleParameter", 1, "documentation",
294  intDoubleValidator));
295  TEST_NOTHROW(validList->set( "intStringParameter", "1", "documentation",
296  intStringValidator));
297  TEST_NOTHROW(validList->set( "intStringParameter", 1, "documentation",
298  intStringValidator));
299  TEST_NOTHROW(validList->set( "intParameter", 1, "documentation",
300  intValidator));
301 
302  // This was a special case that might warrant discussion.
303  // The issue is for validators which accept string/int but not double.
304  // In the original setup the validator would always call getDouble
305  // internally and accept a string of "1.1" without error.
306  TEST_NOTHROW(validList->set( "intStringParameter", "1.1", "documentation",
307  intStringValidator));
308 
309  //
310  // these are some cases which throw independent of HAVE_TEUCHOSCORE_CXX11
311  //
312 
313  // if string it not allowed you can't use a string ever
314  TEST_THROW(validList->set( "intDoubleParameter", "1.1", "documentation",
315  intDoubleValidator), Exceptions::InvalidParameterType);
316 
317  // it also throws for a double number - double not allowed
318  TEST_THROW(validList->set( "intStringParameter", 1.1, "documentation",
319  intStringValidator), Exceptions::InvalidArgument);
320 
321  // for int only it can't be a string - any string will throw
322  TEST_THROW(validList->set( "intParameter", "1", "documentation",
323  intValidator), Exceptions::InvalidParameter);
324 
325  // this int only it can't be a double because double is not allowed
326  TEST_THROW(validList->set( "intParameter", 1.1, "documentation",
327  intValidator), Exceptions::InvalidParameter);
328 
329  //
330  // these behaviors now depend on HAVE_TEUCHOSCORE_CXX11
331  // std::stod and std::stoi will be used for HAVE_TEUCHOSCORE_CXX11
332  // std::atof and std::atoi will be used for no CXX11
333  //
334 #ifdef HAVE_TEUCHOSCORE_CXX11
335  // for double types we throw for badly formatted string on std::stod
336  // this will check the double type first because it is PREFER_DOUBLE
337  TEST_THROW(validList->set( "allParameter", "1.1x", "documentation",
338  allValidator), Exceptions::InvalidArgument);
339  TEST_THROW(validList->set( "intDoubleParameter", "1.1x", "documentation",
340  allValidator), Exceptions::InvalidArgument);
341  TEST_THROW(validList->set( "allParameter", "x1.1", "documentation",
342  allValidator), Exceptions::InvalidArgument);
343  TEST_THROW(validList->set( "intDoubleParameter", "x1.1", "documentation",
344  allValidator), Exceptions::InvalidArgument);
345  // for int/string but no double - std::stoi throws for invalid formatting
346  TEST_THROW(validList->set( "intStringParameter", "1x", "documentation",
347  intStringValidator), Exceptions::InvalidArgument);
348  TEST_THROW(validList->set( "intStringParameter", "x1", "documentation",
349  intStringValidator), Exceptions::InvalidArgument);
350  TEST_THROW(validList->set( "intStringParameter", "1 x", "documentation",
351  intStringValidator), Exceptions::InvalidArgument);
352 #else
353  // for int/double/string std::atod does NOT throw - this is the old behavior
354  // this is different now when HAVE_TEUCHOSCORE_CXX11 is ON - see above
355  TEST_NOTHROW(validList->set( "allParameter", "1.1x", "documentation",
356  allValidator));
357  // for int/string std::atoi does NOT throw - this is the old behavior
358  // this is different now when HAVE_TEUCHOSCORE_CXX11 is ON - see above
359  TEST_NOTHROW(validList->set( "intStringParameter", "1.1x", "documentation",
360  intStringValidator));
361 #endif
362 }
363 
364 TEUCHOS_UNIT_TEST(Teuchos_Validators, boolValidator)
365 {
366  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
367  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
368 
369  // first without validator - accepts only true/false
370  validList->set( "justBool", false, "documentation" );
371  TEST_NOTHROW(userList->set( "justBool", false));
372  TEST_NOTHROW(userList->validateParameters(*validList));
373  TEST_NOTHROW(userList->set( "justBool", true));
374  TEST_NOTHROW(userList->validateParameters(*validList));
375  // this will not validate because we did not add a bool validator
376  TEST_NOTHROW(userList->set( "justBool", "true"));
377  TEST_THROW(userList->validateParameters(*validList),
379  // this will not validate because we did not add a bool validator
380  TEST_NOTHROW(userList->set( "justBool", "false"));
381  TEST_THROW(userList->validateParameters(*validList),
383 
384  // now with BoolParameterEntryValidator validator
385  // accepts true/false/"true"/"false"
388  userList = rcp(new ParameterList("User List")); // make a new list
389  validList = rcp(new ParameterList("Valid List")); // make a new list
390  validList->set( "boolOrString", false, "documentation", boolValidator );
391  TEST_NOTHROW(userList->set( "boolOrString", false));
392  TEST_NOTHROW(userList->validateParameters(*validList));
393  TEST_NOTHROW(userList->set( "boolOrString", true));
394  TEST_NOTHROW(userList->validateParameters(*validList));
395  // this will validate because we added a bool validator
396  TEST_NOTHROW(userList->set( "boolOrString", "true"));
397  TEST_NOTHROW(userList->validateParameters(*validList));
398  // this will validate because we added a bool validator
399  TEST_NOTHROW(userList->set( "boolOrString", "false"));
400  TEST_NOTHROW(userList->validateParameters(*validList));
401  // but only "false" and "true" work - anything else will not validate
402  TEST_NOTHROW(userList->set( "boolOrString", "falsex")); // sets ok
403  TEST_THROW(userList->validateParameters(*validList), // but throws
405 
406  // now with BoolParameterEntryValidator validator
407  // but consider what happens if we created it using "false" instead of false
408  // this should still work identically to the previous case
409  userList = rcp(new ParameterList("User List")); // make a new list
410  validList = rcp(new ParameterList("Valid List")); // make a new list
411  validList->set( "boolOrString", "false", "documentation", boolValidator );
412  TEST_NOTHROW(userList->set( "boolOrString", false));
413  TEST_NOTHROW(userList->validateParameters(*validList));
414  TEST_NOTHROW(userList->set( "boolOrString", true ));
415  TEST_NOTHROW(userList->validateParameters(*validList));
416  // this will validate because we added a bool validator
417  TEST_NOTHROW(userList->set( "boolOrString", "true"));
418  TEST_NOTHROW(userList->validateParameters(*validList));
419  // this will validate because we added a bool validator
420  TEST_NOTHROW(userList->set( "boolOrString", "false"));
421  TEST_NOTHROW(userList->validateParameters(*validList));
422  // but only "false" and "true" work - anything else will not validate
423  TEST_NOTHROW(userList->set( "boolOrString", "falsex")); // sets ok
424  TEST_THROW(userList->validateParameters(*validList), // but throws
426 
427  // do another test using validateParametersAndSetDefaults
428  userList = rcp(new ParameterList("User List")); // make a new list
429  validList = rcp(new ParameterList("Valid List")); // make a new list
430  // Default values for parameters are bool
431  validList->set("boolOne", true, "doc", boolValidator);
432  validList->set("boolTwo", false, "doc", boolValidator);
433  bool defOne = validList->getEntry("boolOne").getValue(&defOne);
434  bool defTwo = validList->getEntry("boolTwo").getValue(&defTwo);
435 
436  // Create user parameter list
437  userList->set("boolOne", false); // User can provide bool value...
438  userList->set("boolTwo", "true"); // or string "true"/"false"
439  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
440 }
441 
442 
443 /*
444  * Testing StringValidator.
445  */
446 TEUCHOS_UNIT_TEST(Teuchos_Validators, stringValidator)
447 {
448  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
449  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
450  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
451  RCP<const Array<std::string> > valiVals = stringVali->validStringValues();
452  /*bool local_success = true;
453  for(int i =0; i<valiVals.size() ++i){
454  TEST_ARRAY_ELE_EQUALITY(*valiVals, i, stringVals[i]);
455  }
456  if (local_success) out << "passed\n";
457  else success = false;*/
458  TEST_COMPARE_ARRAYS(*valiVals, stringVals);
459  TEST_NOTHROW(stringList->set("String param1", "str1", "a string parameter", stringVali));
460  TEST_THROW(stringList->set("String param2", "not in list", "a string parameter", stringVali),
462  TEST_THROW(stringList->set("int param", 5, "a int parameter", stringVali),
464 }
465 
466 
467 /*
468  * Testing StringToIntegralParameterEntryValidator.
469  */
471  Array<std::string> strVals = tuple<std::string>("str1", "str2", "str3");
472  Array<std::string> strDocs = tuple<std::string>("a str1", "a str2", "a str3");
473  Array<int> intVals = tuple<int>(1, 2, 3);
474  bool caseSensitive = true;
476  // Note that validator1 maps the strings to {0, 1, 2} not {1, 2, 3} as in `intVals`
477  RCP<ret_type> validator1 = rcp(new ret_type(strVals, "str1", caseSensitive));
478  RCP<ret_type> validator2 = rcp(new ret_type(strVals, intVals, "str1", caseSensitive));
479  RCP<ret_type> validator3 = rcp(new ret_type(strVals, strDocs, intVals, "str1", caseSensitive));
480  TEST_EQUALITY(strDocs, *validator3->getStringDocs());
481  ParameterList valid_pl = ParameterList();
482  valid_pl.set("Param1", "str1", "Parameter 1", validator1);
483  valid_pl.set("Param2", "str1", "Parameter 2", validator2);
484  valid_pl.set("Param3", "str1", "Parameter 3", validator3);
485  ParameterList user_pl = ParameterList();
486  user_pl.set("Param1", "str1");
487  user_pl.set("Param2", "str2");
488  user_pl.set("Param3", "str3");
489  // Test `getStringValue` and `getIntegralValue` before validation on `valid_pl`
490  TEST_EQUALITY(0, getIntegralValue<int>(valid_pl, "Param1"));
491  TEST_EQUALITY(intVals[0], getIntegralValue<int>(valid_pl, "Param2"));
492  TEST_EQUALITY(strVals[0], getStringValue<int>(valid_pl, "Param2"));
493  // Test `getStringValue` and `getIntegralValue` after validation on `user_pl`
494  user_pl.validateParametersAndSetDefaults(valid_pl);
495  TEST_EQUALITY(0, getIntegralValue<int>(user_pl, "Param1"));
496  TEST_EQUALITY(intVals[1], getIntegralValue<int>(user_pl, "Param2"));
497  TEST_EQUALITY(intVals[2], getIntegralValue<int>(user_pl, "Param3"));
498  TEST_EQUALITY(strVals[0], getStringValue<int>(user_pl, "Param1"));
499  TEST_EQUALITY(strVals[1], getStringValue<int>(user_pl, "Param2"));
500  TEST_EQUALITY(strVals[2], getStringValue<int>(user_pl, "Param3"));
501 }
502 
503 
504 /*
505  * Testing FileNameValidator.
506  */
507 TEUCHOS_UNIT_TEST(Teuchos_Validators, fileNameValidator)
508 {
509  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
510  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
511  TEST_ASSERT(fileNameVali->fileMustExist());
512  fileNameVali->setFileMustExist(false);
513  TEST_ASSERT(!fileNameVali->fileMustExist());
514  TEST_NOTHROW(fileNameList->set("File name param", "../path", "file name parameter",
515  fileNameVali));
516  TEST_THROW(fileNameList->set("int param", 5, "int parameter", fileNameVali),
518  fileNameVali->setFileMustExist(true);
519  TEST_NOTHROW(fileNameList->set("file name param", "testFile.txt", "a file name", fileNameVali));
520  TEST_THROW(fileNameList->set("file name param", "doesntexist.txt", "a file name", fileNameVali),
522 }
523 
524 
525 /*
526  * Testing Array Validators.
527  */
528 TEUCHOS_UNIT_TEST(Teuchos_Validators, arrayValidators)
529 {
530 
531  /*
532  * Testing StringArrayValidator.
533  */
534  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
535  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
536  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
537  RCP<ArrayStringValidator> stringArrayVali = rcp(new ArrayStringValidator(stringVali));
538  TEST_ASSERT(stringVali.get() == stringArrayVali->getPrototype().get());
539  Array<std::string> stringArray = tuple<std::string>("str2","str3","str1","str3","str2");
540  TEST_NOTHROW(stringList->set("String Array Param", stringArray, "string array parameter", stringArrayVali));
541  Array<std::string> badStringArray = tuple<std::string>("not valid","str3","str1","str3","str2");
542  TEST_THROW(stringList->set("String Array Param", badStringArray, "string array parameter", stringArrayVali),
544  TEST_THROW(stringList->set("Int param", 5, "int parameter", stringArrayVali),
546  Array<long> longArray = tuple<long>((long)5,(long)5,(long)3);
547  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", stringArrayVali),
549 
550  /*
551  * Testing Int ArrayValidator.
552  */
553  RCP<ParameterList> intList = rcp(new ParameterList("Int List"));
555  RCP<ArrayNumberValidator<int> > intArrayVali = rcp(new ArrayNumberValidator<int>(intVali));
556  TEST_ASSERT(intVali.get() == intArrayVali->getPrototype().get());
557  Array<int> intArray = tuple<int>(1,4,2,5);
558  TEST_NOTHROW(intList->set("int array param", intArray, "int array parameter", intArrayVali));
559  Array<int> intBadArray = tuple<int>(11,4,2,5);
560  TEST_THROW(intList->set("int bad array param", intBadArray, "int bad array parameter", intArrayVali),
562  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", intArrayVali),
564 
565  /*
566  * Testing Short ArrayValidator.
567  */
568  RCP<ParameterList> shortList = rcp(new ParameterList("Short List"));
571  RCP<ArrayNumberValidator<short> > shortArrayVali =
572  rcp(new ArrayNumberValidator<short>(shortVali));
573  TEST_ASSERT(shortVali.get() == shortArrayVali->getPrototype().get());
574  Array<short> shortArray = tuple<short>(1,4,2,5);
575  TEST_NOTHROW(shortList->set("short array param", shortArray, "short array parameter", shortArrayVali));
576  Array<short> shortBadArray = tuple<short>(11,4,2,5);
577  TEST_THROW(shortList->set("short bad array param", shortBadArray, "short bad array parameter", shortArrayVali),
579  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", shortArrayVali),
581 
582  /*
583  * Testing Float ArrayValidator.
584  */
585  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
587  rcp(new EnhancedNumberValidator<float>(0.0, 10.0));
588  RCP<ArrayNumberValidator<float> > floatArrayVali =
589  rcp(new ArrayNumberValidator<float>(floatVali));
590  TEST_ASSERT(floatVali.get() == floatArrayVali->getPrototype().get());
591  Array<float> floatArray = tuple<float>(1.0,4.0,2.0,5.0);
592  TEST_NOTHROW(floatList->set("float array param", floatArray, "float array parameter", floatArrayVali));
593  Array<float> floatBadArray = tuple<float>(11.0,4.0,2.0,5.0);
594  TEST_THROW(floatList->set("float bad array param", floatBadArray, "float bad array parameter", floatArrayVali),
596  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", floatArrayVali),
598 
599  /*
600  * Testing Double ArrayValidator.
601  */
602  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
604  rcp(new EnhancedNumberValidator<double>(0.0, 10.0));
605  RCP<ArrayNumberValidator<double> > doubleArrayVali =
606  rcp(new ArrayNumberValidator<double>(doubleVali));
607  TEST_ASSERT(doubleVali.get() == doubleArrayVali->getPrototype().get());
608  Array<double> doubleArray = tuple<double>(1.0,4.0,2.0,5.0);
609  TEST_NOTHROW(doubleList->set("double array param", doubleArray, "double array parameter", doubleArrayVali));
610  Array<double> doubleBadArray = tuple<double>(11.0,4.0,2.0,5.0);
611  TEST_THROW(doubleList->set("double bad array param", doubleBadArray, "double bad array parameter", doubleArrayVali),
613  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", doubleArrayVali),
615 
616  /*
617  * Testing FileName ArrayValidator.
618  */
619  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
620  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
621  RCP<ArrayFileNameValidator> arrayFileNameVali = rcp(new ArrayFileNameValidator(fileNameVali));
622  TEST_ASSERT(arrayFileNameVali->getPrototype().get() == fileNameVali.get());
623  Array<std::string> fileNameArray = tuple<std::string>("testFile.txt", "testFile2.txt", "testFile3.txt");
624  Array<std::string> fileNameBadArray = tuple<std::string>("doesnexist.txt", "testFile2.txt", "testFile3.txt");
625  TEST_NOTHROW(fileNameList->set("File name array", fileNameArray, "file name array parameter", arrayFileNameVali));
626  TEST_THROW(fileNameList->set("Bad File name array", fileNameBadArray, "bad file name array parameter", arrayFileNameVali),
628  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", arrayFileNameVali),
630 }
631 
632 /*
633  * Testing TwoDArray Validators.
634  */
635 TEUCHOS_UNIT_TEST(Teuchos_Validators, twoDArrayValidators)
636 {
637 
638  /*
639  * Testing StringArrayValidator.
640  */
641  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
642  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
643  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
644  RCP<TwoDArrayStringValidator> stringArrayVali =
645  rcp(new TwoDArrayStringValidator(stringVali));
646  TEST_ASSERT(stringVali.get() == stringArrayVali->getPrototype().get());
647  TwoDArray<std::string> stringArray(2,2);
648  stringArray(0,0) = "str2";
649  stringArray(0,1) = "str1";
650  stringArray(1,0) = "str3";
651  stringArray(1,1) = "str2";
652  TEST_NOTHROW(stringList->set("String Array Param", stringArray, "string array parameter", stringArrayVali));
653  TwoDArray<std::string> badStringArray(2,2);
654  badStringArray(0,0) = "str2";
655  badStringArray(0,1) = "str1";
656  badStringArray(1,0) = "str3";
657  badStringArray(1,1) = "not valid";
658  TEST_THROW(stringList->set("String Array Param", badStringArray, "string array parameter", stringArrayVali),
660  TEST_THROW(stringList->set("Int param", 5, "int parameter", stringArrayVali),
662  TwoDArray<long> longArray(2,2);
663  longArray(0,0) = (long)5;
664  longArray(0,1) = (long)4;
665  longArray(1,0) = (long)9;
666  longArray(1,1) = (long)1;
667  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", stringArrayVali),
669 
670  /*
671  * Testing Int ArrayValidator.
672  */
673  RCP<ParameterList> intList = rcp(new ParameterList("Int List"));
675  RCP<TwoDArrayNumberValidator<int> > intArrayVali =
676  rcp(new TwoDArrayNumberValidator<int>(intVali));
677  TEST_ASSERT(intVali.get() == intArrayVali->getPrototype().get());
678  TwoDArray<int> intArray(2,2);
679  intArray(0,0) = 1;
680  intArray(0,1) = 4;
681  intArray(1,0) = 2;
682  intArray(1,1) = 5;
683  TEST_NOTHROW(intList->set("int array param", intArray, "int array parameter", intArrayVali));
684  TwoDArray<int> intBadArray(2,2);
685  intBadArray(0,0) = 11;
686  intBadArray(0,1) = 4;
687  intBadArray(1,0) = 2;
688  intBadArray(1,1) = 5;
689  TEST_THROW(intList->set("int bad array param", intBadArray, "int bad array parameter", intArrayVali),
691  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", intArrayVali),
693 
694  /*
695  * Testing Short ArrayValidator.
696  */
697  RCP<ParameterList> shortList = rcp(new ParameterList("Short List"));
700  RCP<TwoDArrayNumberValidator<short> > shortArrayVali =
701  rcp(new TwoDArrayNumberValidator<short>(shortVali));
702  TEST_ASSERT(shortVali.get() == shortArrayVali->getPrototype().get());
703  TwoDArray<short> shortArray(2,2);
704  shortArray(0,0) = 1;
705  shortArray(0,1) = 4;
706  shortArray(1,0) = 2;
707  shortArray(1,1) = 5;
708  TEST_NOTHROW(shortList->set("short array param", shortArray, "short array parameter", shortArrayVali));
709  TwoDArray<short> shortBadArray(2,2);
710  shortBadArray(0,0) = 11;
711  shortBadArray(0,1) = 4;
712  shortBadArray(1,0) = 2;
713  shortBadArray(1,1) = 5;
714  TEST_THROW(shortList->set("short bad array param", shortBadArray, "short bad array parameter", shortArrayVali),
716  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", shortArrayVali),
718 
719  /*
720  * Testing Float ArrayValidator.
721  */
722  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
724  rcp(new EnhancedNumberValidator<float>(0.0, 10.0));
725  RCP<TwoDArrayNumberValidator<float> > floatArrayVali =
726  rcp(new TwoDArrayNumberValidator<float>(floatVali));
727  TEST_ASSERT(floatVali.get() == floatArrayVali->getPrototype().get());
728  TwoDArray<float> floatArray(2,2);
729  floatArray(0,0) = 1.0;
730  floatArray(0,1) = 4.0;
731  floatArray(1,0) = 5.0;
732  floatArray(1,1) = 2.0;
733  TEST_NOTHROW(floatList->set("float array param", floatArray, "float array parameter", floatArrayVali));
734  TwoDArray<float> floatBadArray(2,2);
735  floatBadArray(0,0) = 11.0;
736  floatBadArray(0,1) = 4.0;
737  floatBadArray(1,0) = 5.0;
738  floatBadArray(1,1) = 2.0;
739  TEST_THROW(floatList->set("float bad array param", floatBadArray, "float bad array parameter", floatArrayVali),
741  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", floatArrayVali),
743 
744  /*
745  * Testing Double ArrayValidator.
746  */
747  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
749  rcp(new EnhancedNumberValidator<double>(0.0, 10.0));
750  RCP<TwoDArrayNumberValidator<double> > doubleArrayVali =
751  rcp(new TwoDArrayNumberValidator<double>(doubleVali));
752  TEST_ASSERT(doubleVali.get() == doubleArrayVali->getPrototype().get());
753  TwoDArray<double> doubleArray(2,2);
754  doubleArray(0,0) = 1.0;
755  doubleArray(0,1) = 4.0;
756  doubleArray(1,0) = 5.0;
757  doubleArray(1,1) = 2.0;
758  TEST_NOTHROW(doubleList->set("double array param", doubleArray, "double array parameter", doubleArrayVali));
759  TwoDArray<double> doubleBadArray(2,2);
760  doubleBadArray(0,0) = 11.0;
761  doubleBadArray(0,1) = 4.0;
762  doubleBadArray(1,0) = 5.0;
763  doubleBadArray(1,1) = 2.0;
764  TEST_THROW(doubleList->set("double bad array param", doubleBadArray, "double bad array parameter", doubleArrayVali),
766  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", doubleArrayVali),
768 
769  /*
770  * Testing FileName ArrayValidator.
771  */
772  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
773  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
774  RCP<TwoDArrayFileNameValidator> arrayFileNameVali =
775  rcp(new TwoDArrayFileNameValidator(fileNameVali));
776  TEST_ASSERT(arrayFileNameVali->getPrototype().get() == fileNameVali.get());
777  TwoDArray<std::string> fileNameArray(2,2);
778  fileNameArray(0,0) = "testFile.txt";
779  fileNameArray(0,1) = "testFile2.txt";
780  fileNameArray(1,0) = "testFile3.txt";
781  fileNameArray(1,1) = "testFile.txt";
782  TwoDArray<std::string> fileNameBadArray(2,2);
783  fileNameBadArray(0,0) = "doesntexist.txt";
784  fileNameBadArray(0,1) = "testFile2.txt";
785  fileNameBadArray(1,0) = "testFile3.txt";
786  fileNameBadArray(1,1) = "testFile.txt";
787  TEST_NOTHROW(fileNameList->set("File name array", fileNameArray, "file name array parameter", arrayFileNameVali));
788  TEST_THROW(fileNameList->set("Bad File name array", fileNameBadArray, "bad file name array parameter", arrayFileNameVali),
790  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", arrayFileNameVali),
792 }
793 
794 
795 } // namespace Teuchos
796 
#define TEST_ASSERT(v1)
Assert the given statement is true.
Convience class for StringValidators that are to be applied to TwoDArrays.
bool setFileMustExist(bool shouldFileExist)
Sets whether or not the validator requires the file to already exist.
Convience class for EnhancedNumberValidators that are to be applied to arrays.
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
Standard implementation of a ParameterEntryValidator that maps from a list of strings to an enum or i...
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
T * get() const
Get the raw C++ pointer to the underlying object.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
T & getValue(T *ptr) const
Templated get method that uses the input pointer type to determine the type of parameter to return...
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
RCP< const ValidatorType > getPrototype() const
Returns the prototype validator for this Array Validator.
Convience class for StringValidators that are to be applied to arrays.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
static unsigned short defaultPrecision()
Gets the default precision with which the number type should be displayed.
Convience class for EnhancedNumberValidators that are to be applied to TwoDArray. ...
Unit testing support.
Standard implementation of a BoolParameterEntryValidator that accepts bool values (true/false) or str...
Convience class for FileNameValidators that are to be applied to TwoDArrays.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
A list of parameters of arbitrary type.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
static T min()
Gets the minimum possible value the number type can take on.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
static T defaultStep()
gets default amount a value of the number type should be incremented by when being utilizied in a UI...
static T max()
Gets the maximum possible value the number type can take on.
Class uesd to validate a particular type of number.
Convience class for FileNameValidators that are to be applied to arrays.
AcceptedTypes & allowDouble(bool _allowDouble)
Set allow a double value or not.
Smart reference counting pointer class for automatic garbage collection.
bool fileMustExist() const
Gets the variable describing whether or not this validator wants the file that is specified to alread...
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.