<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_umm_heap.c

/// @brief Test Case Example for Umm Heap API

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include <tinyara/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <sys/wait.h>
#include <tinyara/mm/mm.h>
#include <tinyara/sched.h>
#include "tc_internal.h"

/****************************************************************************
 * Definitions
 ****************************************************************************/

#define ALLOC_SIZE_VAL 10
#define ALLOC_FREE_TIMES 5
#define TEST_TIMES 100
#define ALL_FREE 0
#define MEM_REQ_SIZE(unit, iter) (MM_ALIGN_UP((unit) + SIZEOF_MM_ALLOCNODE) * (iter))

/****************************************************************************
 * Private functions
 ****************************************************************************/

static void mem_deallocate_func(int *mem_arr[], int dealloc_size)
{
	int dealloc_cnt;
	for (dealloc_cnt = 0; dealloc_cnt < dealloc_size; dealloc_cnt++) {
		free(mem_arr[dealloc_cnt]);
		mem_arr[dealloc_cnt] = NULL;
	}
}

/**
* @fn                   :tc_umm_heap_malloc_free
* @brief                :Allocate memory through malloc and free it.
* @scenario             :Allocate memory through malloc\n
*                        free allocated memory
* @API's covered        :malloc, free
* @passcase             :When malloc function returns non null memory and memory is null after free.
* @failcase             :When malloc function returns null memory or memory is not null after free.
* @Preconditions        :NA
*/

static void tc_umm_heap_malloc_free(void)
{
	int *mem_ptr[ALLOC_FREE_TIMES] = { NULL };
	int n_alloc;
	int n_test_iter;
	size_t alloc_size = ALLOC_SIZE_VAL * sizeof(int);
	struct mallinfo minfo;
#ifdef CONFIG_DEBUG_MM_HEAPINFO
	pid_t hash_pid = PIDHASH(getpid());;
	struct mm_heap_s *heap;
#endif

	/* Iteration test */

	for (n_test_iter = 0; n_test_iter < TEST_TIMES; n_test_iter++) {
		/* Allocate memory */

		for (n_alloc = 0; n_alloc < ALLOC_FREE_TIMES; n_alloc++) {
			mem_ptr[n_alloc] = (int *)malloc(alloc_size);
			TC_ASSERT_NEQ("malloc", mem_ptr[n_alloc], NULL);
		}

#ifdef CONFIG_DEBUG_MM_HEAPINFO
		/* Verify Allocation */

		heap = mm_get_heap(mem_ptr[n_alloc - 1]);
		TC_ASSERT_NEQ_CLEANUP("mm_get_heap", heap, NULL, mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES));
		TC_ASSERT_EQ_ERROR_CLEANUP("mm_get_heap", heap->alloc_list[hash_pid].curr_alloc_size,
		          MEM_REQ_SIZE(alloc_size, ALLOC_FREE_TIMES), get_errno(), mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES));
#endif

		/* Free allocated memory */

		mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES);

#ifdef CONFIG_DEBUG_MM_HEAPINFO
		/* Verify freeing */

		TC_ASSERT_EQ("mem_deallocate_func", heap->alloc_list[hash_pid].curr_alloc_size, ALL_FREE);
#endif
	}

#if !defined(CONFIG_MM_ASSERT_ON_FAIL)
	/* Test unavailable allocation due to big size */

#ifdef CONFIG_CAN_PASS_STRUCTS
	minfo = mallinfo();
#else
	(void)mallinfo(&minfo);
#endif
	mem_ptr[0] = (int *)malloc(minfo.mxordblk + 1);
	TC_ASSERT_EQ_CLEANUP("malloc", mem_ptr[0], NULL, free(mem_ptr[0]));
#endif

	TC_SUCCESS_RESULT();
}

/**<commit_msg>testcase/kernel: remove unused value<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_umm_heap.c

/// @brief Test Case Example for Umm Heap API

/****************************************************************************
 * Included Files
 ****************************************************************************/
#include <tinyara/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <sys/wait.h>
#include <tinyara/mm/mm.h>
#include <tinyara/sched.h>
#include "tc_internal.h"

/****************************************************************************
 * Definitions
 ****************************************************************************/

#define ALLOC_SIZE_VAL 10
#define ALLOC_FREE_TIMES 5
#define TEST_TIMES 100
#define ALL_FREE 0
#define MEM_REQ_SIZE(unit, iter) (MM_ALIGN_UP((unit) + SIZEOF_MM_ALLOCNODE) * (iter))

/****************************************************************************
 * Private functions
 ****************************************************************************/

static void mem_deallocate_func(int *mem_arr[], int dealloc_size)
{
	int dealloc_cnt;
	for (dealloc_cnt = 0; dealloc_cnt < dealloc_size; dealloc_cnt++) {
		free(mem_arr[dealloc_cnt]);
		mem_arr[dealloc_cnt] = NULL;
	}
}

/**
* @fn                   :tc_umm_heap_malloc_free
* @brief                :Allocate memory through malloc and free it.
* @scenario             :Allocate memory through malloc\n
*                        free allocated memory
* @API's covered        :malloc, free
* @passcase             :When malloc function returns non null memory and memory is null after free.
* @failcase             :When malloc function returns null memory or memory is not null after free.
* @Preconditions        :NA
*/

static void tc_umm_heap_malloc_free(void)
{
	int *mem_ptr[ALLOC_FREE_TIMES] = { NULL };
	int n_alloc;
	int n_test_iter;
	size_t alloc_size = ALLOC_SIZE_VAL * sizeof(int);
#ifdef CONFIG_DEBUG_MM_HEAPINFO
	pid_t hash_pid = PIDHASH(getpid());;
	struct mm_heap_s *heap;
#endif

	/* Iteration test */

	for (n_test_iter = 0; n_test_iter < TEST_TIMES; n_test_iter++) {
		/* Allocate memory */

		for (n_alloc = 0; n_alloc < ALLOC_FREE_TIMES; n_alloc++) {
			mem_ptr[n_alloc] = (int *)malloc(alloc_size);
			TC_ASSERT_NEQ("malloc", mem_ptr[n_alloc], NULL);
		}

#ifdef CONFIG_DEBUG_MM_HEAPINFO
		/* Verify Allocation */

		heap = mm_get_heap(mem_ptr[n_alloc - 1]);
		TC_ASSERT_NEQ_CLEANUP("mm_get_heap", heap, NULL, mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES));
		TC_ASSERT_EQ_ERROR_CLEANUP("mm_get_heap", heap->alloc_list[hash_pid].curr_alloc_size,
		          MEM_REQ_SIZE(alloc_size, ALLOC_FREE_TIMES), get_errno(), mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES));
#endif

		/* Free allocated memory */

		mem_deallocate_func(mem_ptr, ALLOC_FREE_TIMES);

#ifdef CONFIG_DEBUG_MM_HEAPINFO
		/* Verify freeing */

		TC_ASSERT_EQ("mem_deallocate_func", heap->alloc_list[hash_pid].curr_alloc_size, ALL_FREE);
#endif
	}

#if !defined(CONFIG_MM_ASSERT_ON_FAIL)
	/* Test unavailable allocation due to big size */
	struct mallinfo minfo;
#ifdef CONFIG_CAN_PASS_STRUCTS
	minfo = mallinfo();
#else
	(void)mallinfo(&minfo);
#endif
	mem_ptr[0] = (int *)malloc(minfo.mxordblk + 1);
	TC_ASSERT_EQ_CLEANUP("malloc", mem_ptr[0], NULL, free(mem_ptr[0]));
#endif

	TC_SUCCESS_RESULT();
}

/**
{'fraction_non_alphanumeric': 0.24207980652962516, 'fraction_numerical': 0.005562273276904474, 'mean_word_length': 5.072687224669603, 'pattern_counts': {'":': 0, '<': 29, '<?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': '7143577', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2484, 'n_words': 637}