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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
45 
46 
47 // 2010/07/30: rabartl: Here I just added all the unit tests to the Teuchos
48 // namespace to remove some clutter.
49 
50 
51 namespace Teuchos {
52 
53 
57 TEUCHOS_UNIT_TEST(Teuchos_Validators, numberValidators)
58 {
59  /*
60  * Testing Int Validator.
61  */
62  RCP<ParameterList> intList =
63  rcp(new ParameterList("Int List"));
65  rcp(new EnhancedNumberValidator<int>(0,10,4));
66  TEST_ASSERT(intVali->getMin() == 0);
67  TEST_ASSERT(intVali->getMax() == 10);
68  TEST_ASSERT(intVali->getStep() == 4);
69  TEST_ASSERT(intVali->hasMin());
70  TEST_ASSERT(intVali->hasMax());
73  TEST_ASSERT(!intVali2->hasMin());
74  TEST_ASSERT(!intVali2->hasMax());
75  TEST_ASSERT(intVali2->getMin() == std::numeric_limits<int>::min());
76  TEST_ASSERT(intVali2->getMax() == std::numeric_limits<int>::max());
77  TEST_ASSERT(intVali2->getStep() == EnhancedNumberTraits<int>::defaultStep());
78  intList->set("Int Parameter", 5, "int parameter", intVali);
79  TEST_NOTHROW(intList->validateParameters(*intList));
80  TEST_THROW(intList->set("Int Parameter", 11),
82  TEST_THROW(intList->set("Double Parameter", 5.0, "double parameter", intVali),
84 
85  // Test String Conversions with int
86  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
87  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
88  TEST_NOTHROW(validList->set("Int Parameter", 4, "int parameter",
89  intVali));
90 #ifdef HAVE_TEUCHOSCORE_CXX11
91  TEST_NOTHROW(userList->set("Int Parameter", "x4"));
92  TEST_THROW(userList->validateParameters(*validList),
94  TEST_NOTHROW(userList->set("Int Parameter", "4x"));
95  TEST_THROW(userList->validateParameters(*validList),
97  TEST_NOTHROW(userList->set("Int Parameter", "12")); // ok string bad range
98  TEST_THROW(userList->validateParameters(*validList),
100 #endif
101  userList = rcp(new ParameterList("User List"));
102  TEST_NOTHROW(userList->set("Int Parameter", 4));
103  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
104  TEST_NOTHROW(userList->set("Int Parameter", "8"));
105  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
106  int readInt = userList->getEntry("Int Parameter").getValue<int>(&readInt);
107  TEST_ASSERT(readInt == 8);
108 
109  // check string can generate out of range
110  userList = rcp(new ParameterList("User List"));
111  TEST_NOTHROW(userList->set("Int Parameter", "20"));
112  TEST_THROW(userList->validateParameters(*validList),
114 
115  /*
116  * Testing Short Validator.
117  */
118  RCP<ParameterList> shortList =
119  rcp(new ParameterList("Short List"));
121  rcp(new EnhancedNumberValidator<short>(0,10,4));
122  TEST_ASSERT(shortVali->getMin() == 0);
123  TEST_ASSERT(shortVali->getMax() == 10);
124  TEST_ASSERT(shortVali->getStep() == 4);
125  TEST_ASSERT(shortVali->hasMin());
126  TEST_ASSERT(shortVali->hasMax());
129  TEST_ASSERT(!shortVali2->hasMin());
130  TEST_ASSERT(!shortVali2->hasMax());
131  TEST_ASSERT(shortVali2->getMin() == std::numeric_limits<short>::min());
132  TEST_ASSERT(shortVali2->getMax() == std::numeric_limits<short>::max());
133  TEST_ASSERT(shortVali2->getStep() == EnhancedNumberTraits<short>::defaultStep());
134  shortList->set("Short Parameter", (short)5, "short parameter", shortVali);
135  TEST_NOTHROW(shortList->validateParameters(*shortList));
136  TEST_THROW(shortList->set("Short Parameter", (short)11),
138  TEST_THROW(shortList->set("Double Parameter", 5.0, "double parameter", shortVali),
140 
141  // Test String Conversions with short
142  validList = rcp(new ParameterList("Valid List"));
143  userList = rcp(new ParameterList("User List"));
144  TEST_NOTHROW(validList->set("Short Parameter", (short)4, "short parameter",
145  shortVali));
146 #ifdef HAVE_TEUCHOSCORE_CXX11
147  TEST_NOTHROW(userList->set("Short Parameter", "x4"));
148  TEST_THROW(userList->validateParameters(*validList),
150  TEST_NOTHROW(userList->set("Short Parameter", "4x"));
151  TEST_THROW(userList->validateParameters(*validList),
153  TEST_NOTHROW(userList->set("Short Parameter", "12")); // ok string bad range
154  TEST_THROW(userList->validateParameters(*validList),
156 #endif
157  userList = rcp(new ParameterList("User List"));
158  TEST_NOTHROW(userList->set("Short Parameter", (short)4));
159  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
160  TEST_NOTHROW(userList->set("Short Parameter", "8"));
161  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
162  short readShort = userList->getEntry("Short Parameter").getValue<short>(&readShort);
163  TEST_ASSERT(readShort == 8);
164 
165  // check string can generate out of range
166  userList = rcp(new ParameterList("User List"));
167  TEST_NOTHROW(userList->set("Short Parameter", "20"));
168  TEST_THROW(userList->validateParameters(*validList),
170 
171  /*
172  * Testing Float Validator.
173  */
174  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
176  rcp(new EnhancedNumberValidator<float>(0,10.0,4.0,6));
177  TEST_ASSERT(floatVali->getMin() == 0.0);
178  TEST_ASSERT(floatVali->getMax() == 10.0);
179  TEST_ASSERT(floatVali->getStep() == 4.0);
180  TEST_ASSERT(floatVali->getPrecision() == 6);
181  TEST_ASSERT(floatVali->hasMin());
182  TEST_ASSERT(floatVali->hasMax());
185  TEST_ASSERT(!floatVali2->hasMin());
186  TEST_ASSERT(!floatVali2->hasMax());
187  TEST_ASSERT(floatVali2->getMin() == EnhancedNumberTraits<float>::min());
188  TEST_ASSERT(floatVali2->getMax() == EnhancedNumberTraits<float>::max());
189  TEST_ASSERT(floatVali2->getStep() == EnhancedNumberTraits<float>::defaultStep());
190  TEST_ASSERT(floatVali2->getPrecision() == EnhancedNumberTraits<float>::defaultPrecision());
191  floatList->set("Float Parameter", (float)5.0, "float parameter", floatVali);
192  TEST_NOTHROW(floatList->validateParameters(*floatList));
193  TEST_THROW(floatList->set("Float Parameter", (float)11.0),
195  TEST_THROW(floatList->set("Int Parameter", 5, "int parameter", floatVali),
197 
198  // Test String Conversions with float
199  validList = rcp(new ParameterList("Valid List"));
200  userList = rcp(new ParameterList("User List"));
201  TEST_NOTHROW(validList->set("Float Parameter", (float)4.0, "float parameter",
202  floatVali));
203 #ifdef HAVE_TEUCHOSCORE_CXX11
204  TEST_NOTHROW(userList->set("Float Parameter", "x4.0"));
205  TEST_THROW(userList->validateParameters(*validList),
207  TEST_NOTHROW(userList->set("Float Parameter", "4.0x"));
208  TEST_THROW(userList->validateParameters(*validList),
210  TEST_NOTHROW(userList->set("Float Parameter", "12.0")); // ok string bad range
211  TEST_THROW(userList->validateParameters(*validList),
213 #endif
214  userList = rcp(new ParameterList("User List"));
215  TEST_NOTHROW(userList->set("Float Parameter", (float)8.0));
216  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
217  TEST_NOTHROW(userList->set("Float Parameter", "8.0"));
218  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
219  float readFloat = userList->getEntry("Float Parameter").getValue<float>(&readFloat);
220  TEST_ASSERT(readFloat == 8.0);
221 
222  // check string can generate out of range
223  userList = rcp(new ParameterList("User List"));
224  TEST_NOTHROW(userList->set("Float Parameter", "20.0"));
225  TEST_THROW(userList->validateParameters(*validList),
227 
228  /*
229  * Testing Double Validator.
230  */
231  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
233  rcp(new EnhancedNumberValidator<double>(0,10.0,4.0,6));
234  TEST_ASSERT(doubleVali->getMin() == 0.0);
235  TEST_ASSERT(doubleVali->getMax() == 10.0);
236  TEST_ASSERT(doubleVali->getStep() == 4.0);
237  TEST_ASSERT(doubleVali->getPrecision() == 6);
238  TEST_ASSERT(doubleVali->hasMin());
239  TEST_ASSERT(doubleVali->hasMax());
242  TEST_ASSERT(!doubleVali2->hasMin());
243  TEST_ASSERT(!doubleVali2->hasMax());
244  TEST_ASSERT(doubleVali2->getMin() == EnhancedNumberTraits<double>::min());
245  TEST_ASSERT(doubleVali2->getMax() == EnhancedNumberTraits<double>::max());
246  TEST_ASSERT(doubleVali2->getStep() == EnhancedNumberTraits<double>::defaultStep());
247  TEST_ASSERT(doubleVali2->getPrecision() == EnhancedNumberTraits<double>::defaultPrecision());
248  doubleList->set("Double Parameter", (double)5.0, "double parameter", doubleVali);
249  TEST_NOTHROW(doubleList->validateParameters(*doubleList));
250  TEST_THROW(doubleList->set("Double Parameter", (double)11.0),
252  TEST_THROW(doubleList->set("Int Parameter", 5, "int parameter", doubleVali),
254 
255  // Test String Conversions with double
256  validList = rcp(new ParameterList("Valid List"));
257  userList = rcp(new ParameterList("User List"));
258  TEST_NOTHROW(validList->set("Double Parameter", 4.0, "double parameter",
259  doubleVali));
260 #ifdef HAVE_TEUCHOSCORE_CXX11
261  TEST_NOTHROW(userList->set("Double Parameter", "x4.0"));
262  TEST_THROW(userList->validateParameters(*validList),
264  TEST_NOTHROW(userList->set("Double Parameter", "4.0x"));
265  TEST_THROW(userList->validateParameters(*validList),
267  TEST_NOTHROW(userList->set("Double Parameter", "12.0"));
268  TEST_THROW(userList->validateParameters(*validList), // bad range
270 #endif
271  userList = rcp(new ParameterList("Valid List"));
272  TEST_NOTHROW(userList->set("Double Parameter", 8.0));
273  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
274  TEST_NOTHROW(userList->set("Double Parameter", "8.0"));
275  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
276  double readDouble = userList->getEntry("Double Parameter").getValue<double>(&readDouble);
277  TEST_ASSERT(readDouble == 8.0);
278 
279  // check string can generate out of range
280  userList = rcp(new ParameterList("User List"));
281  TEST_NOTHROW(userList->set("Double Parameter", "20.0"));
282  TEST_THROW(userList->validateParameters(*validList),
284  }
285 
286 TEUCHOS_UNIT_TEST(Teuchos_Validators, anyNumberValidator)
287 {
288  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
289  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
290 
292  intDoubleTypes.allowString(false);
294  intStringTypes.allowDouble(false);
296  intTypes.allowDouble(false);
297  intTypes.allowString(false);
298 
299  // set up validators to test
300  // default prefers double and allows string and int
312 
313  // first check the 'good' setups which do not throw
314  TEST_NOTHROW(validList->set( "allParameter", "1.1", "documentation",
315  allValidator));
316  TEST_NOTHROW(validList->set( "allParameter", 1.1, "documentation",
317  allValidator));
318  TEST_NOTHROW(validList->set( "allParameter", "1", "documentation",
319  allValidator));
320  TEST_NOTHROW(validList->set( "allParameter", 1, "documentation",
321  allValidator));
322  TEST_NOTHROW(validList->set( "intDoubleParameter", 1.1, "documentation",
323  intDoubleValidator));
324  TEST_NOTHROW(validList->set( "intDoubleParameter", 1, "documentation",
325  intDoubleValidator));
326  TEST_NOTHROW(validList->set( "intStringParameter", "1", "documentation",
327  intStringValidator));
328  TEST_NOTHROW(validList->set( "intStringParameter", 1, "documentation",
329  intStringValidator));
330  TEST_NOTHROW(validList->set( "intParameter", 1, "documentation",
331  intValidator));
332 
333  // This was a special case that might warrant discussion.
334  // The issue is for validators which accept string/int but not double.
335  // In the original setup the validator would always call getDouble
336  // internally and accept a string of "1.1" without error.
337  TEST_NOTHROW(validList->set( "intStringParameter", "1.1", "documentation",
338  intStringValidator));
339 
340  //
341  // these are some cases which throw independent of HAVE_TEUCHOSCORE_CXX11
342  //
343 
344  // if string it not allowed you can't use a string ever
345  TEST_THROW(validList->set( "intDoubleParameter", "1.1", "documentation",
346  intDoubleValidator), Exceptions::InvalidParameterType);
347 
348  // it also throws for a double number - double not allowed
349  TEST_THROW(validList->set( "intStringParameter", 1.1, "documentation",
350  intStringValidator), Exceptions::InvalidArgument);
351 
352  // for int only it can't be a string - any string will throw
353  TEST_THROW(validList->set( "intParameter", "1", "documentation",
354  intValidator), Exceptions::InvalidParameter);
355 
356  // this int only it can't be a double because double is not allowed
357  TEST_THROW(validList->set( "intParameter", 1.1, "documentation",
358  intValidator), Exceptions::InvalidParameter);
359 
360  //
361  // these behaviors now depend on HAVE_TEUCHOSCORE_CXX11
362  // std::stod and std::stoi will be used for HAVE_TEUCHOSCORE_CXX11
363  // std::atof and std::atoi will be used for no CXX11
364  //
365 #ifdef HAVE_TEUCHOSCORE_CXX11
366  // for double types we throw for badly formatted string on std::stod
367  // this will check the double type first because it is PREFER_DOUBLE
368  TEST_THROW(validList->set( "allParameter", "1.1x", "documentation",
369  allValidator), Exceptions::InvalidArgument);
370  TEST_THROW(validList->set( "intDoubleParameter", "1.1x", "documentation",
371  allValidator), Exceptions::InvalidArgument);
372  TEST_THROW(validList->set( "allParameter", "x1.1", "documentation",
373  allValidator), Exceptions::InvalidArgument);
374  TEST_THROW(validList->set( "intDoubleParameter", "x1.1", "documentation",
375  allValidator), Exceptions::InvalidArgument);
376  // for int/string but no double - std::stoi throws for invalid formatting
377  TEST_THROW(validList->set( "intStringParameter", "1x", "documentation",
378  intStringValidator), Exceptions::InvalidArgument);
379  TEST_THROW(validList->set( "intStringParameter", "x1", "documentation",
380  intStringValidator), Exceptions::InvalidArgument);
381  TEST_THROW(validList->set( "intStringParameter", "1 x", "documentation",
382  intStringValidator), Exceptions::InvalidArgument);
383 #else
384  // for int/double/string std::atod does NOT throw - this is the old behavior
385  // this is different now when HAVE_TEUCHOSCORE_CXX11 is ON - see above
386  TEST_NOTHROW(validList->set( "allParameter", "1.1x", "documentation",
387  allValidator));
388  // for int/string std::atoi does NOT throw - this is the old behavior
389  // this is different now when HAVE_TEUCHOSCORE_CXX11 is ON - see above
390  TEST_NOTHROW(validList->set( "intStringParameter", "1.1x", "documentation",
391  intStringValidator));
392 #endif
393 }
394 
395 TEUCHOS_UNIT_TEST(Teuchos_Validators, boolValidator)
396 {
397  RCP<ParameterList> userList = rcp(new ParameterList("User List"));
398  RCP<ParameterList> validList = rcp(new ParameterList("Valid List"));
399 
400  // first without validator - accepts only true/false
401  validList->set( "justBool", false, "documentation" );
402  TEST_NOTHROW(userList->set( "justBool", false));
403  TEST_NOTHROW(userList->validateParameters(*validList));
404  TEST_NOTHROW(userList->set( "justBool", true));
405  TEST_NOTHROW(userList->validateParameters(*validList));
406  // this will not validate because we did not add a bool validator
407  TEST_NOTHROW(userList->set( "justBool", "true"));
408  TEST_THROW(userList->validateParameters(*validList),
410  // this will not validate because we did not add a bool validator
411  TEST_NOTHROW(userList->set( "justBool", "false"));
412  TEST_THROW(userList->validateParameters(*validList),
414 
415  // now with BoolParameterEntryValidator validator
416  // accepts true/false/"true"/"false"
419  userList = rcp(new ParameterList("User List")); // make a new list
420  validList = rcp(new ParameterList("Valid List")); // make a new list
421  validList->set( "boolOrString", false, "documentation", boolValidator );
422  TEST_NOTHROW(userList->set( "boolOrString", false));
423  TEST_NOTHROW(userList->validateParameters(*validList));
424  TEST_NOTHROW(userList->set( "boolOrString", true));
425  TEST_NOTHROW(userList->validateParameters(*validList));
426  // this will validate because we added a bool validator
427  TEST_NOTHROW(userList->set( "boolOrString", "true"));
428  TEST_NOTHROW(userList->validateParameters(*validList));
429  // this will validate because we added a bool validator
430  TEST_NOTHROW(userList->set( "boolOrString", "false"));
431  TEST_NOTHROW(userList->validateParameters(*validList));
432  // but only "false" and "true" work - anything else will not validate
433  TEST_NOTHROW(userList->set( "boolOrString", "falsex")); // sets ok
434  TEST_THROW(userList->validateParameters(*validList), // but throws
436 
437  // now with BoolParameterEntryValidator validator
438  // but consider what happens if we created it using "false" instead of false
439  // this should still work identically to the previous case
440  userList = rcp(new ParameterList("User List")); // make a new list
441  validList = rcp(new ParameterList("Valid List")); // make a new list
442  validList->set( "boolOrString", "false", "documentation", boolValidator );
443  TEST_NOTHROW(userList->set( "boolOrString", false));
444  TEST_NOTHROW(userList->validateParameters(*validList));
445  TEST_NOTHROW(userList->set( "boolOrString", true ));
446  TEST_NOTHROW(userList->validateParameters(*validList));
447  // this will validate because we added a bool validator
448  TEST_NOTHROW(userList->set( "boolOrString", "true"));
449  TEST_NOTHROW(userList->validateParameters(*validList));
450  // this will validate because we added a bool validator
451  TEST_NOTHROW(userList->set( "boolOrString", "false"));
452  TEST_NOTHROW(userList->validateParameters(*validList));
453  // but only "false" and "true" work - anything else will not validate
454  TEST_NOTHROW(userList->set( "boolOrString", "falsex")); // sets ok
455  TEST_THROW(userList->validateParameters(*validList), // but throws
457 
458  // do another test using validateParametersAndSetDefaults
459  userList = rcp(new ParameterList("User List")); // make a new list
460  validList = rcp(new ParameterList("Valid List")); // make a new list
461  // Default values for parameters are bool
462  validList->set("boolOne", true, "doc", boolValidator);
463  validList->set("boolTwo", false, "doc", boolValidator);
464  bool defOne = validList->getEntry("boolOne").getValue(&defOne);
465  bool defTwo = validList->getEntry("boolTwo").getValue(&defTwo);
466 
467  // Create user parameter list
468  userList->set("boolOne", false); // User can provide bool value...
469  userList->set("boolTwo", "true"); // or string "true"/"false"
470  TEST_NOTHROW(userList->validateParametersAndSetDefaults(*validList));
471 }
472 
473 
474 /*
475  * Testing StringValidator.
476  */
477 TEUCHOS_UNIT_TEST(Teuchos_Validators, stringValidator)
478 {
479  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
480  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
481  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
482  RCP<const Array<std::string> > valiVals = stringVali->validStringValues();
483  /*bool local_success = true;
484  for(int i =0; i<valiVals.size() ++i){
485  TEST_ARRAY_ELE_EQUALITY(*valiVals, i, stringVals[i]);
486  }
487  if (local_success) out << "passed\n";
488  else success = false;*/
489  TEST_COMPARE_ARRAYS(*valiVals, stringVals);
490  TEST_NOTHROW(stringList->set("String param1", "str1", "a string parameter", stringVali));
491  TEST_THROW(stringList->set("String param2", "not in list", "a string parameter", stringVali),
493  TEST_THROW(stringList->set("int param", 5, "a int parameter", stringVali),
495 }
496 
497 
498 /*
499  * Testing FileNameValidator.
500  */
501 TEUCHOS_UNIT_TEST(Teuchos_Validators, fileNameValidator)
502 {
503  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
504  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
505  TEST_ASSERT(fileNameVali->fileMustExist());
506  fileNameVali->setFileMustExist(false);
507  TEST_ASSERT(!fileNameVali->fileMustExist());
508  TEST_NOTHROW(fileNameList->set("File name param", "../path", "file name parameter",
509  fileNameVali));
510  TEST_THROW(fileNameList->set("int param", 5, "int parameter", fileNameVali),
512  fileNameVali->setFileMustExist(true);
513  TEST_NOTHROW(fileNameList->set("file name param", "testFile.txt", "a file name", fileNameVali));
514  TEST_THROW(fileNameList->set("file name param", "doesntexist.txt", "a file name", fileNameVali),
516 }
517 
518 
519 /*
520  * Testing Array Validators.
521  */
522 TEUCHOS_UNIT_TEST(Teuchos_Validators, arrayValidators)
523 {
524 
525  /*
526  * Testing StringArrayValidator.
527  */
528  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
529  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
530  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
531  RCP<ArrayStringValidator> stringArrayVali = rcp(new ArrayStringValidator(stringVali));
532  TEST_ASSERT(stringVali.get() == stringArrayVali->getPrototype().get());
533  Array<std::string> stringArray = tuple<std::string>("str2","str3","str1","str3","str2");
534  TEST_NOTHROW(stringList->set("String Array Param", stringArray, "string array parameter", stringArrayVali));
535  Array<std::string> badStringArray = tuple<std::string>("not valid","str3","str1","str3","str2");
536  TEST_THROW(stringList->set("String Array Param", badStringArray, "string array parameter", stringArrayVali),
538  TEST_THROW(stringList->set("Int param", 5, "int parameter", stringArrayVali),
540  Array<long> longArray = tuple<long>((long)5,(long)5,(long)3);
541  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", stringArrayVali),
543 
544  /*
545  * Testing Int ArrayValidator.
546  */
547  RCP<ParameterList> intList = rcp(new ParameterList("Int List"));
549  RCP<ArrayNumberValidator<int> > intArrayVali = rcp(new ArrayNumberValidator<int>(intVali));
550  TEST_ASSERT(intVali.get() == intArrayVali->getPrototype().get());
551  Array<int> intArray = tuple<int>(1,4,2,5);
552  TEST_NOTHROW(intList->set("int array param", intArray, "int array parameter", intArrayVali));
553  Array<int> intBadArray = tuple<int>(11,4,2,5);
554  TEST_THROW(intList->set("int bad array param", intBadArray, "int bad array parameter", intArrayVali),
556  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", intArrayVali),
558 
559  /*
560  * Testing Short ArrayValidator.
561  */
562  RCP<ParameterList> shortList = rcp(new ParameterList("Short List"));
565  RCP<ArrayNumberValidator<short> > shortArrayVali =
566  rcp(new ArrayNumberValidator<short>(shortVali));
567  TEST_ASSERT(shortVali.get() == shortArrayVali->getPrototype().get());
568  Array<short> shortArray = tuple<short>(1,4,2,5);
569  TEST_NOTHROW(shortList->set("short array param", shortArray, "short array parameter", shortArrayVali));
570  Array<short> shortBadArray = tuple<short>(11,4,2,5);
571  TEST_THROW(shortList->set("short bad array param", shortBadArray, "short bad array parameter", shortArrayVali),
573  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", shortArrayVali),
575 
576  /*
577  * Testing Float ArrayValidator.
578  */
579  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
581  rcp(new EnhancedNumberValidator<float>(0.0, 10.0));
582  RCP<ArrayNumberValidator<float> > floatArrayVali =
583  rcp(new ArrayNumberValidator<float>(floatVali));
584  TEST_ASSERT(floatVali.get() == floatArrayVali->getPrototype().get());
585  Array<float> floatArray = tuple<float>(1.0,4.0,2.0,5.0);
586  TEST_NOTHROW(floatList->set("float array param", floatArray, "float array parameter", floatArrayVali));
587  Array<float> floatBadArray = tuple<float>(11.0,4.0,2.0,5.0);
588  TEST_THROW(floatList->set("float bad array param", floatBadArray, "float bad array parameter", floatArrayVali),
590  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", floatArrayVali),
592 
593  /*
594  * Testing Double ArrayValidator.
595  */
596  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
598  rcp(new EnhancedNumberValidator<double>(0.0, 10.0));
599  RCP<ArrayNumberValidator<double> > doubleArrayVali =
600  rcp(new ArrayNumberValidator<double>(doubleVali));
601  TEST_ASSERT(doubleVali.get() == doubleArrayVali->getPrototype().get());
602  Array<double> doubleArray = tuple<double>(1.0,4.0,2.0,5.0);
603  TEST_NOTHROW(doubleList->set("double array param", doubleArray, "double array parameter", doubleArrayVali));
604  Array<double> doubleBadArray = tuple<double>(11.0,4.0,2.0,5.0);
605  TEST_THROW(doubleList->set("double bad array param", doubleBadArray, "double bad array parameter", doubleArrayVali),
607  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", doubleArrayVali),
609 
610  /*
611  * Testing FileName ArrayValidator.
612  */
613  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
614  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
615  RCP<ArrayFileNameValidator> arrayFileNameVali = rcp(new ArrayFileNameValidator(fileNameVali));
616  TEST_ASSERT(arrayFileNameVali->getPrototype().get() == fileNameVali.get());
617  Array<std::string> fileNameArray = tuple<std::string>("testFile.txt", "testFile2.txt", "testFile3.txt");
618  Array<std::string> fileNameBadArray = tuple<std::string>("doesnexist.txt", "testFile2.txt", "testFile3.txt");
619  TEST_NOTHROW(fileNameList->set("File name array", fileNameArray, "file name array parameter", arrayFileNameVali));
620  TEST_THROW(fileNameList->set("Bad File name array", fileNameBadArray, "bad file name array parameter", arrayFileNameVali),
622  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", arrayFileNameVali),
624 }
625 
626 /*
627  * Testing TwoDArray Validators.
628  */
629 TEUCHOS_UNIT_TEST(Teuchos_Validators, twoDArrayValidators)
630 {
631 
632  /*
633  * Testing StringArrayValidator.
634  */
635  RCP<ParameterList> stringList = rcp(new ParameterList("String List"));
636  Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
637  RCP<StringValidator> stringVali = rcp(new StringValidator(stringVals));
638  RCP<TwoDArrayStringValidator> stringArrayVali =
639  rcp(new TwoDArrayStringValidator(stringVali));
640  TEST_ASSERT(stringVali.get() == stringArrayVali->getPrototype().get());
641  TwoDArray<std::string> stringArray(2,2);
642  stringArray(0,0) = "str2";
643  stringArray(0,1) = "str1";
644  stringArray(1,0) = "str3";
645  stringArray(1,1) = "str2";
646  TEST_NOTHROW(stringList->set("String Array Param", stringArray, "string array parameter", stringArrayVali));
647  TwoDArray<std::string> badStringArray(2,2);
648  badStringArray(0,0) = "str2";
649  badStringArray(0,1) = "str1";
650  badStringArray(1,0) = "str3";
651  badStringArray(1,1) = "not valid";
652  TEST_THROW(stringList->set("String Array Param", badStringArray, "string array parameter", stringArrayVali),
654  TEST_THROW(stringList->set("Int param", 5, "int parameter", stringArrayVali),
656  TwoDArray<long> longArray(2,2);
657  longArray(0,0) = (long)5;
658  longArray(0,1) = (long)4;
659  longArray(1,0) = (long)9;
660  longArray(1,1) = (long)1;
661  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", stringArrayVali),
663 
664  /*
665  * Testing Int ArrayValidator.
666  */
667  RCP<ParameterList> intList = rcp(new ParameterList("Int List"));
669  RCP<TwoDArrayNumberValidator<int> > intArrayVali =
670  rcp(new TwoDArrayNumberValidator<int>(intVali));
671  TEST_ASSERT(intVali.get() == intArrayVali->getPrototype().get());
672  TwoDArray<int> intArray(2,2);
673  intArray(0,0) = 1;
674  intArray(0,1) = 4;
675  intArray(1,0) = 2;
676  intArray(1,1) = 5;
677  TEST_NOTHROW(intList->set("int array param", intArray, "int array parameter", intArrayVali));
678  TwoDArray<int> intBadArray(2,2);
679  intBadArray(0,0) = 11;
680  intBadArray(0,1) = 4;
681  intBadArray(1,0) = 2;
682  intBadArray(1,1) = 5;
683  TEST_THROW(intList->set("int bad array param", intBadArray, "int bad array parameter", intArrayVali),
685  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", intArrayVali),
687 
688  /*
689  * Testing Short ArrayValidator.
690  */
691  RCP<ParameterList> shortList = rcp(new ParameterList("Short List"));
694  RCP<TwoDArrayNumberValidator<short> > shortArrayVali =
695  rcp(new TwoDArrayNumberValidator<short>(shortVali));
696  TEST_ASSERT(shortVali.get() == shortArrayVali->getPrototype().get());
697  TwoDArray<short> shortArray(2,2);
698  shortArray(0,0) = 1;
699  shortArray(0,1) = 4;
700  shortArray(1,0) = 2;
701  shortArray(1,1) = 5;
702  TEST_NOTHROW(shortList->set("short array param", shortArray, "short array parameter", shortArrayVali));
703  TwoDArray<short> shortBadArray(2,2);
704  shortBadArray(0,0) = 11;
705  shortBadArray(0,1) = 4;
706  shortBadArray(1,0) = 2;
707  shortBadArray(1,1) = 5;
708  TEST_THROW(shortList->set("short bad array param", shortBadArray, "short bad array parameter", shortArrayVali),
710  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", shortArrayVali),
712 
713  /*
714  * Testing Float ArrayValidator.
715  */
716  RCP<ParameterList> floatList = rcp(new ParameterList("Float List"));
718  rcp(new EnhancedNumberValidator<float>(0.0, 10.0));
719  RCP<TwoDArrayNumberValidator<float> > floatArrayVali =
720  rcp(new TwoDArrayNumberValidator<float>(floatVali));
721  TEST_ASSERT(floatVali.get() == floatArrayVali->getPrototype().get());
722  TwoDArray<float> floatArray(2,2);
723  floatArray(0,0) = 1.0;
724  floatArray(0,1) = 4.0;
725  floatArray(1,0) = 5.0;
726  floatArray(1,1) = 2.0;
727  TEST_NOTHROW(floatList->set("float array param", floatArray, "float array parameter", floatArrayVali));
728  TwoDArray<float> floatBadArray(2,2);
729  floatBadArray(0,0) = 11.0;
730  floatBadArray(0,1) = 4.0;
731  floatBadArray(1,0) = 5.0;
732  floatBadArray(1,1) = 2.0;
733  TEST_THROW(floatList->set("float bad array param", floatBadArray, "float bad array parameter", floatArrayVali),
735  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", floatArrayVali),
737 
738  /*
739  * Testing Double ArrayValidator.
740  */
741  RCP<ParameterList> doubleList = rcp(new ParameterList("Double List"));
743  rcp(new EnhancedNumberValidator<double>(0.0, 10.0));
744  RCP<TwoDArrayNumberValidator<double> > doubleArrayVali =
745  rcp(new TwoDArrayNumberValidator<double>(doubleVali));
746  TEST_ASSERT(doubleVali.get() == doubleArrayVali->getPrototype().get());
747  TwoDArray<double> doubleArray(2,2);
748  doubleArray(0,0) = 1.0;
749  doubleArray(0,1) = 4.0;
750  doubleArray(1,0) = 5.0;
751  doubleArray(1,1) = 2.0;
752  TEST_NOTHROW(doubleList->set("double array param", doubleArray, "double array parameter", doubleArrayVali));
753  TwoDArray<double> doubleBadArray(2,2);
754  doubleBadArray(0,0) = 11.0;
755  doubleBadArray(0,1) = 4.0;
756  doubleBadArray(1,0) = 5.0;
757  doubleBadArray(1,1) = 2.0;
758  TEST_THROW(doubleList->set("double bad array param", doubleBadArray, "double bad array parameter", doubleArrayVali),
760  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", doubleArrayVali),
762 
763  /*
764  * Testing FileName ArrayValidator.
765  */
766  RCP<ParameterList> fileNameList = rcp(new ParameterList("Filename List"));
767  RCP<FileNameValidator> fileNameVali = rcp(new FileNameValidator(true));
768  RCP<TwoDArrayFileNameValidator> arrayFileNameVali =
769  rcp(new TwoDArrayFileNameValidator(fileNameVali));
770  TEST_ASSERT(arrayFileNameVali->getPrototype().get() == fileNameVali.get());
771  TwoDArray<std::string> fileNameArray(2,2);
772  fileNameArray(0,0) = "testFile.txt";
773  fileNameArray(0,1) = "testFile2.txt";
774  fileNameArray(1,0) = "testFile3.txt";
775  fileNameArray(1,1) = "testFile.txt";
776  TwoDArray<std::string> fileNameBadArray(2,2);
777  fileNameBadArray(0,0) = "doesntexist.txt";
778  fileNameBadArray(0,1) = "testFile2.txt";
779  fileNameBadArray(1,0) = "testFile3.txt";
780  fileNameBadArray(1,1) = "testFile.txt";
781  TEST_NOTHROW(fileNameList->set("File name array", fileNameArray, "file name array parameter", arrayFileNameVali));
782  TEST_THROW(fileNameList->set("Bad File name array", fileNameBadArray, "bad file name array parameter", arrayFileNameVali),
784  TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", arrayFileNameVali),
786 }
787 
788 
789 } // namespace Teuchos
790 
#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.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
AcceptedTypes & allowString(bool _allowString)
Set allow an std::string value or not.
T * get() const
Get the raw C++ pointer to the underlying object.
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.
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.