http://hi.baidu.com/richardma_/blog/item/99a973114bac86dcf7039e84.html
Qemu中用的是这个做测试。
头文件:
#ifndef __CRIEDT_H__
#define __CRIEDT_H__
int criedt(int c);
#endif /* __CRIEDT_H__ */
被测试文件(函数):
#include "criedt.h"
int criedt(int c)
{
if (c > 500) return 1;
else if (c > 300 && c <= 500) return 2;
else if (c > 150 && c <= 300) return 3;
else if (c>0 && c<= 150) return 4;
else return 0;
}
测试文件:
#include <glib.h>
#include "criedt.h"
static void test_500(void)
{
int test;
test = 600;
g_assert_cmpint(criedt(test), ==, 1);
test = 501;
g_assert_cmpint(criedt(test), ==, 1);
test = 500;
g_assert_cmpint(criedt(test), ==, 2);
test = 499;
g_assert_cmpint(criedt(test), ==, 2);
}
static void test_300(void)
{
int test;
test = 301;
g_assert_cmpint(criedt(test), ==, 2);
test = 300;
g_assert_cmpint(criedt(test), ==, 3);
test = 299;
g_assert_cmpint(criedt(test), ==, 3);
}
static void test_150(void)
{
int test;
test = 151;
g_assert_cmpint(criedt(test), ==, 3);
test = 150;
g_assert_cmpint(criedt(test), ==, 4);
test = 149;
g_assert_cmpint(criedt(test), ==, 4);
}
static void test_0(void)
{
int test;
test = 1;
g_assert_cmpint(criedt(test), ==, 4);
test = 0;
g_assert_cmpint(criedt(test), ==, 0);
test = -1;
g_assert_cmpint(criedt(test), ==, 0);
}
int main(int argc, char *argv[])
{
/* 初始化测试框架,必须传入NULL作为结束 */
g_test_init(&argc, &argv, NULL);
/* 注册测试函数,在测试过程中,这些函数将按顺序执行 */
g_test_add_func("/criedt/500", test_500);
g_test_add_func("/criedt/300", test_300);
g_test_add_func("/criedt/150", test_150);
g_test_add_func("/criedt/0", test_0);
/* 运行测试 */
return g_test_run();
}
编译:
gcc -c -o criedt.o criedt.c
gcc -o gtest `pkg-config --cflags --libs glib-2.0` criedt.o gtest.c
运行测试:
gtester -k --verbose -o=test.log ./gtest
TEST: ./gtest... (pid=20690)
/criedt/500: OK
/criedt/300: OK
/criedt/150: OK
/criedt/0: OK
PASS: ./gtest
生成html的报告:
gtest-report test.log > test-report.html
蛮好的一个工具。