# cutest **Repository Path**: blh-gitee/cutest ## Basic Information - **Project Name**: cutest - **Description**: A lightweight unit testing framework for pure C. - **Primary Language**: C - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-25 - **Last Updated**: 2026-05-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: unit-test, C-language ## README # cutest A lightweight unit testing framework for C. ## Features - **Zero dependencies** — pure C99, only POSIX headers. - **Auto-registration** — `TestCase()` macro uses `__attribute__((constructor))` to register tests at load time. No manual test list needed. - **Rich assertions** — boolean, integer comparison (EQ / NE / GT / GE / LT / LE), string equality, and pointer checks. - **Crash-safe** — catches `SIGSEGV`, `SIGBUS`, `SIGILL` and reports the last check location. - **Optional memory-leak checker** — `cutest_mem` wraps malloc/calloc/realloc/free to detect leaks and double-frees with call-stack dumps. - **pkg-config support** — `make install` installs headers, static library, and a `cutest.pc` file. ## Project Layout ``` cutest/ ├── include/ │ ├── cutest.h # framework header │ └── cutest_mem.h # memory-check header ├── src/ │ ├── cutest.c # framework implementation │ └── cutest_mem.c # memory-check implementation ├── build/ │ ├── Makefile # build libcutest.a & make install │ └── cutest.pc.in # pkg-config template ├── examples/ │ ├── foo.h # example library header (code under test) │ ├── foo.c # example library source (code under test) │ ├── ut_foo.c # unit tests for foo.c │ └── Makefile # build & run examples with -lcutest └── README.md ``` ## Build & Install ```bash # Build the static library cd build make # Install (default prefix: /usr/local) sudo make install # Or install to a custom prefix make PREFIX=/opt/cutest install # Uninstall sudo make uninstall ``` After installation, the following files are placed: | File | Path | |---|---| | `libcutest.a` | `$(LIBDIR)/libcutest.a` | | `cutest.h` | `$(INCLUDEDIR)/cutest/cutest.h` | | `cutest_mem.h` | `$(INCLUDEDIR)/cutest/cutest_mem.h` | | `cutest.pc` | `$(LIBDIR)/pkgconfig/cutest.pc` | ## Quick Start (examples) ```bash cd examples make test ``` This automatically builds `libcutest.a` first, then compiles and runs `ut_foo`. ## Writing Tests ### 1. Declare setup / teardown per test group ```c #include "cutest.h" TestSetup(mygroup) { /* runs before each case in mygroup */ } TestTeardown(mygroup) { /* runs after each case in mygroup */ } ``` ### 2. Write test cases ```c TestCase(mygroup, addition) { EXPECT_EQ(4, 2 + 2); EXPECT_NE(5, 2 + 2); } TestCase(mygroup, string_equal) { EXPECT_STREQ("hello", "hello"); } ``` ### 3. Provide main() ```c int main(void) { return cutest_run_all_testcases(); } ``` ### 4. Compile and run Using pkg-config (after `make install`): ```bash gcc $(pkg-config --cflags cutest) -o ut_mytest ut_mytest.c mycode.c $(pkg-config --libs cutest) ./ut_mytest ``` Or link directly against the build tree: ```bash gcc -I../include -o ut_mytest ut_mytest.c mycode.c -L../build -lcutest ./ut_mytest ``` ## Assertion Reference | Macro | Description | |---|---| | `EXPECT_TRUE(expr)` | expr is truthy | | `EXPECT_FALSE(expr)` | expr is falsy | | `EXPECT_EQ(expected, actual)` | integer equal | | `EXPECT_NE(expected, actual)` | integer not equal | | `EXPECT_GT(a, b)` | a > b | | `EXPECT_GE(a, b)` | a >= b | | `EXPECT_LT(a, b)` | a < b | | `EXPECT_LE(a, b)` | a <= b | | `EXPECT_STREQ(expected, actual)` | string equal | | `EXPECT_STRNE(expected, actual)` | string not equal | | `EXPECT_PTREQ(expected, actual)` | pointer equal | | `EXPECT_PTRNE(expected, actual)` | pointer not equal | | `EXPECT_PTRNULL(ptr)` | pointer is NULL | | `EXPECT_PTRNOTNULL(ptr)` | pointer is not NULL | ## Example Output ``` Run testcase 'foo_max_positive' .. PASS Run testcase 'foo_max_equal' . PASS Run testcase 'foo_factorial_small' ... PASS Run testcase 'foo_strrev_normal' . PASS Run testcase 'foo_split_kv_ok' ... PASS Run testcase 'foo_greeting_normal' .. PASS Run 30 cases and 47 checks, 30 cases passed, 0 cases failed. ``` ## License MIT