<commit_before>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

/// @file tc_sched.c

/// @brief Test Case Example for Sched API

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include <tinyara/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <tinyara/sched.h>
#include <tinyara/os_api_test_drv.h>

#include "tc_internal.h"

#define SCHED_PRIORITY  13
#define LOOPCOUNT       2
#define ARRLEN          2
#define VAL_3           3
#define VAL_5           5
#define TASK_STACKSIZE  2048
#define INVALID_PID     -2
#define PID_IDLE        0
#define TASK_CANCEL_INVALID  -1

pthread_t thread1, thread2;

pid_t g_task_pid;
bool g_callback = false;
bool g_pthread_callback = true;

/**
* @fn                   :sched_foreach_callback
* @description          :Function for tc_sched_sched_foreach
* @return               :void
*/
static void sched_foreach_callback(struct tcb_s *tcb, void *arg)
{
	/* it enumerate every task tcb, that means tcb created for current process will also be enumerated */
	if (tcb->pid == g_task_pid) {
		g_callback = true;
	}
}

#ifdef CONFIG_SCHED_WAITPID
static int sleep1sec_taskdel(int argc, char *argv[])
{
	sleep(1);
	task_delete(0);
	return 0;
}

#ifdef CONFIG_SCHED_HAVE_PARENT
static int sleep2sec_taskdel(int argc, char *argv[])
{
	sleep(2);
	task_delete(0);
	return 0;
}
#endif /* CONFIG_SCHED_HAVE_PARENT */
#endif /* CONFIG_SCHED_WAITPID */

/**
* @fn                   :threadfunc_callback
* @description          :Function for tc_sched_sched_yield
* @return               :void*
*/
static void *threadfunc_callback(void *param)
{
	g_pthread_callback = true;
	sleep(VAL_3);
	sched_yield();
	pthread_exit((pthread_addr_t)1);
	/* yield to another thread, g_pthread_callback will remain true in main process */
	g_pthread_callback = false;
	return NULL;
}

/**
* @fn                   :tc_sched_sched_setget_scheduler_param
* @brief                :set and get scheduler policies for the named process
* @scenario             :set and get scheduler policies for the named process, sched_getscheduler should return scheduler set
* API's covered         :sched_setscheduler, sched_getscheduler
* Preconditions         :none
* Postconditions        :none
* @return               :void
*/
static void tc_sched_sched_setget_scheduler_param(void)
{
	int ret_chk = ERROR;
	struct sched_param st_setparam;
	struct sched_param st_getparam;
	struct sched_param prio_origin;
	int loop_cnt = LOOPCOUNT;
	int arr_idx = 0;
	int sched_arr[ARRLEN] = { SCHED_RR, SCHED_FIFO };

	/* get original priority of task into prio_origin */
	ret_chk = sched_getparam(getpid(), &prio_origin);
	TC_ASSERT_EQ("sched_getparam", ret_chk, OK);

	/*  Check null priority parameter */

	ret_chk = sched_setparam(getpid(), NULL);
	TC_ASSERT_EQ_CLEANUP("sched_setparam", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	ret_chk = sched_getparam(getpid(), NULL);
	TC_ASSERT_EQ_CLEANUP("sched_getparam", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	/*  Check invalid priority parameter */

	st_setparam.sched_priority = SCHED_OTHER;
	ret_chk = sched_setscheduler(0, SCHED_OTHER, &st_setparam);
	TC_ASSERT_EQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	/*  Check for invalid scheduling policy */

	ret_chk = sched_setscheduler(0, SCHED_OTHER, &st_setparam);
	TC_ASSERT_EQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));
	TC_ASSERT_EQ("sched_setscheduler", errno, EINVAL);

	while (arr_idx < loop_cnt) {
		st_setparam.sched_priority = SCHED_PRIORITY;
		ret_chk = sched_setparam(getpid(), &st_setparam);
		TC_ASSERT_EQ_CLEANUP("sched_setparam", ret_chk, OK, sched_setparam(getpid(), &prio_origin));

		ret_chk = sched_setscheduler(getpid(), sched_arr[arr_idx], &st_setparam);
		TC_ASSERT_NEQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

		/* ret_chk should be SCHED set */
		ret_chk = sched_getscheduler(getpid());
		TC_ASSERT_EQ_CLEANUP("sched_getscheduler", ret_chk, sched_arr[arr_idx], sched_setparam(getpid(), &prio_origin));

		ret_chk = sched_getparam(getpid(), &st_getparam);
		TC_ASSERT_EQ_CLEANUP("sched_getparam", ret_chk, OK, sched_setparam(getpid(), &prio_origin));
		TC_ASSERT_EQ_CLEANUP("sched_getparam", st_setparam.sched_priority, st_getparam.sched_priority, sched_setparam(getpid(), &prio_origin));
		arr_idx++;
	}

	/* restore the task priority as previous after testing */
	ret_chk = sched_setparam(getpid(), &prio_origin);
	TC_ASSERT_EQ_CLEANUP("sched_setparam", ret_chk, OK, sched_setparam(getpid(), &prio_origin));

	TC_SUCCESS_RESULT();
}<commit_msg>testcase/kernel: Restore original scheduler and priority after TC execution in tc_sched<commit_after>/****************************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

/// @file tc_sched.c

/// @brief Test Case Example for Sched API

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include <tinyara/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <tinyara/sched.h>
#include <tinyara/os_api_test_drv.h>

#include "tc_internal.h"

#define SCHED_PRIORITY  13
#define LOOPCOUNT       2
#define ARRLEN          2
#define VAL_3           3
#define VAL_5           5
#define TASK_STACKSIZE  2048
#define INVALID_PID     -2
#define PID_IDLE        0
#define TASK_CANCEL_INVALID  -1

pthread_t thread1, thread2;

pid_t g_task_pid;
bool g_callback = false;
bool g_pthread_callback = true;

/**
* @fn                   :sched_foreach_callback
* @description          :Function for tc_sched_sched_foreach
* @return               :void
*/
static void sched_foreach_callback(struct tcb_s *tcb, void *arg)
{
	/* it enumerate every task tcb, that means tcb created for current process will also be enumerated */
	if (tcb->pid == g_task_pid) {
		g_callback = true;
	}
}

#ifdef CONFIG_SCHED_WAITPID
static int sleep1sec_taskdel(int argc, char *argv[])
{
	sleep(1);
	task_delete(0);
	return 0;
}

#ifdef CONFIG_SCHED_HAVE_PARENT
static int sleep2sec_taskdel(int argc, char *argv[])
{
	sleep(2);
	task_delete(0);
	return 0;
}
#endif /* CONFIG_SCHED_HAVE_PARENT */
#endif /* CONFIG_SCHED_WAITPID */

/**
* @fn                   :threadfunc_callback
* @description          :Function for tc_sched_sched_yield
* @return               :void*
*/
static void *threadfunc_callback(void *param)
{
	g_pthread_callback = true;
	sleep(VAL_3);
	sched_yield();
	pthread_exit((pthread_addr_t)1);
	/* yield to another thread, g_pthread_callback will remain true in main process */
	g_pthread_callback = false;
	return NULL;
}

/**
* @fn                   :tc_sched_sched_setget_scheduler_param
* @brief                :set and get scheduler policies for the named process
* @scenario             :set and get scheduler policies for the named process, sched_getscheduler should return scheduler set
* API's covered         :sched_setscheduler, sched_getscheduler
* Preconditions         :none
* Postconditions        :none
* @return               :void
*/
static void tc_sched_sched_setget_scheduler_param(void)
{
	int ret_chk = ERROR;
	struct sched_param st_setparam;
	struct sched_param st_getparam;
	struct sched_param prio_origin;
	int scheduler_origin;
	int loop_cnt = LOOPCOUNT;
	int arr_idx = 0;
	int sched_arr[ARRLEN] = { SCHED_RR, SCHED_FIFO };

	/* get original priority and scheduler of task */
	ret_chk = sched_getparam(getpid(), &prio_origin);
	TC_ASSERT_EQ("sched_getparam", ret_chk, OK);

	scheduler_origin = sched_getscheduler(getpid());
	TC_ASSERT_NEQ("sched_getscheduler", scheduler_origin, ERROR);

	/*  Check null priority parameter */

	ret_chk = sched_setparam(getpid(), NULL);
	TC_ASSERT_EQ_CLEANUP("sched_setparam", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	ret_chk = sched_getparam(getpid(), NULL);
	TC_ASSERT_EQ_CLEANUP("sched_getparam", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	/*  Check invalid priority parameter */

	st_setparam.sched_priority = SCHED_OTHER;
	ret_chk = sched_setscheduler(0, SCHED_OTHER, &st_setparam);
	TC_ASSERT_EQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

	/*  Check for invalid scheduling policy */

	ret_chk = sched_setscheduler(0, SCHED_OTHER, &st_setparam);
	TC_ASSERT_EQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));
	TC_ASSERT_EQ("sched_setscheduler", errno, EINVAL);

	while (arr_idx < loop_cnt) {
		st_setparam.sched_priority = SCHED_PRIORITY;
		ret_chk = sched_setparam(getpid(), &st_setparam);
		TC_ASSERT_EQ_CLEANUP("sched_setparam", ret_chk, OK, sched_setparam(getpid(), &prio_origin));

		ret_chk = sched_setscheduler(getpid(), sched_arr[arr_idx], &st_setparam);
		TC_ASSERT_NEQ_CLEANUP("sched_setscheduler", ret_chk, ERROR, sched_setparam(getpid(), &prio_origin));

		/* ret_chk should be SCHED set */
		ret_chk = sched_getscheduler(getpid());
		TC_ASSERT_EQ_CLEANUP("sched_getscheduler", ret_chk, sched_arr[arr_idx], sched_setparam(getpid(), &prio_origin));

		ret_chk = sched_getparam(getpid(), &st_getparam);
		TC_ASSERT_EQ_CLEANUP("sched_getparam", ret_chk, OK, sched_setparam(getpid(), &prio_origin));
		TC_ASSERT_EQ_CLEANUP("sched_getparam", st_setparam.sched_priority, st_getparam.sched_priority, sched_setparam(getpid(), &prio_origin));
		arr_idx++;
	}

	/* restore the task priority and scheduler as previous after testing */
	ret_chk = sched_setscheduler(getpid(), scheduler_origin, &prio_origin);
	TC_ASSERT_NEQ("sched_setscheduler", ret_chk, ERROR);

	TC_SUCCESS_RESULT();
}
{'fraction_non_alphanumeric': 0.15411558669001751, 'fraction_numerical': 0.006654991243432574, 'mean_word_length': 4.926829268292683, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '1879062', 'n_tokens_mistral': 3798, 'n_tokens_neox': 3717, 'n_words': 912}