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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
20 
22 
23 
24 namespace Teuchos{
25 
26 
30 TEUCHOS_UNIT_TEST(Teuchos_Conditions, StringConditionSerialization){
32  std::string paramName1 = "string param";
33  std::string paramName2 = "string param2";
34  std::string dependent1Name = "dependent1";
35  std::string dependent2Name = "dependent2";
36  std::string paramValue = "cheese";
37  StringCondition::ValueList conditionVal1 = tuple<std::string>("steve");
38  StringCondition::ValueList conditionVal2 =
39  tuple<std::string>("steve", "blah", "your face");
40  ParameterList testList("Condition Test List");
41  testList.set(paramName1, paramValue);
42  testList.set(paramName2, paramValue);
43  testList.set(dependent1Name, paramValue);
44  testList.set(dependent2Name, paramValue);
45  RCP<StringCondition> simpleStringCon =
46  rcp(new StringCondition(testList.getEntryRCP(paramName1), conditionVal1));
47  RCP<StringCondition> complexStringCon =
48  rcp(new StringCondition(
49  testList.getEntryRCP(paramName2), conditionVal2));
50 
51  RCP<ConditionVisualDependency> simpleConDep =
53  simpleStringCon,
54  testList.getEntryRCP(dependent1Name)));
55 
56  RCP<ConditionVisualDependency> complexConDep =
58  complexStringCon,
59  testList.getEntryRCP(dependent2Name)));
60 
61  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
62  depSheet1->addDependency(simpleConDep);
63  depSheet1->addDependency(complexConDep);
64 
65  writeParameterListToXmlOStream(testList, out, depSheet1);
66 
67  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
68  RCP<ParameterList> readinList =
69  writeThenReadPL(testList, depSheet1, depSheetIn);
70 
71  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
72  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
73 
74  RCP<ConditionVisualDependency> simpleReadInDep =
75  rcp_dynamic_cast<ConditionVisualDependency>(
76  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
78  simpleReadInDep->getCondition()->getTypeAttributeValue(),
79  DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
80  RCP<const StringCondition> simpleReadInCon =
81  rcp_dynamic_cast<const StringCondition>(simpleReadInDep->getCondition(), true);
82  TEST_ASSERT(nonnull(simpleReadInCon));
83 
84 
85  RCP<ConditionVisualDependency> complexReadInDep =
86  rcp_dynamic_cast<ConditionVisualDependency>(
87  *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
89  complexReadInDep->getCondition()->getTypeAttributeValue(),
90  DummyObjectGetter<StringCondition>::getDummyObject()->getTypeAttributeValue());
91  RCP<const StringCondition> complexReadInCon =
92  rcp_dynamic_cast<const StringCondition>(complexReadInDep->getCondition(), true);
93  TEST_ASSERT(nonnull(complexReadInCon));
94 
95 
97  simpleReadInCon->getValueList(), simpleStringCon->getValueList());
99  complexReadInCon->getValueList(), complexStringCon->getValueList());
100 
101 }
102 
103 TEUCHOS_UNIT_TEST(Teuchos_Conditions, BoolConditionSerialization){
105  std::string paramName1 = "bool param";
106  std::string dependent1Name = "dependent1";
107  bool paramValue = true;
108  std::string dependentValue = "hi there!";
109  ParameterList testList("Condition Test List");
110  testList.set(paramName1, paramValue);
111  testList.set(dependent1Name, dependentValue);
112  RCP<BoolCondition> boolCon =
113  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
114 
115  RCP<ConditionVisualDependency> boolConDep =
117  boolCon,
118  testList.getEntryRCP(dependent1Name)));
119 
120  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
121  depSheet1->addDependency(boolConDep);
122 
123  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
124  RCP<ParameterList> readinList =
125  writeThenReadPL(testList, depSheet1, depSheetIn);
126 
127  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
128 
129  RCP<ConditionVisualDependency> simpleReadInDep =
130  rcp_dynamic_cast<ConditionVisualDependency>(
131  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
133  simpleReadInDep->getCondition()->getTypeAttributeValue(),
134  DummyObjectGetter<BoolCondition>::getDummyObject()->getTypeAttributeValue());
135  RCP<const BoolCondition> simpleReadInCon =
136  rcp_dynamic_cast<const BoolCondition>(simpleReadInDep->getCondition(), true);
137  TEST_ASSERT(nonnull(simpleReadInCon));
138 
139 }
140 
141 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, NumberConditionSerialization, T){
143  std::string paramName1 = "T param";
144  std::string paramName2 = "T param 2";
145  std::string dependent1Name = "dependent1";
146  std::string dependent2Name = "dependent2";
147  T paramValue = ScalarTraits< T >::one();
148  T ten = 10 * ScalarTraits< T >::one();
149  std::string dependentValue = "hi there!";
150  ParameterList testList("Condition Test List");
151  testList.set(paramName1, paramValue);
152  testList.set(paramName2, paramValue);
153  testList.set(dependent1Name, dependentValue);
154  testList.set(dependent2Name, dependentValue);
155 
156  RCP<NumberCondition< T > > numberCon =
157  rcp(new NumberCondition< T >(testList.getEntryRCP(paramName1)));
158 
159  RCP<SubtractionFunction< T > > funcTester =
160  rcp(new SubtractionFunction< T >(ten));
161 
162  RCP<NumberCondition< T > > numberFuncCon =
163  rcp(new NumberCondition< T >(testList.getEntryRCP(paramName2), funcTester));
164 
165  RCP<ConditionVisualDependency> numberConDep =
167  numberCon,
168  testList.getEntryRCP(dependent1Name)));
169 
170  RCP<ConditionVisualDependency> funcNumberConDep =
172  numberFuncCon,
173  testList.getEntryRCP(dependent2Name)));
174 
175  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
176  depSheet1->addDependency(numberConDep);
177  depSheet1->addDependency(funcNumberConDep);
178 
179  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
180  RCP<ParameterList> readinList =
181  writeThenReadPL(testList, depSheet1, depSheetIn);
182 
183  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
184  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
185 
186 
187 
188 
189  RCP<ConditionVisualDependency> simpleReadInDep =
190  rcp_dynamic_cast<ConditionVisualDependency>(
191  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
193  simpleReadInDep->getCondition()->getTypeAttributeValue(),
194  DummyObjectGetter<NumberCondition< T > >::getDummyObject()->getTypeAttributeValue());
195  RCP<const NumberCondition< T > > simpleReadInCon =
196  rcp_dynamic_cast<const NumberCondition< T > >(simpleReadInDep->getCondition(), true);
197  TEST_ASSERT(nonnull(simpleReadInCon));
198 
199 
200  RCP<ConditionVisualDependency> funcReadInDep =
201  rcp_dynamic_cast<ConditionVisualDependency>(
202  *(depSheetIn->getDependenciesForParameter(readInDependee2)->begin()));
203  TEST_ASSERT(funcReadInDep != null);
204 
205  RCP<const NumberCondition< T > > funcReadInCon =
206  rcp_dynamic_cast<const NumberCondition< T > >(funcReadInDep->getCondition());
207 
208  TEST_ASSERT(funcReadInCon != null);
209 
210  RCP<const SubtractionFunction< T > > funcReadInFunc =
211  rcp_dynamic_cast<const SubtractionFunction< T > >(
212  funcReadInCon->getFunctionObject());
213  TEST_ASSERT(funcReadInFunc != null);
215  funcReadInFunc->getModifiyingOperand(),
216  funcTester->getModifiyingOperand());
217 
218 
219 }
220 
221 #define NUMBER_PARAM_TYPE_TEST( T ) \
222 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, NumberConditionSerialization, T )
223 
224 typedef unsigned int uint;
225 typedef unsigned short ushort;
226 typedef unsigned long ulong;
227 
236 typedef long long int llint;
237 typedef unsigned long long int ullint;
240 
241 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition){
243  std::string paramName1 = "bool param1";
244  std::string paramName2 = "bool param2";
245  std::string dependent1Name = "dependent1";
246  bool paramValue1 = true;
247  bool paramValue2 = false;
248  std::string dependentValue = "hi there!";
249  ParameterList testList("Condition Test List");
250  testList.set(paramName1, paramValue1);
251  testList.set(paramName2, paramValue2);
252  testList.set(dependent1Name, dependentValue);
253  RCP<BoolCondition> boolCon1 =
254  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
255  RCP<BoolCondition> boolCon2 =
256  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
257 
259  tuple<RCP<const Condition> >(boolCon1, boolCon2);
260 
261  RCP< BinCondition > binCon = rcp(new BinCondition (conList));
262 
265  binCon,
266  testList.getEntryRCP(dependent1Name)));
267 
268  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
269  depSheet1->addDependency(binConDep);
270 
271  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
272  RCP<ParameterList> readinList =
273  writeThenReadPL(testList, depSheet1, depSheetIn);
274 
275  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
276  RCP<ParameterEntry> readInDependee2 = readinList->getEntryRCP(paramName2);
277 
278  RCP<ConditionVisualDependency> readInDep1 =
279  rcp_dynamic_cast<ConditionVisualDependency>(
280  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
281  RCP<ConditionVisualDependency> readInDep2 =
282  rcp_dynamic_cast<ConditionVisualDependency>(
283  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
284  TEST_EQUALITY(readInDep1.get(), readInDep1.get());
286  readInDep1->getCondition()->getTypeAttributeValue(),
287  DummyObjectGetter< BinCondition >::getDummyObject()->getTypeAttributeValue());
288  RCP<const BinCondition > readInCon =
289  rcp_dynamic_cast<const BinCondition >(readInDep1->getCondition(), true);
290  TEST_ASSERT(nonnull(readInCon));
291 
292  Condition::ConstConditionList readInConList = readInCon->getConditions();
293  TEST_ASSERT(readInConList.size() ==2);
294 
295 }
296 
297 #define BIN_CON_TEST( BinCondition ) \
298 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT(Teuchos_Conditions, BoolLogicConditionSerialization, BinCondition)
299 
300 BIN_CON_TEST(AndCondition)
301 BIN_CON_TEST(OrCondition)
302 BIN_CON_TEST(EqualsCondition)
303 
304 TEUCHOS_UNIT_TEST(Teuchos_Conditions, NotConditionSerialization){
306  std::string paramName1 = "bool param1";
307  std::string dependent1Name = "dependent1";
308  bool paramValue1 = true;
309  std::string dependentValue = "hi there!";
310  ParameterList testList("Condition Test List");
311  testList.set(paramName1, paramValue1);
312  testList.set(dependent1Name, dependentValue);
313  RCP<BoolCondition> boolCon1 =
314  rcp(new BoolCondition(testList.getEntryRCP(paramName1)));
315 
316 
317  RCP<NotCondition> notCon = rcp(new NotCondition(boolCon1));
318 
321  notCon,
322  testList.getEntryRCP(dependent1Name)));
323 
324  RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
325  depSheet1->addDependency(notConDep);
326 
327  RCP<DependencySheet> depSheetIn = rcp(new DependencySheet);
328  RCP<ParameterList> readinList =
329  writeThenReadPL(testList, depSheet1, depSheetIn);
330 
331  RCP<ParameterEntry> readInDependee1 = readinList->getEntryRCP(paramName1);
332 
333  RCP<ConditionVisualDependency> readInDep1 =
334  rcp_dynamic_cast<ConditionVisualDependency>(
335  *(depSheetIn->getDependenciesForParameter(readInDependee1)->begin()));
337  readInDep1->getCondition()->getTypeAttributeValue(),
338  DummyObjectGetter<NotCondition>::getDummyObject()->getTypeAttributeValue());
339  RCP<const NotCondition> readInCon =
340  rcp_dynamic_cast<const NotCondition>(readInDep1->getCondition(), true);
341  TEST_ASSERT(nonnull(readInCon));
342 }
343 
344 TEUCHOS_UNIT_TEST(Teuchos_Conditions, ConditionSerializationExceptions){
346  RCP<DependencySheet> depSheet = rcp(new DependencySheet);
347 
348 
349  TEST_THROW(RCP<ParameterList> missingParameterList =
350  getParametersFromXmlFile(
351  "MissingParameterEntryDefinition.xml", depSheet),
353 
354  RCP<ParameterEntry> notInListParam = rcp(new ParameterEntry(3.0));
355  RCP<NumberCondition<double> > doubleCon =
356  rcp(new NumberCondition<double>(notInListParam));
357 
358  NumberConditionConverter<double> doubleConConverter;
360  XMLObject toWriteTo;
361  TEST_THROW(doubleConConverter.fromConditiontoXML(doubleCon, emptyMap),
363 
364  TEST_THROW(RCP<ParameterList> missingValuesList =
365  getParametersFromXmlFile(
366  "MissingValuesTag.xml", depSheet),
368 
369 
370 }
371 
372 
373 } // 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.
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.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
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 ...