Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Condition_Serialization_UnitTests.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
41 
52 
54 
55 
56 namespace Teuchos{
57 
58 
62 TEUCHOS_UNIT_TEST(Teuchos_Conditions, StringConditionSerialization){
64  std::string paramName1 = "string param";
65  std::string paramName2 = "string param2";
66  std::string dependent1Name = "dependent1";
67  std::string dependent2Name = "dependent2";
68  std::string paramValue = "cheese";
69  StringCondition::ValueList conditionVal1 = tuple<std::string>("steve");
70  StringCondition::ValueList conditionVal2 =
71  tuple<std::string>("steve", "blah", "your face");
72  ParameterList testList("Condition Test List");
73  testList.set(paramName1, paramValue);
74  testList.set(paramName2, paramValue);
75  testList.set(dependent1Name, paramValue);
76  testList.set(dependent2Name, paramValue);
77  RCP<StringCondition> simpleStringCon =
78  rcp(new StringCondition(testList.getEntryRCP(paramName1), conditionVal1));
79  RCP<StringCondition> complexStringCon =
80  rcp(new StringCondition(
81  testList.getEntryRCP(paramName2), conditionVal2));
82 
83  RCP<ConditionVisualDependency> simpleConDep =
85  simpleStringCon,
86  testList.getEntryRCP(dependent1Name)));
87 
88  RCP<ConditionVisualDependency> complexConDep =
90  complexStringCon,
91  testList.getEntryRCP(dependent2Name)));
92 
93  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
94  depSheet1->addDependency(simpleConDep);
95  depSheet1->addDependency(complexConDep);
96 
97  writeParameterListToXmlOStream(testList, out, depSheet1);
98 
99  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
100  RCP<ParameterList> readinList =
101  writeThenReadPL(testList, depSheet1, depSheetIn);
102 
103  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
104  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
105 
106  RCP<ConditionVisualDependency> simpleReadInDep =
107  rcp_dynamic_cast<ConditionVisualDependency>(
108  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
110  simpleReadInDep->getCondition()->getTypeAttributeValue(),
111  DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
112  RCP<const StringCondition> simpleReadInCon =
113  rcp_dynamic_cast<const StringCondition>(simpleReadInDep->getCondition(), true);
114  TEST_ASSERT(nonnull(simpleReadInCon));
115 
116 
117  RCP<ConditionVisualDependency> complexReadInDep =
118  rcp_dynamic_cast<ConditionVisualDependency>(
119  *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
121  complexReadInDep->getCondition()->getTypeAttributeValue(),
122  DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
123  RCP<const StringCondition> complexReadInCon =
124  rcp_dynamic_cast<const StringCondition>(complexReadInDep->getCondition(), true);
125  TEST_ASSERT(nonnull(complexReadInCon));
126 
127 
129  simpleReadInCon->getValueList(), simpleStringCon->getValueList());
131  complexReadInCon->getValueList(), complexStringCon->getValueList());
132 
133 }
134 
135 TEUCHOS_UNIT_TEST(Teuchos_Conditions, BoolConditionSerialization){
137  std::string paramName1 = "bool param";
138  std::string dependent1Name = "dependent1";
139  bool paramValue = true;
140  std::string dependentValue = "hi there!";
141  ParameterList testList("Condition Test List");
142  testList.set(paramName1, paramValue);
143  testList.set(dependent1Name, dependentValue);
144  RCP<BoolCondition> boolCon =
145  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
146 
147  RCP<ConditionVisualDependency> boolConDep =
149  boolCon,
150  testList.getEntryRCP(dependent1Name)));
151 
152  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
153  depSheet1->addDependency(boolConDep);
154 
155  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
156  RCP<ParameterList> readinList =
157  writeThenReadPL(testList, depSheet1, depSheetIn);
158 
159  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
160 
161  RCP<ConditionVisualDependency> simpleReadInDep =
162  rcp_dynamic_cast<ConditionVisualDependency>(
163  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
165  simpleReadInDep->getCondition()->getTypeAttributeValue(),
166  DummyObjectGetter<BoolCondition>::getDummyObject()->getTypeAttributeValue());
167  RCP<const BoolCondition> simpleReadInCon =
168  rcp_dynamic_cast<const BoolCondition>(simpleReadInDep->getCondition(), true);
169  TEST_ASSERT(nonnull(simpleReadInCon));
170 
171 }
172 
173 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, NumberConditionSerialization, T){
175  std::string paramName1 = "T param";
176  std::string paramName2 = "T param 2";
177  std::string dependent1Name = "dependent1";
178  std::string dependent2Name = "dependent2";
179  T paramValue = ScalarTraits< T >::one();
180  T ten = 10 * ScalarTraits< T >::one();
181  std::string dependentValue = "hi there!";
182  ParameterList testList("Condition Test List");
183  testList.set(paramName1, paramValue);
184  testList.set(paramName2, paramValue);
185  testList.set(dependent1Name, dependentValue);
186  testList.set(dependent2Name, dependentValue);
187 
188  RCP<NumberCondition< T > > numberCon =
189  rcp(new NumberCondition< T >(testList.getEntryRCP(paramName1)));
190 
191  RCP<SubtractionFunction< T > > funcTester =
192  rcp(new SubtractionFunction< T >(ten));
193 
194  RCP<NumberCondition< T > > numberFuncCon =
195  rcp(new NumberCondition< T >(testList.getEntryRCP(paramName2), funcTester));
196 
197  RCP<ConditionVisualDependency> numberConDep =
199  numberCon,
200  testList.getEntryRCP(dependent1Name)));
201 
202  RCP<ConditionVisualDependency> funcNumberConDep =
204  numberFuncCon,
205  testList.getEntryRCP(dependent2Name)));
206 
207  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
208  depSheet1->addDependency(numberConDep);
209  depSheet1->addDependency(funcNumberConDep);
210 
211  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
212  RCP<ParameterList> readinList =
213  writeThenReadPL(testList, depSheet1, depSheetIn);
214 
215  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
216  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
217 
218 
219 
220 
221  RCP<ConditionVisualDependency> simpleReadInDep =
222  rcp_dynamic_cast<ConditionVisualDependency>(
223  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
225  simpleReadInDep->getCondition()->getTypeAttributeValue(),
226  DummyObjectGetter<NumberCondition< T > >::getDummyObject()->getTypeAttributeValue());
227  RCP<const NumberCondition< T > > simpleReadInCon =
228  rcp_dynamic_cast<const NumberCondition< T > >(simpleReadInDep->getCondition(), true);
229  TEST_ASSERT(nonnull(simpleReadInCon));
230 
231 
232  RCP<ConditionVisualDependency> funcReadInDep =
233  rcp_dynamic_cast<ConditionVisualDependency>(
234  *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
235  TEST_ASSERT(funcReadInDep != null);
236 
237  RCP<const NumberCondition< T > > funcReadInCon =
238  rcp_dynamic_cast<const NumberCondition< T > >(funcReadInDep->getCondition());
239 
240  TEST_ASSERT(funcReadInCon != null);
241 
242  RCP<const SubtractionFunction< T > > funcReadInFunc =
243  rcp_dynamic_cast<const SubtractionFunction< T > >(
244  funcReadInCon->getFunctionObject());
245  TEST_ASSERT(funcReadInFunc != null);
247  funcReadInFunc->getModifiyingOperand(),
248  funcTester->getModifiyingOperand());
249 
250 
251 }
252 
253 #define NUMBER_PARAM_TYPE_TEST( T ) \
254 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, NumberConditionSerialization, T )
255 
256 typedef unsigned int uint;
257 typedef unsigned short ushort;
258 typedef unsigned long ulong;
259 
268 typedef long long int llint;
269 typedef unsigned long long int ullint;
272 
273 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition){
275  std::string paramName1 = "bool param1";
276  std::string paramName2 = "bool param2";
277  std::string dependent1Name = "dependent1";
278  bool paramValue1 = true;
279  bool paramValue2 = false;
280  std::string dependentValue = "hi there!";
281  ParameterList testList("Condition Test List");
282  testList.set(paramName1, paramValue1);
283  testList.set(paramName2, paramValue2);
284  testList.set(dependent1Name, dependentValue);
285  RCP<BoolCondition> boolCon1 =
286  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
287  RCP<BoolCondition> boolCon2 =
288  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
289 
291  tuple<RCP<const Condition> >(boolCon1, boolCon2);
292 
293  RCP< BinCondition > binCon = rcp(new BinCondition (conList));
294 
297  binCon,
298  testList.getEntryRCP(dependent1Name)));
299 
300  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
301  depSheet1->addDependency(binConDep);
302 
303  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
304  RCP<ParameterList> readinList =
305  writeThenReadPL(testList, depSheet1, depSheetIn);
306 
307  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
308  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
309 
310  RCP<ConditionVisualDependency> readInDep1 =
311  rcp_dynamic_cast<ConditionVisualDependency>(
312  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
313  RCP<ConditionVisualDependency> readInDep2 =
314  rcp_dynamic_cast<ConditionVisualDependency>(
315  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
316  TEST_EQUALITY(readInDep1.get(), readInDep1.get());
318  readInDep1->getCondition()->getTypeAttributeValue(),
319  DummyObjectGetter< BinCondition >::getDummyObject()->getTypeAttributeValue());
320  RCP<const BinCondition > readInCon =
321  rcp_dynamic_cast<const BinCondition >(readInDep1->getCondition(), true);
322  TEST_ASSERT(nonnull(readInCon));
323 
324  Condition::ConstConditionList readInConList = readInCon->getConditions();
325  TEST_ASSERT(readInConList.size() ==2);
326 
327 }
328 
329 #define BIN_CON_TEST( BinCondition ) \
330 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition)
331 
332 BIN_CON_TEST(AndCondition)
333 BIN_CON_TEST(OrCondition)
334 BIN_CON_TEST(EqualsCondition)
335 
336 TEUCHOS_UNIT_TEST(Teuchos_Conditions, NotConditionSerialization){
338  std::string paramName1 = "bool param1";
339  std::string dependent1Name = "dependent1";
340  bool paramValue1 = true;
341  std::string dependentValue = "hi there!";
342  ParameterList testList("Condition Test List");
343  testList.set(paramName1, paramValue1);
344  testList.set(dependent1Name, dependentValue);
345  RCP<BoolCondition> boolCon1 =
346  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
347 
348 
349  RCP<NotCondition> notCon = rcp(new NotCondition(boolCon1));
350 
353  notCon,
354  testList.getEntryRCP(dependent1Name)));
355 
356  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
357  depSheet1->addDependency(notConDep);
358 
359  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
360  RCP<ParameterList> readinList =
361  writeThenReadPL(testList, depSheet1, depSheetIn);
362 
363  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
364 
365  RCP<ConditionVisualDependency> readInDep1 =
366  rcp_dynamic_cast<ConditionVisualDependency>(
367  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
369  readInDep1->getCondition()->getTypeAttributeValue(),
370  DummyObjectGetter<NotCondition>::getDummyObject()->getTypeAttributeValue());
371  RCP<const NotCondition> readInCon =
372  rcp_dynamic_cast<const NotCondition>(readInDep1->getCondition(), true);
373  TEST_ASSERT(nonnull(readInCon));
374 }
375 
376 TEUCHOS_UNIT_TEST(Teuchos_Conditions, ConditionSerializationExceptions){
378  RCP<DependencySheet> depSheet = rcp(new DependencySheet);
379 
380 
381  TEST_THROW(RCP<ParameterList> missingParameterList =
382  getParametersFromXmlFile(
383  "MissingParameterEntryDefinition.xml", depSheet),
385 
386  RCP<ParameterEntry> notInListParam = rcp(new ParameterEntry(3.0));
387  RCP<NumberCondition<double> > doubleCon =
388  rcp(new NumberCondition<double>(notInListParam));
389 
390  NumberConditionConverter<double> doubleConConverter;
392  XMLObject toWriteTo;
393  TEST_THROW(doubleConConverter.fromConditiontoXML(doubleCon, emptyMap),
395 
396  TEST_THROW(RCP<ParameterList> missingValuesList =
397  getParametersFromXmlFile(
398  "MissingValuesTag.xml", depSheet),
400 
401 
402 }
403 
404 
405 } // namespace Teuchos
A Dependency sheet keeps track of dependencies between various ParameterEntries.
#define TEST_ASSERT(v1)
Assert the given statement is true.
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
RCP< ParameterEntry > getEntryRCP(const std::string &name)
Retrieves the RCP for an entry with the name name if it exists.
unsigned long long int ullint
A collection of Exceptions thrown when converting Conditions to and from XML.
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.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
static void printKnownConverters(std::ostream &out)
prints the xml tags associated with all known converters
#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)...
A Not condition returns the result of performing a logical NOT on a given condition.
T * get() const
Get the raw C++ pointer to the underlying object.
const ValueList & getValueList() const
Returns the value list being used with this StringCondition.
A Number Condition is a Parameter Condition that evaluates whether or not a number parameter is great...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
void addDependency(RCP< Dependency > dependency)
Adds a dependency to the sheet.
Thrown when a referenced ParameterEntry can&#39;t be found.
A simple function object that subtracts a specififed value from the given arguement in the runFunctio...
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(RCPNodeHandle, basicConstruct_owns_mem, T)
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Thrown when a StringConditon is missing it&#39;s Value tag.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
#define BIN_CON_TEST(BinCondition)
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
A database for ConditionXMLConverters.
A String Condition is a Parameter Condition that evaluates whether or not a string parameter has take...
std::map< RCP< const ParameterEntry >, ParameterEntry::ParameterEntryID, RCPConstComp > EntryIDsMap
Unit testing support.
An xml converter for NumberConditions The valid XML represntation for a NumberCondition is: ...
A list of parameters of arbitrary type.
A collection of standard dependencies.
#define NUMBER_PARAM_TYPE_TEST(T)
A Bool Condition is a Parameter Condition that evaluates whether or not a Boolean parameter is ture...
RCP< const Condition > getCondition() const
Gets the condition being used in this dependency.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
Class for retrieving a dummy object of type T.
A collection of Exceptions that can be potentially thrown when converting a ParameterList to and from...
size_type size() const
XMLObject fromConditiontoXML(const RCP< const Condition > condition, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
Converters a given ParameterEntryValidator to XML.
Smart reference counting pointer class for automatic garbage collection.
A condition visual dependency says the following about the relationship between elements in a Paramet...
A collection of standard ConditionXMLConverters.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT RCP< ParameterList > writeThenReadPL(ParameterList &myList)
Write a parameter list to xml and then read that xml back in via a string. The intent of this functio...
static T one()
Returns representation of one for this scalar type.
Standard Conditions to be used.
RCP< const DepSet > getDependenciesForParameter(RCP< const ParameterEntry > dependee) const
Returns a set of all the dependencies associated with a particular dependee. If no dependencies with ...