20260829 Redesign
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#include <c_Compiler.h>
|
||||
|
||||
#if (C_IS_COMPILER_GCC)
|
||||
#include "c_Compiler_GCC.cx"
|
||||
#elif (C_IS_COMPILER_CLANG)
|
||||
#include "c_Compiler_CLANG.cx"
|
||||
#elif (C_IS_COMPILER_MSVC)
|
||||
#include "c_Compiler_MSVC.cx"
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef INCLUDED_C_COMPILER_H
|
||||
#define INCLUDED_C_COMPILER_H
|
||||
|
||||
#ifndef INCLUDED_C_PLATFORM_H
|
||||
#include <c_Platform.h>
|
||||
#endif /*INCLUDED_C_PLATFORM_H*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* C_PACKED 举例 */
|
||||
|
||||
/*
|
||||
C_PACKED_STRUCT_START
|
||||
struct MyHeader {
|
||||
char id;
|
||||
int length;
|
||||
} C_PACKED;
|
||||
C_PACKED_STRUCT_END
|
||||
*/
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#if (C_IS_COMPILER_GCC)
|
||||
#include "c_Compiler_GCC.h"
|
||||
#elif (C_IS_COMPILER_CLANG)
|
||||
#include "c_Compiler_CLANG.h"
|
||||
#elif (C_IS_COMPILER_MSVC)
|
||||
#include "c_Compiler_MSVC.h"
|
||||
#else
|
||||
#define C_INLINE inline
|
||||
#define C_NOINLINE
|
||||
#define C_STATIC_FORCE_INLINE static C_INLINE
|
||||
#define C_PACKED_STRUCT_START
|
||||
#define C_PACKED_STRUCT_END
|
||||
#define C_PACKED
|
||||
#define C_ALIGN(bytes)
|
||||
#define C_RESTRICT restrict
|
||||
#define C_LIKELY(x) (x)
|
||||
#define C_UNLIKELY(x) (x)
|
||||
#endif
|
||||
|
||||
#define C_UNUSED(x) ((void)(x))
|
||||
|
||||
#endif /*INCLUDED_C_COMPILER_H*/
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Compiler_CLANG.h>
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef INCLUDED_C_COMPILER_CLANG_H
|
||||
#define INCLUDED_C_COMPILER_CLANG_H
|
||||
|
||||
#define C_INLINE inline __attribute__((always_inline))
|
||||
#define C_NOINLINE __attribute__((noinline))
|
||||
|
||||
#define C_STATIC_FORCE_INLINE static C_INLINE
|
||||
|
||||
#define C_PACKED_STRUCT_START
|
||||
#define C_PACKED_STRUCT_END
|
||||
#define C_PACKED __attribute__((packed))
|
||||
|
||||
#define C_ALIGN(bytes) __attribute__((aligned(bytes)))
|
||||
|
||||
#define C_RESTRICT __restrict__
|
||||
|
||||
#endif /*INCLUDED_C_COMPILER_CLANG_H*/
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Compiler_GCC.h>
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef INCLUDED_C_COMPILER_GCC_H
|
||||
#define INCLUDED_C_COMPILER_GCC_H
|
||||
|
||||
#define C_INLINE inline __attribute__((always_inline))
|
||||
#define C_NOINLINE __attribute__((noinline))
|
||||
|
||||
#define C_STATIC_FORCE_INLINE static C_INLINE
|
||||
|
||||
#define C_PACKED_STRUCT_START
|
||||
#define C_PACKED_STRUCT_END
|
||||
#define C_PACKED __attribute__((packed))
|
||||
|
||||
#define C_ALIGN(bytes) __attribute__((aligned(bytes)))
|
||||
|
||||
#define C_RESTRICT __restrict__
|
||||
|
||||
/* 用于 Search / Sort 等高频判断分支,提示 CPU 哪条分支概率更高 */
|
||||
#define C_LIKELY(x) __builtin_expect(!!(x), 1)
|
||||
#define C_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||
|
||||
#endif /*INCLUDED_C_COMPILER_GCC_H*/
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Compiler_MSVC.h>
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef INCLUDED_C_COMPILER_MSVC_H
|
||||
#define INCLUDED_C_COMPILER_MSVC_H
|
||||
|
||||
#define C_INLINE __forceinline
|
||||
#define C_NOINLINE __declspec(noinline)
|
||||
|
||||
#define C_STATIC_FORCE_INLINE static C_INLINE
|
||||
|
||||
#define C_PACKED_STRUCT_START __pragma(pack(push, 1))
|
||||
#define C_PACKED_STRUCT_END __pragma(pack(pop))
|
||||
#define C_PACKED /* MSVC 不支持通过单行后缀修饰变量 */
|
||||
|
||||
#define C_ALIGN(bytes) __declspec(align(bytes))
|
||||
|
||||
/* 通知编译器两个指针指向的内存绝不重叠,用于大幅提升数学库(Math)的向量化效率 */
|
||||
#define C_RESTRICT __restrict
|
||||
|
||||
/* 用于 Search / Sort 等高频判断分支,提示 CPU 哪条分支概率更高 */
|
||||
#define C_LIKELY(x) (x)
|
||||
#define C_UNLIKELY(x) (x)
|
||||
|
||||
#endif /*INCLUDED_C_COMPILER_MSVC_H*/
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Macros.h>
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
#ifndef INCLUDED_C_MACROS_H
|
||||
#define INCLUDED_C_MACROS_H
|
||||
|
||||
#ifndef INCLUDED_MATH_H
|
||||
#define INCLUDED_MATH_H
|
||||
#include <math.h>
|
||||
#endif /*INCLUDED_MATH_H*/
|
||||
|
||||
#ifndef INCLUDED_C_TYPES_H
|
||||
#include <c_Types.h>
|
||||
#endif /*INCLUDED_C_TYPES_H*/
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define C_MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_RAD2DEG(x) ((x)/M_PI*180.0)
|
||||
#define C_DEG2RAD(x) ((x)*M_PI/180.0)
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_ALIGN_UPB(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
|
||||
#define C_ALIGN_UP(x, align) ((((x) + ((align) - 1)) / (align)) * (align))
|
||||
|
||||
#define C_ALIGN_DOWNB(x, align) ((x) & ~((align) - 1))
|
||||
#define C_ALIGN_DOWN(x, align) (((x) / (align)) * (align))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_CLIP(x, min, max) (((x) < (min)) ? (min) : \
|
||||
(((x) > (max)) ? (max) : (x)))
|
||||
|
||||
#define C_MAX_CLIP(x, max) (((x) > (max)) ? (max) : (x))
|
||||
#define C_MIN_CLIP(x, min) (((x) < (min)) ? (min) : (x))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_ABS(x) (((x) < 0) ? -(x) : (x))
|
||||
#define C_DIFF(a,b) C_ABS((a)-(b))
|
||||
#define C_IS_NAN(x) ((x) != (x))
|
||||
#define C_COMPARE(x, y) (((x) > (y)) - ((x) < (y)))
|
||||
#define C_SIGN(x) C_COMPARE(x, 0)
|
||||
#define C_IS_ODD( num ) ((num) & 1)
|
||||
#define C_IS_EVEN( num ) (!IS_ODD( (num) ))
|
||||
#define C_IS_BETWEEN(n,L,H) ((unsigned char)((n) >= (L) && (n) <= (H)))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
|
||||
#define C_BIT(x) (1<<(x))
|
||||
#define C_BIT_SET(x,p) ((x)|(1<<(p)))
|
||||
#define C_BIT_CLEAR(x,p) ((x)&(~(1<<(p))))
|
||||
#define C_BIT_GET(x,p) (((x)>>(p))&1)
|
||||
#define C_BIT_TOGGLE(x,p) ((x)^(1<<(p)))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#define C_SET(d, n, v) do{ c_size_t i_, n_; \
|
||||
for ( n_ = (n), i_ = 0; n_ > 0; --n_, ++i_) \
|
||||
(d)[i_] = (v); } while(0)
|
||||
#define C_ZERO(d, n) C_SET(d, n, 0)
|
||||
#define C_COLUMNS(S,E) ( (E) - (S) + 1 )
|
||||
#define C_IS_ARRAY(a) ((void *)&a == (void *)a)
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_STMT( stuff ) do { stuff } while (0)
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
// 1. 底层拼接宏(真正执行拼接)
|
||||
#define C_UNIQUE_NAME_CONCAT(a, b) a ## b
|
||||
|
||||
// 2. 中间转换宏(强制将 __LINE__ 或 __COUNTER__ 展开为实际的数字/符号)
|
||||
#define C_UNIQUE_NAME_EVAL(a, b) C_UNIQUE_NAME_CONCAT(a, b)
|
||||
|
||||
// 3. 面向用户的唯一名称生成宏
|
||||
#if defined(__COUNTER__)
|
||||
// 工业级推荐:__COUNTER__ 从 0 开始,每次调用自动递增,保证绝对唯一
|
||||
#define C_UNIQUE_NAME(prefix) C_UNIQUE_NAME_EVAL(prefix, __COUNTER__)
|
||||
#else
|
||||
// 兼容标准:__LINE__ 使用当前行号,同一行内多次调用可能会冲突
|
||||
#define C_UNIQUE_NAME(prefix) C_UNIQUE_NAME_EVAL(prefix, __LINE__)
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_ONCE2(stmts, var) {static int var = 1;\
|
||||
if(var){stmts\
|
||||
var = 0;}}
|
||||
#define C_ONCE(stmts) C_ONCE2(stmts, C_UNIQUE_NAME(__onceVar__))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_CONTAINER_OF(ptr, type, member) \
|
||||
((type *)( (char *)(ptr) - offsetof(type, member) ))
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define C_UNUSED(x) ((void)(x))
|
||||
|
||||
#endif /*INCLUDED_C_MACROS_H*/
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Platform.h>
|
||||
@@ -0,0 +1,131 @@
|
||||
#ifndef INCLUDED_C_PLATFORM_H
|
||||
#define INCLUDED_C_PLATFORM_H
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 1. 编译器类型常量(严格以 C_ 开头)
|
||||
* ============================================================================== */
|
||||
#define C_COMPILER_TYPE_UNKNOWN 0
|
||||
#define C_COMPILER_TYPE_GCC 1
|
||||
#define C_COMPILER_TYPE_CLANG 2
|
||||
#define C_COMPILER_TYPE_MSVC 3
|
||||
|
||||
/* 初始化默认值 */
|
||||
#define C_CURRENT_COMPILER_TYPE C_COMPILER_TYPE_UNKNOWN
|
||||
#define C_CURRENT_COMPILER_NAME "Unknown Compiler"
|
||||
#define C_CURRENT_COMPILER_VERSION 0
|
||||
|
||||
/* 核心阻断:Clang 具有欺骗性(会同时定义 __GNUC__ 或 _MSC_VER),必须优先判定 */
|
||||
#if defined(__clang__)
|
||||
#undef C_CURRENT_COMPILER_TYPE
|
||||
#undef C_CURRENT_COMPILER_NAME
|
||||
#define C_CURRENT_COMPILER_TYPE C_COMPILER_TYPE_CLANG
|
||||
#define C_CURRENT_COMPILER_NAME "Clang"
|
||||
|
||||
#undef C_CURRENT_COMPILER_VERSION
|
||||
#define C_CURRENT_COMPILER_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
|
||||
|
||||
#elif defined(__GNUC__) || defined(__GNUG__)
|
||||
#undef C_CURRENT_COMPILER_TYPE
|
||||
#undef C_CURRENT_COMPILER_NAME
|
||||
#define C_CURRENT_COMPILER_TYPE C_COMPILER_TYPE_GCC
|
||||
#define C_CURRENT_COMPILER_NAME "GCC"
|
||||
|
||||
#undef C_CURRENT_COMPILER_VERSION
|
||||
#if defined(__GNUC_PATCHLEVEL__)
|
||||
#define C_CURRENT_COMPILER_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
||||
#else
|
||||
#define C_CURRENT_COMPILER_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
|
||||
#endif
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
#undef C_CURRENT_COMPILER_TYPE
|
||||
#undef C_CURRENT_COMPILER_NAME
|
||||
#define C_CURRENT_COMPILER_TYPE C_COMPILER_TYPE_MSVC
|
||||
#define C_CURRENT_COMPILER_NAME "Microsoft Visual C++"
|
||||
|
||||
#undef C_CURRENT_COMPILER_VERSION
|
||||
#if defined(_MSC_FULL_VER)
|
||||
#define C_CURRENT_COMPILER_VERSION (_MSC_FULL_VER / 1000)
|
||||
#else
|
||||
#define C_CURRENT_COMPILER_VERSION (_MSC_VER * 100)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 2. 快捷判定辅助宏(严格以 C_ 开头,0 或 1 显式映射)
|
||||
* ============================================================================== */
|
||||
#if C_CURRENT_COMPILER_TYPE == C_COMPILER_TYPE_GCC
|
||||
#define C_IS_COMPILER_GCC 1
|
||||
#else
|
||||
#define C_IS_COMPILER_GCC 0
|
||||
#endif
|
||||
|
||||
#if C_CURRENT_COMPILER_TYPE == C_COMPILER_TYPE_CLANG
|
||||
#define C_IS_COMPILER_CLANG 1
|
||||
#else
|
||||
#define C_IS_COMPILER_CLANG 0
|
||||
#endif
|
||||
|
||||
#if C_CURRENT_COMPILER_TYPE == C_COMPILER_TYPE_MSVC
|
||||
#define C_IS_COMPILER_MSVC 1
|
||||
#else
|
||||
#define C_IS_COMPILER_MSVC 0
|
||||
#endif
|
||||
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 3. 操作系统环境识别矩阵(严格以 C_ 开头,无缝对接跨平台需求)
|
||||
* ============================================================================== */
|
||||
#define C_OS_TYPE_UNKNOWN 0
|
||||
#define C_OS_TYPE_WINDOWS 1
|
||||
#define C_OS_TYPE_LINUX 2
|
||||
#define C_OS_TYPE_MACOS 3
|
||||
#define C_OS_TYPE_FREEBSD 4
|
||||
|
||||
#define C_CURRENT_OS_TYPE C_OS_TYPE_UNKNOWN
|
||||
#define C_CURRENT_OS_NAME "Unknown OS"
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
|
||||
#undef C_CURRENT_OS_TYPE
|
||||
#undef C_CURRENT_OS_NAME
|
||||
#define C_CURRENT_OS_TYPE C_OS_TYPE_WINDOWS
|
||||
#define C_CURRENT_OS_NAME "Windows"
|
||||
|
||||
#elif defined(__APPLE__) && defined(__MACH__)
|
||||
#undef C_CURRENT_OS_TYPE
|
||||
#undef C_CURRENT_OS_NAME
|
||||
#define C_CURRENT_OS_TYPE C_OS_TYPE_MACOS
|
||||
#define C_CURRENT_OS_NAME "macOS"
|
||||
|
||||
#elif defined(__linux__) || defined(__linux)
|
||||
#undef C_CURRENT_OS_TYPE
|
||||
#undef C_CURRENT_OS_NAME
|
||||
#define C_CURRENT_OS_TYPE C_OS_TYPE_LINUX
|
||||
#define C_CURRENT_OS_NAME "Linux"
|
||||
|
||||
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
#undef C_CURRENT_OS_TYPE
|
||||
#undef C_CURRENT_OS_NAME
|
||||
#define C_CURRENT_OS_TYPE C_OS_TYPE_FREEBSD
|
||||
#define C_CURRENT_OS_NAME "FreeBSD"
|
||||
#endif
|
||||
|
||||
/* 操作系统快捷判定 */
|
||||
#define C_IS_OS_WINDOWS (C_CURRENT_OS_TYPE == C_OS_TYPE_WINDOWS)
|
||||
#define C_IS_OS_LINUX (C_CURRENT_OS_TYPE == C_OS_TYPE_LINUX)
|
||||
#define C_IS_OS_MACOS (C_CURRENT_OS_TYPE == C_OS_TYPE_MACOS)
|
||||
#define C_IS_OS_FREEBSD (C_CURRENT_OS_TYPE == C_OS_TYPE_FREEBSD)
|
||||
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 4. 特化环境:MinGW / MSYS2 环境检测(严格以 C_ 开头)
|
||||
* ============================================================================== */
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
#define C_ENVIRONMENT_IS_MINGW 1
|
||||
#else
|
||||
#define C_ENVIRONMENT_IS_MINGW 0
|
||||
#endif
|
||||
|
||||
|
||||
#endif /*INCLUDED_C_PLATFORM_H*/
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "c_Platform.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv){
|
||||
// 1. 打印基础架构诊断信息
|
||||
printf("操作系统: %s, 编译器: %s (版本: %d)\n",
|
||||
C_CURRENT_OS_NAME, C_CURRENT_COMPILER_NAME, C_CURRENT_COMPILER_VERSION);
|
||||
|
||||
// 2. 结合操作系统与编译器的复合拦截
|
||||
#if C_IS_OS_WINDOWS
|
||||
if (C_ENVIRONMENT_IS_MINGW) {
|
||||
printf("正在您的本地环境 (Windows + MSYS2 MinGW) 下高效执行...\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// 3. 多平台特化高性能代码分支
|
||||
#if C_IS_OS_LINUX && C_IS_COMPILER_GCC
|
||||
// Linux 下使用 GCC 的特化高性能系统调用
|
||||
printf("启用 Linux Epoll & GCC 优化算法\n");
|
||||
#elif C_IS_OS_MACOS || C_IS_OS_FREEBSD
|
||||
// macOS 和 FreeBSD 属于 BSD 谱系,可以使用类似 kqueue 的特性
|
||||
printf("启用 BSD 谱系特化内核优化\n");
|
||||
#else
|
||||
printf("启用跨平台标准通用算法\n");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Test.h>
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
#ifndef INCLUDED_C_TEST_H
|
||||
#define INCLUDED_C_TEST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <time.h>
|
||||
#include "c_Compiler.h"
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 1. 测试上下文状态追踪
|
||||
* ============================================================================== */
|
||||
static int c_test_suite_failed = 0; /* 当前套件是否有失败的断言 */
|
||||
static int c_test_case_failed = 0; /* 当前正在运行的测试用例是否有失败 */
|
||||
static int c_test_total_assertions = 0; /* 总断言数 */
|
||||
static int c_test_passed_assertions = 0; /* 成功的断言数 */
|
||||
static int c_test_cases_run = 0; /* 运行的用例数 */
|
||||
static int c_test_cases_passed = 0; /* 成功的用例数 */
|
||||
static double c_test_suite_total_ms = 0.0; /* 整个套件的总耗时 */
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 2. 核心断言宏矩阵(严格以 C_ASSERT_ 开头)
|
||||
* ============================================================================== */
|
||||
|
||||
/* 统一的断言失败内部打印函数 */
|
||||
C_STATIC_FORCE_INLINE
|
||||
void c_test_print_fail(const char* file, int line, const char* expr, const char* msg) {
|
||||
c_test_suite_failed = 1;
|
||||
c_test_case_failed = 1;
|
||||
printf(" [FAIL] %s:%d -> 断言失败: (%s) ", file, line, expr);
|
||||
if (msg && strlen(msg) > 0) {
|
||||
printf("| 说明: %s", msg);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* 基础布尔断言 */
|
||||
#define C_ASSERT(expr, msg) do { \
|
||||
c_test_total_assertions++; \
|
||||
if (C_LIKELY(expr)) { \
|
||||
c_test_passed_assertions++; \
|
||||
} else { \
|
||||
c_test_print_fail(__FILE__, __LINE__, #expr, msg); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* 整型相等断言 */
|
||||
#define C_ASSERT_INT_EQ(actual, expected, msg) do { \
|
||||
c_test_total_assertions++; \
|
||||
long long act = (long long)(actual); \
|
||||
long long exp = (long long)(expected); \
|
||||
if (C_LIKELY(act == exp)) { \
|
||||
c_test_passed_assertions++; \
|
||||
} else { \
|
||||
char buf[256]; \
|
||||
snprintf(buf, sizeof(buf), "期望值: %lld, 实际值: %lld | %s", exp, act, msg); \
|
||||
c_test_print_fail(__FILE__, __LINE__, #actual " == " #expected, buf); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* 字符串相等断言 */
|
||||
#define C_ASSERT_STR_EQ(actual, expected, msg) do { \
|
||||
c_test_total_assertions++; \
|
||||
const char* act = (const char*)(actual); \
|
||||
const char* exp = (const char*)(expected); \
|
||||
if (C_LIKELY(act && exp && strcmp(act, exp) == 0)) { \
|
||||
c_test_passed_assertions++; \
|
||||
} else { \
|
||||
char buf[256]; \
|
||||
snprintf(buf, sizeof(buf), "期望: \"%s\", 实际: \"%s\" | %s", exp ? exp : "NULL", act ? act : "NULL", msg); \
|
||||
c_test_print_fail(__FILE__, __LINE__, "strcmp(" #actual ", " #expected ") == 0", buf); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* 浮点数安全相等断言(内置之前讨论的 IEEE 754 Epsilon 比较) */
|
||||
#define C_ASSERT_DOUBLE_EQ(actual, expected, msg) do { \
|
||||
c_test_total_assertions++; \
|
||||
double act = (double)(actual); \
|
||||
double exp = (double)(expected); \
|
||||
int is_eq = 0; \
|
||||
if (isnan(act) && isnan(exp) || (fabs(act - exp) < DBL_EPSILON) ) { is_eq = 1; } \
|
||||
if (C_LIKELY(is_eq)) { \
|
||||
c_test_passed_assertions++; \
|
||||
} else { \
|
||||
char buf[256]; \
|
||||
snprintf(buf, sizeof(buf), "期望: %.32f, 实际: %.32f (误差 > DBL_EPSILON[%.32f]) | %s", exp, act, DBL_EPSILON, msg); \
|
||||
c_test_print_fail(__FILE__, __LINE__, "fabs(" #actual " - " #expected ") < DBL_EPSILON", buf); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
/* ==============================================================================
|
||||
* 🎯 3. 测试套件生命周期控制宏
|
||||
* ============================================================================== */
|
||||
|
||||
/* 初始化一个测试套件文件 */
|
||||
#define C_TEST_SUITE_BEGIN(name) \
|
||||
printf("==================================================\n"); \
|
||||
printf("🧪 运行测试套件: %s [%s环境]\n", #name, C_CURRENT_OS_NAME); \
|
||||
printf("==================================================\n");
|
||||
|
||||
/* 结束测试套件并返回标准状态码给 CMake CTest */
|
||||
#define C_TEST_SUITE_END() \
|
||||
printf("\n--------------------------------------------------------------------\n"); \
|
||||
printf("📊 测试结果统计:\n"); \
|
||||
printf(" 测试用例数: %d 运行, %d 通过, %d 失败\n", \
|
||||
c_test_cases_run, c_test_cases_passed, c_test_cases_run - c_test_cases_passed); \
|
||||
printf(" 底层断言数: %d 总计, %d 成功, %d 失败\n", \
|
||||
c_test_total_assertions, c_test_passed_assertions, c_test_total_assertions - c_test_passed_assertions); \
|
||||
printf(" 套件总耗时: %.3f ms\n", c_test_suite_total_ms); \
|
||||
if (c_test_suite_failed) { \
|
||||
printf("🚨 结论 : [ FAILURE ] 有测试未通过!\n"); \
|
||||
printf("====================================================================\n"); \
|
||||
return 1; \
|
||||
} \
|
||||
printf("🎉 结论 : [ SUCCESS ] 全套单元测试完美通过!\n"); \
|
||||
printf("====================================================================\n"); \
|
||||
return 0;
|
||||
|
||||
/* 声明一个测试用例块 */
|
||||
#define C_TEST_CASE(name) static void name(void)
|
||||
|
||||
#define C_RUN_TEST_CASE(name) do { \
|
||||
c_test_cases_run++; \
|
||||
c_test_case_failed = 0; \
|
||||
printf(" ▶ 运行测试用例: %-30s ... ", #name); \
|
||||
fflush(stdout); \
|
||||
struct timespec c_start_time, c_end_time; \
|
||||
timespec_get(&c_start_time, TIME_UTC); \
|
||||
name(); \
|
||||
timespec_get(&c_end_time, TIME_UTC); \
|
||||
double c_elapsed_ms = (double)(c_end_time.tv_sec - c_start_time.tv_sec) * 1000.0 + \
|
||||
(double)(c_end_time.tv_nsec - c_start_time.tv_nsec) / 1000000.0; \
|
||||
c_test_suite_total_ms += c_elapsed_ms; \
|
||||
if (c_test_case_failed) { \
|
||||
printf("[ ❌ 失败 ] (耗时: %8.3f ms)\n", c_elapsed_ms); \
|
||||
} else { \
|
||||
c_test_cases_passed++; \
|
||||
printf("[ ✨ 通过 ] (耗时: %8.3f ms)\n", c_elapsed_ms); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
#endif /*INCLUDED_C_TEST_H*/
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "c_Test.h"
|
||||
#include "c_Compiler.h"
|
||||
|
||||
// Math/matrix.t.c
|
||||
#include "c_Test.h"
|
||||
|
||||
void heavy_calculation(void) {
|
||||
double sum = 0.0;
|
||||
for (int i = 0; i < 5000000; ++i) {
|
||||
sum += sin((double)i) * cos((double)i);
|
||||
}
|
||||
// 随便加一个没意义的断言防止被编译器完全优化掉
|
||||
C_ASSERT(sum != 12345.6, "密集数学计算检验");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// 🛠️ 步骤 A: 编写独立的测试用例函数 (C_TEST_CASE)
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
C_TEST_CASE(test_quick_operations) {
|
||||
int a = 1, b = 1;
|
||||
C_ASSERT_INT_EQ(a, b, "极速操作验证");
|
||||
}
|
||||
|
||||
C_TEST_CASE(test_heavy_workload) {
|
||||
heavy_calculation();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// 🚀 步骤 B: 在主执行块中注册并依次运行它们
|
||||
// ------------------------------------------------------------------------------
|
||||
int main(int argc, char** argv) {
|
||||
C_TEST_SUITE_BEGIN(MathMatrixFunctionSuite)
|
||||
|
||||
C_RUN_TEST_CASE(test_quick_operations);
|
||||
C_RUN_TEST_CASE(test_heavy_workload);
|
||||
|
||||
C_TEST_SUITE_END()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include <c_Types.h>
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
#ifndef INCLUDED_C_TYPES_H
|
||||
#define INCLUDED_C_TYPES_H
|
||||
|
||||
#ifndef INCLUDED_C_CONFIG_H
|
||||
#include <c_Config.h>
|
||||
#endif /*INCLUDED_C_CONFIG_H*/
|
||||
|
||||
#ifndef INCLUDED_STDINT_H
|
||||
#define INCLUDED_STDINT_H
|
||||
#include <stdint.h>
|
||||
#endif /*INCLUDED_STDINT_H*/
|
||||
|
||||
#ifndef INCLUDED_STDBOOL_H
|
||||
#define INCLUDED_STDBOOL_H
|
||||
#include <stdbool.h>
|
||||
#endif /*INCLUDED_STDBOOL_H*/
|
||||
|
||||
#ifndef INCLUDED_STRING_H
|
||||
#define INCLUDED_STRING_H
|
||||
#include <string.h>
|
||||
#endif /*INCLUDED_STRING_H*/
|
||||
|
||||
#ifndef INCLUDED_TIME_H
|
||||
#define INCLUDED_TIME_H
|
||||
#include <time.h>
|
||||
#endif /*INCLUDED_TIME_H*/
|
||||
|
||||
#ifndef INCLUDED_INTTYPES_H
|
||||
#define INCLUDED_INTTYPES_H
|
||||
#include <inttypes.h>
|
||||
#endif /*INCLUDED_INTTYPES_H*/
|
||||
|
||||
#ifndef INCLUDED_FLOAT_H
|
||||
#define INCLUDED_FLOAT_H
|
||||
#include <float.h>
|
||||
#endif /*INCLUDED_FLOAT_H*/
|
||||
|
||||
#ifndef INCLUDED_C_PLATFORM_H
|
||||
#include <c_Platform.h>
|
||||
#endif /*INCLUDED_C_PLATFORM_H*/
|
||||
|
||||
|
||||
#ifndef INCLUDED_C_COMPILER_H
|
||||
#include <c_Compiler.h>
|
||||
#endif /*INCLUDED_C_COMPILER_H*/
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* TYPES */
|
||||
|
||||
#if (C_SIZEOF_VOID_P==8)
|
||||
typedef int64_t c_int_t;
|
||||
typedef uint64_t c_uint_t;
|
||||
typedef uint64_t c_size_t;
|
||||
typedef int64_t c_intptr_t;
|
||||
typedef uint64_t c_uintptr_t;
|
||||
#define C_INT_MIN INT64_MIN
|
||||
#define C_INT_MAX INT64_MAX
|
||||
#define C_UINT_MAX UINT64_MAX
|
||||
#define C_SIZE_MAX UINT64_MAX
|
||||
#define C_PRId PRId64
|
||||
#define C_PRIi PRIi64
|
||||
#define C_PRIo PRIo64
|
||||
#define C_PRIu PRIu64
|
||||
#define C_PRIx PRIx64
|
||||
#define C_PRIX PRIX64
|
||||
#define C_SCNd SCNd64
|
||||
#define C_SCNi SCNi64
|
||||
#define C_SCNo SCNo64
|
||||
#define C_SCNu SCNu64
|
||||
#define C_SCNx SCNx64
|
||||
#endif
|
||||
|
||||
#if (C_SIZEOF_VOID_P==4)
|
||||
typedef int32_t c_int_t;
|
||||
typedef uint32_t c_uint_t;
|
||||
typedef uint32_t c_size_t;
|
||||
typedef int32_t c_intptr_t;
|
||||
typedef uint32_t c_uintptr_t;
|
||||
#define C_INT_MIN INT32_MIN
|
||||
#define C_INT_MAX INT32_MAX
|
||||
#define C_UINT_MAX UINT32_MAX
|
||||
#define C_SIZE_MAX UINT32_MAX
|
||||
#define C_PRId PRId32
|
||||
#define C_PRIi PRIi32
|
||||
#define C_PRIo PRIo32
|
||||
#define C_PRIu PRIu32
|
||||
#define C_PRIx PRIx32
|
||||
#define C_PRIX PRIX32
|
||||
#define C_SCNd SCNd32
|
||||
#define C_SCNi SCNi32
|
||||
#define C_SCNo SCNo32
|
||||
#define C_SCNu SCNu32
|
||||
#define C_SCNx SCNx32
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* ERRORS */
|
||||
|
||||
typedef int c_err_t;
|
||||
|
||||
#define C_ERR_OK 0
|
||||
#define C_ERR_FAIL (-1)
|
||||
#define C_ERR_NOTFOUND (-2)
|
||||
#define C_ERR_NOMEM (-3)
|
||||
#define C_ERR_NOTIMPLEMENTED (-4)
|
||||
#define C_ERR_PARAM (-5)
|
||||
#define C_ERR_OUTOFBOUND (-6)
|
||||
#define C_ERR_EMPTY (-7)
|
||||
#define C_ERR_FULL (-8)
|
||||
|
||||
#define C_SUCCESS C_ERR_OK
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define c_bool_t bool
|
||||
#define C_TRUE true
|
||||
#define C_FALSE false
|
||||
|
||||
|
||||
#endif /*INCLUDED_C_TYPES_H*/
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv){
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user