<commit_before>/*
	This file is part of cpp-ethereum.

	cpp-ethereum 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.

	cpp-ethereum 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 cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file rlp.cpp
 * @author <NAME> <<EMAIL>>
 * @date 2014
 * RLP test functions.
 */

#include <fstream>
#include <sstream>

#include <boost/test/unit_test.hpp>

#include <libdevcore/Log.h>
#include <libdevcore/RLP.h>
#include <libdevcore/Common.h>
#include <libdevcore/CommonIO.h>
#include <algorithm>
#include "../JsonSpiritHeaders.h"
#include "../TestHelper.h"

using namespace std;
using namespace dev;
namespace js = json_spirit;

namespace dev
{
	namespace test
	{	
		void buildRLP(js::mValue& _v, RLPStream& _rlp);
		void checkRLPAgainstJson(js::mValue& v, RLP& u);

		void doRlpTests(json_spirit::mValue& v, bool _fillin)
		{
			for (auto& i: v.get_obj())
			{
				js::mObject& o = i.second.get_obj();
				if (test::Options::get().singleTest && test::Options::get().singleTestName != i.first)
				{
					o.clear();
					continue;
				}

				std::cout << "  " << i.first << std::endl;
				TBOOST_REQUIRE((o.count("out") > 0));
				TBOOST_REQUIRE((!o["out"].is_null()));

				if (_fillin)
				{
					try
					{
						bytes payloadToDecode = fromHex(o["out"].get_str());
						RLP payload(payloadToDecode);
						if (payload.isEmpty())
							BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("Decoded Empty RLP!"));
						o["in"] = "VALID";
					}
					catch (Exception const& _e)
					{
						cnote << "Exception: " << diagnostic_information(_e);
						o["in"] = "INVALID";
					}
					catch (std::exception const& _e)
					{
						cnote << "rlp exception: " << _e.what();
						o["in"] = "INVALID";
					}
				}
				else
				{
					//Check Encode
					TBOOST_REQUIRE((o.count("in") > 0));
					int skipEncode = 0;
					if (o["in"].type() == js::str_type)
					{
						if (o["in"].get_str() == "INVALID")
							skipEncode = 1;
						else
						if (o["in"].get_str() == "VALID")
							skipEncode = 2;
					}

					if (skipEncode == 0)
					{
						RLPStream s;
						dev::test::buildRLP(o["in"], s);
						string computedText = toHex(s.out());

						std::string expectedText(o["out"].get_str());
						std::transform(expectedText.begin(), expectedText.end(), expectedText.begin(), ::tolower );

						std::stringstream msg;
						msg << "Encoding Failed: expected: " << expectedText << std::endl;
						msg << " But Computed: " << computedText;
						TBOOST_CHECK_MESSAGE(
							(expectedText == computedText),
							msg.str()
							);
					}

					//Check Decode
					// Uses the same test cases as encoding but in reverse.
					// We read into the string of hex values, convert to bytes,
					// and then compare the output structure to the json of the
					// input object.
					bool was_exception = false;
					js::mValue& inputData = o["in"];<commit_msg>newtests: style<commit_after>/*
	This file is part of cpp-ethereum.

	cpp-ethereum 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.

	cpp-ethereum 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 cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file rlp.cpp
 * @author <NAME> <<EMAIL>>
 * @date 2014
 * RLP test functions.
 */

#include <fstream>
#include <sstream>

#include <boost/test/unit_test.hpp>

#include <libdevcore/Log.h>
#include <libdevcore/RLP.h>
#include <libdevcore/Common.h>
#include <libdevcore/CommonIO.h>
#include <algorithm>
#include "../JsonSpiritHeaders.h"
#include "../TestHelper.h"

using namespace std;
using namespace dev;
namespace js = json_spirit;

namespace dev
{
	namespace test
	{	
		void buildRLP(js::mValue& _v, RLPStream& _rlp);
		void checkRLPAgainstJson(js::mValue& v, RLP& u);

		void doRlpTests(json_spirit::mValue& v, bool _fillin)
		{
			for (auto& i: v.get_obj())
			{
				js::mObject& o = i.second.get_obj();
				if (test::Options::get().singleTest && test::Options::get().singleTestName != i.first)
				{
					o.clear();
					continue;
				}

				std::cout << "  " << i.first << std::endl;
				TBOOST_REQUIRE((o.count("out") > 0));
				TBOOST_REQUIRE((!o["out"].is_null()));

				if (_fillin)
				{
					try
					{
						bytes payloadToDecode = fromHex(o["out"].get_str());
						RLP payload(payloadToDecode);
						if (payload.isEmpty())
							BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("Decoded Empty RLP!"));
						o["in"] = "VALID";
					}
					catch (Exception const& _e)
					{
						cnote << "Exception: " << diagnostic_information(_e);
						o["in"] = "INVALID";
					}
					catch (std::exception const& _e)
					{
						cnote << "rlp exception: " << _e.what();
						o["in"] = "INVALID";
					}
				}
				else
				{
					//Check Encode
					TBOOST_REQUIRE((o.count("in") > 0));
					int skipEncode = 0;
					if (o["in"].type() == js::str_type)
					{
						if (o["in"].get_str() == "INVALID")
							skipEncode = 1;
						else	if (o["in"].get_str() == "VALID")
							skipEncode = 2;
					}

					if (skipEncode == 0)
					{
						RLPStream s;
						dev::test::buildRLP(o["in"], s);
						string computedText = toHex(s.out());

						std::string expectedText(o["out"].get_str());
						std::transform(expectedText.begin(), expectedText.end(), expectedText.begin(), ::tolower );

						std::stringstream msg;
						msg << "Encoding Failed: expected: " << expectedText << std::endl;
						msg << " But Computed: " << computedText;
						TBOOST_CHECK_MESSAGE(
							(expectedText == computedText),
							msg.str()
							);
					}

					//Check Decode
					// Uses the same test cases as encoding but in reverse.
					// We read into the string of hex values, convert to bytes,
					// and then compare the output structure to the json of the
					// input object.
					bool was_exception = false;
					js::mValue& inputData = o["in"];
{'fraction_non_alphanumeric': 0.145218945487042, 'fraction_numerical': 0.0032767351802204347, 'mean_word_length': 2.9314988290398127, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968427', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2180, 'n_words': 622}