Files
LCUI/lib/thread/tests/test_thread.c
T
Liu faa7058837 test: reorganize test suite with xmake test integration
Restructure the test infrastructure with these changes:

* Extend libctest with ctest_suite_t, ctest_parse_args, ctest_run_suites and CTEST_MAIN macro to standardize test entry points; support --grep / --grep= / --list / --interactive flags; add ctest-tests for self-tests.

* Regroup tests by lib boundary: sink unit tests into lib/<name>/tests/ with <lib>_<topic> entry function names, move fixtures into lib/<name>/tests/fixtures/, rename top-level tests/cases/ to tests/integration/ for cross-lib integration tests.

* Add tests/scenes/ for reusable widget tree builders shared by integration cases and visual preview binaries; add tests/previews/ with 4 diagnostic binaries (flex_layout, block_layout, widget_opacity, scrollbar).

* Rewrite all describe/equal description strings using 'should ...' style or noun topic names; drop redundant lcui_quit/lcui_main calls from integration cases.

* Integrate xmake test via add_tests(\default\) and a shared tests.runnable rule defined in the root xmake.lua; the rule wraps the test binary with drmemory (Windows) or valgrind (Linux) when option memcheck=y.

* Drop the lcui-tests aggregation: lcui-tests now contains only integration cases and scenes, not the union of lib tests. Use xmake test to run everything across libs.

* Fix .clang-format: InsertNewlineAtEndOfFile -> InsertNewlineAtEOF (correct key for clang-format 16+).

* Update AGENTS.md with the new test layout, naming rules, description style and run commands.

* Update CI to use xmake test and xmake f --memcheck=y instead of per-lib runs.
2026-05-24 17:56:23 +08:00

96 lines
2.7 KiB
C

/*
* lib/thread/tests/test_thread.c
*
* Copyright (c) 2023, Liu Chao <i@lc-soft.io> All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* This file is part of LCUI, distributed under the MIT License found in the
* LICENSE.TXT file in the root directory of this source tree.
*/
#include <string.h>
#include <stdio.h>
#include <thread.h>
#include <yutil.h>
#include <ctest.h>
typedef struct TestWorkerRec_ {
char data[32];
int data_count;
bool cancel;
bool active;
thread_cond_t cond;
thread_mutex_t mutex;
thread_t thread;
} TestWorkerRec, *TestWorker;
static void TestWorker_Thread(void *arg)
{
TestWorker worker = arg;
worker->cancel = false;
worker->active = true;
worker->data_count = 0;
thread_mutex_lock(&worker->mutex);
while (!worker->cancel && worker->data_count < 20) {
logger_debug("waiting...\n");
thread_cond_wait(&worker->cond, &worker->mutex);
logger_debug("get data: %s\n", worker->data);
worker->data_count += 1;
}
thread_mutex_unlock(&worker->mutex);
logger_debug("count: %d\n", worker->data_count);
worker->active = false;
thread_exit(NULL);
}
static void TestWorker_Send(TestWorker worker, const char *data)
{
thread_mutex_lock(&worker->mutex);
strcpy(worker->data, data);
thread_cond_signal(&worker->cond);
thread_mutex_unlock(&worker->mutex);
}
static void TestWorker_Init(TestWorker worker)
{
thread_mutex_init(&worker->mutex);
thread_cond_init(&worker->cond);
}
static void TestWorker_Destroy(TestWorker worker)
{
thread_mutex_lock(&worker->mutex);
worker->cancel = true;
thread_cond_signal(&worker->cond);
thread_mutex_unlock(&worker->mutex);
thread_join(worker->thread, NULL);
thread_mutex_destroy(&worker->mutex);
thread_cond_destroy(&worker->cond);
}
void test_thread_worker(void)
{
TestWorkerRec worker;
TestWorker_Init(&worker);
thread_create(&worker.thread, TestWorker_Thread, &worker);
sleep_ms(100);
TestWorker_Send(&worker, "hello");
sleep_ms(20);
TestWorker_Send(&worker, "world");
sleep_ms(100);
TestWorker_Send(&worker, "this");
sleep_ms(50);
TestWorker_Send(&worker, "is");
sleep_ms(100);
TestWorker_Send(&worker, "test");
sleep_ms(50);
TestWorker_Send(&worker, "bye!");
sleep_ms(100);
TestWorker_Destroy(&worker);
ctest_equal_int("should receive 7 data items", worker.data_count, 7);
ctest_equal_bool("should no longer be active", worker.active, false);
}