这个assert功能比较齐全,原帖地址是
http://blog.csdn.net/sealyao/archive/2011/01/30/6169568.aspx
- //-------file main.c----------
- #include <stdio.h>
- #include "myassert.h"
- int func( const char *filename);
- int main( int argc, char **argv)
- {
- MyAssert("two args are needed" ,argc==2);
- func(argv[1]);
- return 0;
- }
- //-------file func.c----------
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "myassert.h"
- int func( const char *filename)
- {
- int fd;
- MyAssert("filename can not be null" ,filename);
- MyAssert("file not exist" ,0==access(filename,F_OK));
- fd = open(filename,O_RDONLY);
- close(fd);
- return 0;
- }
- //-------file myassert.h----------
- #ifndef __MY_ASSERT_H__
- #define __MY_ASSERT_H__
- #include <stdio.h>
- #include <stdlib.h>
- #define MyAssert(message,assertion) do{/
- if (!(assertion)){/
- printf("line %d in %s(%s)" , __LINE__, __FILE__,__FUNCTION__);/
- if (message){/
- printf(" : %s" ,message);/
- }/
- printf("/n" );/
- abort();/
- }/
- }while (0);
- #endif
- #Makefile
- TARGET = test
- CC = gcc
- CCFLAGS = -Wall
- OBJS = main.o func.o
- $(TARGET) : $(OBJS)
- $(CC) -o $@ $(OBJS) $(CCFLAGS)
- %.o : %.c
- $(CC) -o $@ -c $< $(CCFLAGS)
- clean:
- rm -rf *.o
- rm -rf $(TARGET)
#include <stdio.h>
#include <stdlib.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define BUG_ON(condition) do /
{ if (unlikely((condition)!=0)) /
printf("BUG: failure at %s:%d/%s()!/n", __FILE__, __LINE__, __FUNCTION__); /
} while(0)
int main(void)
{
int i = 7;
BUG_ON(i!=10);
return 0;
}