以下の内容はhttps://devlights.hatenablog.com/entry/2025/08/27/073000より取得しました。


gkoos/cnt (とても小さなC言語向けのユニットテストフレームワーク)(Minimalistic C unit testing framework, ヘッダファイル1つだけ)

関連記事

GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ

概要

以下、自分用のメモです。忘れないうちにメモメモ。。。

たまたまですが、以下のライブラリを知りました。

github.com

Minimalistic C unit testing framework と銘打ってる通り、とても小さいライブラリです。cnt.hというヘッダファイルが1つだけです。

このヘッダファイルの中にユニットテストで利用できる定数やマクロが定義されています。個人的にはこのようなシンプルでミニマムな構成は好きです。依存関係も何も無いです。

上記リポジトリのREADMEにとても丁寧に利用方法が書いてあるので、それを見れば良いのですが、自分のためにもメモ残しておきます。

サンプル

app.h

#pragma once

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cnt.h"

typedef struct {
    char         name[32];
    unsigned int age;
} Person;

#define cnt_assertPersonEqual(want, got)                                       \
    do {                                                                       \
        bool _b1 = (strcmp((want)->name, (got)->name) != 0);                   \
        bool _b2 = ((want)->age != (got)->age);                                \
        if (_b1 || _b2) {                                                      \
            cnt_fail();                                                        \
            printf("  want: (%s, %d), got: (%s, %d)\n", (want)->name,          \
                   (want)->age, (got)->name, (got)->age);                      \
        }                                                                      \
    } while (0)

app.c

#include "app.h"

char *get_str() { return "hello"; }

void set_person(Person *p, const char *name, const unsigned int age) {
    snprintf(p->name, sizeof(p->name), "%s", name);
    memcpy(&p->age, &age, sizeof(age));
}

void cnt_setUp() {}
void cnt_tearDown() {}

void test_1plus1() {
    int want = 2;
    int got  = (1 + 1);

    cnt_assertIntEqual(want, got);
}

void test_hello() {
    char *want = "hello";
    char *got  = get_str();

    cnt_assertStringEqual(want, got);
}

void test_failed() {
    char *want = "world";
    char *got  = get_str();

    cnt_assertStringEqual(want, got);
}

void test_person() {
    const char        *NAME = "John Doe";
    const unsigned int AGE  = 50;

    Person want = {};
    {
        snprintf(want.name, sizeof(want.name), "%s", NAME);
        want.age = AGE;
    }

    Person got = {};
    {
        set_person(&got, NAME, AGE);
    }

    cnt_assertPersonEqual(&want, &got);
}

int main(void) {

    cnt_start();
    {
        cnt_run("1+1", test_1plus1);
        cnt_run("string", test_hello);
        cnt_run("failed", test_failed);
        cnt_run("custom", test_person);
    }
    cnt_end();

    return EXIT_SUCCESS;
}

Makefile

CC       = gcc
CPPFLAGS = -I.
CFLAGS   = -Wall -Wextra -std=c99 -g -DDEBUG -O0
LDFLAGS  = 
LDLIBS   = 

TARGET   = app
SRCS     = app.c
OBJS     = $(SRCS:.c=.o)

.DEFAULT_GOAL := run

fmt:
    clang-format -i *.c *.h

build: $(TARGET)

run: build
    ./$(TARGET)

clean:
    $(RM) $(TARGET) $(OBJS)

$(TARGET): $(OBJS)
    $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

.PHONY: fmt run build clean

.clang-format

UseTab: Never
IndentWidth: 4
TabWidth: 4
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true

実行結果

$ make build
gcc -Wall -Wextra -std=c99 -g -DDEBUG -O0 -I.  -c -o app.o app.c
gcc  -o app app.o

$ make run
./app
app.c
PASSED 1+1 (0ms)
PASSED string (0ms)
 failure in test_failed() app.c:31
  expected: world, actual: hello
FAILED failed (0ms)
PASSED custom (0ms)
*** Test suite FAILED
Tests run: 4 - Passed: 3, Failed: 1, Skipped: 0
Time: 0.000s

参考情報


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。




以上の内容はhttps://devlights.hatenablog.com/entry/2025/08/27/073000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14