<commit_before>/**
 * \file
 * \brief ThreadOperationsTestCase class implementation
 *
 * \author Copyright (C) 2015-2017 <NAME> http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "ThreadOperationsTestCase.hpp"

#include "waitForNextTick.hpp"

#include "distortos/DynamicThread.hpp"
#include "distortos/InterruptMaskingLock.hpp"
#include "distortos/StaticThread.hpp"
#include "distortos/statistics.hpp"
#include "distortos/ThisThread.hpp"

#include <malloc.h>

#include <cerrno>

namespace distortos
{

namespace test
{

namespace
{

/*---------------------------------------------------------------------------------------------------------------------+
| local functions
+---------------------------------------------------------------------------------------------------------------------*/

/**
 * \brief Empty function used for test threads
 */

void emptyFunction()
{

}

/**
 * \brief Phase 1 of test case
 *
 * Tests behavior of creating, starting and joining the test thread.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase1()
{
	auto testThread = makeDynamicThread({192, 1}, emptyFunction);

	// state of created (but not yet started) thread must be "ThreadState::created"
	if (testThread.getState() != ThreadState::created)
		return false;

	bool result {true};

	{
		const InterruptMaskingLock interruptMaskingLock;	// make sure the thread doesn't get to run yet

		{
			const auto ret = testThread.start();
			if (ret != 0)
				return false;
		}

		if (testThread.getState() != ThreadState::runnable)	// started thread's state must be "ThreadState::runnable"
			result = false;

		{
			const auto ret = testThread.start();	// attempting to start a thread that is already started must fail
			if (ret != EINVAL)
				result = false;
		}
	}

	{
		const auto ret = testThread.join();
		if (ret != 0)
			result = false;
	}

	// terminated thread's state must be "ThreadState::terminated"
	if (testThread.getState() != ThreadState::terminated)
		result = false;

	{
		const auto ret = testThread.start();	// attempting to start a thread that is already started must fail
		if (ret != EINVAL)
			result = false;
	}

	return result;
}

/**
 * \brief Phase 2 of test case
 *
 * Tests behavior Thread::start() and Thread::join() when used with current thread.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase2()
{
	{
		const auto ret = ThisThread::get().start();	// attempting to start current thread must fail
		if (ret != EINVAL)
			return false;
	}

	{
		const auto ret = ThisThread::get().join();	// attempting to join current thread must fail
		if (ret != EDEADLK)
			return false;
	}

	return true;
}

/**
 * \brief Phase 3 of test case
 *
 * Tests scenarios involving thread detachment.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase3()
{
#ifdef CONFIG_THREAD_DETACH_ENABLE

	const auto allocatedMemory = mallinfo().uordblks;
	const auto lambda =
			[](int& sharedRet)
			{
				sharedRet = ThisThread::detach();
			};

	// attempt to detach static thread must fail with ENOTSUP
	{
		int sharedRet {};<commit_msg>Remove test of starting current thread<commit_after>/**
 * \file
 * \brief ThreadOperationsTestCase class implementation
 *
 * \author Copyright (C) 2015-2017 <NAME> http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "ThreadOperationsTestCase.hpp"

#include "waitForNextTick.hpp"

#include "distortos/DynamicThread.hpp"
#include "distortos/InterruptMaskingLock.hpp"
#include "distortos/StaticThread.hpp"
#include "distortos/statistics.hpp"
#include "distortos/ThisThread.hpp"

#include <malloc.h>

#include <cerrno>

namespace distortos
{

namespace test
{

namespace
{

/*---------------------------------------------------------------------------------------------------------------------+
| local functions
+---------------------------------------------------------------------------------------------------------------------*/

/**
 * \brief Empty function used for test threads
 */

void emptyFunction()
{

}

/**
 * \brief Phase 1 of test case
 *
 * Tests behavior of creating, starting and joining the test thread.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase1()
{
	auto testThread = makeDynamicThread({192, 1}, emptyFunction);

	// state of created (but not yet started) thread must be "ThreadState::created"
	if (testThread.getState() != ThreadState::created)
		return false;

	bool result {true};

	{
		const InterruptMaskingLock interruptMaskingLock;	// make sure the thread doesn't get to run yet

		{
			const auto ret = testThread.start();
			if (ret != 0)
				return false;
		}

		if (testThread.getState() != ThreadState::runnable)	// started thread's state must be "ThreadState::runnable"
			result = false;

		{
			const auto ret = testThread.start();	// attempting to start a thread that is already started must fail
			if (ret != EINVAL)
				result = false;
		}
	}

	{
		const auto ret = testThread.join();
		if (ret != 0)
			result = false;
	}

	// terminated thread's state must be "ThreadState::terminated"
	if (testThread.getState() != ThreadState::terminated)
		result = false;

	{
		const auto ret = testThread.start();	// attempting to start a thread that is already started must fail
		if (ret != EINVAL)
			result = false;
	}

	return result;
}

/**
 * \brief Phase 2 of test case
 *
 * Tests behavior of joining current thread.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase2()
{
	{
		const auto ret = ThisThread::get().join();	// attempting to join current thread must fail
		if (ret != EDEADLK)
			return false;
	}

	return true;
}

/**
 * \brief Phase 3 of test case
 *
 * Tests scenarios involving thread detachment.
 *
 * \return true if test succeeded, false otherwise
 */

bool phase3()
{
#ifdef CONFIG_THREAD_DETACH_ENABLE

	const auto allocatedMemory = mallinfo().uordblks;
	const auto lambda =
			[](int& sharedRet)
			{
				sharedRet = ThisThread::detach();
			};

	// attempt to detach static thread must fail with ENOTSUP
	{
		int sharedRet {};
{'fraction_non_alphanumeric': 0.1798306389530408, 'fraction_numerical': 0.007390300230946882, 'mean_word_length': 2.629050279329609, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3507185', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1849, 'n_words': 643}