From f73cb8f855d94e0ec4dbbac207d6a40cbe94cfb2 Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Thu, 1 Feb 2018 16:53:07 +0100
Subject: [PATCH] Cleanup from old PyPersistent test machinery

---
 .../Python/PyPersistence/CMakeLists.txt       |  25 ---
 .../PyPersistence/PyPersistenceTest.cpp       | 204 ------------------
 .../Python/PyPersistence/PyPersistenceTest.h  |  63 ------
 .../Functional/Python/PyPersistence/main.cpp  |  31 ---
 4 files changed, 323 deletions(-)
 delete mode 100644 Tests/Functional/Python/PyPersistence/PyPersistenceTest.cpp
 delete mode 100644 Tests/Functional/Python/PyPersistence/PyPersistenceTest.h
 delete mode 100644 Tests/Functional/Python/PyPersistence/main.cpp

diff --git a/Tests/Functional/Python/PyPersistence/CMakeLists.txt b/Tests/Functional/Python/PyPersistence/CMakeLists.txt
index 9453c4c0f41..f354c613492 100644
--- a/Tests/Functional/Python/PyPersistence/CMakeLists.txt
+++ b/Tests/Functional/Python/PyPersistence/CMakeLists.txt
@@ -7,31 +7,6 @@ file(MAKE_DIRECTORY ${OUTPUT_DIR})
 
 set(PYPERSIST_REFERENCE_DIR ${TEST_REFERENCE_DIR}/PyPersist)
 
-#set(PYPERSIST_REF_DIR ${REFERENCE_DIR}/PyPersist)
-#set(PYPERSIST_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
-#file(MAKE_DIRECTORY ${PYPERSIST_OUT_DIR})
-
-#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPYPERSIST_OUT_DIR=\\\"${PYPERSIST_OUT_DIR}\\\"")
-#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPYPERSIST_REF_DIR=\\\"${PYPERSIST_REF_DIR}\\\"")
-
-## for some reason these flags aren't propagated here by SetUpWindows.cmake
-#if(MSVC)
-#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc ")
-#    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /EHsc ")
-#    add_definitions(-DYAML_CPP_DLL)
-#endif()
-
-#include_directories(
-#    ${BornAgainCore_INCLUDE_DIRS}
-#    ${EIGEN3_INCLUDE_DIR}
-#    ${YAMLCPP_INCLUDE_DIR}
-#    ${CMAKE_SOURCE_DIR}/Core/Simulation
-#    ${CMAKE_CURRENT_SOURCE_DIR}/../../TestMachinery
-#)
-
-#add_executable(PyPersistenceTest main.cpp PyPersistenceTest.h PyPersistenceTest.cpp)
-#target_link_libraries(PyPersistenceTest BornAgainCore BornAgainTestMachinery)
-
 # Python persistence test
 function(test_example example tolerance)
     set(script_path ${PY_EXAMPLES_DIR}/${example}.py)
diff --git a/Tests/Functional/Python/PyPersistence/PyPersistenceTest.cpp b/Tests/Functional/Python/PyPersistence/PyPersistenceTest.cpp
deleted file mode 100644
index 41fd1b1f6ac..00000000000
--- a/Tests/Functional/Python/PyPersistence/PyPersistenceTest.cpp
+++ /dev/null
@@ -1,204 +0,0 @@
-// ************************************************************************** //
-//
-//  BornAgain: simulate and fit scattering at grazing incidence
-//
-//! @file      Tests/Functional/PyCore/persistence/PyPersistenceTest.cpp
-//! @brief     Implements class PyPersistenceTest
-//!
-//! @homepage  http://www.bornagainproject.org
-//! @license   GNU General Public License v3 or higher (see COPYING)
-//! @copyright Forschungszentrum Jülich GmbH 2018
-//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
-//
-// ************************************************************************** //
-
-#include "PyPersistenceTest.h"
-#include "FileSystemUtils.h"
-#include "GISASSimulation.h"
-#include "IntensityDataFunctions.h"
-#include "IntensityDataIOFactory.h"
-#include "PythonFormatting.h"
-#include "SimulationFactory.h"
-#include "StringUtils.h"
-#include "TestUtils.h"
-#include <yaml-cpp/yaml.h>
-#include <cstdio>
-#include <fstream>
-#include <sstream>
-#include <iomanip>
-
-PyPersistenceTest::PyPersistenceTest(
-    const std::string& path, const std::string& name, double threshold)
-    : IFunctionalTest(name, "persistence test on script "+name)
-    , m_path(path)
-    , m_threshold(threshold)
-{}
-
-//! Runs a Python script, and returns true if the output of the script agrees with reference data.
-bool PyPersistenceTest::runTest()
-{
-    // Remove old output
-    for (const std::string& fname:
-             FileSystemUtils::glob(PYPERSIST_OUT_DIR, getName()+"\\.\\w+\\..+")) {
-        std::remove( fname.c_str() );
-        std::cout << "Removed old output " << fname.c_str() << "\n";
-    }
-
-    // Run Python script, which writes output to PYPERSIST_OUT_DIR.
-    std::string dat_stem = FileSystemUtils::jointPath(PYPERSIST_OUT_DIR, getName());
-    if (!TestUtils::runPython(m_path + " " + dat_stem))
-        return false;
-
-    // Retrieve new output and reference files
-    std::map<const std::string, const std::string> dat = glob2map(PYPERSIST_OUT_DIR, getName());
-    std::map<const std::string, const std::string> ref = glob2map(PYPERSIST_REF_DIR, getName());
-    if (dat.size()==0) {
-        std::cerr << "There is no test output of form " << dat_stem << ".*.*\n";
-        return false;
-    }
-    // Compare the keys in the file names
-    if (!compareFileMaps(dat, ref))
-        return false;
-
-    // Compare files one by one
-    for (auto const& it: dat)
-        if (!compareFilePair(it.second, ref[it.first]))
-            return false;
-    return true;
-}
-
-//! Globs for files of form <stem>.<key1>.<key2>[.*], and returns a map with keys of the form
-//! <key1>.<key2>, and values containing full file paths.
-std::map<const std::string, const std::string>
-PyPersistenceTest::glob2map(const std::string& dir, const std::string& stem)
-{
-    std::map<const std::string, const std::string> ret;
-    for (const std::string& fname: FileSystemUtils::glob(dir, stem+"\\.\\w+\\..+")) {
-        std::vector<std::string> fname_segments =
-            StringUtils::split(FileSystemUtils::filename(fname), ".");
-        ret.insert(make_pair(fname_segments[1]+"."+fname_segments[2],
-                             FileSystemUtils::jointPath(dir, fname)));
-    }
-    return ret;
-}
-
-//! Returns true if two file maps have the same set of keys.
-bool PyPersistenceTest::compareFileMaps(
-    const std::map<const std::string, const std::string>& dat,
-    const std::map<const std::string, const std::string>& ref)
-{
-    bool success = true;
-    // All dat files present in ref?
-    for (auto const& it: dat) {
-        if (ref.find(it.first)==ref.end()) {
-            std::cerr << "For test output "  << it.second
-                      << " there is no reference file in " << PYPERSIST_REF_DIR << "\n";
-            success = false;
-        }
-    }
-    // All ref files present in dat?
-    for (auto const& it: ref) {
-        if (dat.find(it.first)==dat.end()) {
-            std::cerr << "For reference file "  << it.second
-                      << " there is no test output in " << PYPERSIST_OUT_DIR << "\n";
-            success = false;
-        }
-    }
-    return success;
-}
-
-//! Returns true if test output and reference file agree.
-bool PyPersistenceTest::compareFilePair(
-    const std::string& dat_fpath, const std::string& ref_fpath) const
-{
-    std::cout << "Comparing dat='" << dat_fpath << "' with ref='" << ref_fpath << "':\n";
-    if ( dat_fpath.find(".int") != std::string::npos )
-        return compareIntensityPair( dat_fpath, ref_fpath );
-    if ( dat_fpath.find(".yaml") != std::string::npos )
-        return compareYamlPair( dat_fpath, ref_fpath );
-    std::cerr << "Failed: Unsupported file type '" << dat_fpath << "'\n";
-    return false;
-}
-
-//! Returns true if intensity maps from test output and reference file agree reasonably.
-bool PyPersistenceTest::compareIntensityPair(
-    const std::string& dat_fpath, const std::string& ref_fpath) const
-{
-    const OutputData<double>* dat = IntensityDataIOFactory::readOutputData( dat_fpath );
-    const OutputData<double>* ref = IntensityDataIOFactory::readOutputData( ref_fpath );
-    return TestUtils::isTheSame(*dat, *ref, m_threshold);
-}
-
-//! Returns true if YAML files from test output and reference agree.
-bool PyPersistenceTest::compareYamlPair(
-    const std::string& dat_fpath, const std::string& ref_fpath)
-{
-    std::fstream fdat(dat_fpath);
-    std::fstream fref(ref_fpath);
-    return compareYamlNode( YAML::Load(fdat), YAML::Load(fref) );
-}
-
-//! Returns true if all entries of the two YAML files agree.
-//! Floating-point entries must agree within a certain tolerance.
-//! The investigation is performed recursively, iterating over all lists and maps.
-bool PyPersistenceTest::compareYamlNode(const YAML::Node& dat, const YAML::Node& ref)
-{
-    if (dat.Type() != ref.Type()) {
-        std::cerr << "YAML node type differs: " << dat.Type() << " vs " << ref.Type() << "\n";
-        return false;
-    }
-    if      (dat.Type()==YAML::NodeType::Undefined)
-        throw std::runtime_error("Invalid node type 'Undefined' in YAML tree");
-    else if (dat.Type()==YAML::NodeType::Null)
-        throw std::runtime_error("Invalid node type 'Null' in YAML tree");
-    else if (dat.Type()==YAML::NodeType::Sequence) {
-        if (dat.size()!=ref.size()) {
-            std::cerr << "lists have different length\n";
-            return false;
-        }
-        for (size_t i=0; i<dat.size(); ++i) {
-            if (!compareYamlNode(dat[i], ref[i])) {
-                std::cerr << "at list position " << i << "\n";
-                return false;
-            }
-        }
-    } else if (dat.Type()==YAML::NodeType::Map) {
-        for (const auto& it_ref: ref) {
-            const YAML::Node& datval = dat[it_ref.first.as<std::string>()];
-            if (!datval) {
-                std::cerr << "map entry [" << it_ref.first << "] has no correspondence in dat\n";
-                return false;
-            }
-        }
-        for (const auto& it_dat: dat) {
-            const YAML::Node& refval = ref[it_dat.first.as<std::string>()];
-            if (!refval) {
-                std::cerr << "map entry [" << it_dat.first << "] has no correspondence in ref\n";
-                return false;
-            }
-            if (!compareYamlNode(it_dat.second, refval)) {
-                std::cerr << "at map entry [" << it_dat.first << "]\n";
-                return false;
-            }
-        }
-        return true;
-    } else if (dat.Type()==YAML::NodeType::Scalar) {
-        if (dat.as<std::string>() == ref.as<std::string>())
-            return true;
-        try {
-            double d = dat.as<double>();
-            double r = ref.as<double>();
-            const double tol = 1e-1;
-            if (std::abs(d-r)>(std::abs(d)+std::abs(r))*tol) {
-                std::cerr << "numbers differ: " << dat << " vs " << ref << "\n";
-                return false;
-            }
-            return true;
-        } catch(...) {
-            std::cerr << "scalar entries differ: '" << dat << "' vs '" << ref << "'\n";
-            return false;
-        }
-    } else
-        throw std::runtime_error("Bug in PyPersistenceTest::compareYamlNode: unexpected node type");
-    return true;
-}
diff --git a/Tests/Functional/Python/PyPersistence/PyPersistenceTest.h b/Tests/Functional/Python/PyPersistence/PyPersistenceTest.h
deleted file mode 100644
index 4391a99d1cb..00000000000
--- a/Tests/Functional/Python/PyPersistence/PyPersistenceTest.h
+++ /dev/null
@@ -1,63 +0,0 @@
-// ************************************************************************** //
-//
-//  BornAgain: simulate and fit scattering at grazing incidence
-//
-//! @file      Tests/Functional/PyCore/persistence/PyPersistenceTest.h
-//! @brief     Defines class PyPersistenceTest
-//!
-//! @homepage  http://www.bornagainproject.org
-//! @license   GNU General Public License v3 or higher (see COPYING)
-//! @copyright Forschungszentrum Jülich GmbH 2018
-//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
-//
-// ************************************************************************** //
-
-#ifndef PYPERSISTENCETEST_H
-#define PYPERSISTENCETEST_H
-
-#include "IFunctionalTest.h"
-#include "OutputData.h"
-#include <map>
-#include <string>
-
-namespace YAML {
-    class Node;
-}
-
-//! A functional test that runs a Python script and compares its output with a reference.
-//!   Invoked from RunPyPersistenceTest.
-//!   Output and reference directories will be determined from environment variables
-//!     PYPERSIST_OUT_DIR and PYPERSIST_REF_DIR set by CMake.
-//!   Output and reference file names are obtained by globbing the directories;
-//!     the stem must be the script name; the extension is arbitrary.
-//!   One script may generate several output files, which must have different extensions.
-
-class PyPersistenceTest : public IFunctionalTest
-{
-public:
-    PyPersistenceTest(const std::string& path, const std::string& name, double threshold);
-    ~PyPersistenceTest() final {}
-
-    bool runTest() final;
-
-private:
-    std::string m_path;
-    double m_threshold;
-
-    static std::map<const std::string, const std::string>
-        glob2map(const std::string& dir, const std::string& stem);
-
-    bool compareFileMaps(
-        const std::map<const std::string, const std::string>& dat,
-        const std::map<const std::string, const std::string>& ref);
-    bool compareFilePair(
-        const std::string& dat_fname, const std::string& ref_fname) const;
-    bool compareIntensityPair(
-        const std::string& dat_fname, const std::string& ref_fname) const;
-    static bool compareYamlPair(
-        const std::string& dat_fname, const std::string& ref_fname);
-    static bool compareYamlNode(
-        const YAML::Node& dat, const YAML::Node& ref);
-};
-
-#endif // PYPERSISTENCETEST_H
diff --git a/Tests/Functional/Python/PyPersistence/main.cpp b/Tests/Functional/Python/PyPersistence/main.cpp
deleted file mode 100644
index 7b0c851556c..00000000000
--- a/Tests/Functional/Python/PyPersistence/main.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// ************************************************************************** //
-//
-//  BornAgain: simulate and fit scattering at grazing incidence
-//
-//! @file      Tests/Functional/PyCore/persistence/main.cpp
-//! @brief     Implements program to run PyPersistence functional tests
-//!
-//! @homepage  http://www.bornagainproject.org
-//! @license   GNU General Public License v3 or higher (see COPYING)
-//! @copyright Forschungszentrum Jülich GmbH 2018
-//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
-//
-// ************************************************************************** //
-
-#include "PyPersistenceTest.h"
-#include <cstdlib>
-#include <iostream>
-#include <string>
-
-//! Runs persistence test on a Python script, which writes output to PYPERSIST_OUT_DIR.
-
-int main(int argc, char** argv)
-{
-    if (argc<4) {
-        std::cout << "Usage: " << argv[0] << " <directory> <script name> <tolerance>\n"
-                  << "  where <script name> is the stem without extension '.py'\n";
-        std::exit(1);
-    }
-    PyPersistenceTest test(argv[1], argv[2], std::stod(argv[3]));
-    return test.runTest() ? 0 : 1;
-}
-- 
GitLab