<commit_before>#include <boost/test/unit_test.hpp>

#include <ports/ports.hpp>
#include <ports/region_aware.hpp>

using namespace fc;

BOOST_AUTO_TEST_SUITE(test_parallle_region);

BOOST_AUTO_TEST_CASE(test_region_aware_node)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	auto region = std::make_shared<parallel_region>();

	int test_value = 0;
	auto write_param = [&](int i) {test_value = i;};
	test_in_port test_in(region, write_param);

	BOOST_CHECK_EQUAL(test_value, 0);
	test_in(1);
	BOOST_CHECK_EQUAL(test_value, 1);
}


BOOST_AUTO_TEST_CASE(test_same_region)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;


	auto region = std::make_shared<parallel_region>();

	std::vector<int> test_sink;
	auto write_param = [&](int i) {test_sink.push_back(i);};
	test_in_port test_in(region, write_param);
	test_out_port test_out(region);

	static_assert(is_passive_sink<test_in_port>::value, "");
	static_assert(has_result<test_out_port>::value,
			"its an out port, that has result_t defined");

	test_out >> test_in;

	BOOST_CHECK_EQUAL(test_sink.size(), 0);
	test_out.fire(1);
	BOOST_CHECK_EQUAL(test_sink.size(), 1);
	BOOST_CHECK_EQUAL(test_sink.at(0), 1);

	auto tmp = test_out >> [](int i ){ return ++i;};
	tmp >> test_in;

	test_out.fire(1);
	BOOST_CHECK_EQUAL(test_sink.at(1), 1);
	BOOST_CHECK_EQUAL(test_sink.at(2), 2);
}

BOOST_AUTO_TEST_CASE(test_different_region)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;
	auto region_1 = std::make_shared<parallel_region>("r1");
	auto region_2 = std::make_shared<parallel_region>("r2");

	int test_value = 0;
	auto write_param = [&test_value](int i) {test_value = i;};
	test_in_port test_in(region_2, write_param);
	test_out_port test_out(region_1);

	test_out >> test_in;

	BOOST_CHECK_EQUAL(test_value, 0);
	test_out.fire(1);
	//since we have a region transition, we need a switch tick
	BOOST_CHECK_EQUAL(test_value, 0);

	region_1->ticks.in_switch_buffers()();
	BOOST_CHECK_EQUAL(test_value, 0);
	region_2->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 1);
}

BOOST_AUTO_TEST_CASE(test_connectable_in_between)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;
	auto region_1 = std::make_shared<parallel_region>("r1");
	auto region_2 = std::make_shared<parallel_region>("r2");

	int test_value = 0;
	auto write_param = [&test_value](int i) {test_value = i;};
	test_in_port test_in(region_2, write_param);
	test_out_port test_out(region_1);

	test_out >> [](int i){ return i+1;} >> test_in;

	BOOST_CHECK_EQUAL(test_value, 0);
	test_out.fire(1);
	//since we have a region transition, we need a switch tick
	BOOST_CHECK_EQUAL(test_value, 0);

	region_1->ticks.in_switch_buffers()();
	BOOST_CHECK_EQUAL(test_value, 0);
	region_2->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 2);

	// test more than one lambda in between
	auto region_3 = std::make_shared<parallel_region>("r3");

	test_in_port test_in_2(region_3, write_param);
	test_out >>
			[](int i){ return i+1;} >>
			[](int i){ return i*2;} >>
			test_in_2;

	test_out.fire(1);
	region_1->ticks.in_switch_buffers()();
	region_2->ticks.in_work()();
//wrong region ticked, expect no change
	BOOST_CHECK_EQUAL(test_value, 2);

	region_3->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 4);
}

BOOST_AUTO_TEST_CASE(test_state_transition)
{
	typedef region_aware<state_sink<int>> test_in_port;<commit_msg>Add proof for connection limitations bug refs #19759 region_aware ports cannot be connected in some associative cases with multiple lambdas.<commit_after>#include <boost/test/unit_test.hpp>

#include <ports/ports.hpp>
#include <ports/region_aware.hpp>

using namespace fc;

BOOST_AUTO_TEST_SUITE(test_parallle_region);

BOOST_AUTO_TEST_CASE(test_region_aware_node)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	auto region = std::make_shared<parallel_region>();

	int test_value = 0;
	auto write_param = [&](int i) {test_value = i;};
	test_in_port test_in(region, write_param);

	BOOST_CHECK_EQUAL(test_value, 0);
	test_in(1);
	BOOST_CHECK_EQUAL(test_value, 1);
}


BOOST_AUTO_TEST_CASE(test_same_region)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;


	auto region = std::make_shared<parallel_region>();

	std::vector<int> test_sink;
	auto write_param = [&](int i) {test_sink.push_back(i);};
	test_in_port test_in(region, write_param);
	test_out_port test_out(region);

	static_assert(is_passive_sink<test_in_port>::value, "");
	static_assert(has_result<test_out_port>::value,
			"its an out port, that has result_t defined");

	test_out >> test_in;

	BOOST_CHECK_EQUAL(test_sink.size(), 0);
	test_out.fire(1);
	BOOST_CHECK_EQUAL(test_sink.size(), 1);
	BOOST_CHECK_EQUAL(test_sink.at(0), 1);

	auto tmp = test_out >> [](int i ){ return ++i;};
	tmp >> test_in;

	test_out.fire(1);
	BOOST_CHECK_EQUAL(test_sink.at(1), 1);
	BOOST_CHECK_EQUAL(test_sink.at(2), 2);
}

BOOST_AUTO_TEST_CASE(test_different_region)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;
	auto region_1 = std::make_shared<parallel_region>("r1");
	auto region_2 = std::make_shared<parallel_region>("r2");

	int test_value = 0;
	auto write_param = [&test_value](int i) {test_value = i;};
	test_in_port test_in(region_2, write_param);
	test_out_port test_out(region_1);

	test_out >> test_in;

	BOOST_CHECK_EQUAL(test_value, 0);
	test_out.fire(1);
	//since we have a region transition, we need a switch tick
	BOOST_CHECK_EQUAL(test_value, 0);

	region_1->ticks.in_switch_buffers()();
	BOOST_CHECK_EQUAL(test_value, 0);
	region_2->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 1);
}

BOOST_AUTO_TEST_CASE(test_connectable_in_between)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;
	auto region_1 = std::make_shared<parallel_region>("r1");
	auto region_2 = std::make_shared<parallel_region>("r2");

	int test_value = 0;
	auto write_param = [&test_value](int i) {test_value = i;};
	test_in_port test_in(region_2, write_param);
	test_out_port test_out(region_1);

	test_out >> [](int i){ return i+1;} >> test_in;

	BOOST_CHECK_EQUAL(test_value, 0);
	test_out.fire(1);
	//since we have a region transition, we need a switch tick
	BOOST_CHECK_EQUAL(test_value, 0);

	region_1->ticks.in_switch_buffers()();
	BOOST_CHECK_EQUAL(test_value, 0);
	region_2->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 2);

	// test more than one lambda in between
	auto region_3 = std::make_shared<parallel_region>("r3");

	test_in_port test_in_2(region_3, write_param);
	test_out >>
			[](int i){ return i+1;} >>
			[](int i){ return i*2;} >>
			test_in_2;

	test_out.fire(1);
	region_1->ticks.in_switch_buffers()();
	region_2->ticks.in_work()();
//wrong region ticked, expect no change
	BOOST_CHECK_EQUAL(test_value, 2);

	region_3->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 4);
}

BOOST_AUTO_TEST_CASE(test_multiple_connectable_in_between)
{
	typedef region_aware<event_in_port<int>> test_in_port;
	typedef region_aware<event_out_port<int>> test_out_port;
	auto region_1 = std::make_shared<parallel_region>("r1");
	auto region_2 = std::make_shared<parallel_region>("r2");

	int test_value = 0;
	auto write_param = [&test_value](int i) {test_value = i;};
	test_in_port test_in(region_2, write_param);
	test_out_port test_out(region_1);

	auto inc = [](int i){ return i + 1; };

#warning this does not compile
#warning FIXME region connection limitations
//	(test_out >> inc) >> inc >> (inc >> test_in);
	test_out >> inc >> inc >> inc >> test_in;

	BOOST_CHECK_EQUAL(test_value, 0);
	test_out.fire(1);
	//since we have a region transition, we need a switch tick
	BOOST_CHECK_EQUAL(test_value, 0);

	region_1->ticks.in_switch_buffers()();
	BOOST_CHECK_EQUAL(test_value, 0);
	region_2->ticks.in_work()();
	BOOST_CHECK_EQUAL(test_value, 4);
}

BOOST_AUTO_TEST_CASE(test_state_transition)
{
	typedef region_aware<state_sink<int>> test_in_port;
{'fraction_non_alphanumeric': 0.13542050337630449, 'fraction_numerical': 0.016206261510128914, 'mean_word_length': 7.089374379344588, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7226083', 'n_tokens_mistral': 3345, 'n_tokens_neox': 3163, 'n_words': 567}