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
167 lines
5.7 KiB
C
167 lines
5.7 KiB
C
/*
|
|
* lib/ptk/tests/test_steptimer.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 <yutil.h>
|
|
#include <ptk/steptimer.h>
|
|
#include <ctest.h>
|
|
|
|
typedef struct steptimer_probe {
|
|
unsigned call_count;
|
|
uint64_t last_elapsed_time;
|
|
} steptimer_probe_t;
|
|
|
|
static void on_steptimer_tick(ptk_steptimer_t *timer, void *data)
|
|
{
|
|
steptimer_probe_t *probe = data;
|
|
|
|
probe->call_count++;
|
|
probe->last_elapsed_time = timer->elapsed_time;
|
|
}
|
|
|
|
static void test_steptimer_init_sets_last_time(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
|
|
ptk_steptimer_init(&timer);
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
// After init, last_time is the wall clock, so the first tick should
|
|
// observe a near-zero delta rather than a huge one.
|
|
ctest_equal_uint("first tick should call handler once",
|
|
probe.call_count, 1);
|
|
ctest_equal_bool("first tick elapsed_time should be small (< 100ms)",
|
|
probe.last_elapsed_time < 100, true);
|
|
}
|
|
|
|
static void test_steptimer_variable_step(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
|
|
ptk_steptimer_init(&timer);
|
|
timer.is_fixed_time_step = false;
|
|
// Simulate 500ms elapsed since last tick.
|
|
timer.last_time = (uint64_t)y_gettime() - 500;
|
|
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
ctest_equal_uint("variable-step tick calls handler exactly once",
|
|
probe.call_count, 1);
|
|
}
|
|
|
|
static void test_steptimer_fixed_step_catch_up(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
bool count_in_range;
|
|
|
|
ptk_steptimer_init(&timer);
|
|
timer.is_fixed_time_step = true;
|
|
timer.enable_catch_up = true;
|
|
timer.target_elapsed_time = 8;
|
|
// max_delta defaults to 1000; a 500ms delta will not be clamped.
|
|
timer.last_time = (uint64_t)y_gettime() - 500;
|
|
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
// Expected: 500 / 8 = 62 catch-up calls. Allow tolerance because the
|
|
// real wall-clock delta may drift by a few milliseconds.
|
|
count_in_range = probe.call_count >= 58 && probe.call_count <= 66;
|
|
ctest_equal_bool(
|
|
"fixed-step catch-up should invoke handler about 62 times",
|
|
count_in_range, true);
|
|
}
|
|
|
|
static void test_steptimer_fixed_step_no_catch_up(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
|
|
ptk_steptimer_init(&timer);
|
|
timer.is_fixed_time_step = true;
|
|
timer.enable_catch_up = false;
|
|
timer.target_elapsed_time = 8;
|
|
timer.last_time = (uint64_t)y_gettime() - 500;
|
|
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
ctest_equal_uint("no-catch-up should invoke handler exactly once",
|
|
probe.call_count, 1);
|
|
ctest_equal_bool(
|
|
"no-catch-up elapsed_time should reflect real delta (~500ms)",
|
|
probe.last_elapsed_time >= 400 && probe.last_elapsed_time <= 600,
|
|
true);
|
|
}
|
|
|
|
static void test_steptimer_reset_elapsed_time(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
|
|
ptk_steptimer_init(&timer);
|
|
timer.is_fixed_time_step = true;
|
|
timer.enable_catch_up = false;
|
|
timer.target_elapsed_time = 16;
|
|
|
|
// Accumulate some left-over time by ticking twice with a gap.
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
y_sleep(20);
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
// Before reset, left_over_time should be non-zero.
|
|
ctest_equal_bool("left_over_time is non-zero before reset",
|
|
timer.left_over_time > 0, true);
|
|
|
|
ptk_steptimer_reset_elapsed_time(&timer);
|
|
|
|
ctest_equal_bool("left_over_time is zero after reset",
|
|
timer.left_over_time == 0, true);
|
|
ctest_equal_uint("frames_per_second is zero after reset",
|
|
timer.frames_per_second, 0);
|
|
}
|
|
|
|
static void test_steptimer_max_delta_clamp(void)
|
|
{
|
|
ptk_steptimer_t timer;
|
|
steptimer_probe_t probe = { 0 };
|
|
|
|
ptk_steptimer_init(&timer);
|
|
timer.is_fixed_time_step = false;
|
|
timer.max_delta = 1000;
|
|
// Pretend 10 seconds have elapsed; max_delta should clamp this.
|
|
timer.last_time = (uint64_t)y_gettime() - 10000;
|
|
|
|
ptk_steptimer_tick(&timer, on_steptimer_tick, &probe);
|
|
|
|
ctest_equal_uint("variable-step still calls handler once",
|
|
probe.call_count, 1);
|
|
ctest_equal_bool(
|
|
"elapsed_time should be clamped to <= max_delta (1000ms)",
|
|
probe.last_elapsed_time <= 1000, true);
|
|
}
|
|
|
|
void test_ptk_steptimer(void)
|
|
{
|
|
ctest_describe("init sets last_time to wall clock",
|
|
test_steptimer_init_sets_last_time);
|
|
ctest_describe("variable-step calls handler exactly once",
|
|
test_steptimer_variable_step);
|
|
ctest_describe("fixed-step catch-up replays missed updates",
|
|
test_steptimer_fixed_step_catch_up);
|
|
ctest_describe("fixed-step no-catch-up fires handler once",
|
|
test_steptimer_fixed_step_no_catch_up);
|
|
ctest_describe("reset_elapsed_time clears leftovers",
|
|
test_steptimer_reset_elapsed_time);
|
|
ctest_describe("max_delta clamps excessive deltas",
|
|
test_steptimer_max_delta_clamp);
|
|
}
|