Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Condition_Serialization_UnitTests.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_UnitTestHarness.hpp"
00043 #include "Teuchos_VerboseObject.hpp"
00044 #include "Teuchos_ParameterList.hpp"
00045 #include "Teuchos_StandardConditions.hpp"
00046 #include "Teuchos_ConditionXMLConverterDB.hpp"
00047 #include "Teuchos_StandardDependencies.hpp"
00048 #include "Teuchos_XMLParameterListCoreHelpers.hpp"
00049 #include "Teuchos_XMLParameterListExceptions.hpp"
00050 #include "Teuchos_StandardConditionXMLConverters.hpp"
00051 #include "Teuchos_XMLConditionExceptions.hpp"
00052 
00053 #include "Teuchos_XMLParameterListTestHelpers.hpp"
00054 
00055 
00056 namespace Teuchos{
00057 
00058 
00062 TEUCHOS_UNIT_TEST(Teuchos_Conditions, StringConditionSerialization){
00063   ConditionXMLConverterDB::printKnownConverters(out);
00064   std::string paramName1 = "string param";
00065   std::string paramName2 = "string param2";
00066   std::string dependent1Name = "dependent1";
00067   std::string dependent2Name = "dependent2";
00068   std::string paramValue = "cheese";
00069   StringCondition::ValueList conditionVal1 = tuple<std::string>("steve");
00070   StringCondition::ValueList conditionVal2 = 
00071     tuple<std::string>("steve", "blah", "your face");
00072   ParameterList testList("Condition Test List");
00073   testList.set(paramName1, paramValue); 
00074   testList.set(paramName2, paramValue);
00075   testList.set(dependent1Name, paramValue);
00076   testList.set(dependent2Name, paramValue);
00077   RCP<StringCondition> simpleStringCon = 
00078     rcp(new StringCondition(testList.getEntryRCP(paramName1), conditionVal1));
00079   RCP<StringCondition> complexStringCon = 
00080     rcp(new StringCondition(
00081       testList.getEntryRCP(paramName2), conditionVal2));
00082   
00083   RCP<ConditionVisualDependency> simpleConDep = 
00084     rcp(new ConditionVisualDependency(
00085       simpleStringCon, 
00086       testList.getEntryRCP(dependent1Name)));
00087 
00088   RCP<ConditionVisualDependency> complexConDep = 
00089     rcp(new ConditionVisualDependency(
00090       complexStringCon, 
00091       testList.getEntryRCP(dependent2Name)));
00092  
00093   RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
00094   depSheet1->addDependency(simpleConDep);
00095   depSheet1->addDependency(complexConDep);
00096 
00097   writeParameterListToXmlOStream(testList, out, depSheet1);
00098 
00099   RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
00100   RCP<ParameterList> readinList = 
00101     writeThenReadPL(testList, depSheet1, depSheetIn);
00102   
00103   RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
00104   RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
00105 
00106   RCP<ConditionVisualDependency> simpleReadInDep = 
00107     rcp_dynamic_cast<ConditionVisualDependency>(
00108       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00109   TEST_EQUALITY(
00110     simpleReadInDep->getCondition()->getTypeAttributeValue(),
00111     DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
00112   RCP<const StringCondition> simpleReadInCon = 
00113     rcp_dynamic_cast<const StringCondition>(simpleReadInDep->getCondition(), true);
00114   TEST_ASSERT(nonnull(simpleReadInCon));
00115 
00116 
00117   RCP<ConditionVisualDependency> complexReadInDep = 
00118     rcp_dynamic_cast<ConditionVisualDependency>(
00119       *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
00120   TEST_EQUALITY(
00121     complexReadInDep->getCondition()->getTypeAttributeValue(),
00122     DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
00123   RCP<const StringCondition> complexReadInCon = 
00124     rcp_dynamic_cast<const StringCondition>(complexReadInDep->getCondition(), true);
00125   TEST_ASSERT(nonnull(complexReadInCon));
00126  
00127     
00128   TEST_COMPARE_ARRAYS(
00129     simpleReadInCon->getValueList(), simpleStringCon->getValueList());
00130   TEST_COMPARE_ARRAYS(
00131     complexReadInCon->getValueList(), complexStringCon->getValueList());
00132 
00133 }
00134 
00135 TEUCHOS_UNIT_TEST(Teuchos_Conditions, BoolConditionSerialization){
00136   ConditionXMLConverterDB::printKnownConverters(out);
00137   std::string paramName1 = "bool param";
00138   std::string dependent1Name = "dependent1";
00139   bool paramValue = true;
00140   std::string dependentValue = "hi there!";
00141   ParameterList testList("Condition Test List");
00142   testList.set(paramName1, paramValue); 
00143   testList.set(dependent1Name, dependentValue);
00144   RCP<BoolCondition> boolCon = 
00145     rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
00146   
00147   RCP<ConditionVisualDependency> boolConDep = 
00148     rcp(new ConditionVisualDependency(
00149       boolCon, 
00150       testList.getEntryRCP(dependent1Name)));
00151 
00152   RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
00153   depSheet1->addDependency(boolConDep);
00154 
00155   RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
00156   RCP<ParameterList> readinList = 
00157     writeThenReadPL(testList, depSheet1, depSheetIn);
00158   
00159   RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
00160 
00161   RCP<ConditionVisualDependency> simpleReadInDep = 
00162     rcp_dynamic_cast<ConditionVisualDependency>(
00163       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00164   TEST_EQUALITY(
00165     simpleReadInDep->getCondition()->getTypeAttributeValue(),
00166     DummyObjectGetter<BoolCondition>::getDummyObject()->getTypeAttributeValue());
00167   RCP<const BoolCondition> simpleReadInCon = 
00168     rcp_dynamic_cast<const BoolCondition>(simpleReadInDep->getCondition(), true);
00169   TEST_ASSERT(nonnull(simpleReadInCon));
00170 
00171 }
00172 
00173 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, NumberConditionSerialization, T){
00174   ConditionXMLConverterDB::printKnownConverters(out);
00175   std::string paramName1 = "T param";
00176   std::string paramName2 = "T param 2";
00177   std::string dependent1Name = "dependent1";
00178   std::string dependent2Name = "dependent2";
00179   T paramValue = ScalarTraits< T >::one();
00180   T ten = 10 * ScalarTraits< T >::one();
00181   std::string dependentValue = "hi there!";
00182   ParameterList testList("Condition Test List");
00183   testList.set(paramName1, paramValue); 
00184   testList.set(paramName2, paramValue); 
00185   testList.set(dependent1Name, dependentValue);
00186   testList.set(dependent2Name, dependentValue);
00187 
00188   RCP<NumberCondition< T > > numberCon = 
00189     rcp(new NumberCondition< T >(testList.getEntryRCP(paramName1)));
00190 
00191   RCP<SubtractionFunction< T > > funcTester = 
00192     rcp(new SubtractionFunction< T >(ten));
00193 
00194   RCP<NumberCondition< T > > numberFuncCon = 
00195     rcp(new NumberCondition< T >(testList.getEntryRCP(paramName2), funcTester));
00196   
00197   RCP<ConditionVisualDependency> numberConDep = 
00198     rcp(new ConditionVisualDependency(
00199       numberCon, 
00200       testList.getEntryRCP(dependent1Name)));
00201 
00202   RCP<ConditionVisualDependency> funcNumberConDep = 
00203     rcp(new ConditionVisualDependency(
00204       numberFuncCon, 
00205       testList.getEntryRCP(dependent2Name)));
00206 
00207   RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
00208   depSheet1->addDependency(numberConDep);
00209   depSheet1->addDependency(funcNumberConDep);
00210 
00211   RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
00212   RCP<ParameterList> readinList = 
00213     writeThenReadPL(testList, depSheet1, depSheetIn);
00214   
00215   RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
00216   RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
00217 
00218 
00219 
00220 
00221   RCP<ConditionVisualDependency> simpleReadInDep = 
00222     rcp_dynamic_cast<ConditionVisualDependency>(
00223       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00224   TEST_EQUALITY(
00225     simpleReadInDep->getCondition()->getTypeAttributeValue(),
00226     DummyObjectGetter<NumberCondition< T > >::getDummyObject()->getTypeAttributeValue());
00227   RCP<const NumberCondition< T > > simpleReadInCon = 
00228     rcp_dynamic_cast<const NumberCondition< T > >(simpleReadInDep->getCondition(), true);
00229   TEST_ASSERT(nonnull(simpleReadInCon));
00230 
00231 
00232   RCP<ConditionVisualDependency> funcReadInDep = 
00233     rcp_dynamic_cast<ConditionVisualDependency>(
00234       *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
00235   TEST_ASSERT(funcReadInDep != null);
00236 
00237   RCP<const NumberCondition< T > > funcReadInCon = 
00238     rcp_dynamic_cast<const NumberCondition< T > >(funcReadInDep->getCondition());
00239 
00240   TEST_ASSERT(funcReadInCon != null);
00241 
00242   RCP<const SubtractionFunction< T > > funcReadInFunc = 
00243     rcp_dynamic_cast<const SubtractionFunction< T > >(
00244       funcReadInCon->getFunctionObject());
00245   TEST_ASSERT(funcReadInFunc != null);
00246   TEST_EQUALITY(
00247     funcReadInFunc->getModifiyingOperand(),
00248     funcTester->getModifiyingOperand());
00249 
00250 
00251 }
00252 
00253 #define NUMBER_PARAM_TYPE_TEST( T ) \
00254 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, NumberConditionSerialization, T )
00255 
00256 typedef unsigned int uint;
00257 typedef unsigned short ushort;
00258 typedef unsigned long ulong;
00259 
00260 NUMBER_PARAM_TYPE_TEST(int)
00261 NUMBER_PARAM_TYPE_TEST(uint)
00262 NUMBER_PARAM_TYPE_TEST(short)
00263 NUMBER_PARAM_TYPE_TEST(ushort)
00264 NUMBER_PARAM_TYPE_TEST(long)
00265 NUMBER_PARAM_TYPE_TEST(ulong)
00266 NUMBER_PARAM_TYPE_TEST(float)
00267 NUMBER_PARAM_TYPE_TEST(double)
00268 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00269 typedef long long int llint;
00270 typedef unsigned long long int ullint;
00271 NUMBER_PARAM_TYPE_TEST(llint)
00272 NUMBER_PARAM_TYPE_TEST(ullint)
00273 #endif
00274 
00275 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition){
00276   ConditionXMLConverterDB::printKnownConverters(out);
00277   std::string paramName1 = "bool param1";
00278   std::string paramName2 = "bool param2";
00279   std::string dependent1Name = "dependent1";
00280   bool paramValue1 = true;
00281   bool paramValue2 = false;
00282   std::string dependentValue = "hi there!";
00283   ParameterList testList("Condition Test List");
00284   testList.set(paramName1, paramValue1); 
00285   testList.set(paramName2, paramValue2); 
00286   testList.set(dependent1Name, dependentValue);
00287   RCP<BoolCondition> boolCon1 = 
00288     rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
00289   RCP<BoolCondition> boolCon2 = 
00290     rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
00291 
00292   Condition::ConstConditionList conList = 
00293     tuple<RCP<const Condition> >(boolCon1, boolCon2);
00294 
00295   RCP< BinCondition > binCon = rcp(new BinCondition (conList));
00296   
00297   RCP<ConditionVisualDependency> binConDep = 
00298     rcp(new ConditionVisualDependency(
00299       binCon, 
00300       testList.getEntryRCP(dependent1Name)));
00301 
00302   RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
00303   depSheet1->addDependency(binConDep);
00304 
00305   RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
00306   RCP<ParameterList> readinList = 
00307     writeThenReadPL(testList, depSheet1, depSheetIn);
00308   
00309   RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
00310   RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
00311 
00312   RCP<ConditionVisualDependency> readInDep1 = 
00313     rcp_dynamic_cast<ConditionVisualDependency>(
00314       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00315   RCP<ConditionVisualDependency> readInDep2 = 
00316     rcp_dynamic_cast<ConditionVisualDependency>(
00317       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00318   TEST_EQUALITY(readInDep1.get(), readInDep1.get());
00319   TEST_EQUALITY(
00320     readInDep1->getCondition()->getTypeAttributeValue(),
00321     DummyObjectGetter< BinCondition >::getDummyObject()->getTypeAttributeValue());
00322   RCP<const BinCondition > readInCon = 
00323     rcp_dynamic_cast<const BinCondition >(readInDep1->getCondition(), true);
00324   TEST_ASSERT(nonnull(readInCon));
00325 
00326   Condition::ConstConditionList readInConList = readInCon->getConditions();
00327   TEST_ASSERT(readInConList.size() ==2);
00328 
00329 }
00330 
00331 #define BIN_CON_TEST( BinCondition ) \
00332 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition)
00333 
00334 BIN_CON_TEST(AndCondition)
00335 BIN_CON_TEST(OrCondition)
00336 BIN_CON_TEST(EqualsCondition)
00337 
00338 TEUCHOS_UNIT_TEST(Teuchos_Conditions, NotConditionSerialization){
00339   ConditionXMLConverterDB::printKnownConverters(out);
00340   std::string paramName1 = "bool param1";
00341   std::string dependent1Name = "dependent1";
00342   bool paramValue1 = true;
00343   std::string dependentValue = "hi there!";
00344   ParameterList testList("Condition Test List");
00345   testList.set(paramName1, paramValue1); 
00346   testList.set(dependent1Name, dependentValue);
00347   RCP<BoolCondition> boolCon1 = 
00348     rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
00349 
00350 
00351   RCP<NotCondition> notCon = rcp(new NotCondition(boolCon1));
00352   
00353   RCP<ConditionVisualDependency> notConDep = 
00354     rcp(new ConditionVisualDependency(
00355       notCon, 
00356       testList.getEntryRCP(dependent1Name)));
00357 
00358   RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
00359   depSheet1->addDependency(notConDep);
00360 
00361   RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
00362   RCP<ParameterList> readinList = 
00363     writeThenReadPL(testList, depSheet1, depSheetIn);
00364   
00365   RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
00366 
00367   RCP<ConditionVisualDependency> readInDep1 = 
00368     rcp_dynamic_cast<ConditionVisualDependency>(
00369       *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
00370   TEST_EQUALITY(
00371     readInDep1->getCondition()->getTypeAttributeValue(),
00372     DummyObjectGetter<NotCondition>::getDummyObject()->getTypeAttributeValue());
00373   RCP<const NotCondition> readInCon = 
00374     rcp_dynamic_cast<const NotCondition>(readInDep1->getCondition(), true);
00375   TEST_ASSERT(nonnull(readInCon));
00376 }
00377 
00378 TEUCHOS_UNIT_TEST(Teuchos_Conditions, ConditionSerializationExceptions){
00379   ConditionXMLConverterDB::printKnownConverters(out);
00380   RCP<DependencySheet> depSheet = rcp(new DependencySheet);
00381 
00382 
00383   TEST_THROW(RCP<ParameterList> missingParameterList = 
00384     getParametersFromXmlFile(
00385       "MissingParameterEntryDefinition.xml", depSheet),
00386     MissingParameterEntryDefinitionException);
00387     
00388   RCP<ParameterEntry> notInListParam = rcp(new ParameterEntry(3.0));
00389   RCP<NumberCondition<double> > doubleCon =
00390     rcp(new NumberCondition<double>(notInListParam));
00391 
00392   NumberConditionConverter<double> doubleConConverter;
00393   XMLParameterListWriter::EntryIDsMap emptyMap;
00394   XMLObject toWriteTo;
00395   TEST_THROW(doubleConConverter.fromConditiontoXML(doubleCon, emptyMap),
00396     MissingParameterEntryDefinitionException);
00397 
00398   TEST_THROW(RCP<ParameterList> missingValuesList = 
00399     getParametersFromXmlFile(
00400       "MissingValuesTag.xml", depSheet),
00401     MissingValuesTagException);
00402 
00403 
00404 }
00405 
00406 
00407 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines