[置顶] C语言单元测试框架
unitest.h
/******************************************************************************
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed *
* under the GNU Lesser General Public License Version 3, 29 June 2007. *
* The complete license can be accessed from the following location: *
* http://opensource.org/licenses/lgpl-3.0.html *
* *
* Author: Yun Li (yunli.open@gmail.com) *
* Date: 03/06/2010 *
* Author: Gavin Ding (gavin_ding@sina.cn / fanfan.ding.ict@gmail.com) *
* Date: 07/07/2013 *
******************************************************************************/ /******************************************************************************
REVISION HISTORY
================ Date Version Name Description
---------- ------------ ---------------- --------------------------------
03/17/2013 V001.000.000 Gavin Ding rewrite this file
---------- ------------ ---------------- -------------------------------- Date Version Name Description
---------- ------------ ---------------- --------------------------------
07/07/2013 V001.001.000 Gavin Ding make it more useful
---------- ------------ ---------------- -------------------------------- ******************************************************************************/ #ifndef __UNITEST_H
#define __UNITEST_H #include <stdio.h>
#include <string.h> static int g_case_total = 0;
static int g_case_succeeded = 0;
static int g_case_failed = 0; #ifndef NULL
#define NULL ((void*)0)
#endif #define INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, _oper) \
printf(" Expected: \""#_a"\" "#_oper" \""#_b"\"\n Result: Succeeded\n")
#define INTERNAL_USE_ONLY_FAILED(_a, _b, _oper) \
printf(" Expected: \""#_a"\" "#_oper" \""#_b"\"\n (X) Result: Failed\n")
#define INTERNAL_USE_ONLY_CASE_SHOW() \
printf("Case %d =====>\n", g_case_total); \
printf(" Location: %s:%d\n", __FILE__, __LINE__); \ #define UNITEST_ERROR(_string) do { \
printf("Error: "#_string"\n"); \
return -1;\
} while (0) #define UNITEST_EQUALS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) == (_b)) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, ==); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, ==); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_DIFFERS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) == (_b)) { \
INTERNAL_USE_ONLY_FAILED(_a, _b, !=); \
g_case_failed++; \
} \
else { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, !=); \
g_case_succeeded++; \
} \
printf("\n"); \
} while (0) #define UNITEST_LESS_THAN(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) < (_b)) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, <); \
g_case_succeeded ++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, <); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_LESS_THAN_EQUALS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) <= (_b)) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, <=); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, <=); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_GREATER_THAN(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) > (_b)) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, >); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, >); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_GREATER_THAN_EQUALS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ((_a) >= (_b)) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, >=); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, >=); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_STR_EQUALS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ( NULL == (_a) || NULL == (_b) ) { \
UNITEST_ERROR(ptr should not be NULL); \
} \
if ( !strcmp((_a), (_b)) ) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, ==); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, ==); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_STR_DIFFERS(_a, _b) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ( NULL == (_a) || NULL == (_b) ) { \
UNITEST_ERROR(ptr should not be NULL); \
} \
if ( !strcmp((_a), (_b)) ) { \
INTERNAL_USE_ONLY_FAILED(_a, _b, !=); \
g_case_failed++; \
} \
else { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, !=); \
g_case_succeeded++; \
} \
printf("\n"); \
} while (0) #define UNITEST_MEM_EQUALS(_a, _b, _len) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ( NULL == (_a) || NULL == (_b) ) { \
UNITEST_ERROR(ptr should not be NULL); \
} \
if ( (_len) <= 0 ) { \
UNITEST_ERROR(mem len should be positive); \
} \
if ( !memcmp((void*)(_a),(void*)(_b), (int)(_len)) ) { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, ==); \
g_case_succeeded++; \
} \
else { \
INTERNAL_USE_ONLY_FAILED(_a, _b, ==); \
g_case_failed++; \
} \
printf("\n"); \
} while (0) #define UNITEST_MEM_DIFFERS(_a, _b, _len) do { \
g_case_total++; \
INTERNAL_USE_ONLY_CASE_SHOW(); \
if ( NULL == (_a) || NULL == (_b) ) { \
UNITEST_ERROR(ptr should not be NULL); \
} \
if ( (_len) <= 0 ) { \
UNITEST_ERROR(mem len should be positive); \
} \
if ( !memcmp((void*)(_a), (void*)(_b), (int)(_len)) ) { \
INTERNAL_USE_ONLY_FAILED(_a, _b, !=); \
g_case_failed++; \
} \
else { \
INTERNAL_USE_ONLY_SUCCEEDED(_a, _b, !=); \
g_case_succeeded++; \
} \
printf("\n"); \
} while (0) static void unitest_bar()
{
printf("********************************************************\n");
printf("* Unit Test *\n");
printf("********************************************************\n");
printf("\n");
} static int unitest_report()
{
printf("\n");
printf("********************************************************\n");
printf("* Test Report *\n");
printf("********************************************************\n"); if (0 == g_case_total) {
printf("No test case is run\n");
goto pass;
}
printf(" Total: %d\n", g_case_total);
printf("Succeeded: %d (%d%%)\n", g_case_succeeded, g_case_succeeded * 100 / g_case_total);
printf("********************************************************\n");
printf("\n"); if (g_case_total != g_case_succeeded) {
printf("\n");
printf(":-( Failed\n");
printf("\n");
return -1;
} pass:
printf("\n");
printf(":-) Passed\n");
printf("\n"); return 0;
} #ifndef HAVE_MAIN
extern int unitest_main(int _argc, char *_argv[]); int main(int _argc, char *_argv[])
{
unitest_bar();
unitest_main(_argc, _argv);
return unitest_report();
}
#endif /* end of HAVE_MAIN */ #endif /* end of __UNITEST_H */
unitest_demo.c
/******************************************************************************
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed *
* under the GNU Lesser General Public License Version 3, 29 June 2007. *
* The complete license can be accessed from the following location: *
* http://opensource.org/licenses/lgpl-3.0.html *
* *
* Author: Gavin Ding (gavin_ding@sina.cn / fanfan.ding.ict@gmail.com) *
* Date: 07/07/2013 *
* *
******************************************************************************/ /******************************************************************************
REVISION HISTORY
================ Date Version Name Description
-------- ------- ------------ -------------------------------------------- -------- ------- ------------ -------------------------------------------- ******************************************************************************/
#include "unitest.h"
#include <stdlib.h> #define UNUSED(param) ((param) = (param)) int unitest_main (int argc, char *argv[])
{
char *str1 = "YT";
char *str2 = "HW";
char *str3 = "HW";
void *p1 = NULL;
void *p2 = NULL; UNUSED(argc);
UNUSED(argv); UNITEST_EQUALS(87, 87); UNITEST_DIFFERS(10, 13); UNITEST_LESS_THAN(41, 70); UNITEST_LESS_THAN_EQUALS(8, 8);
UNITEST_LESS_THAN_EQUALS(7, 8); UNITEST_GREATER_THAN(70, 41); UNITEST_GREATER_THAN_EQUALS(8, 8);
UNITEST_GREATER_THAN_EQUALS(8, 7); UNITEST_STR_EQUALS(str2, str3); UNITEST_STR_DIFFERS(str1, str2); p1 = malloc(10);
memset(p1, 2, 10); p2 = malloc(10);
memset(p2, 2, 10); UNITEST_MEM_EQUALS(p1, p2, 10); memset(p2, 5, 10);
UNITEST_MEM_DIFFERS(p1, p2, 10); free(p1);
p1 = NULL; free(p2);
p2 = NULL; return 0;
}
unitest_demo.result
./unitest/unitest_demo.exe
********************************************************
* Unit Test *
******************************************************** Case 1 =====>
Location: unitest_demo.c:41
Expected: "87" == "87"
Result: Succeeded Case 2 =====>
Location: unitest_demo.c:43
Expected: "10" != "13"
Result: Succeeded Case 3 =====>
Location: unitest_demo.c:45
Expected: "41" < "70"
Result: Succeeded Case 4 =====>
Location: unitest_demo.c:47
Expected: "8" <= "8"
Result: Succeeded Case 5 =====>
Location: unitest_demo.c:48
Expected: "7" <= "8"
Result: Succeeded Case 6 =====>
Location: unitest_demo.c:50
Expected: "70" > "41"
Result: Succeeded Case 7 =====>
Location: unitest_demo.c:52
Expected: "8" >= "8"
Result: Succeeded Case 8 =====>
Location: unitest_demo.c:53
Expected: "8" >= "7"
Result: Succeeded Case 9 =====>
Location: unitest_demo.c:55
Expected: "str2" == "str3"
Result: Succeeded Case 10 =====>
Location: unitest_demo.c:57
Expected: "str1" != "str2"
Result: Succeeded Case 11 =====>
Location: unitest_demo.c:65
Expected: "p1" == "p2"
Result: Succeeded Case 12 =====>
Location: unitest_demo.c:68
Expected: "p1" != "p2"
Result: Succeeded ********************************************************
* Test Report *
********************************************************
Total: 12
Succeeded: 12 (100%)
******************************************************** :-) Passed
[置顶] C语言单元测试框架的更多相关文章
- C语言单元测试框架--EmbedUnit
1.简介 Embedded Unit是个纯标准c构建的单元测试框架,主要用在嵌入式c的单体测试上,其主要特点是不依赖于任何C的标准库,所有的对象都是静态分配. 最早这个项目托管在SourceForge ...
- c语言单元测试框架--CuTest
1.简介 CuTest是一款微小的C语言单元测试框,是我迄今为止见到的最简洁的测试框架之一,只有2个文件,CuTest.c和CuTest.h,全部代码加起来不到一千行.麻雀虽小,五脏俱全,测试的构建. ...
- [置顶] EasyMock构建单元测试
1. 背景 单元测试作为程序的基本保障.很多时候构建测试场景是一件令人头疼的事.因为之前的单元测试都是内部代码引用的,环境自给自足.开发到了一定程度,你不得不到开始调用外部的接口来完成你的功能.而外部 ...
- [置顶] 轻量级语言Lua入门
作为一个脚本爱好者,而且是脚本(Perl)起家的我,一有空就喜欢学习下这些脚本语言.据说魔兽世界.愤怒小鸟都用到了它,所以今天研究下Lua这个叫法有点奇特的脚本 [转载请注明出处:http://blo ...
- [置顶] CSS语言精粹
本文主要是对CSS中一些比较重要的高级部分作了一些整理,这些内容也许不是经常使用,但是都很强大.本文将长期更新. 边框 CSS2.1 规定:元素的背景是内容.内边距和边框区的背景.设置背景颜色时是没有 ...
- [置顶]
滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理
上周末,滴滴与360都开源了各自的插件化框架,VirtualAPK与RePlugin,作为一个插件化方面的狂热研究者,在周末就迫不及待的下载了Virtualapk框架来进行研究,本篇博客带来的是Vir ...
- [置顶]
C语言学习入门
编译文件:cc -c one.c two.c 生成.o目标文件 链接文件:cc one.o two.o 默认生成 a.out 执行文件 指定生成的可执行文件名 cc -o one one ...
- [置顶]
C语言itoa()函数和atoi()函数详解(整数转字符C实现)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...
- 编C语言单元测试框架CUnit方法库
/********************************************************************* * Author : Samson * Date ...
随机推荐
- PHP 用Class构造JSON数据
header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...
- OA系统权限管理设计方案学习
学习之:http://www.cnblogs.com/kivenhou/archive/2009/10/19/1586106.html 此为模型图: 据此写了sql语句: drop table if ...
- yzoi2226最小步数的详细解法
Description - 问题描述 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字. ...
- Grunt:多个css,js,进行单独压缩
module.exports = function (grunt) { // 构建任务配置 grunt.initConfig({ //读取package.json的内容,形成个json数据 pkg: ...
- 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。
源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...
- vim技巧:折叠快捷键
vim技巧:折叠快捷键 以前用的挺熟的,一段时间不用了,快捷键又忘了,不得不重新再看手册,今天专门整理一下,以后查找起来也比较方便. zc 折叠,只折叠最外层的折叠zC 对所在范围内所有嵌套的折叠点进 ...
- 关于移动手机端富文本编辑器qeditor图片上传改造
日前项目需要在移动端增加富文本编辑,上网找了下,大多数都是针对pc版的,不太兼容手机,当然由于手机屏幕小等原因也限制富文本编辑器的众多强大功能,所以要找的编辑器功能必须是精简的. 找了好久,发现qed ...
- Sereja and Suffixes
Codeforces Round #215 (Div. 2) B:http://codeforces.com/problemset/problem/368/B 题意:给你一个序列,然后查询i--n中没 ...
- Jenkins安装入门
这是一次兴奋之旅哈..说不定用得着呢~~~:) 嘿嘿.. 安装很简单,JDK,MAVEN(如果),YUM或RPM包安装JENKINS(因为好像YUM安装好慢,不如RPM下载安装) 参考URL: htt ...
- 【HDOJ】3085 Nightmare Ⅱ
双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...