/* file: sample.h */ #include <stdio.h> #include <string.h> #include <stdlib.h> struct Student { char* name; int score; }; void ret_void(void); int ret_int(int, int); double ret_double(double, double); char* ret_pchar(char*, char*); struct Student* init_student(struct Student* s, char* name, int score); /* file: sample.c */ #include "sample.h" #ifndef CPPUTEST int main(int argc, char *argv[]) { char* pa; char* pb; pa = (char*) malloc(sizeof(char) * 80); pb = (char*) malloc(sizeof(char) * 20); strcpy(pa, "abcdefg\0"); strcpy(pb, "hijklmn\0"); printf ("Sample Start......\n"); ret_void(); printf ("ret_int: %d\n", ret_int(100, 10)); printf ("ret_double: %.2f\n", ret_double(100.0, 10.0)); printf ("ret_pchar: %s\n", ret_pchar(pa, pb)); struct Student* s = (struct Student*) malloc(sizeof(struct Student)); s->name = (char*) malloc(sizeof(char) * 80); init_student(s, "test cpputest", 100); printf ("init_Student: name=%s, score=%d\n", s->name, s->score); printf ("Sample End ......\n"); free(pa); free(pb); free(s->name); free(s); return 0; } #endif void ret_void() { printf ("Hello CPPUTest!\n"); } /* ia + ib */ int ret_int(int ia, int ib) { return ia + ib; } /* da / db */ double ret_double(double da, double db) { return da / db; } /* pa = pa + pb */ char* ret_pchar(char* pa, char* pb) { return strcat(pa, pb); } /* s->name = name, s->score = score */ void init_student(struct Student* s, char* name, int score) { strcpy(s->name, name); s->score = score; } |
/* file: test.c */ #include <CppUTest/CommandLineTestRunner.h> #include <CppUTest/TestHarness.h> extern "C" { #include "sample.h" } /* 定义个 TEST_GROUP, 名称为 sample */ TEST_GROUP(sample) {}; /* 定义一个属于 TEST_GROUP 的 TEST, 名称为 ret_int_success */ TEST(sample, ret_int_success) { int sum = ret_int(1, 2); CHECK_EQUAL(sum, 3); } /* 定义一个属于 TEST_GROUP 的 TEST, 名称为 ret_int_failed */ TEST(sample, ret_int_failed) { int sum = ret_int(1, 2); CHECK_EQUAL(sum, 4); } int main(int argc, char *argv[]) { CommandLineTestRunner::RunAllTests(argc, argv); return 0; } |
# makefile for sample cpputest CPPUTEST_HOME = /home/wangyubin/Downloads/cpputest-3.6 CC := gcc CFLAGS := -g -Wall CFLAGS += -std=c99 CFLAGS += -D CPPUTEST # 编译测试文件时, 忽略sample.c的main函数, sample.c的代码中用了宏CPPUTEST # CPPUTest 是C++写的, 所以用 g++ 来编译 测试文件 CPP := g++ CPPFLAGS := -g -Wall CPPFLAGS += -I$(CPPUTEST_HOME)/include LDFLAGS := -L$(CPPUTEST_HOME)/lib -lCppUTest sample: sample.o sample.o: sample.h sample.c $(CC) -c -o sample.o sample.c $(CFLAGS) # 追加的测试程序编译 test: test.o sample.o $(CPP) -o $@ test.o sample.o $(LDFLAGS) test.o: sample.h test.c $(CPP) -c -o test.o test.c $(CPPFLAGS) .PHONY: clean clean: @echo "clean..." rm -f test sample rm -f sample.o test.o |
$ ./test <-- 默认执行, 没有参数 test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ .. Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) ================================================================================= $ ./test -c <-- -c 执行结果加上颜色 (成功绿色, 失败红色) test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ .. Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) <-- bash中显示红色 ================================================================================= $ ./test -v <-- -v 显示更为详细的信息 TEST(sample, ret_int_failed) test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ - 1 ms TEST(sample, ret_int_success) - 0 ms Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) ================================================================================= $ ./test -r 2 <-- -r 指定测试执行的次数, 这里把测试重复执行2遍 Test run 1 of 2 test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ .. Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms) Test run 2 of 2 test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ .. Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) ================================================================================= $ ./test -g sample <-- -g 指定 TEST_GROUP, 本例其实只有一个 TEST_GROUP sample test.c:34: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ .. Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) ================================================================================= $ ./test -n ret_int_success <-- -s 指定执行其中一个 TEST, 名称为 ret_int_success . OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms) ================================================================================= $ ./test -v -n ret_int_success <-- 参数也可以搭配使用 TEST(sample, ret_int_success) - 0 ms OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms) |
/* 定义个 TEST_GROUP, 名称为 sample */ TEST_GROUP(sample) { void setup() { printf ("测试开始......\n"); } void teardown() { printf ("测试结束......\n"); } }; |
$ make clean clean... rm -f test sample rm -f sample.o test.o $ make test g++ -c -o test.o test.c -g -Wall -I/home/wangyubin/Downloads/cpputest-3.6/include gcc -c -o sample.o sample.c -g -Wall -std=c99 -D CPPUTEST g++ -o test test.o sample.o -L/home/wangyubin/Downloads/cpputest-3.6/lib -lCppUTest $ ./test -v TEST(sample, ret_int_failed)测试开始...... test.c:44: error: Failure in TEST(sample, ret_int_failed) expected <3> but was <4> difference starts at position 0 at: < 4 > ^ |
# makefile for sample cpputest CPPUTEST_HOME = /home/wangyubin/Downloads/cpputest-3.6 CC := gcc CFLAGS := -g -Wall CFLAGS += -std=c99 CFLAGS += -D CPPUTEST # 编译测试文件时, 忽略sample.c的main函数, sample.c的代码中用了宏CPPUTEST # CPPUTest 是C++写的, 所以用 g++ 来编译 测试文件 CPP := g++ CPPFLAGS := -g -Wall CPPFLAGS += -I$(CPPUTEST_HOME)/include LDFLAGS := -L$(CPPUTEST_HOME)/lib -lCppUTest # 内存泄露检测 MEMFLAGS = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h sample: sample.o sample.o: sample.h sample.c $(CC) -c -o sample.o sample.c $(CFLAGS) $(MEMFLAGS) # 追加的测试程序编译 test: test.o sample.o $(CPP) -o $@ test.o sample.o $(LDFLAGS) test.o: sample.h test.c $(CPP) -c -o test.o test.c $(CPPFLAGS) $(MEMFLAGS) .PHONY: clean clean: @echo "clean..." rm -f test sample rm -f sample.o test.o |
/* s->name = name, s->score = score */ void init_student(struct Student* s, char* name, int score) { char* name2 = NULL; name2 = (char*) malloc(sizeof(char) * 80); /* 这里申请的内存, 最后没有释放 */ strcpy(s->name, name2); strcpy(s->name, name); s->score = score; } |
$ make clean clean... rm -f test sample rm -f sample.o test.o $ make test g++ -c -o test.o test.c -g -Wall -I/home/wangyubin/Downloads/cpputest-3.6/include -include /home/wangyubin/Downloads/cpputest-3.6/include/CppUTest/MemoryLeakDetectorMallocMacros.h gcc -c -o sample.o sample.c -g -Wall -std=c99 -D CPPUTEST -include /home/wangyubin/Downloads/cpputest-3.6/include/CppUTest/MemoryLeakDetectorMallocMacros.h g++ -o test test.o sample.o -L/home/wangyubin/Downloads/cpputest-3.6/lib -lCppUTest $ ./test -v -n init_student |
test.c:47: error: Failure in TEST(sample, init_student) Memory leak(s) found. Alloc num (4) Leak size: 80 Allocated at: sample.c and line: 72. Type: "malloc" Memory: <0x120c5f0> Content: "" Total number of leaks: 1 NOTE: Memory leak reports about malloc and free can be caused by allocating using the cpputest version of malloc, but deallocate using the standard free. If this is the case, check whether your malloc/free replacements are working (#define malloc cpputest_malloc etc). - 0 ms Errors (1 failures, 3 tests, 1 ran, 0 checks, 0 ignored, 2 filtered out, 0 ms) |
原文:http://www.blogjava.net/qileilove/archive/2014/10/30/419208.html