torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common/TestHelpers.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Source for Boost.Test helper functions.
18 
20 #include <fstream>
21 
22 namespace torc {
23 namespace common {
24 
25 bool readFileIntoString(const boost::filesystem::path& inPath, std::string& outString) {
26  // open the file and point to the end of it
27  std::ifstream input(inPath.string().c_str(), std::ios::in | std::ios::binary | std::ios::ate);
28  if(input.is_open()) {
29  // resize the string according to the file length
30  std::ifstream::pos_type size = input.tellg();
31  outString.resize(size);
32  // rewind to the beginning and read the entire file directly into the string
33  input.seekg(0, std::ios::beg);
34  input.read(const_cast<char*>(outString.data()), size);
35  // close and return success
36  input.close();
37  return true;
38  } else {
39  return false;
40  }
41 }
42 
44  std::string a, b;
45  if(!readFileIntoString(inA, a)) return false;
46  if(!readFileIntoString(inB, b)) return false;
47  return a == b;
48 }
49 
50 } // namespace common
51 } // namespace torc
bool readFileIntoString(const boost::filesystem::path &inPath, std::string &outString)
Read the raw contents of the specified file into a std::string.
std::string string
Header for Boost.Test helper functions.
boost::filesystem::path path
bool fileContentsAreEqual(const boost::filesystem::path &inA, const boost::filesystem::path &inB)
Compare the raw contents of two files to determine whether they are identical.