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 //
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 
58 namespace Teuchos {
59 
60 
61 #define ADD_TYPE_PARAMETER(T,VALUE) \
62  myList.set( #T , as<T>(VALUE));
63 const 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"),
79  TwoDArray<int>(g_arraySize, g_arraySize, as<int>(2)) );
80 }
81 
82 TEUCHOS_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 
111 TEUCHOS_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
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...