Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterList_SerializationTest.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 
15 #include "Teuchos_as.hpp"
20 #include "Teuchos_XMLParser.hpp"
21 #include "Teuchos_TwoDArray.hpp"
22 
24 
25 
26 namespace Teuchos {
27 
28 
29 #define ADD_TYPE_PARAMETER(T,VALUE) \
30  myList.set( #T , as<T>(VALUE));
31 const int g_arraySize = 5;
32 #define ADD_ARRAY_TYPE_PARAMETER(T,VALUE) \
33  myList.set( #T " Array", Array< T >(g_arraySize, ( T ) VALUE ));\
34  myList.set( #T " 2DArray", TwoDArray< T >(g_arraySize, g_arraySize, ( T ) VALUE ));
35 #define ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(T,VALUE) \
36  ADD_TYPE_PARAMETER(T,VALUE); \
37  ADD_ARRAY_TYPE_PARAMETER(T,VALUE);
38 
40 {
41  ParameterList myList;
43  TEST_EQUALITY( getParameter<int>(myList, "int"), 2 );
44  TEST_EQUALITY( getParameter<Array<int> >(myList, "int Array"),
45  Array<int>(g_arraySize, as<int>(2)) );
46  TEST_EQUALITY( getParameter<TwoDArray<int> >(myList, "int 2DArray"),
47  TwoDArray<int>(g_arraySize, g_arraySize, as<int>(2)) );
48 }
49 
50 TEUCHOS_UNIT_TEST(Teuchos_ParameterList, parameterEntryXMLConverters)
51 {
52  ParameterList myList;
54  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned int, 3);
56  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned short int, 5);
58  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned long int, 7);
59  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(long long int, 8);
60  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned long long int, 9);
63 
64  ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(std::string, "hello");
65 
66  ADD_TYPE_PARAMETER(char, 'a');
67  ADD_TYPE_PARAMETER(bool, true);
68 
69  RCP<ParameterList> readInPL = writeThenReadPL(myList);
70 
71  out << "\nmyList:\n";
72  myList.print(out);
73  out << "\n*readInPL:\n";
74  readInPL->print(out);
75 
76  TEST_ASSERT(haveSameValues(myList, *readInPL));
77 }
78 
79 TEUCHOS_UNIT_TEST(Teuchos_ParameterList, parameterEntryConverterExceptions)
80 {
81 
83  badRootElementList = getParametersFromXmlFile("BadRootElement.xml"),
85 
87  badParameterListElementList = getParametersFromXmlFile("BadParameterListElement.xml"),
89 
91  noNameAttributeList = getParametersFromXmlFile("NoNameAttribute.xml"),
93 
95  noTypeAttributeList = getParametersFromXmlFile("NoTypeAttribute.xml"),
97 
99  noValueAttributeList = getParametersFromXmlFile("NoValueAttribute.xml"),
101 
103  badIdsList = getParametersFromXmlFile("DuplicateParameterIDs.xml"),
105 
107  badParameterEntryConverterList = getParametersFromXmlFile("CantFindParameterEntryConverter.xml"),
109 
110 
111  #ifdef HAVE_TEUCHOS_DEBUG
112 
115  ValidatortoIDMap dummmyValidatorMap;
116  RCP<ParameterEntry> floatParameter = rcp(
117  new ParameterEntry((float)3.0));
118  TEST_THROW(intConverter.fromParameterEntrytoXML(floatParameter, "blah", 1, dummmyValidatorMap),
120 
121  XMLObject floatXML = floatConverter.fromParameterEntrytoXML(floatParameter, "float", 1, dummmyValidatorMap);
122  TEST_THROW(intConverter.fromXMLtoParameterEntry(floatXML),
124 
125  #endif
126 
127 }
128 
129 
130 } //namespace Teuchos
A thin wrapper around the Teuchos Array class that allows for 2 dimensional arrays.
void print() const
Print function to use in debugging in a debugger.
A collection of standard ValidatorXMLConverters.
#define TEST_ASSERT(v1)
Assert the given statement is true.
XMLObject fromParameterEntrytoXML(RCP< const ParameterEntry > entry, const std::string &name, const ParameterEntry::ParameterEntryID &id, const ValidatortoIDMap &validatorIDsMap) const
Converts the given parameter entry to xml.
#define ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(T, VALUE)
Thrown when a parameter entry tag is missing it&#39;s value attribute.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
#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 collection of standard ParameterEntryXMLConverters.
Thrown when a converter is being used to convert either and XML tag or ParameterEntry with an innappr...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Thrown when a parameter entry tag is missing it&#39;s name attribute.
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Thrown when an appropriate ParameterEntryXMLConverter can&#39;t be found.
Templated Parameter List class.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
Thrown when an element inside a parameter list is bad.
A database for ValidatorXMLConverters.
Writes a ParameterList to an XML object.
ParameterEntry fromXMLtoParameterEntry(const XMLObject &xmlObj) const
Converts the given xml into a parameter entry.
Unit testing support.
A standard ParameterEntryXMLConverter for most data types.
A class for mapping validators to integers.
Thrown when a parameter entry tag is missing it&#39;s type attribute.
A list of parameters of arbitrary type.
Thrown when the root xml tag for a parameter list is incorrect.
A collection of Exceptions that can be potentially thrown when converting a ParameterList to and from...
Definition of XMLInputStream derived class for reading XML from a std::string.
#define ADD_TYPE_PARAMETER(T, VALUE)
Thrown when two parameters in an XML file have the same ID.
Smart reference counting pointer class for automatic garbage collection.
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...
Definition of Teuchos::as, for conversions between types.
A class providing a simple XML parser. Methods can be overloaded to exploit external XML parsing libr...