TestData.cpp 6.71 KB
Newer Older
1 2
///////////////////////////////////////////////////////////////////////////////
//
3
// File: TestData.cpp
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Encapsulation of test XML file.
//
///////////////////////////////////////////////////////////////////////////////
35 36

#include <boost/lexical_cast.hpp>
37
#include <boost/algorithm/string.hpp>
38 39

#include <TestData.h>
40

41 42
#define ASSERTL0(condition,msg)

43 44 45 46
using namespace std;

namespace Nektar
{
47 48
    TestData::TestData(const fs::path& pFilename, po::variables_map& pVm)
            : m_cmdoptions(pVm)
49 50
    {
        // Process test file format.
51
        m_doc = new TiXmlDocument(pFilename.string().c_str());
52

53
        bool loadOkay = m_doc->LoadFile();
54

55 56 57
        ASSERTL0(loadOkay, "Failed to load test definition file: "
                 + pFilename.string() + "\n"
                 + string(m_doc->ErrorDesc()));
58

59 60
        Parse(m_doc);
    }
61

62 63
    TestData::TestData(const TestData& pSrc)
    {
64

65
    }
66

67 68 69 70
    const std::string& TestData::GetDescription() const
    {
        return m_description;
    }
71

72
    const fs::path& TestData::GetExecutable() const
73
    {
74
        return m_executable;
75
    }
76

77 78 79 80
    const std::string& TestData::GetParameters() const
    {
        return m_parameters;
    }
81

82 83 84 85 86
    const std::string& TestData::GetCommand() const
    {
        return m_command;
    }

87 88 89 90
    const unsigned int& TestData::GetNProcesses() const
    {
        return m_processes;
    }
91

92 93 94
    std::string TestData::GetMetricType(unsigned int pId) const
    {
        ASSERTL0(pId < m_metrics.size(), "Metric ID out of range.");
95

96 97 98 99 100 101
        // read the property name
        ASSERTL0(m_metrics[pId]->Attribute("type"),
                 "Missing 'type' attribute in metric "
                 + boost::lexical_cast<string>(pId) + ").");
        return boost::to_upper_copy(string(m_metrics[pId]->Attribute("type")));
    }
102

103 104 105 106
    unsigned int TestData::GetNumMetrics() const
    {
        return m_metrics.size();
    }
107

108 109 110 111 112
    TiXmlElement* TestData::GetMetric(unsigned int pId)
    {
        ASSERTL0(pId < m_metrics.size(), "Metric index out of range.");
        return m_metrics[pId];
    }
113

114
    unsigned int TestData::GetMetricId(unsigned int pId)
115
    {
116 117 118 119
        ASSERTL0(pId < m_metrics.size(), "Metric index out of range.");
        const char *id = m_metrics[pId]->Attribute("id");
        ASSERTL0(id, "No ID found for metric!");
        return boost::lexical_cast<unsigned int>(id);
120
    }
121 122
    
    DependentFile TestData::GetDependentFile(unsigned int pId) const
123
    {
124 125
        ASSERTL0(pId < m_files.size(), "File index out of range.");
        return m_files[pId];
126
    }
127

128
    unsigned int TestData::GetNumDependentFiles() const
129
    {
130
        return m_files.size();
131 132
    }

133
    void TestData::Parse(TiXmlDocument* pDoc)
134
    {
135 136 137 138 139 140 141 142 143 144
        TiXmlHandle handle(m_doc);
        TiXmlElement *testElement, *tmp, *metrics, *files;
        testElement = handle.FirstChildElement("test").Element();
        ASSERTL0(testElement, "Cannot find 'test' root element.");

        // Find description tag.
        tmp = testElement->FirstChildElement("description");
        ASSERTL0(tmp, "Cannot find 'description' for test.");
        m_description = string(tmp->GetText());

145 146 147 148 149 150 151 152 153 154
        // Find executable.
        if (m_cmdoptions.count("executable"))
        {
            m_executable = fs::path(
                            m_cmdoptions["executable"].as<std::string>());
        }
        else
        {
            tmp = testElement->FirstChildElement("executable");
            ASSERTL0(tmp, "Cannot find 'executable' for test.");
155
            m_executable = fs::path(tmp->GetText());
156 157 158 159 160 161
#if defined(RELWITHDEBINFO)
            m_executable += "-rg";
#elif !defined(NDEBUG)
            m_executable += "-g";
#endif
        }
162 163 164 165

        // Find parameters tag.
        tmp = testElement->FirstChildElement("parameters");
        ASSERTL0(tmp, "Cannot find 'parameters' for test.");
166 167 168 169
        if (tmp->GetText())
        {
            m_parameters = string(tmp->GetText());
        }
170

171 172 173 174 175 176 177 178 179 180
        tmp = testElement->FirstChildElement("command");
        if (tmp)
        {
            m_command = string(tmp->GetText());
        }
        else
        {
            m_command = "";
        }

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        // Find parallel processes tah.
        tmp = testElement->FirstChildElement("processes");
        if (tmp)
        {
            m_processes = atoi(tmp->GetText());
        }
        else
        {
            m_processes = 1;
        }

        // Extract metric tags
        metrics = testElement->FirstChildElement("metrics");
        ASSERTL0(metrics, "No metrics defined for test.");

        tmp = metrics->FirstChildElement("metric");
197 198
        while (tmp)
        {
199 200 201 202 203 204 205 206 207 208
            m_metrics.push_back(tmp);
            tmp = tmp->NextSiblingElement("metric");
        }

        // Extract list of dependent files
        files = testElement->FirstChildElement("files");
        if (files)
        {
            tmp = files->FirstChildElement("file");
            while (tmp)
209
            {
210 211 212 213 214 215 216 217
                DependentFile f;
                f.m_filename = string(tmp->GetText());
                if (tmp->Attribute("description"))
                {
                    f.m_description = string(tmp->Attribute("description"));
                }
                m_files.push_back(f);
                tmp = tmp->NextSiblingElement("file");
218 219 220
            }
        }
    }
221 222 223 224 225
    
    void TestData::SaveFile()
    {
        m_doc->SaveFile();
    }
226
}