mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add serializeChoice() and deserializeChoice()
This commit is contained in:
parent
0c3de9ef99
commit
deaf1d0c6f
@ -17,13 +17,112 @@
|
|||||||
|
|
||||||
#include <tools/yulPhaser/Common.h>
|
#include <tools/yulPhaser/Common.h>
|
||||||
|
|
||||||
|
#include <libsolutil/CommonData.h>
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <boost/test/tools/output_test_stream.hpp>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost::test_tools;
|
||||||
|
using namespace solidity::util;
|
||||||
|
|
||||||
namespace solidity::phaser::test
|
namespace solidity::phaser::test
|
||||||
{
|
{
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
BOOST_AUTO_TEST_SUITE(CommonTest)
|
BOOST_AUTO_TEST_SUITE(CommonTest)
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
@ -20,7 +20,49 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
namespace solidity::phaser
|
namespace solidity::phaser
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
template <typename C>
|
||||||
|
std::istream& deserializeChoice(
|
||||||
|
std::istream& _inputStream,
|
||||||
|
C& _choice,
|
||||||
|
std::map<std::string, C> const& _stringToValueMap
|
||||||
|
)
|
||||||
|
{
|
||||||
|
std::string deserializedValue;
|
||||||
|
_inputStream >> deserializedValue;
|
||||||
|
|
||||||
|
auto const& pair = _stringToValueMap.find(deserializedValue);
|
||||||
|
if (pair != _stringToValueMap.end())
|
||||||
|
_choice = pair->second;
|
||||||
|
else
|
||||||
|
_inputStream.setstate(std::ios_base::failbit);
|
||||||
|
|
||||||
|
return _inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Translates a value to a string using a map and prints it to the output stream.
|
||||||
|
/// Sets the failbit if the value is not in the map.
|
||||||
|
template <typename C>
|
||||||
|
std::ostream& serializeChoice(
|
||||||
|
std::ostream& _outputStream,
|
||||||
|
C const& _choice,
|
||||||
|
std::map<C, std::string> const& _valueToStringMap
|
||||||
|
)
|
||||||
|
{
|
||||||
|
auto const& pair = _valueToStringMap.find(_choice);
|
||||||
|
if (pair != _valueToStringMap.end())
|
||||||
|
_outputStream << pair->second;
|
||||||
|
else
|
||||||
|
_outputStream.setstate(std::ios_base::failbit);
|
||||||
|
|
||||||
|
return _outputStream;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user