#include <stdio.h>
#include <errno.h>
#include <kc.h>
#include <kc_ut.h>
#include <kc_assert.h>
#include <kc_memory.h>
#include <kc_threads.h>
#include "ut.h"
// プロトタイプ宣言
static void test_thread_new(void);
static void test_thread_sleep(void);
static void test_thread_memory_error(void);
/**
* KcThread 単体テストスイート
*/
void suite_thread(void)
{
KcUt *ut = KcUt_get_instance();
ut->add(ut, UT_TESTCASE, "thread new/delete", test_thread_new);
ut->add(ut, UT_TESTCASE, "thread sleep", test_thread_sleep);
ut->add(ut, UT_TESTCASE, "thread memory error", test_thread_memory_error);
}
static int pass_value = 0;
static int test_func(void *args)
{
int *value = (int *)args;
if (*value == 1)
{
pass_value &= 0x01;
}
if (*value == 2)
{
pass_value &= 0x02;
}
for (int i = 0; i < 10; i++)
{
printf("Thread %d : %03d\n", *value, i);
KcThread_msleep(1, true);
}
return 0;
}
/**
* Thread 生成/破棄。
*
* @process KcThread_new を実行する。。
* @result KcThread が生成されること。
*
* @process KcThread_delete にて Thread を破棄する。
* @result Thread が破棄されること。
*/
static void test_thread_new(void)
{
KcThread *thread_1 = KcThread_new(test_func);
KcThread *thread_2 = KcThread_new(test_func);
pass_value = 0;
bool is_alive_1 = thread_1->is_alive(thread_1);
bool is_alive_2 = thread_2->is_alive(thread_2);
assert_false(is_alive_1);
assert_false(is_alive_2);
int val_1 = 1;
int val_2 = 2;
thread_1->start(thread_1, &val_1);
thread_2->start(thread_2, &val_2);
KcThread_msleep(10, false);
is_alive_1 = thread_1->is_alive(thread_1);
is_alive_2 = thread_2->is_alive(thread_2);
assert_true(is_alive_1);
assert_true(is_alive_2);
thread_1->join(thread_1);
thread_2->join(thread_2);
KcThread_delete(thread_1);
KcThread_delete(thread_2);
}
/**
* Thread sleep テスト。
*/
static void test_thread_sleep(void)
{
KcThread_sleep(0, 100, false);
}
/**
* メモリ確保エラー
*/
static void test_thread_memory_error(void)
{
ut_alloc_control(0)
{
KcThread *thread = KcThread_new(test_func);
assert_null(thread);
}
}