C เขียนเทสต์



C ไม่มี test framework มาตรฐาน แต่ระบบทดสอบที่ใช้ได้จริงเขียนเองได้ในไม่กี่บรรทัด

แยกโค้ดให้ทดสอบได้ก่อน

ทดสอบยากถ้าโค้ดผูกกับ I/O — แยก logic บริสุทธิ์ (รับ input คืน output) ออกจากส่วนที่อ่านไฟล์/พิมพ์จอ

// ❌ ทดสอบยาก
void run(void) {
    int n;
    scanf("%d", &n);
    printf("%d\n", n * n);
}

// ✅ ทดสอบง่าย — logic แยกออกมา
int square(int n) { return n * n; }

void run(void) {
    int n;
    if (scanf("%d", &n) != 1) return;   // อ่านไม่ได้ = ไม่ทำต่อ
    printf("%d\n", square(n));
}

เทสต์ด้วย assert ล้วน ๆ

// test_math.c
#include <assert.h>
#include <stdio.h>
#include "mathutil.h"

int main(void)
{
    assert(gcd(12, 18) == 6);
    assert(gcd(17, 5) == 1);
    assert(gcd(0, 5) == 5);

    assert(factorial(0) == 1);
    assert(factorial(5) == 120);

    assert(is_prime(2) == 1);
    assert(is_prime(1) == 0);
    assert(is_prime(15) == 0);
    assert(is_prime(17) == 1);

    printf("เทสต์ผ่านทั้งหมด\n");
    return 0;
}
gcc -std=c17 -g test_math.c mathutil.c -o test_math
./test_math

ข้อเสีย: assert ตัวแรกที่ fail → โปรแกรมตายทันที ไม่รู้ว่าตัวอื่นเป็นไง และ -DNDEBUG ปิด assert หมด

test runner จิ๋ว — นับ pass/fail

// test.h
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#include <string.h>

static int tests_run = 0;
static int tests_failed = 0;

#define CHECK(cond) do {                                       \
    tests_run++;                                               \
    if (!(cond)) {                                             \
        tests_failed++;                                        \
        printf("  FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
    }                                                          \
} while (0)

#define CHECK_EQ_INT(a, b) do {                                \
    tests_run++;                                               \
    long _a = (a), _b = (b);                                   \
    if (_a != _b) {                                            \
        tests_failed++;                                        \
        printf("  FAIL: %s == %s  (got %ld, want %ld) (%s:%d)\n", \
               #a, #b, _a, _b, __FILE__, __LINE__);            \
    }                                                          \
} while (0)

#define CHECK_STR(a, b) do {                                   \
    tests_run++;                                               \
    if (strcmp((a), (b)) != 0) {                               \
        tests_failed++;                                        \
        printf("  FAIL: got \"%s\", want \"%s\" (%s:%d)\n",    \
               (a), (b), __FILE__, __LINE__);                  \
    }                                                          \
} while (0)

#define TEST(name) printf("[%s]\n", name)

#define TEST_SUMMARY() do {                                    \
    printf("\n%d ผ่าน, %d ล้มเหลว (จาก %d)\n",                \
           tests_run - tests_failed, tests_failed, tests_run); \
    return tests_failed > 0 ? 1 : 0;                           \
} while (0)

#endif

ใช้:

// test_math.c
#include "test.h"
#include "mathutil.h"

static void test_gcd(void)
{
    TEST("gcd");
    CHECK_EQ_INT(gcd(12, 18), 6);
    CHECK_EQ_INT(gcd(17, 5), 1);
    CHECK_EQ_INT(gcd(100, 100), 100);
    CHECK_EQ_INT(gcd(0, 7), 7);
}

static void test_factorial(void)
{
    TEST("factorial");
    CHECK_EQ_INT(factorial(0), 1);
    CHECK_EQ_INT(factorial(1), 1);
    CHECK_EQ_INT(factorial(5), 120);
}

static void test_is_prime(void)
{
    TEST("is_prime");
    CHECK(is_prime(2));
    CHECK(!is_prime(1));
    CHECK(!is_prime(0));
    CHECK(is_prime(97));
    CHECK(!is_prime(100));
}

int main(void)
{
    test_gcd();
    test_factorial();
    test_is_prime();
    TEST_SUMMARY();
}
[gcd]
[factorial]
[is_prime]

12 ผ่าน, 0 ล้มเหลว (จาก 12)

ถ้ามี fail:

[gcd]
  FAIL: gcd(17, 5) == 1  (got 5, want 1) (test_math.c:9)

11 ผ่าน, 1 ล้มเหลว (จาก 12)

exit code ไม่ใช่ 0 → CI จับได้

รวมเข้า Makefile

CC = gcc
CFLAGS = -std=c17 -Wall -Wextra -g -fsanitize=address,undefined

test_math: test_math.c mathutil.c
	$(CC) $(CFLAGS) -o $@ $^

.PHONY: test
test: test_math
	./test_math

.PHONY: clean
clean:
	rm -f test_math *.o
make test

รันเทสต์พร้อม ASan/UBSan — จับบั๊กหน่วยความจำในโค้ดที่ทดสอบด้วย

ทดสอบกรณี edge

static void test_edge_cases(void)
{
    TEST("edge cases");
    CHECK_EQ_INT(square(0), 0);
    CHECK_EQ_INT(square(-4), 16);

    // ทดสอบ overflow / ขอบเขต
    CHECK(safe_multiply(INT_MAX, 2, &(int){0}) == -1);

    // อาร์เรย์ว่าง, NULL, ขนาด 1
    Vector *v = vec_new();
    CHECK(v != NULL);
    CHECK_EQ_INT(vec_len(v), 0);
    CHECK(vec_get(v, 0, &(int){0}) != 0);   // อ่านจากอาร์เรย์ว่าง = error
    vec_free(v);
}

edge cases ที่ควรทดสอบเสมอ: 0, ค่าลบ, ค่าสูงสุด (INT_MAX), อาร์เรย์ว่าง, ขนาด 1, NULL pointer, สตริงว่าง

framework ที่มีจริง

สำหรับโปรเจกต์ใหญ่:

  • Unity — เล็ก, header-only-ish, นิยมใน embedded
  • Criterion — modern, แต่ละ test แยก process (crash ไม่ล้มทั้งชุด), มี parameterized test
  • CMocka — มี mocking, ใช้กับ CMake
  • greatest / µnit — single-header, เบามาก

แต่ test runner จิ๋วข้างบนเพียงพอสำหรับเริ่มต้นและโปรเจกต์เล็ก–กลาง

เรื่องที่คนมักพลาด

  • ทดสอบเฉพาะ happy path — บั๊กอยู่ใน edge case ทดสอบ 0, ลบ, สูงสุด, ว่าง, NULL
  • ใช้ assert ล้วน — fail ตัวแรกแล้วหยุด, -DNDEBUG ปิดหมด ใช้ macro ที่นับต่อ
  • เทสต์ไม่รันใน CI / ไม่คืน exit code — เทสต์ที่ไม่รันอัตโนมัติก็เหมือนไม่มี
  • ไม่รันเทสต์พร้อม sanitizer — พลาดบั๊กหน่วยความจำในโค้ดที่ทดสอบ
  • โค้ดผูกกับ I/O — แยก logic บริสุทธิ์ออกมาก่อน
  • เทสต์พึ่งกัน / พึ่งลำดับการรัน — แต่ละ test ต้องเป็นอิสระ, สร้าง fixture ใหม่

สรุป

  • แยก logic บริสุทธิ์ (input → output) ออกจาก I/O ก่อน — ทดสอบได้ง่าย
  • assert ล้วนก็เริ่มได้ แต่ macro CHECK ที่นับ pass/fail และคืน exit code ดีกว่า (~30 บรรทัด)
  • รวมเข้า make test พร้อม -fsanitize=address,undefined
  • ทดสอบ edge case เสมอ: 0, ลบ, INT_MAX, ว่าง, ขนาด 1, NULL
  • framework จริง: Unity, Criterion, CMocka — ใช้เมื่อโปรเจกต์โต

จบเฟสความถูกต้อง บทถัดไปเข้าเฟสขั้นสูง เริ่มที่ printf/scanf ให้ครบและปลอดภัย — format specifier ทั้งหมดและ snprintf