mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-12-01 16:56:34 +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
142 lines
3.8 KiB
C
142 lines
3.8 KiB
C
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <stdarg.h>
|
||
#include <stdint.h>
|
||
#include "libtest.h"
|
||
#include "yutil/keywords.h"
|
||
#include "yutil/time.h"
|
||
|
||
#ifdef _WIN32
|
||
#define COLOR_NONE
|
||
#define COLOR_RED
|
||
#define COLOR_GREEN
|
||
#else
|
||
#define COLOR_NONE "\e[0m"
|
||
#define COLOR_RED "\e[0;31m"
|
||
#define COLOR_GREEN "\e[0;32m"
|
||
#endif
|
||
#define RED(TEXT) COLOR_RED TEXT COLOR_NONE
|
||
#define GREEN(TEXT) COLOR_GREEN TEXT COLOR_NONE
|
||
|
||
#if defined(CI_ENV) || defined(_WIN32)
|
||
#define CHECK_MARK GREEN("* ")
|
||
#define ERROR_MARK "x "
|
||
#else
|
||
#define CHECK_MARK GREEN("√ ")
|
||
#define ERROR_MARK "× "
|
||
#endif
|
||
|
||
static size_t tests_passed = 0;
|
||
static size_t tests_total = 0;
|
||
static int test_msg_indent = 0;
|
||
static int64_t test_start_time = 0;
|
||
|
||
static int test_msg(const char *fmt, ...)
|
||
{
|
||
int ret;
|
||
char str[1024];
|
||
va_list args;
|
||
|
||
va_start(args, fmt);
|
||
ret = vsnprintf(str, 1023, fmt, args);
|
||
va_end(args);
|
||
printf("%*s%s", test_msg_indent * 2, "", str);
|
||
return ret;
|
||
}
|
||
|
||
void test_begin(void)
|
||
{
|
||
if (test_start_time == 0) {
|
||
test_start_time = y_gettime();
|
||
}
|
||
test_msg_indent++;
|
||
}
|
||
|
||
void test_end(void)
|
||
{
|
||
test_msg_indent--;
|
||
}
|
||
|
||
int test_result(void)
|
||
{
|
||
if (tests_total > tests_passed) {
|
||
return (int)(tests_total - tests_passed);
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int print_test_result(void)
|
||
{
|
||
printf(GREEN(" %zu passing") " (%ums)\n", tests_passed,
|
||
(unsigned)(y_gettime() - test_start_time));
|
||
if (tests_total > tests_passed) {
|
||
printf(RED(" %zu faling\n\n"), tests_total - tests_passed);
|
||
return (int)(tests_total - tests_passed);
|
||
}
|
||
printf("\n");
|
||
return 0;
|
||
}
|
||
|
||
void describe(const char *name, void (*func)())
|
||
{
|
||
test_msg("%s\n", name);
|
||
test_begin();
|
||
func();
|
||
test_end();
|
||
test_msg("\n");
|
||
}
|
||
|
||
void it_i(const char *name, int actual, int expected)
|
||
{
|
||
tests_total++;
|
||
if (actual == expected) {
|
||
test_msg(CHECK_MARK "%s == %d\n", name, expected);
|
||
tests_passed++;
|
||
return;
|
||
}
|
||
test_msg(RED(ERROR_MARK "%s == %d\n"), name, expected);
|
||
test_msg(RED(" AssertionError: %d == %d\n"), actual, expected);
|
||
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
|
||
test_msg(RED(" - %d\n"), actual);
|
||
test_msg(GREEN(" + %d\n\n"), expected);
|
||
}
|
||
|
||
void it_b(const char *name, int actual, int expected)
|
||
{
|
||
const char *actual_str = actual ? "true" : "false";
|
||
const char *expected_str = expected ? "true" : "false";
|
||
|
||
tests_total++;
|
||
if (!actual == !expected) {
|
||
test_msg(CHECK_MARK "%s\n", name);
|
||
tests_passed++;
|
||
return;
|
||
}
|
||
test_msg(RED(ERROR_MARK "%s\n"), name);
|
||
test_msg(RED(" AssertionError: %s == %s\n"), actual_str, expected_str);
|
||
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
|
||
test_msg(RED(" - %s\n"), actual_str);
|
||
test_msg(GREEN(" + %s\n\n"), expected_str);
|
||
}
|
||
|
||
void it_s(const char *name, const char *actual, const char *expected)
|
||
{
|
||
tests_total++;
|
||
if ((actual && expected && strcmp(actual, expected) == 0) ||
|
||
actual == expected) {
|
||
test_msg(CHECK_MARK "%s == '%s'\n", name, expected);
|
||
tests_passed++;
|
||
return;
|
||
}
|
||
test_msg(RED(ERROR_MARK "%s == '%s'\n"), name, expected);
|
||
if (expected) {
|
||
test_msg(RED(" AssertionError: '%s' == '%s'\n"), actual,
|
||
expected);
|
||
} else {
|
||
test_msg(RED(" AssertionError: '%s' == null\n"), actual);
|
||
}
|
||
test_msg(GREEN(" + expected ") RED("- actual\n\n"));
|
||
test_msg(RED(" - %s\n"), actual);
|
||
test_msg(GREEN(" + %s\n\n"), expected);
|
||
}
|