Teuchos Package Browser (Single Doxygen Collection) Version of the Day
XMLParser_ordered.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_XMLParameterListWriter.hpp"
00043 #include "Teuchos_XMLParameterListReader.hpp"
00044 #include "Teuchos_StringInputSource.hpp"
00045 #include "Teuchos_XMLParser.hpp"
00046 #include "Teuchos_UnitTestHarness.hpp"
00047 
00048 using std::string;
00049 using std::endl;
00050 
00051 namespace Teuchos {
00052 
00053 
00054 /* Simple test of Teuchos XMLParser class */
00055 TEUCHOS_UNIT_TEST( XMLParser, orderedWriteRead )
00056 {
00057   out << endl;
00058 
00059   /* create a ParameterList object */
00060   string xmlstring1;
00061   ParameterList wrotepl1("Parent");
00062   {
00063     ParameterList c1("Child1");
00064     ParameterList c2("Child2");
00065     c1.set("cp1", "first1");
00066     c1.set("cp2", "second1");
00067     c2.set("cp3", "first2");
00068     c2.set("cp4", "second2");
00069     wrotepl1.set("FirstSublist",c1);
00070     wrotepl1.set("SecondSublist",c2);
00071     /* create an XML object from the ParameterList and write it to a string */
00072     XMLParameterListWriter xml2pl;
00073     XMLObject xmlprob = xml2pl.toXML(wrotepl1);
00074     std::ostringstream ss;
00075     ss << xmlprob;
00076     xmlstring1 = ss.str();
00077     out << "*** String 1" << endl;
00078     out << xmlstring1 << endl;
00079   }
00080 
00081   string xmlstring2;
00082   ParameterList wrotepl2("Parent");
00083   {
00084     ParameterList c1("Child1");
00085     ParameterList c2("Child2");
00086     // swap the ordering
00087     c1.set("cp2", "second1");
00088     c1.set("cp1", "first1");
00089     c2.set("cp4", "second2");
00090     c2.set("cp3", "first2");
00091     wrotepl2.set("SecondSublist",c2);
00092     wrotepl2.set("FirstSublist",c1);
00093     /* create an XML object from the ParameterList and write it to a string */
00094     XMLParameterListWriter xml2pl;
00095     XMLObject xmlprob = xml2pl.toXML(wrotepl2);
00096     std::ostringstream ss;
00097     ss << xmlprob;
00098     xmlstring2 = ss.str();
00099     out << "*** String 2" << endl;
00100     out << xmlstring2 << endl;
00101   }
00102 
00103   // the different PL orderings should be reflected in the ParameterLists and their XML string representations
00104   TEST_INEQUALITY(wrotepl1, wrotepl2);
00105   TEST_INEQUALITY(xmlstring1, xmlstring2);
00106 
00107   /* create a input source, parser to read the string */
00108   ParameterList readpl1, readpl2;
00109   {
00110     StringInputSource src(xmlstring1);
00111     XMLParser parser(src.stream());
00112     XMLObject xmlprob = parser.parse();
00113     XMLParameterListReader pl2xml;
00114     readpl1 = pl2xml.toParameterList(xmlprob);
00115   }
00116   {
00117     StringInputSource src(xmlstring2);
00118     XMLParser parser(src.stream());
00119     XMLObject xmlprob = parser.parse();
00120     XMLParameterListReader pl2xml;
00121     readpl2 = pl2xml.toParameterList(xmlprob);
00122   }
00123 
00124   /* check that the parameter lists do not match */
00125   TEST_INEQUALITY(readpl1, readpl2);
00126 
00127 }
00128 
00129 
00130 TEUCHOS_UNIT_TEST( XMLParser, simpleOrderedRead )
00131 {
00132 
00133   /* create a ParameterList object */
00134   string xmlstring1;
00135   ParameterList plGold("ParentList");
00136   {
00137     ParameterList c1("Z");
00138     ParameterList c2("A");
00139     c1.set("A", "first1");
00140     c1.set("Z", "second1");
00141     c2.set("Z", "first2");
00142     c2.set("A", "second2");
00143     plGold.set("9FirstSublist",c1);
00144     plGold.set("1SecondSublist",c2);
00145   }
00146 
00147   string xmlsrc(
00148     "<ParameterList name=\"ParentList\">\n"
00149     "  <ParameterList name=\"9FirstSublist\">\n"
00150     "    <Parameter name=\"A\" type=\"string\" value=\"first1\"/>\n"
00151     "    <Parameter name=\"Z\" type=\"string\" value=\"second1\"/>\n"
00152     "  </ParameterList>\n"
00153     "  <ParameterList name=\"1SecondSublist\">\n"
00154     "    <Parameter name=\"Z\" type=\"string\" value=\"first2\"/>\n"
00155     "    <Parameter name=\"A\" type=\"string\" value=\"second2\"/>\n"
00156     "  </ParameterList>\n"
00157     "</ParameterList>\n");
00158   ParameterList plTest;
00159   {
00160     StringInputSource src(xmlsrc);
00161     XMLParser parser(src.stream());
00162     XMLObject xmlprob = parser.parse();
00163     XMLParameterListReader pl2xml;
00164     plTest = pl2xml.toParameterList(xmlprob);
00165   }
00166 
00167   TEST_EQUALITY(plTest, plGold);
00168 }
00169 
00170 
00171 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines