mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
- charset module renamed to encoding (decode/encode kept)
- time: get_time_ms → y_gettime, sleep_ms → y_sleep
- time: delete get_time_us, get_time_delta, sleep_s
- string: strdup2 → y_strdup, wcsdup2 → y_wcsdup
- string: strtolower → y_strlower, strtrim → y_strtrim
- string: wcstrim → y_wcstrim, strhash → y_strhash
- string: strreplace → y_strreplace, strsplit → y_strsplit
- string: delete wcsreplace, cmdsplit
- math: delete y_iround, y_pow2
- timer module migrated from yutil to ptk as internal timer_scheduler
- WinMain simplified to use __argc/__argv instead of cmdsplit
BREAKING CHANGE: all renamed yutil public APIs require downstream
to update function names. Deleted APIs require code changes:
- get_time_us: use y_gettime() for ms timestamps
- get_time_delta: use y_gettime() - start instead
- sleep_s: use y_sleep(ms) with millisecond argument
- wcsreplace: no replacement, had zero callers
- cmdsplit: WinMain now uses __argc/__argv from CRT
- timer_list_*: migrated to ptk timer_scheduler_*;
ptk_reset_timer still available for external use
113 lines
2.9 KiB
C
113 lines
2.9 KiB
C
/*
|
|
* lib/ptk/tests/test_timer_scheduler.c
|
|
*
|
|
* Copyright (c) 2026, Liu Chao <hello@lcui.dev> 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 <ctest.h>
|
|
#include <yutil.h>
|
|
#include "timer_scheduler.h"
|
|
|
|
static timer_scheduler_t *scheduler;
|
|
static int timeout_triggered;
|
|
static int interval_count;
|
|
|
|
static void on_timeout(void *arg)
|
|
{
|
|
timeout_triggered = 1;
|
|
}
|
|
|
|
static void on_interval(void *arg)
|
|
{
|
|
interval_count++;
|
|
}
|
|
|
|
static void create_scheduler(void)
|
|
{
|
|
scheduler = timer_scheduler_create();
|
|
ctest_equal_bool("should create scheduler", scheduler != NULL, true);
|
|
}
|
|
|
|
static void destroy_scheduler(void)
|
|
{
|
|
timer_scheduler_destroy(scheduler);
|
|
}
|
|
|
|
static void test_timer_scheduler_lifecycle(void)
|
|
{
|
|
ctest_describe("lifecycle", create_scheduler);
|
|
ctest_describe("lifecycle", destroy_scheduler);
|
|
}
|
|
|
|
static void test_timeout(void)
|
|
{
|
|
int id;
|
|
|
|
create_scheduler();
|
|
id = timer_scheduler_add_timeout(scheduler, 10, on_timeout, NULL);
|
|
ctest_equal_bool("should return valid id", id != -1, true);
|
|
|
|
timeout_triggered = 0;
|
|
y_sleep(20L);
|
|
timer_scheduler_process(scheduler);
|
|
ctest_equal_int("should trigger timeout callback", timeout_triggered,
|
|
1);
|
|
|
|
destroy_scheduler();
|
|
}
|
|
|
|
static void test_interval(void)
|
|
{
|
|
int id;
|
|
|
|
create_scheduler();
|
|
id = timer_scheduler_add_interval(scheduler, 10, on_interval, NULL);
|
|
ctest_equal_bool("should return valid id", id != -1, true);
|
|
|
|
interval_count = 0;
|
|
y_sleep(20L);
|
|
timer_scheduler_process(scheduler);
|
|
ctest_equal_bool("should trigger interval callback at least once",
|
|
interval_count > 0, true);
|
|
|
|
timer_scheduler_remove(scheduler, id);
|
|
interval_count = 0;
|
|
y_sleep(20L);
|
|
timer_scheduler_process(scheduler);
|
|
ctest_equal_int("should stop after remove", interval_count, 0);
|
|
|
|
destroy_scheduler();
|
|
}
|
|
|
|
static void test_remove(void)
|
|
{
|
|
int id;
|
|
|
|
create_scheduler();
|
|
id = timer_scheduler_add_timeout(scheduler, 50, on_timeout, NULL);
|
|
timer_scheduler_remove(scheduler, id);
|
|
timeout_triggered = 0;
|
|
y_sleep(60L);
|
|
timer_scheduler_process(scheduler);
|
|
ctest_equal_int("should not trigger after remove", timeout_triggered,
|
|
0);
|
|
|
|
ctest_equal_int("should return -1 for unknown id",
|
|
timer_scheduler_remove(scheduler, 999), -1);
|
|
|
|
destroy_scheduler();
|
|
}
|
|
|
|
void test_ptk_timer_scheduler(void)
|
|
{
|
|
ctest_describe("lifecycle", test_timer_scheduler_lifecycle);
|
|
ctest_describe("timeout", test_timeout);
|
|
ctest_describe("interval", test_interval);
|
|
ctest_describe("remove", test_remove);
|
|
}
|