2020-03-02 09:19:44 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-03-02 09:19:44 +00:00
|
|
|
|
2020-02-21 23:48:35 +00:00
|
|
|
#include <test/yulPhaser/TestHelpers.h>
|
|
|
|
|
2020-03-02 09:19:44 +00:00
|
|
|
#include <tools/yulPhaser/Common.h>
|
|
|
|
|
2020-03-02 09:20:28 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
2020-03-02 09:19:44 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2020-03-02 09:20:28 +00:00
|
|
|
#include <boost/test/tools/output_test_stream.hpp>
|
|
|
|
|
2020-02-21 23:48:35 +00:00
|
|
|
#include <fstream>
|
2020-03-02 09:20:28 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost::test_tools;
|
|
|
|
using namespace solidity::util;
|
2020-03-02 09:19:44 +00:00
|
|
|
|
|
|
|
namespace solidity::phaser::test
|
|
|
|
{
|
|
|
|
|
2020-02-21 23:48:35 +00:00
|
|
|
class ReadLinesFromFileFixture
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
TemporaryDirectory m_tempDir;
|
|
|
|
};
|
|
|
|
|
2020-03-02 09:20:28 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class TestEnum
|
|
|
|
{
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
AB,
|
|
|
|
CD,
|
|
|
|
EF,
|
|
|
|
GH,
|
|
|
|
};
|
|
|
|
|
|
|
|
map<TestEnum, string> const TestEnumToStringMap =
|
|
|
|
{
|
|
|
|
{TestEnum::A, "a"},
|
|
|
|
{TestEnum::B, "b"},
|
|
|
|
{TestEnum::AB, "a b"},
|
|
|
|
{TestEnum::CD, "c-d"},
|
|
|
|
{TestEnum::EF, "e f"},
|
|
|
|
};
|
|
|
|
map<string, TestEnum> const StringToTestEnumMap = invertMap(TestEnumToStringMap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-08 15:56:14 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions"))
|
2020-03-02 09:19:44 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(CommonTest)
|
2020-03-02 09:20:28 +00:00
|
|
|
|
2020-02-21 23:48:35 +00:00
|
|
|
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", "#", ""}));
|
|
|
|
}
|
|
|
|
|
2020-03-02 09:20:28 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(deserializeChoice_should_convert_string_to_enum)
|
|
|
|
{
|
|
|
|
istringstream aStream("a");
|
|
|
|
TestEnum aResult;
|
|
|
|
deserializeChoice(aStream, aResult, StringToTestEnumMap);
|
|
|
|
BOOST_CHECK(aResult == TestEnum::A);
|
|
|
|
BOOST_TEST(!aStream.fail());
|
|
|
|
|
|
|
|
istringstream bStream("b");
|
|
|
|
TestEnum bResult;
|
|
|
|
deserializeChoice(bStream, bResult, StringToTestEnumMap);
|
|
|
|
BOOST_CHECK(bResult == TestEnum::B);
|
|
|
|
BOOST_TEST(!bStream.fail());
|
|
|
|
|
|
|
|
istringstream cdStream("c-d");
|
|
|
|
TestEnum cdResult;
|
|
|
|
deserializeChoice(cdStream, cdResult, StringToTestEnumMap);
|
|
|
|
BOOST_CHECK(cdResult == TestEnum::CD);
|
|
|
|
BOOST_TEST(!cdStream.fail());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(deserializeChoice_should_set_failbit_if_there_is_no_enum_corresponding_to_string)
|
|
|
|
{
|
|
|
|
istringstream xyzStream("xyz");
|
|
|
|
TestEnum xyzResult;
|
|
|
|
deserializeChoice(xyzStream, xyzResult, StringToTestEnumMap);
|
|
|
|
BOOST_TEST(xyzStream.fail());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(deserializeChoice_does_not_have_to_support_strings_with_spaces)
|
|
|
|
{
|
|
|
|
istringstream abStream("a b");
|
|
|
|
TestEnum abResult;
|
|
|
|
deserializeChoice(abStream, abResult, StringToTestEnumMap);
|
|
|
|
BOOST_CHECK(abResult == TestEnum::A);
|
|
|
|
BOOST_TEST(!abStream.fail());
|
|
|
|
|
|
|
|
istringstream efStream("e f");
|
|
|
|
TestEnum efResult;
|
|
|
|
deserializeChoice(efStream, efResult, StringToTestEnumMap);
|
|
|
|
BOOST_TEST(efStream.fail());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(serializeChoice_should_convert_enum_to_string)
|
|
|
|
{
|
|
|
|
output_test_stream output;
|
|
|
|
|
|
|
|
serializeChoice(output, TestEnum::A, TestEnumToStringMap);
|
|
|
|
BOOST_CHECK(output.is_equal("a"));
|
|
|
|
BOOST_TEST(!output.fail());
|
|
|
|
|
|
|
|
serializeChoice(output, TestEnum::AB, TestEnumToStringMap);
|
|
|
|
BOOST_CHECK(output.is_equal("a b"));
|
|
|
|
BOOST_TEST(!output.fail());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(serializeChoice_should_set_failbit_if_there_is_no_string_corresponding_to_enum)
|
|
|
|
{
|
|
|
|
output_test_stream output;
|
|
|
|
serializeChoice(output, TestEnum::GH, TestEnumToStringMap);
|
|
|
|
BOOST_TEST(output.fail());
|
|
|
|
}
|
|
|
|
|
2020-03-02 09:19:44 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|