Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
YamlParameterList.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Teuchos
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 
47 
49 #include <Teuchos_ScalarTraits.hpp>
54 #include <Teuchos_RCP.hpp>
55 #include <Teuchos_Exceptions.hpp>
56 
57 
58 
60 using Teuchos::RCP;
61 
62 namespace TeuchosTests
63 {
64  TEUCHOS_UNIT_TEST(YAML, XmlEquivalence)
65  {
66  using std::string;
67  using std::vector;
68  vector<string> matchStems = {"Match1", "Match2", "Match3", "Match4"};
69  for(size_t i = 0; i < matchStems.size(); i++)
70  {
71  string xmlFile = matchStems[i] + ".xml";
72  string yamlFile = matchStems[i] + ".yaml";
73  RCP<ParameterList> xmlList = Teuchos::getParametersFromXmlFile(xmlFile);
75  TEST_EQUALITY(Teuchos::haveSameValuesUnordered(*xmlList, *yamlList), true);
76  }
77  }
78  TEUCHOS_UNIT_TEST(YAML, IntVsDouble)
79  {
80  //YAML2 has a double param that has the same name/value as an int param in XML2
81  //YAML reader should recognize the double and the param lists should not be equivalent
82  RCP<ParameterList> xmlList = Teuchos::getParametersFromXmlFile("IntVsDouble.xml");
83  RCP<ParameterList> yamlList = Teuchos::getParametersFromYamlFile("IntVsDouble.yaml");
84  TEST_EQUALITY(Teuchos::haveSameValuesUnordered(*xmlList, *yamlList), false);
85  }
86  TEUCHOS_UNIT_TEST(YAML, IllegalKeyString)
87  {
88  TEST_THROW(Teuchos::getParametersFromYamlFile("IllegalKeyString.yaml");, YAML::ParserException);
89  }
90  TEUCHOS_UNIT_TEST(YAML, IntAndDoubleArray)
91  {
92  int correctInts[5] = {2, 3, 5, 7, 11};
93  //the last number with 10 dec digits of precision should test correct double conversion
94  double correctDoubles[5] = {2.718, 3.14159, 1.618, 1.23456789, 42.1337};
95  TEST_NOTHROW({
97  //Retrieve arrays from a specific sublist (tests the mixed nesting of sequence/map)
98  ParameterList& sublist = params->get<ParameterList>("smoother: params");
99  Teuchos::Array<int>& intArr = sublist.get<Teuchos::Array<int> >("intArray");
100  Teuchos::Array<double>& doubleArr = sublist.get<Teuchos::Array<double> >("doubleArray");
101  TEST_EQUALITY(intArr.size(), 5);
102  TEST_EQUALITY(doubleArr.size(), 5);
103  for(int i = 0; i < 5; i++)
104  {
105  TEST_EQUALITY(correctInts[i], intArr[i]);
106  TEST_EQUALITY(correctDoubles[i], doubleArr[i]);
107  }
108  });
109  }
110  TEUCHOS_UNIT_TEST(YAML, InconsistentArrayType)
111  {
112  std::string correctStrings[5] = {"2", "3", "5", "7", "imastring"};
113  double correctDoubles[5] = {2, 3, 1.618, 1.23456789, 42.1337};
114  TEST_NOTHROW({
115  RCP<ParameterList> plist = Teuchos::getParametersFromYamlFile("InconsistentArray.yaml");
116  //verify that stringArray and doubleArray have the correct types and the correct values
117  const Teuchos::Array<std::string>& stringArr = plist->get<Teuchos::Array<std::string> >("stringArray");
118  const Teuchos::Array<double>& doubleArr = plist->get<Teuchos::Array<double> >("doubleArray");
119  for(int i = 0; i < 5; i++)
120  {
121  if(stringArr[i] != correctStrings[i])
122  {
123  throw std::runtime_error(std::string("stringArray[") + std::to_string(i) + "] is incorrect.");
124  }
125  if(doubleArr[i] != correctDoubles[i])
126  {
127  throw std::runtime_error(std::string("doubleArray value [") + std::to_string(i) + "] is incorrect.");
128  }
129  }
130  });
131  }
132 
133 } //namespace TeuchosTests
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
T & get(const std::string &name, T def_value)
Return the parameter&#39;s value, or the default value if it is not there.
#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)...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
Templated Parameter List class.
size_type size() const
Unit testing support.
A list of parameters of arbitrary type.
TEUCHOS_UNIT_TEST(YAML, XmlEquivalence)
Defines basic traits for the scalar field type.
Smart reference counting pointer class for automatic garbage collection.
Teuchos::RCP< Teuchos::ParameterList > getParametersFromYamlFile(const std::string &yamlFileName)
Simple helper functions that make it easy to read and write Yaml to and from a parameterlist.
Reference-counted pointer class and non-member templated function implementations.
bool haveSameValuesUnordered(const Teuchos::ParameterList &lhs, const Teuchos::ParameterList &rhs, bool verbose)