用特殊标示符编写简单的测试程序
来源:互联网 发布:农产品网络营销策划 编辑:程序博客网 时间:2024/05/29 15:41
自己动手写的一个测试执行语句对错的程序,C函数库中其实已有。
assert.h文件,定义assert宏函数,并实现测试功能
/** assert是用来判断test执行是否出错,若出错则输出错误文件及行号*/#undef assert //如果以前定义了宏assert, 取消#ifdef NDUBUG //若定义了宏NDUBUG,NDEBUG一般是用来使宏assert失效的#define assert(test) ((void)0) #elsevoid _Assert(char *); //声明_Assert函数,#define _STR(x) _VAL(x)#define _VAL(x) #x//测试test的正确性,正确则无返回,错误,则打印错误的文件及相应的行号#define assert(test) ((test) ? (void)0 : _Assert(__FILE__ ":" _STR(__LINE__) " " #test))#endif
assert.c文件, 实现_Assert()函数,即对测试结果进行输出功能
#include <stdio.h>#include <stdlib.h>void _Assert(char *mesg){//print assertion message and abortfputs(mesg, stderr);fputs(" -- assertion failed\n", stderr);abort();}
main,c文件,调用宏assert进行测试
/* * 三个特殊标识符 * __FILE__ 文件名(string) * __LINE__ 当前执行的行号 (int) * __func__ 当前执行的函数名(string)*/#include <stdio.h>#include "assert.h"int main(void){printf("%s\n", __func__); //这条语句不能放在 assert(2 > 3)后面,因为assert判断语句若是错误的话,则会中止程序(abort())assert(2 > 3);return 0;}
- 用特殊标示符编写简单的测试程序
- 用C编写的简单程序
- 简单测试的makefile编写
- 编写可测试的程序
- 编写可测试的程序
- 驱动测试程序的编写
- netlink测试程序的编写
- netlink测试程序的编写
- 简单的测试程序
- 循环的标示符
- 编写简单的XPath程序
- 编写简单的OpenGL程序
- 编写简单的XPath程序
- 编写简单的java程序
- 测试用C/C++编写的程序运行的时间
- Applet web导航基本演习...只是简单的按钮转向网址功能程序测试编写
- 简单的用PHP编写的导航条程序
- 全能正则表达式测试程序的编写
- cocos2d游戏 判断触摸点是否触摸到精灵上
- Linux root忘记密码如何登陆-详解
- PackManager和ActivityManager的功能介绍
- 校园网认证
- Uva-11210-Chinese Mahjong
- 用特殊标示符编写简单的测试程序
- python单例模式
- 应用里面调用安装应用的方法
- 感
- 一个网站的建设步骤(写给新手)
- delphi 使用image动态加载图片时的错误提醒
- android平板最小system/app
- Java EE体系结构
- POJ 1511 Invitation Cards