mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Common: Add readLinesFromFile()
This commit is contained in:
parent
5e814acc3c
commit
ff99d25bc3
@ -158,6 +158,7 @@ set(yul_phaser_sources
|
|||||||
# My current workaround is just to include its source files here but this introduces
|
# My current workaround is just to include its source files here but this introduces
|
||||||
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
|
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
|
||||||
../tools/yulPhaser/AlgorithmRunner.cpp
|
../tools/yulPhaser/AlgorithmRunner.cpp
|
||||||
|
../tools/yulPhaser/Common.cpp
|
||||||
../tools/yulPhaser/Chromosome.cpp
|
../tools/yulPhaser/Chromosome.cpp
|
||||||
../tools/yulPhaser/FitnessMetrics.cpp
|
../tools/yulPhaser/FitnessMetrics.cpp
|
||||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <test/yulPhaser/TestHelpers.h>
|
||||||
|
|
||||||
#include <tools/yulPhaser/Common.h>
|
#include <tools/yulPhaser/Common.h>
|
||||||
|
|
||||||
#include <libsolutil/CommonData.h>
|
#include <libsolutil/CommonData.h>
|
||||||
@ -22,6 +24,7 @@
|
|||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <boost/test/tools/output_test_stream.hpp>
|
#include <boost/test/tools/output_test_stream.hpp>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -32,6 +35,12 @@ using namespace solidity::util;
|
|||||||
namespace solidity::phaser::test
|
namespace solidity::phaser::test
|
||||||
{
|
{
|
||||||
|
|
||||||
|
class ReadLinesFromFileFixture
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
TemporaryDirectory m_tempDir;
|
||||||
|
};
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -60,6 +69,17 @@ map<string, TestEnum> const StringToTestEnumMap = invertMap(TestEnumToStringMap)
|
|||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(readLinesFromFile_should_return_all_lines_from_a_text_file_as_strings_without_newlines, ReadLinesFromFileFixture)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
ofstream tmpFile(m_tempDir.memberPath("test-file.txt"));
|
||||||
|
tmpFile << endl << "Line 1" << endl << endl << endl << "Line 2" << endl << "#" << endl << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<string> lines = readLinesFromFile(m_tempDir.memberPath("test-file.txt"));
|
||||||
|
BOOST_TEST((lines == vector<string>{"", "Line 1", "", "", "Line 2", "#", ""}));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(deserializeChoice_should_convert_string_to_enum)
|
BOOST_AUTO_TEST_CASE(deserializeChoice_should_convert_string_to_enum)
|
||||||
{
|
{
|
||||||
istringstream aStream("a");
|
istringstream aStream("a");
|
||||||
|
@ -16,6 +16,7 @@ install(TARGETS solidity-upgrade DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
|||||||
add_executable(yul-phaser
|
add_executable(yul-phaser
|
||||||
yulPhaser/main.cpp
|
yulPhaser/main.cpp
|
||||||
yulPhaser/Common.h
|
yulPhaser/Common.h
|
||||||
|
yulPhaser/Common.cpp
|
||||||
yulPhaser/AlgorithmRunner.h
|
yulPhaser/AlgorithmRunner.h
|
||||||
yulPhaser/AlgorithmRunner.cpp
|
yulPhaser/AlgorithmRunner.cpp
|
||||||
yulPhaser/Phaser.h
|
yulPhaser/Phaser.h
|
||||||
|
45
tools/yulPhaser/Common.cpp
Normal file
45
tools/yulPhaser/Common.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
This file is part of solidity.
|
||||||
|
|
||||||
|
solidity is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
solidity is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <tools/yulPhaser/Common.h>
|
||||||
|
|
||||||
|
#include <tools/yulPhaser/Exceptions.h>
|
||||||
|
|
||||||
|
#include <libsolutil/Assertions.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace solidity;
|
||||||
|
using namespace solidity::phaser;
|
||||||
|
|
||||||
|
vector<string> phaser::readLinesFromFile(string const& _path)
|
||||||
|
{
|
||||||
|
ifstream inputStream(_path);
|
||||||
|
assertThrow(inputStream.is_open(), FileOpenError, "Could not open file '" + _path + "': " + strerror(errno));
|
||||||
|
|
||||||
|
string line;
|
||||||
|
vector<string> lines;
|
||||||
|
while (!getline(inputStream, line).fail())
|
||||||
|
lines.push_back(line);
|
||||||
|
|
||||||
|
assertThrow(!inputStream.bad(), FileReadError, "Error while reading from file '" + _path + "': " + strerror(errno));
|
||||||
|
|
||||||
|
return lines;
|
||||||
|
}
|
@ -22,10 +22,19 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace solidity::phaser
|
namespace solidity::phaser
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// Loads the whole file into memory, splits the content into lines, strips newlines and
|
||||||
|
/// returns the result as a list of strings.
|
||||||
|
///
|
||||||
|
/// Throws FileOpenError if the file does not exist or cannot be opened for reading.
|
||||||
|
/// Throws FileReadError if any read operation fails during the whole process.
|
||||||
|
std::vector<std::string> readLinesFromFile(std::string const& _path);
|
||||||
|
|
||||||
/// Reads a token from the input stream and translates it to a string using a map.
|
/// Reads a token from the input stream and translates it to a string using a map.
|
||||||
/// Sets the failbit in the stream if there's no matching value in the map.
|
/// Sets the failbit in the stream if there's no matching value in the map.
|
||||||
template <typename C>
|
template <typename C>
|
||||||
|
@ -27,4 +27,7 @@ struct InvalidProgram: virtual BadInput {};
|
|||||||
struct NoInputFiles: virtual BadInput {};
|
struct NoInputFiles: virtual BadInput {};
|
||||||
struct MissingFile: virtual BadInput {};
|
struct MissingFile: virtual BadInput {};
|
||||||
|
|
||||||
|
struct FileOpenError: virtual util::Exception {};
|
||||||
|
struct FileReadError: virtual util::Exception {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user