/* test_fmt_utc.c — fmt_utc() 边界单测(宿主)。 * * fmt_utc 是 nyai.c 里的 static 函数;这里把 main 改名后直接 include 源码来测, * 不引入测试框架、不复制实现(避免单测与实现漂移)。 * 覆盖:epoch/负数、1 月、2 月(闰/平)、3 月、12 月 31 日、闰年 2/29、2100 世纪闰年规则、 * 2038 int32 边界。真机时区无关(fmt_utc 固定 UTC)。 * * 用法:cc -Wall -Wextra test_fmt_utc.c -o test_fmt_utc && ./test_fmt_utc */ #define main nyai_main_unused #include "nyai.c" #undef main #include #include struct utc_case { long long t; const char *want; }; int main(void) { static const struct utc_case cases[] = { { 0LL, "t0" }, /* epoch 由调用方定义为「无时间」 */ { -1LL, "t0" }, { 1LL, "19700101-000001" }, { 1LL * 1767225600, "20260101-000000" }, /* 2026-01-01(曾打成 20251901) */ { 1769904000LL, "20260201-000000" }, /* 2026-02-01(曾打成 20251931) */ { 1772236800LL, "20260228-000000" }, /* 2026-02-28(平年) */ { 1709164800LL, "20240229-000000" }, /* 2024-02-29(闰年) */ { 951782400LL, "20000229-000000" }, /* 2000-02-29(400 年闰) */ { 4102444800LL, "21000101-000000" }, /* 2100-01-01(非闰世纪) */ { 1772323200LL, "20260301-000000" }, /* 2026-03-01(March-based 起点) */ { 1798761599LL, "20261231-235959" }, /* 年末最后一秒 */ { 2145916800LL, "20380101-000000" }, { 2147483647LL, "20380119-031407" }, /* int32 time_t 上界 */ }; int fail = 0; for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) { char got[32]; fmt_utc(cases[i].t, got, sizeof got); if (strcmp(got, cases[i].want) != 0) { printf("FAIL fmt_utc(%lld) = %s, want %s\n", cases[i].t, got, cases[i].want); fail++; } } if (fail == 0) { printf("PASS fmt_utc: %u cases\n", (unsigned)(sizeof cases / sizeof cases[0])); return 0; } printf("FAILED %d/%u\n", fail, (unsigned)(sizeof cases / sizeof cases[0])); return 1; }