tests: kernel: Move k_thread_foreach() API test to thread_apis test

Move k_thread_foreach() API test from tests/kernel/profiling to
tests/kernel/threads/thread_apis.

Fixes #7966

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
diff --git a/tests/kernel/profiling/profiling_api/src/main.c b/tests/kernel/profiling/profiling_api/src/main.c
index 180393d..d3aa818 100644
--- a/tests/kernel/profiling/profiling_api/src/main.c
+++ b/tests/kernel/profiling/profiling_api/src/main.c
@@ -15,14 +15,6 @@
 static struct k_work work[NUM_OF_WORK];
 static struct k_sem sync_sema;
 
-#define STACKSIZE 512
-K_THREAD_STACK_DEFINE(test_stack, STACKSIZE);
-__kernel struct k_thread test_thread;
-
-static int tcount;
-static bool thread_flag;
-static bool stack_flag;
-
 /**TESTPOINT: stack analyze*/
 static void tdata_dump_callback(const struct k_thread *thread, void *user_data)
 {
@@ -81,79 +73,13 @@
 	}
 }
 
-static void thread_entry(void *p1, void *p2, void *p3)
-{
-	k_sleep(SLEEP_MS);
-}
-
-static void thread_callback(const struct k_thread *thread, void *user_data)
-{
-	char *str = (char *)user_data;
-
-	if (thread == &test_thread) {
-		TC_PRINT("%s: Newly added thread found\n", str);
-		TC_PRINT("%s: tid: %p, prio: %d\n",
-				str, thread, thread->base.prio);
-		thread_flag = true;
-	}
-
-	if ((char *)thread->stack_info.start ==
-			K_THREAD_STACK_BUFFER(test_stack)) {
-		TC_PRINT("%s: Newly added thread stack found\n", str);
-		TC_PRINT("%s: stack:%p, size:%u\n", str,
-					(char *)thread->stack_info.start,
-					thread->stack_info.size);
-		stack_flag = true;
-	}
-
-	tcount++;
-}
-
-static void test_k_thread_foreach(void)
-{
-	int count;
-
-	/* Call k_thread_foreach() and check
-	 * thread_callback is getting called.
-	 */
-	k_thread_foreach(thread_callback, TEST_STRING);
-
-	/* Check thread_count non-zero, thread_flag
-	 * and stack_flag are not set.
-	 */
-	zassert_true(tcount && !thread_flag && !stack_flag,
-				"thread_callback() not getting called");
-	/* Save the initial thread count */
-	count = tcount;
-
-	/* Create new thread which should add a new entry to the thread list */
-	k_tid_t tid = k_thread_create(&test_thread, test_stack,
-			STACKSIZE, (k_thread_entry_t)thread_entry, NULL,
-			NULL, NULL, K_PRIO_PREEMPT(0), 0, 0);
-	k_sleep(1);
-
-	/* Call k_thread_foreach() and check
-	 * thread_callback is getting called for
-	 * the newly added thread.
-	 */
-	tcount = 0;
-	k_thread_foreach(thread_callback, TEST_STRING);
-
-	/* Check thread_count > temp, thread_flag and stack_flag are set */
-	zassert_true((tcount > count) && thread_flag && stack_flag,
-					"thread_callback() not getting called");
-	k_thread_abort(tid);
-}
-
 /*TODO: add test case to capture the usage of interrupt call stack*/
 
-
 void test_main(void)
 {
 	ztest_test_suite(profiling_api,
 			 ztest_unit_test(test_call_stacks_analyze_main),
 			 ztest_unit_test(test_call_stacks_analyze_idle),
-			 ztest_unit_test(test_call_stacks_analyze_workq),
-			 ztest_unit_test(test_k_thread_foreach));
+			 ztest_unit_test(test_call_stacks_analyze_workq));
 	ztest_run_test_suite(profiling_api);
 }
diff --git a/tests/kernel/threads/thread_apis/prj.conf b/tests/kernel/threads/thread_apis/prj.conf
index 7531996..5cf0fbd 100644
--- a/tests/kernel/threads/thread_apis/prj.conf
+++ b/tests/kernel/threads/thread_apis/prj.conf
@@ -1,4 +1,5 @@
 CONFIG_ZTEST=y
 CONFIG_THREAD_MONITOR=y
-CONFIG_HEAP_MEM_POOL_SIZE=256
 CONFIG_THREAD_CUSTOM_DATA=y
+CONFIG_THREAD_STACK_INFO=y
+CONFIG_HEAP_MEM_POOL_SIZE=256
diff --git a/tests/kernel/threads/thread_apis/src/main.c b/tests/kernel/threads/thread_apis/src/main.c
index fe1b4a9..000a13c 100644
--- a/tests/kernel/threads/thread_apis/src/main.c
+++ b/tests/kernel/threads/thread_apis/src/main.c
@@ -28,6 +28,7 @@
 extern void test_essential_thread_operation(void);
 extern void test_threads_priority_set(void);
 extern void test_delayed_thread_abort(void);
+extern void test_k_thread_foreach(void);
 
 __kernel struct k_thread tdata;
 #define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
@@ -135,7 +136,8 @@
 			 ztest_unit_test(test_systhreads_main),
 			 ztest_unit_test(test_systhreads_idle),
 			 ztest_unit_test(test_customdata_get_set_coop),
-			 ztest_user_unit_test(test_customdata_get_set_preempt)
+			 ztest_user_unit_test(test_customdata_get_set_preempt),
+			 ztest_unit_test(test_k_thread_foreach)
 			 );
 
 	ztest_run_test_suite(threads_lifecycle);
diff --git a/tests/kernel/threads/thread_apis/src/test_kthread_for_each.c b/tests/kernel/threads/thread_apis/src/test_kthread_for_each.c
new file mode 100644
index 0000000..d7cfa8a
--- /dev/null
+++ b/tests/kernel/threads/thread_apis/src/test_kthread_for_each.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2018 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <ztest.h>
+#include <irq_offload.h>
+#include <misc/stack.h>
+
+#define SLEEP_MS 100
+#define TEST_STRING "TEST"
+
+#define STACKSIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
+K_THREAD_STACK_EXTERN(tstack);
+extern struct k_thread tdata;
+
+static int tcount;
+static bool thread_flag;
+static bool stack_flag;
+
+static void thread_entry(void *p1, void *p2, void *p3)
+{
+	k_sleep(SLEEP_MS);
+}
+
+static void thread_callback(const struct k_thread *thread, void *user_data)
+{
+	char *str = (char *)user_data;
+
+	if (thread == &tdata) {
+		TC_PRINT("%s: Newly added thread found\n", str);
+		TC_PRINT("%s: tid: %p, prio: %d\n",
+				str, thread, thread->base.prio);
+		thread_flag = true;
+	}
+
+	if ((char *)thread->stack_info.start ==
+			K_THREAD_STACK_BUFFER(tstack)) {
+		TC_PRINT("%s: Newly added thread stack found\n", str);
+		TC_PRINT("%s: stack:%p, size:%u\n", str,
+					(char *)thread->stack_info.start,
+					thread->stack_info.size);
+		stack_flag = true;
+	}
+
+	tcount++;
+}
+
+/**
+ * @ingroup kernel_thread_tests
+ * @brief Test k_thread_foreach API
+ *
+ * @details Call k_thread_foreach() at the beginning of the test and
+ * call it again after creating a thread, See k_thread_foreach()
+ * iterates over the newly created thread and calls the user passed
+ * callback function.
+ *
+ * @see k_thread_foreach()
+ */
+void test_k_thread_foreach(void)
+{
+	int count;
+
+	k_thread_foreach(thread_callback, TEST_STRING);
+
+	/* Check thread_count non-zero, thread_flag
+	 * and stack_flag are not set.
+	 */
+	zassert_true(tcount && !thread_flag && !stack_flag,
+				"thread_callback() not getting called");
+	/* Save the initial thread count */
+	count = tcount;
+
+	/* Create new thread which should add a new entry to the thread list */
+	k_tid_t tid = k_thread_create(&tdata, tstack,
+			STACKSIZE, (k_thread_entry_t)thread_entry, NULL,
+			NULL, NULL, K_PRIO_PREEMPT(0), 0, 0);
+	k_sleep(1);
+
+	/* Call k_thread_foreach() and check
+	 * thread_callback is getting called for
+	 * the newly added thread.
+	 */
+	tcount = 0;
+	k_thread_foreach(thread_callback, TEST_STRING);
+
+	/* Check thread_count > temp, thread_flag and stack_flag are set */
+	zassert_true((tcount > count) && thread_flag && stack_flag,
+					"thread_callback() not getting called");
+	k_thread_abort(tid);
+}
+