OpenASIP  2.0
TestApplication.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TestApplication.cc
26  *
27  * Implementation of TestApplication class.
28  *
29  * @author Jari Mäntyneva 2007 (jari.mantyneva-no.spam-tut.fi)
30  * @author Esa Määttä 2008 (esa.maatta-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include <cstdio>
35 #include <fstream>
36 
37 #include "TestApplication.hh"
38 #include "FileSystem.hh"
39 #include "Conversion.hh"
40 
41 
42 using std::string;
43 
44 const string TestApplication::DESCRIPTION_FILE_NAME_ = "description.txt";
45 const string TestApplication::APPLICATION_BASE_FILE_NAME_ = "program";
46 const string TestApplication::SETUP_FILE_NAME_ = "setup.sh";
47 const string TestApplication::SIMULATE_TTASIM_FILE_NAME_ = "simulate.ttasim";
48 const string
49 TestApplication::CORRECT_OUTPUT_FILE_NAME_ = "correct_simulation_output";
50 const string TestApplication::VERIFY_FILE_NAME_ = "verify.sh";
51 const string TestApplication::CLEANUP_FILE_NAME_ = "cleanup.sh";
52 const string TestApplication::MAX_RUNTIME_ = "max_runtime";
53 const int TestApplication::MAX_LINE_LENGTH_ = 512;
54 
55 /**
56  * Constructor.
57  *
58  * @param testApplicationPath Path of the test application directory.
59  */
60 TestApplication::TestApplication(const string& testApplicationPath)
61  : testApplicationPath_(testApplicationPath), maxRuntime_(0) {
62  string maxRuntimeFile = testApplicationPath_
64  + MAX_RUNTIME_;
65  if (FileSystem::fileExists(maxRuntimeFile)) {
66  char line[MAX_LINE_LENGTH_];
67  FILE* file = fopen(maxRuntimeFile.c_str(), "r");
68  if (fgets(line, MAX_LINE_LENGTH_, file) == NULL)
69  abort();
70  try {
72  } catch (const NumberFormatException& exception) {
73  fclose(file);
74  throw IOException(
75  __FILE__, __LINE__, __func__, exception.errorMessage());
76  }
77  fclose(file);
78  }
79 }
80 
81 /**
82  * Destructor.
83  */
85 }
86 
87 /**
88  * Returns the test application description.
89  *
90  * The description is given in the description.txt file of the test
91  * application directory.
92  *
93  * @return The test application description. Returns an empty vector if the
94  * desctription.txt file was not found.
95  */
96 std::vector<std::string>
98 
99  std::vector<string> description;
100  string descriptionFile =
103  if (FileSystem::fileExists(descriptionFile)) {
104  char line[MAX_LINE_LENGTH_];
105  FILE* file = fopen(descriptionFile.c_str(), "r");
106  while (fgets(line, MAX_LINE_LENGTH_, file)) {
107  description.push_back(line);
108  }
109  fclose(file);
110  }
111  return description;
112 }
113 
114 /**
115  * Verifies the previous simulation run.
116  *
117  * Uses 'verify.sh' to verify the previous simulation run.
118  *
119  * @return True if the simulation result was verified as correct.
120  */
121 bool
123 
124  string verifyFile =
127 
128  if (FileSystem::fileExists(verifyFile)) {
129  return FileSystem::runShellCommand(verifyFile);
130  }
131  return false;
132 }
133 
134 /**
135  * Returns the correct output from file 'correct_simulation_output' as string.
136  *
137  * @return Correct output as string.
138  */
139 const std::string
141 
142  string correctOutputFile =
145  if (!FileSystem::fileExists(correctOutputFile)) {
146  return "";
147  }
148  std::string buf;
149  std::string line;
150  std::ifstream in(correctOutputFile.c_str());
151 
152  if (std::getline(in,line)) {
153  buf += line;
154  }
155  while(std::getline(in,line)) {
156  buf += "\n";
157  buf += line;
158  }
159  return buf;
160 }
161 
162 /**
163  * Set up the simulation run.
164  *
165  * Uses 'setup.sh' to set up the simulation run.
166  */
167 void
169 
170  string setupFile =
173  if (FileSystem::fileExists(setupFile)) {
174  FileSystem::runShellCommand(setupFile);
175  }
176 }
177 
178 /**
179  * Returns a istream to 'simulate.ttasim' file.
180  *
181  * Client takes responsibility of destroying the stream.
182  *
183  * @return istream to 'simulate.ttasim' file.
184  */
185 std::istream*
187 
188  std::ifstream* ifStream = new std::ifstream;
189  if (!hasSimulateTTASim()) {
190  return ifStream;
191  }
192  delete ifStream;
193  ifStream = NULL;
194 
195  string simulateFile =
198  return new std::ifstream(simulateFile.c_str());
199 }
200 
201 
202 /**
203  * Cleans up the previous simulation run.
204  *
205  * Uses 'cleanup.sh' script if it exists.
206  */
207 void
209  string cleanupFile =
212  if (FileSystem::fileExists(cleanupFile)) {
213  FileSystem::runShellCommand(cleanupFile);
214  }
215 }
216 
217 /**
218  * Returns the maximum runtime that is set in file "max_runtime".
219  */
222  return maxRuntime_;
223 }
224 
225 /**
226  * Returns true if 'sequential_program' file is in the test application
227  * directory.
228  */
229 bool
231 
232  string applicationBCFile =
235  string applicationLLFile =
238 
239  return FileSystem::fileExists(applicationBCFile) ||
240  FileSystem::fileExists(applicationLLFile);
241 }
242 
243 /**
244  * Returns true if 'setup.sh' file is in the test application directory.
245  */
246 bool
248 
249  string setupFile =
252  return FileSystem::fileExists(setupFile);
253 }
254 
255 /**
256  * Returns true if 'simulate.ttasim' file is in the test application directory.
257  */
258 bool
260 
261  string simulateFile =
264  return FileSystem::fileExists(simulateFile);
265 }
266 
267 /**
268  * Returns true if 'correct_simulation_output' file is in the test
269  * application directory.
270  */
271 bool
273 
274  string correctOutputFile =
277  return FileSystem::fileExists(correctOutputFile);
278 }
279 
280 /**
281  * Returns true if 'verify.sh' file is in the test application directory.
282  */
283 bool
285 
286  string verifyFile =
289  return FileSystem::fileExists(verifyFile);
290 }
291 
292 /**
293  * Returns true if 'cleanup.sh' file is in the test application directory.
294  */
295 bool
297 
298  string cleanupFile =
301  return FileSystem::fileExists(cleanupFile);
302 }
303 
304 /**
305  * Returns the application path in the test application directory.
306  *
307  * If the file 'sequential_program' does not exists returns an empty string.
308  *
309  * @return The path of the 'sequential_program' file.
310  */
311 const std::string
313 
314  if (hasApplication()) {
315  string applicationBCFile =
318  string applicationLLFile =
321 
322  // prioritize the .ll file as it's more portable across
323  // LLVM versions than the bitcode
324  if (FileSystem::fileExists(applicationLLFile)) {
325  return applicationLLFile;
326  }
327  if (FileSystem::fileExists(applicationBCFile)) {
328  return applicationBCFile;
329  }
330 
331  }
332  return "";
333 }
TestApplication::simulateTTASim
std::istream * simulateTTASim() const
Definition: TestApplication.cc:186
TestApplication::setupSimulation
void setupSimulation() const
Definition: TestApplication.cc:168
FileSystem.hh
TestApplication::applicationPath
const std::string applicationPath() const
Definition: TestApplication.cc:312
NumberFormatException
Definition: Exception.hh:421
TestApplication::maxRuntime_
Runtime maxRuntime_
Maximum runtime of the test appication in nano seconds.
Definition: TestApplication.hh:80
TestApplication::correctOutput
const std::string correctOutput() const
Definition: TestApplication.cc:140
TestApplication::hasCleanupSimulation
bool hasCleanupSimulation() const
Definition: TestApplication.cc:296
TestApplication::CLEANUP_FILE_NAME_
static const std::string CLEANUP_FILE_NAME_
Name of the clean up file.
Definition: TestApplication.hh:98
TestApplication::DESCRIPTION_FILE_NAME_
static const std::string DESCRIPTION_FILE_NAME_
File name of the description text for the application.
Definition: TestApplication.hh:83
TestApplication::hasSimulateTTASim
bool hasSimulateTTASim() const
Definition: TestApplication.cc:259
TestApplication::maxRuntime
Runtime maxRuntime() const
Definition: TestApplication.cc:221
Conversion::toDouble
static double toDouble(const T &source)
TestApplication::hasApplication
bool hasApplication() const
Definition: TestApplication.cc:230
TestApplication::TestApplication
TestApplication(const std::string &testApplicationPath)
Definition: TestApplication.cc:60
TestApplication::hasVerifySimulation
bool hasVerifySimulation() const
Definition: TestApplication.cc:284
Conversion.hh
TestApplication::verifySimulation
bool verifySimulation() const
Definition: TestApplication.cc:122
__func__
#define __func__
Definition: Application.hh:67
TestApplication::CORRECT_OUTPUT_FILE_NAME_
static const std::string CORRECT_OUTPUT_FILE_NAME_
Name of the correct simulation output file.
Definition: TestApplication.hh:94
TestApplication::MAX_LINE_LENGTH_
static const int MAX_LINE_LENGTH_
Maximum line length in a file.
Definition: TestApplication.hh:102
TestApplication::SETUP_FILE_NAME_
static const std::string SETUP_FILE_NAME_
Name of the file that contains setup script.
Definition: TestApplication.hh:90
TestApplication::hasSetupSimulation
bool hasSetupSimulation() const
Definition: TestApplication.cc:247
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
TestApplication::cleanupSimulation
void cleanupSimulation() const
Definition: TestApplication.cc:208
TestApplication::description
std::vector< std::string > description() const
Definition: TestApplication.cc:97
TestApplication::Runtime
double Runtime
Definition: TestApplication.hh:48
TestApplication::SIMULATE_TTASIM_FILE_NAME_
static const std::string SIMULATE_TTASIM_FILE_NAME_
Name of the file that runs the simulation.
Definition: TestApplication.hh:92
TestApplication::MAX_RUNTIME_
static const std::string MAX_RUNTIME_
Name of the file that contains maximum runtime.
Definition: TestApplication.hh:100
TestApplication::APPLICATION_BASE_FILE_NAME_
static const std::string APPLICATION_BASE_FILE_NAME_
Name of the sequential program file. Base name of the file that contains the fully linked program....
Definition: TestApplication.hh:88
TestApplication::testApplicationPath_
const std::string testApplicationPath_
Path of the test application directory.
Definition: TestApplication.hh:78
FileSystem::fileExists
static bool fileExists(const std::string fileName)
TestApplication::VERIFY_FILE_NAME_
static const std::string VERIFY_FILE_NAME_
Name of the verify script file.
Definition: TestApplication.hh:96
IOException
Definition: Exception.hh:130
FileSystem::runShellCommand
static bool runShellCommand(const std::string command)
TestApplication::~TestApplication
virtual ~TestApplication()
Definition: TestApplication.cc:84
TestApplication::hasCorrectOutput
bool hasCorrectOutput() const
Definition: TestApplication.cc:272
TestApplication.hh