test/conditions/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_StandardConditions.hpp"
00031 #include "Optika_SpecificParameterEntryValidators.hpp"
00032 #include "Optika_StandardConditions.hpp"
00033 
00034 double doubleTesterFunc(double argument){
00035   return argument-100.0;
00036 }
00037 
00041 int testConditions(Teuchos::FancyOStream &out){
00042   bool success = true;
00043   //Settin up initial list
00044   Teuchos::RCP<Teuchos::ParameterList> testingList = Teuchos::rcp(new Teuchos::ParameterList("Condition Testing List"));
00045 
00046   /*
00047    * Testing for string condition
00048    */
00049   Optika::StringValidator::ValueList validValues(Teuchos::tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
00050   Teuchos::RCP<Optika::StringValidator> stringVali1 = Teuchos::rcp(new Optika::StringValidator(validValues));
00051 
00052   testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
00053 
00054   Optika::StringCondition::ValueList conValues1(Teuchos::tuple<std::string>("pepsi", "coke"));
00055   Teuchos::RCP<Optika::StringCondition> stringCon1 = Teuchos::rcp( new Optika::StringCondition("string param", testingList, conValues1));
00056   TEST_ASSERT(!stringCon1->isConditionTrue());
00057   testingList->set("string param", "coke");
00058   TEST_ASSERT(stringCon1->isConditionTrue());
00059   Teuchos::RCP<Optika::StringCondition> stringCon2 = Teuchos::rcp( new Optika::StringCondition("string param", testingList, conValues1, false));
00060   testingList->set("string param", "fanta");
00061   TEST_ASSERT(stringCon2->isConditionTrue());
00062   testingList->set("string param", "coke");
00063   TEST_ASSERT(!stringCon2->isConditionTrue());
00064 
00065   /*
00066    * Testing for number condition
00067    */
00068   testingList->set("double param", 5.0, "parameter for testing number conditions");
00069 
00070   Teuchos::RCP<Optika::NumberCondition<double> > numberCon1 = Teuchos::rcp( new Optika::NumberCondition<double>("double param", testingList, true));
00071   TEST_ASSERT(numberCon1->isConditionTrue());
00072   testingList->set("double param", -1.0);
00073   TEST_ASSERT(!numberCon1->isConditionTrue());
00074 
00075   Teuchos::RCP<Optika::NumberCondition<double> > numberCon2 = Teuchos::rcp( new Optika::NumberCondition<double>("double param", testingList, doubleTesterFunc, false));
00076   TEST_ASSERT(numberCon2->isConditionTrue());
00077   testingList->set("double param", 101.0);
00078   TEST_ASSERT(!numberCon2->isConditionTrue());
00079 
00080   /*
00081    * Testing bool conditions
00082    */
00083   testingList->set("bool param", true, "parameter for testing bool conditions");
00084 
00085   Teuchos::RCP<Optika::BoolCondition> boolCon1 = Teuchos::rcp( new Optika::BoolCondition("bool param", testingList));
00086   TEST_ASSERT(boolCon1->isConditionTrue());
00087   testingList->set("bool param", false);
00088   TEST_ASSERT(!boolCon1->isConditionTrue());
00089 
00090   Teuchos::RCP<Optika::BoolCondition> boolCon2 = Teuchos::rcp( new Optika::BoolCondition("bool param", testingList, false));
00091   TEST_ASSERT(boolCon2->isConditionTrue());
00092   testingList->set("bool param", true);
00093   TEST_ASSERT(!boolCon2->isConditionTrue());
00094 
00095   /*
00096    * Test Not condition
00097    */
00098   Teuchos::RCP<Optika::NotCondition> notCon1 = Teuchos::rcp(new Optika::NotCondition(numberCon1));
00099   TEST_ASSERT(!notCon1->isConditionTrue());
00100   testingList->set("double param", -1.0);
00101   TEST_ASSERT(notCon1->isConditionTrue());
00102 
00103   /*
00104    * Test And condition
00105    */
00106   Optika::Condition::ConditionList conList1(Teuchos::tuple<Teuchos::RCP<Optika::Condition> >(stringCon1, boolCon1));
00107   Teuchos::RCP<Optika::AndCondition> andCon1 = Teuchos::rcp(new Optika::AndCondition(conList1));
00108   TEST_ASSERT(andCon1->isConditionTrue());
00109   Optika::Condition::ConditionList conList2(Teuchos::tuple<Teuchos::RCP<Optika::Condition> >(stringCon1, boolCon2));
00110   Teuchos::RCP<Optika::AndCondition> andCon2 = Teuchos::rcp(new Optika::AndCondition(conList2));
00111   TEST_ASSERT(!andCon2->isConditionTrue());
00112   Optika::Condition::ConditionList conList3(Teuchos::tuple<Teuchos::RCP<Optika::Condition> >(stringCon2, boolCon2));
00113   Teuchos::RCP<Optika::AndCondition> andCon3 = Teuchos::rcp(new Optika::AndCondition(conList3));
00114   TEST_ASSERT(!andCon3->isConditionTrue());
00115 
00116   /*
00117    * Testing or condition
00118    */
00119   Teuchos::RCP<Optika::OrCondition> orCon1 = Teuchos::rcp(new Optika::OrCondition(conList1));
00120   TEST_ASSERT(orCon1->isConditionTrue());
00121   Teuchos::RCP<Optika::OrCondition> orCon2 = Teuchos::rcp(new Optika::OrCondition(conList2));
00122   TEST_ASSERT(orCon2->isConditionTrue());
00123   Teuchos::RCP<Optika::OrCondition> orCon3 = Teuchos::rcp(new Optika::OrCondition(conList3));
00124   TEST_ASSERT(!orCon3->isConditionTrue());
00125 
00126   /*
00127    * Testing equal condition
00128    */
00129   Teuchos::RCP<Optika::EqualsCondition> equalsCon1 = Teuchos::rcp(new Optika::EqualsCondition(conList1));
00130   TEST_ASSERT(equalsCon1->isConditionTrue());
00131   Teuchos::RCP<Optika::EqualsCondition> equalsCon2 = Teuchos::rcp(new Optika::EqualsCondition(conList2));
00132   TEST_ASSERT(!equalsCon2->isConditionTrue());
00133   Teuchos::RCP<Optika::EqualsCondition> equalsCon3 = Teuchos::rcp(new Optika::EqualsCondition(conList3));
00134   TEST_ASSERT(equalsCon3->isConditionTrue());
00135 
00136   return (success ? 0:1);
00137 }
00138 
00139 //Test getters and setters
00140 int testConditionGetterAndSetters(Teuchos::FancyOStream &out){
00141   bool success = true;
00142   //Settin up initial list
00143   Teuchos::RCP<Teuchos::ParameterList> testingList = Teuchos::rcp(new Teuchos::ParameterList("Condition Testing List"));
00144 
00145   Optika::StringValidator::ValueList validValues(Teuchos::tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
00146   Teuchos::RCP<Optika::StringValidator> stringVali1 = Teuchos::rcp(new Optika::StringValidator(validValues));
00147 
00148   testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
00149 
00150   Optika::StringCondition::ValueList conValues1(Teuchos::tuple<std::string>("pepsi", "coke"));
00151   Teuchos::RCP<Optika::StringCondition> stringCon1 = Teuchos::rcp( new Optika::StringCondition("string param", testingList, conValues1));
00152   TEST_ASSERT(stringCon1->getType() == Optika::Condition::ParamCon);
00153   Optika::Dependency::ParameterParentMap stringParameters = stringCon1->getAllParameters();
00154   TEST_ASSERT(stringParameters.size() == 1);
00155   TEST_ASSERT(stringParameters.find("string param") != stringParameters.end());
00156 
00157   /*
00158    * Testing for number condition
00159    */
00160   testingList->set("double param", 5.0, "parameter for testing number conditions");
00161 
00162   Teuchos::RCP<Optika::NumberCondition<double> > numberCon1 = Teuchos::rcp( new Optika::NumberCondition<double>("double param", testingList, true));
00163   TEST_ASSERT(numberCon1->getType() == Optika::Condition::ParamCon);
00164   Optika::Dependency::ParameterParentMap numberParameters = numberCon1->getAllParameters();
00165   TEST_ASSERT(numberParameters.size() == 1);
00166   TEST_ASSERT(numberParameters.find("double param") != numberParameters.end());
00167 
00168   /*
00169    * Testing bool conditions
00170    */
00171   testingList->set("bool param", true, "parameter for testing bool conditions");
00172 
00173   Teuchos::RCP<Optika::BoolCondition> boolCon1 = Teuchos::rcp( new Optika::BoolCondition("bool param", testingList));
00174   TEST_ASSERT(boolCon1->getType() == Optika::Condition::ParamCon);
00175   Optika::Dependency::ParameterParentMap boolParameters = boolCon1->getAllParameters();
00176   TEST_ASSERT(boolParameters.size() == 1);
00177   TEST_ASSERT(boolParameters.find("bool param") != boolParameters.end());
00178 
00179   /*
00180    * Test Not condition
00181    */
00182   Teuchos::RCP<Optika::NotCondition> notCon1 = Teuchos::rcp(new Optika::NotCondition(numberCon1));
00183   TEST_ASSERT(notCon1->getType() == Optika::Condition::NotCon);
00184   Optika::Dependency::ParameterParentMap notParameters = notCon1->getAllParameters();
00185   TEST_ASSERT(notParameters.size() == 1);
00186   TEST_ASSERT(notParameters.find("double param") != notParameters.end());
00187 
00188   /*
00189    * Test And condition
00190    */
00191   Optika::Condition::ConditionList conList1(Teuchos::tuple<Teuchos::RCP<Optika::Condition> >(stringCon1, boolCon1));
00192   Teuchos::RCP<Optika::AndCondition> andCon1 = Teuchos::rcp(new Optika::AndCondition(conList1));
00193   TEST_ASSERT(andCon1->getType() == Optika::Condition::BinLogicCon);
00194   Optika::Dependency::ParameterParentMap andParameters = andCon1->getAllParameters();
00195   TEST_ASSERT(andParameters.size() == 2);
00196   TEST_ASSERT(andParameters.find("string param") != andParameters.end());
00197   TEST_ASSERT(andParameters.find("bool param") != andParameters.end());
00198 
00199   /*
00200    * Testing or condition
00201    */
00202   Teuchos::RCP<Optika::OrCondition> orCon1 = Teuchos::rcp(new Optika::OrCondition(conList1));
00203   TEST_ASSERT(orCon1->getType() == Optika::Condition::BinLogicCon);
00204   Optika::Dependency::ParameterParentMap orParameters = orCon1->getAllParameters();
00205   TEST_ASSERT(orParameters.size() == 2);
00206   TEST_ASSERT(orParameters.find("string param") != orParameters.end());
00207   TEST_ASSERT(orParameters.find("bool param") != orParameters.end());
00208 
00209   /*
00210    * Testing Equsl condition
00211    */
00212   Optika::Condition::ConditionList conList2(Teuchos::tuple<Teuchos::RCP<Optika::Condition> >(numberCon1, boolCon1));
00213   Teuchos::RCP<Optika::EqualsCondition> equalsCon1 = Teuchos::rcp(new Optika::EqualsCondition(conList2));
00214   TEST_ASSERT(equalsCon1->getType() == Optika::Condition::BinLogicCon);
00215   Optika::Dependency::ParameterParentMap equalsParameters = equalsCon1->getAllParameters();
00216   TEST_ASSERT(equalsParameters.size() == 2);
00217   TEST_ASSERT(equalsParameters.find("double param") != equalsParameters.end());
00218   TEST_ASSERT(equalsParameters.find("bool param") != equalsParameters.end());
00219 
00220   /*
00221    * Testing BinaryLogicCondition add
00222    */
00223   equalsCon1->addCondition(orCon1);
00224   Optika::Dependency::ParameterParentMap equalsParameters2 = equalsCon1->getAllParameters();
00225   TEST_ASSERT(equalsParameters2.size() == 3);
00226   TEST_ASSERT(equalsParameters2.find("double param") != equalsParameters2.end());
00227   TEST_ASSERT(equalsParameters2.find("bool param") != equalsParameters2.end());
00228   TEST_ASSERT(equalsParameters2.find("string param") != equalsParameters2.end());
00229 
00230 
00231 
00232   return (success ? 0:1);
00233 }
00234 
00235 //Test that exceptions get thrown when they should.
00236 int testConditionException(Teuchos::FancyOStream &out){
00237   bool success = true;
00238   //Settin up initial list
00239   Teuchos::RCP<Teuchos::ParameterList> testingList = Teuchos::rcp(new Teuchos::ParameterList("Condition Testing List"));
00240   testingList->set("double param",1.0);
00241   testingList->set("string param", "awesome");
00242   Teuchos::RCP<Teuchos::ParameterList> testingList2 = Teuchos::rcp(new Teuchos::ParameterList("Condition Testing List"));
00243   testingList2->set("bool param", true);
00244 
00245   TEST_THROW(Optika::BoolCondition boolCon1("bool param", testingList), Optika::InvalidConditionException);
00246   TEST_THROW(Optika::StringCondition stringCon1("double param", testingList, "coke"), Optika::InvalidConditionException);
00247   TEST_THROW(Optika::NumberCondition<double> doubleCon1("string param", testingList, true), Optika::InvalidConditionException);
00248   TEST_THROW(Optika::BoolCondition boolCon1("double param", testingList), Optika::InvalidConditionException);
00249   Optika::Condition::ConditionList conList1;
00250   TEST_THROW(Optika::AndCondition andCon1(conList1), Optika::InvalidConditionException);
00251   Teuchos::RCP<Optika::Condition> con1;
00252   TEST_THROW(Optika::NotCondition notCon1(con1), Optika::InvalidConditionException);
00253 
00254   return (success ? 0:1);
00255 }
00256 
00257 int main(int argc, char* argv[]){
00258   bool success = true;
00259   Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
00260   if(testConditions(*out) == 1){
00261     success = false;
00262   }
00263 
00264   if(testConditionGetterAndSetters(*out) == 1){
00265     success = false;
00266   }
00267 
00268   return (success ? 0:1);
00269 }
00270 
00271 
 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