test/validators/main.cpp

Go to the documentation of this file.
00001 // @HEADER // ***********************************************************************
00002 // 
00003 //         Optika: A Tool For Developing Parameter Obtaining GUIs
00004 //                Copyright (2009) Sandia Corporation
00005 // 
00006 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the 
00007 // U.S. Government retains certain rights in this software.
00008 // 
00009 // This library is free software; you can redistribute it and/or modify
00010 // it under the terms of the GNU Lesser General Public License as
00011 // published by the Free Software Foundation; either version 2.1 of the
00012 // License, or (at your option) any later version.
00013 //  
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 //  
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022 // USA
00023 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00024 // 
00025 // ***********************************************************************
00026 // @HEADER
00027 #include "Teuchos_LocalTestingHelpers.hpp"
00028 #include "Teuchos_VerboseObject.hpp"
00029 #include "Teuchos_ParameterList.hpp"
00030 #include "Optika_SpecificParameterEntryValidators.hpp"
00031 
00032 using namespace Teuchos;
00033 using namespace Optika;
00034 
00038 int testNumberValis(Teuchos::FancyOStream &out){
00039   bool success = true;
00040   /*
00041    * Testing Int Validator.
00042    */
00043   RCP<ParameterList> intList = RCP<ParameterList>(new ParameterList("Int List"));
00044   RCP<EnhancedNumberValidator<int> > intVali = RCP<EnhancedNumberValidator<int> >(new EnhancedNumberValidator<int>(0,10,4));
00045   TEST_ASSERT(intVali->min() == 0);
00046   TEST_ASSERT(intVali->max() == 10);
00047   TEST_ASSERT(intVali->getStep() == 4);
00048   TEST_ASSERT(intVali->hasMin());
00049   TEST_ASSERT(intVali->hasMax());
00050   TEST_ASSERT(intVali->getType() == intId);
00051   RCP<EnhancedNumberValidator<int> > intVali2 = RCP<EnhancedNumberValidator<int> >(new EnhancedNumberValidator<int>());
00052   TEST_ASSERT(!intVali2->hasMin());
00053   TEST_ASSERT(!intVali2->hasMax());
00054   TEST_ASSERT(intVali2->min() == std::numeric_limits<int>::min());
00055   TEST_ASSERT(intVali2->max() == std::numeric_limits<int>::max());
00056   TEST_ASSERT(intVali2->getStep() == intDefaultStep);
00057   intList->set("Int Parameter", 5, "int parameter", intVali);
00058   TEST_NOTHROW(intList->validateParameters(*intList));
00059   TEST_THROW(intList->set("Int Parameter", 11), Exceptions::InvalidParameterValue);
00060   TEST_THROW(intList->set("Double Parameter", 5.0, "double parameter", intVali), Exceptions::InvalidParameterType);
00061 
00062   /*
00063    * Testing Short Validator.
00064    */
00065   RCP<ParameterList> shortList = RCP<ParameterList>(new ParameterList("Short List"));
00066   RCP<EnhancedNumberValidator<short> > shortVali = RCP<EnhancedNumberValidator<short> >(new EnhancedNumberValidator<short>(0,10,4));
00067   TEST_ASSERT(shortVali->min() == 0);
00068   TEST_ASSERT(shortVali->max() == 10);
00069   TEST_ASSERT(shortVali->getStep() == 4);
00070   TEST_ASSERT(shortVali->hasMin());
00071   TEST_ASSERT(shortVali->hasMax());
00072   TEST_ASSERT(shortVali->getType() == shortId);
00073   RCP<EnhancedNumberValidator<short> > shortVali2 = RCP<EnhancedNumberValidator<short> >(new EnhancedNumberValidator<short>());
00074   TEST_ASSERT(!shortVali2->hasMin());
00075   TEST_ASSERT(!shortVali2->hasMax());
00076   TEST_ASSERT(shortVali2->min() == std::numeric_limits<short>::min());
00077   TEST_ASSERT(shortVali2->max() == std::numeric_limits<short>::max());
00078   TEST_ASSERT(shortVali2->getStep() == shortDefaultStep);
00079   shortList->set("Short Parameter", (short)5, "short parameter", shortVali);
00080   TEST_NOTHROW(shortList->validateParameters(*shortList));
00081   TEST_THROW(shortList->set("Short Parameter", (short)11), Exceptions::InvalidParameterValue);
00082   TEST_THROW(shortList->set("Double Parameter", 5.0, "double parameter", shortVali), Exceptions::InvalidParameterType);
00083 
00084   /*
00085    * Testing Float Validator.
00086    */
00087   RCP<ParameterList> floatList = RCP<ParameterList>(new ParameterList("Float List"));
00088   RCP<EnhancedNumberValidator<float> > floatVali = RCP<EnhancedNumberValidator<float> >(new EnhancedNumberValidator<float>(0,10.0,4.0,6));
00089   TEST_ASSERT(floatVali->min() == 0.0);
00090   TEST_ASSERT(floatVali->max() == 10.0);
00091   TEST_ASSERT(floatVali->getStep() == 4.0);
00092   TEST_ASSERT(floatVali->getPrecision() == 6);
00093   TEST_ASSERT(floatVali->hasMin());
00094   TEST_ASSERT(floatVali->hasMax());
00095   TEST_ASSERT(floatVali->getType() == floatId);
00096   RCP<EnhancedNumberValidator<float> > floatVali2 = RCP<EnhancedNumberValidator<float> >(new EnhancedNumberValidator<float>());
00097   TEST_ASSERT(!floatVali2->hasMin());
00098   TEST_ASSERT(!floatVali2->hasMax());
00099   TEST_ASSERT(floatVali2->min() == -std::numeric_limits<float>::max());
00100   TEST_ASSERT(floatVali2->max() == std::numeric_limits<float>::max());
00101   TEST_ASSERT(floatVali2->getStep() == floatDefaultStep);
00102   TEST_ASSERT(floatVali2->getPrecision() == floatDefaultPrecision);
00103   floatList->set("Float Parameter", (float)5.0, "float parameter", floatVali);
00104   TEST_NOTHROW(floatList->validateParameters(*floatList));
00105   TEST_THROW(floatList->set("Float Parameter", (float)11.0), Exceptions::InvalidParameterValue);
00106   TEST_THROW(floatList->set("Int Parameter", 5, "int parameter", floatVali), Exceptions::InvalidParameterType);
00107 
00108   /*
00109    * Testing Double Validator.
00110    */
00111   RCP<ParameterList> doubleList = RCP<ParameterList>(new ParameterList("Double List"));
00112   RCP<EnhancedNumberValidator<double> > doubleVali = RCP<EnhancedNumberValidator<double> >(new EnhancedNumberValidator<double>(0,10.0,4.0,6));
00113   TEST_ASSERT(doubleVali->min() == 0.0);
00114   TEST_ASSERT(doubleVali->max() == 10.0);
00115   TEST_ASSERT(doubleVali->getStep() == 4.0);
00116   TEST_ASSERT(doubleVali->getPrecision() == 6);
00117   TEST_ASSERT(doubleVali->hasMin());
00118   TEST_ASSERT(doubleVali->hasMax());
00119   TEST_ASSERT(doubleVali->getType() == doubleId);
00120   RCP<EnhancedNumberValidator<double> > doubleVali2 = RCP<EnhancedNumberValidator<double> >(new EnhancedNumberValidator<double>());
00121   TEST_ASSERT(!doubleVali2->hasMin());
00122   TEST_ASSERT(!doubleVali2->hasMax());
00123   TEST_ASSERT(doubleVali2->min() == -std::numeric_limits<double>::max());
00124   TEST_ASSERT(doubleVali2->max() == std::numeric_limits<double>::max());
00125   TEST_ASSERT(doubleVali2->getStep() == doubleDefaultStep);
00126   TEST_ASSERT(doubleVali2->getPrecision() == doubleDefaultPrecision);
00127   doubleList->set("Double Parameter", (double)5.0, "double parameter", doubleVali);
00128   TEST_NOTHROW(doubleList->validateParameters(*doubleList));
00129   TEST_THROW(doubleList->set("Double Parameter", (double)11.0), Exceptions::InvalidParameterValue);
00130   TEST_THROW(doubleList->set("Int Parameter", 5, "int parameter", doubleVali), Exceptions::InvalidParameterType);
00131   return (success ? 0:1);
00132 }
00133 
00134 /*
00135  * Testing StringValidator.
00136  */
00137 int testStringVali(Teuchos::FancyOStream &out){
00138   bool success = true;
00139   RCP<ParameterList> stringList = RCP<ParameterList>(new ParameterList("String List"));
00140   Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
00141   RCP<StringValidator> stringVali = RCP<StringValidator>(new StringValidator(stringVals));
00142   RCP<const Array<std::string> > valiVals = stringVali->validStringValues();
00143   bool local_success = true;
00144   for(int i=0;i<stringVals.size(); i++){
00145     TEST_ARRAY_ELE_EQUALITY(*valiVals, i, stringVals[i]);
00146   }
00147   if(!local_success){
00148     success = false;
00149   }
00150   TEST_NOTHROW(stringList->set("String param1", "str1", "a string parameter", stringVali));
00151   TEST_THROW(stringList->set("String param2", "not in list", "a string parameter", stringVali), Exceptions::InvalidParameterValue);
00152   TEST_THROW(stringList->set("int param", 5, "a int parameter", stringVali), Exceptions::InvalidParameterType);
00153 
00154   return (success ? 0:1);
00155 }
00156 
00157 /*
00158  * Testing FileNameValidator.
00159  */
00160 int testFileNameVali(Teuchos::FancyOStream &out){
00161   bool success = true;
00162   RCP<ParameterList> fileNameList = RCP<ParameterList>(new ParameterList("Filename List"));
00163   RCP<FileNameValidator> fileNameVali = RCP<FileNameValidator>(new FileNameValidator(true));
00164   TEST_ASSERT(fileNameVali->fileMustExist());
00165   fileNameVali->setFileMustExist(false);
00166   TEST_ASSERT(!fileNameVali->fileMustExist());
00167   TEST_NOTHROW(fileNameList->set("File name param", "../path", "file name parameter", fileNameVali));
00168   TEST_THROW(fileNameList->set("int param", 5, "int parameter", fileNameVali), Exceptions::InvalidParameterType);
00169   fileNameVali->setFileMustExist(true);
00170   TEST_NOTHROW(fileNameList->set("file name param", "testFile.txt", "a file name", fileNameVali));
00171   TEST_THROW(fileNameList->set("file name param", "doesntexist.txt", "a file name", fileNameVali), Exceptions::InvalidParameterValue);
00172 
00173   return (success ? 0:1);
00174 }
00175 
00176 /*
00177  * Testing Array Validators.
00178  */
00179 int testArrayValis(Teuchos::FancyOStream &out){
00180   bool success = true;
00181   /*
00182    * Testing StringArrayValidator.
00183    */
00184   RCP<ParameterList> stringList = RCP<ParameterList>(new ParameterList("String List"));
00185   Array<std::string> stringVals = tuple<std::string>("str1", "str2", "str3");
00186   RCP<StringValidator> stringVali = RCP<StringValidator>(new StringValidator(stringVals));
00187   RCP<ArrayStringValidator> stringArrayVali = RCP<ArrayStringValidator>(new ArrayStringValidator(stringVali));
00188   TEST_ASSERT(stringVali.get() == stringArrayVali->getPrototype().get());
00189   Array<std::string> stringArray = tuple<std::string>("str2","str3","str1","str3","str2");
00190   TEST_NOTHROW(stringList->set("String Array Param", stringArray, "string array parameter", stringArrayVali));
00191   Array<std::string> badStringArray = tuple<std::string>("not valid","str3","str1","str3","str2");
00192   TEST_THROW(stringList->set("String Array Param", badStringArray, "string array parameter", stringArrayVali), Exceptions::InvalidParameterValue);
00193   TEST_THROW(stringList->set("Int param", 5, "int parameter", stringArrayVali), Exceptions::InvalidParameterType);
00194   Array<long> longArray = tuple<long>((long)5,(long)5,(long)3);
00195   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", stringArrayVali), Exceptions::InvalidParameterType);
00196 
00197   
00198   /*
00199    * Testing Int ArrayValidator.
00200    */
00201   RCP<ParameterList> intList = RCP<ParameterList>(new ParameterList("Int List"));
00202   RCP<EnhancedNumberValidator<int> > intVali = RCP<EnhancedNumberValidator<int> >(new EnhancedNumberValidator<int>(0, 10));
00203   RCP<ArrayNumberValidator<int> > intArrayVali = RCP<ArrayNumberValidator<int> >(new ArrayNumberValidator<int>(intVali));
00204   TEST_ASSERT(intVali.get() == intArrayVali->getPrototype().get());
00205   Array<int> intArray = tuple<int>(1,4,2,5);
00206   TEST_NOTHROW(intList->set("int array param", intArray, "int array parameter", intArrayVali));
00207   Array<int> intBadArray = tuple<int>(11,4,2,5);
00208   TEST_THROW(intList->set("int bad array param", intBadArray, "int bad array parameter", intArrayVali), Exceptions::InvalidParameterValue);
00209   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", intArrayVali), Exceptions::InvalidParameterType);
00210 
00211   /*
00212    * Testing Short ArrayValidator.
00213    */
00214   RCP<ParameterList> shortList = RCP<ParameterList>(new ParameterList("Short List"));
00215   RCP<EnhancedNumberValidator<short> > shortVali = RCP<EnhancedNumberValidator<short> >(new EnhancedNumberValidator<short>(0, 10));
00216   RCP<ArrayNumberValidator<short> > shortArrayVali = RCP<ArrayNumberValidator<short> >(new ArrayNumberValidator<short>(shortVali));
00217   TEST_ASSERT(shortVali.get() == shortArrayVali->getPrototype().get());
00218   Array<short> shortArray = tuple<short>(1,4,2,5);
00219   TEST_NOTHROW(shortList->set("short array param", shortArray, "short array parameter", shortArrayVali));
00220   Array<short> shortBadArray = tuple<short>(11,4,2,5);
00221   TEST_THROW(shortList->set("short bad array param", shortBadArray, "short bad array parameter", shortArrayVali), Exceptions::InvalidParameterValue);
00222   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", shortArrayVali), Exceptions::InvalidParameterType);
00223 
00224   /*
00225    * Testing Float ArrayValidator.
00226    */
00227   RCP<ParameterList> floatList = RCP<ParameterList>(new ParameterList("Float List"));
00228   RCP<EnhancedNumberValidator<float> > floatVali = RCP<EnhancedNumberValidator<float> >(new EnhancedNumberValidator<float>(0.0, 10.0));
00229   RCP<ArrayNumberValidator<float> > floatArrayVali = RCP<ArrayNumberValidator<float> >(new ArrayNumberValidator<float>(floatVali));
00230   TEST_ASSERT(floatVali.get() == floatArrayVali->getPrototype().get());
00231   Array<float> floatArray = tuple<float>(1.0,4.0,2.0,5.0);
00232   TEST_NOTHROW(floatList->set("float array param", floatArray, "float array parameter", floatArrayVali));
00233   Array<float> floatBadArray = tuple<float>(11.0,4.0,2.0,5.0);
00234   TEST_THROW(floatList->set("float bad array param", floatBadArray, "float bad array parameter", floatArrayVali), Exceptions::InvalidParameterValue);
00235   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", floatArrayVali), Exceptions::InvalidParameterType);
00236 
00237   /*
00238    * Testing Double ArrayValidator.
00239    */
00240   RCP<ParameterList> doubleList = RCP<ParameterList>(new ParameterList("Double List"));
00241   RCP<EnhancedNumberValidator<double> > doubleVali = RCP<EnhancedNumberValidator<double> >(new EnhancedNumberValidator<double>(0.0, 10.0));
00242   RCP<ArrayNumberValidator<double> > doubleArrayVali = RCP<ArrayNumberValidator<double> >(new ArrayNumberValidator<double>(doubleVali));
00243   TEST_ASSERT(doubleVali.get() == doubleArrayVali->getPrototype().get());
00244   Array<double> doubleArray = tuple<double>(1.0,4.0,2.0,5.0);
00245   TEST_NOTHROW(doubleList->set("double array param", doubleArray, "double array parameter", doubleArrayVali));
00246   Array<double> doubleBadArray = tuple<double>(11.0,4.0,2.0,5.0);
00247   TEST_THROW(doubleList->set("double bad array param", doubleBadArray, "double bad array parameter", doubleArrayVali), Exceptions::InvalidParameterValue);
00248   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", doubleArrayVali), Exceptions::InvalidParameterType);
00249 
00250   /*
00251    * Testing FileName ArrayValidator.
00252    */
00253   RCP<ParameterList> fileNameList = RCP<ParameterList>(new ParameterList("Filename List"));
00254   RCP<FileNameValidator> fileNameVali = RCP<FileNameValidator>(new FileNameValidator(true));
00255   RCP<ArrayFileNameValidator> arrayFileNameVali = RCP<ArrayFileNameValidator>(new ArrayFileNameValidator(fileNameVali));
00256   TEST_ASSERT(arrayFileNameVali->getPrototype().get() == fileNameVali.get());
00257   Array<std::string> fileNameArray = tuple<std::string>("testFile.txt", "testFile2.txt", "testFile3.txt");
00258   Array<std::string> fileNameBadArray = tuple<std::string>("doesnexist.txt", "testFile2.txt", "testFile3.txt");
00259   TEST_NOTHROW(fileNameList->set("File name array", fileNameArray, "file name array parameter", arrayFileNameVali));
00260   TEST_THROW(fileNameList->set("Bad File name array", fileNameBadArray, "bad file name array parameter", arrayFileNameVali), Exceptions::InvalidParameterValue);
00261   TEST_THROW(stringList->set("Long array param", longArray, "long array parameter", arrayFileNameVali), Exceptions::InvalidParameterType);
00262 
00263 
00264   return (success ? 0:1);
00265 }
00266 
00267 int main(int argc, char* argv[]){
00268   bool success = true;
00269   Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
00270   if(testNumberValis(*out) == 1){
00271     success = false;
00272   }
00273   if(testStringVali(*out) == 1){
00274     success = false;
00275   }
00276   if(testFileNameVali(*out) == 1){
00277     success = false;
00278   }
00279   if(testArrayValis(*out) == 1){
00280     success = false;
00281   }
00282   return (success ? 0:1);
00283 }
00284 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Generated on Wed Apr 13 10:05:58 2011 for Optika GUI Toolik by  doxygen 1.6.3