がんばって英語で書くブログ
英語勉強中なので、ブログも英語で書こうかと。
スポンサーサイト
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

unit test for c
simple unit test framework


/*
* c_unit_test.h
* simpe unit test framework for C
*
* Copyright (C) 2006 foobar <storm_of_the_death at yahoo.co.jp>
*
* GNU General Public License (GPL) version 2.
*/
#ifndef C_UNIT_TEST_H
#define C_UNIT_TEST_H 1

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

typedef int test_result_t;

#define TEST_OK 1
#define TEST_FAIL 0

#ifndef N_C_UNIT_TEST
#define assert_NULL(result) c_assert_NULL(result, __FILE__, __LINE__, __FUNCTION__)

#define assert_char(expected, result) assert_long((long) expected, (long) result)
#define assert_uchar(expected, result) assert_ulong((unsigned long) expected, (unsigned long) result)

#define assert_short(expected, result) assert_long((long) expected, (long) result)
#define assert_ushort(expected, result) assert_ulong((unsigned long) expected, (unsigned long) result)

#define assert_int(expected, result) assert_long((long) expected, (long) result)
#define assert_uint(expected, result) assert_ulong((unsigned long) expected, (unsigned long) result)

#define assert_long(expected, result) c_assert_long(expected, result, __FILE__, __LINE__, __FUNCTION__)
#define assert_ulong(expected, result) c_assert_ulong(expected, result, __FILE__, __LINE__, __FUNCTION__)

#define assert_float(expected, result) assert_double((double) expected, (double) result)
#define assert_double(expected, result) c_assert_double(expected, result, __FILE__, __LINE__, __FUNCTION__)

#define assert_equal(expected, result) c_assert_equal(expected, result, __FILE__, __LINE__, __FUNCTION__)

#else /* N_C_UNIT_TEST */

#define assert_NULL(result) /* none */

#define assert_char(expected, result) /* none */
#define assert_uchar(expected, result) /* none */

#define assert_short(expected, result) /* none */
#define assert_ushort(expected, result) /* none */

#define assert_int(expected, result) /* none */
#define assert_uint(expected, result) /* none */

#define assert_long(expected, result) /* none */
#define assert_ulong(expected, result) /* none */

#define assert_float(expected, result) /* none */
#define assert_double(expected, result) /* none */

#define assert_equal(expected, result) /* none */

#endif /* N_C_UNIT_TEST */

static inline test_result_t c_assert_NULL(void *result, char *file, int line, char *func)
{
if (result == NULL) {
printf("%s:%d: %s: Assertion: result expected is NULL\n", file, line, func);
return TEST_FAIL;
}

return TEST_OK;
}

static inline test_result_t c_assert_long(long expected, long result, char *file, int line, char *func)
{
if (expected != result) {
printf("%s:%d: %s: Assertion: expected is %ld but result is %ld\n", file, line, func, expected, result);
return TEST_FAIL;
}

return TEST_OK;
}

static inline test_result_t c_assert_ulong(unsigned long expected, unsigned long result, char *file, int line, char *func)
{
if (expected != result) {
printf("%s:%d: %s: Assertion: expected is %ld but result is %ld\n", file, line, func, expected, result);
return TEST_FAIL;
}

return TEST_OK;
}

static inline test_result_t c_assert_double(double expected, double result, char *file, int line, char *func)
{
if (expected != result) {
printf("%s:%d: %s: Assertion: expected is %f but result is %f\n", file, line, func, expected, result);
return TEST_FAIL;
}

return TEST_OK;
}

static inline test_result_t c_assert_equal(char *expected, char *result, char *file, int line, char *func)
{
if (strcmp(expected, result)) {
printf("%s:%d: %s: Assertion: expected is %s but result is %s\n", file, line, func, expected, result);
return TEST_FAIL;
}

return TEST_OK;
}

#endif /* C_UNIT_TEST_H */



How to use? like this.

I wanna test this file.
#include <stdio.h>

int add(int x, int y)
{
return x + y;
}

test code is this.
sample_test.c
#include "c_unit_test.h"

void test_add(void)
{
int ret;

/* 3 + 5 = 8 */
ret = add(3, 5);
assert_int(8, ret);

/* -1 + -1 = -2 */
ret = add(-1, -1);
assert_int(-2, ret);
}

int main(int argc, char **argv)
{
test_add();
return 0;
}

% gcc sample.c sample_test.c -o sample
% ./sample

if test is fail, output will be this.
sample_test.c:10: test_add: Assertion: value is 8 but result is 9
sample_test.c:14: test_add: Assertion: value is -2 but result is -1
スポンサーサイト

テーマ:プログラミング - ジャンル:コンピュータ

この記事に対するコメント

この記事に対するコメントの投稿














管理者にだけ表示を許可する


この記事に対するトラックバック
トラックバックURL
→http://foobar.blog23.fc2.com/tb.php/136-a82c94d9
この記事にトラックバックする(FC2ブログユーザー)

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。