Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterList_yaml_Parallel.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 
14 #include <Teuchos_ScalarTraits.hpp>
16 #include <Teuchos_Exceptions.hpp>
18 #include <Teuchos_Exceptions.hpp>
19 #include <Teuchos_RCP.hpp>
20 #include <Teuchos_DefaultComm.hpp>
21 
22 #include <fstream>
23 #include <iomanip>
24 #include <sstream>
25 
26 using Teuchos::RCP;
27 using Teuchos::rcp;
30 
31 namespace TeuchosTests
32 {
33  TEUCHOS_UNIT_TEST(YAML, MPIBroadcast)
34  {
35  //load Match1.xml and Match1.yaml on proc 0, broadcast them, and make sure it matches on all procs
36  //note: the following code is run on all procs
37  RCP<ParameterList> xmlList = rcp(new ParameterList);
38  RCP<ParameterList> yamlList = rcp(new ParameterList);
39 
41  const std::string n1("Match1.xml");
42  Teuchos::updateParametersFromXmlFileAndBroadcast(n1, xmlList.ptr(), *comm, true);
43  const std::string n2("Match1.yaml");
44  Teuchos::updateParametersFromYamlFileAndBroadcast(n2, yamlList.ptr(), *comm, true);
45  TEST_EQUALITY(Teuchos::haveSameValues(*xmlList, *yamlList), true);
46  }
47  TEUCHOS_UNIT_TEST(YAML, ConvertFromXML)
48  {
49  using std::string;
50 
52 
53  //This list can contain any valid XML param lists in the unit_tests/yaml/
54  std::vector<string> xmlFiles;
55  xmlFiles.push_back("Match1.xml");
56  xmlFiles.push_back("Match2.xml");
57  xmlFiles.push_back("Match3.xml");
58  xmlFiles.push_back("Match4.xml");
59  xmlFiles.push_back("input_restingHydrostatic_RK4.xml");
60  xmlFiles.push_back("plasma_oscillation_rtc.xml");
61  for(size_t i = 0; i < xmlFiles.size(); i++)
62  {
63  //reading from XML
64  std::ifstream xmlStream(xmlFiles[i].c_str());
65  //emitting converted YAML
66  std::ostringstream yamlStream;
67  yamlStream << std::setprecision(17) << std::scientific;
68  Teuchos::convertXmlToYaml(xmlStream, yamlStream);
69  //now read back both formats to compare
70  RCP<ParameterList> xmlList = Teuchos::getParametersFromXmlFile(xmlFiles[i]);
71  string yamlText = yamlStream.str();
72  string debugYamlFileName = xmlFiles[i] + ".yaml";
73  {
74  std::ofstream debugYamlFileStream(debugYamlFileName.c_str());
75  debugYamlFileStream << yamlText;
76  }
78  debugYamlFileName);
79  TEST_EQUALITY(Teuchos::haveSameValues(*xmlList, *yamlList, true), true);
80  }
81  }
82 
83 } //namespace TeuchosTests
84 
Macros for defining unit tests.
Functions to convert between ParameterList and YAML.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
Teuchos::RCP< Teuchos::ParameterList > parseYamlText(const std::string &text, const std::string &name)
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
Unit testing support.
Return a default global communicator appropriate for the build.
Additional ParameterList XML helper functions including parallel 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.
std::string convertXmlToYaml(const std::string &xmlFileName)
Reference-counted pointer class and non-member templated function implementations.