Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
ParameterList_SerializationTest.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
47#include "Teuchos_as.hpp"
52#include "Teuchos_XMLParser.hpp"
53#include "Teuchos_TwoDArray.hpp"
54
56
57
58namespace Teuchos {
59
60
61#define ADD_TYPE_PARAMETER(T,VALUE) \
62 myList.set( #T , as<T>(VALUE));
63const int g_arraySize = 5;
64#define ADD_ARRAY_TYPE_PARAMETER(T,VALUE) \
65 myList.set( #T " Array", Array< T >(g_arraySize, ( T ) VALUE ));\
66 myList.set( #T " 2DArray", TwoDArray< T >(g_arraySize, g_arraySize, ( T ) VALUE ));
67#define ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(T,VALUE) \
68 ADD_TYPE_PARAMETER(T,VALUE); \
69 ADD_ARRAY_TYPE_PARAMETER(T,VALUE);
70
72{
73 ParameterList myList;
75 TEST_EQUALITY( getParameter<int>(myList, "int"), 2 );
76 TEST_EQUALITY( getParameter<Array<int> >(myList, "int Array"),
77 Array<int>(g_arraySize, as<int>(2)) );
78 TEST_EQUALITY( getParameter<TwoDArray<int> >(myList, "int 2DArray"),
80}
81
82TEUCHOS_UNIT_TEST(Teuchos_ParameterList, parameterEntryXMLConverters)
83{
84 ParameterList myList;
86 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned int, 3);
88 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned short int, 5);
90 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned long int, 7);
91 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(long long int, 8);
92 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(unsigned long long int, 9);
95
96 ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(std::string, "hello");
97
98 ADD_TYPE_PARAMETER(char, 'a');
99 ADD_TYPE_PARAMETER(bool, true);
100
101 RCP<ParameterList> readInPL = writeThenReadPL(myList);
102
103 out << "\nmyList:\n";
104 myList.print(out);
105 out << "\n*readInPL:\n";
106 readInPL->print(out);
107
108 TEST_ASSERT(haveSameValues(myList, *readInPL));
109}
110
111TEUCHOS_UNIT_TEST(Teuchos_ParameterList, parameterEntryConverterExceptions)
112{
113
115 badRootElementList = getParametersFromXmlFile("BadRootElement.xml"),
117
119 badParameterListElementList = getParametersFromXmlFile("BadParameterListElement.xml"),
121
123 noNameAttributeList = getParametersFromXmlFile("NoNameAttribute.xml"),
125
127 noTypeAttributeList = getParametersFromXmlFile("NoTypeAttribute.xml"),
129
131 noValueAttributeList = getParametersFromXmlFile("NoValueAttribute.xml"),
133
135 badIdsList = getParametersFromXmlFile("DuplicateParameterIDs.xml"),
137
139 badParameterEntryConverterList = getParametersFromXmlFile("CantFindParameterEntryConverter.xml"),
141
142
143 #ifdef HAVE_TEUCHOS_DEBUG
144
147 ValidatortoIDMap dummmyValidatorMap;
148 RCP<ParameterEntry> floatParameter = rcp(
149 new ParameterEntry((float)3.0));
150 TEST_THROW(intConverter.fromParameterEntrytoXML(floatParameter, "blah", 1, dummmyValidatorMap),
152
153 XMLObject floatXML = floatConverter.fromParameterEntrytoXML(floatParameter, "float", 1, dummmyValidatorMap);
154 TEST_THROW(intConverter.fromXMLtoParameterEntry(floatXML),
156
157 #endif
158
159}
160
161
162} //namespace Teuchos
#define ADD_TYPE_PARAMETER(T, VALUE)
#define ADD_TYPE_AND_ARRAY_TYPE_PARAMETER(T, VALUE)
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
Templated Parameter List class.
A collection of standard ParameterEntryXMLConverters.
A collection of standard ValidatorXMLConverters.
Definition of XMLInputStream derived class for reading XML from a std::string.
A thin wrapper around the Teuchos Array class that allows for 2 dimensional arrays.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
A database for ValidatorXMLConverters.
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
A collection of Exceptions that can be potentially thrown when converting a ParameterList to and from...
Writes a ParameterList to an XML object.
A class providing a simple XML parser. Methods can be overloaded to exploit external XML parsing libr...
Definition of Teuchos::as, for conversions between types.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Thrown when a converter is being used to convert either and XML tag or ParameterEntry with an innappr...
Thrown when an element inside a parameter list is bad.
Thrown when the root xml tag for a parameter list is incorrect.
Thrown when an appropriate ParameterEntryXMLConverter can't be found.
Thrown when two parameters in an XML file have the same ID.
Thrown when a parameter entry tag is missing it's name attribute.
Thrown when a parameter entry tag is missing it's type attribute.
Thrown when a parameter entry tag is missing it's value attribute.
ParameterEntry fromXMLtoParameterEntry(const XMLObject &xmlObj) const
Converts the given xml into a parameter entry.
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.
This object is held as the "value" in the Teuchos::ParameterList std::map.
A list of parameters of arbitrary type.
void print() const
Print function to use in debugging in a debugger.
Smart reference counting pointer class for automatic garbage collection.
A standard ParameterEntryXMLConverter for most data types.
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
A class for mapping validators to integers.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
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...