cunit.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include "CUnit/Basic.h"
00031
00032
00033 static FILE* temp_file = NULL;
00034
00035
00036
00037
00038
00039 int init_suite1(void)
00040 {
00041 if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
00042 return -1;
00043 }
00044 else {
00045 return 0;
00046 }
00047 }
00048
00049
00050
00051
00052
00053 int clean_suite1(void)
00054 {
00055 if (0 != fclose(temp_file)) {
00056 return -1;
00057 }
00058 else {
00059 temp_file = NULL;
00060 return 0;
00061 }
00062 }
00063
00064
00065
00066
00067
00068 void testFPRINTF(void)
00069 {
00070 int i1 = 10;
00071
00072 if (NULL != temp_file) {
00073 CU_ASSERT(0 == fprintf(temp_file, ""));
00074 CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
00075 CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
00076 }
00077 }
00078
00079
00080
00081
00082
00083
00084 void testFREAD(void)
00085 {
00086 unsigned char buffer[20];
00087
00088 if (NULL != temp_file) {
00089 rewind(temp_file);
00090 CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
00091 CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
00092 }
00093 }
00094
00095
00096
00097
00098
00099 int main()
00100 {
00101 CU_pSuite pSuite = NULL;
00102
00103
00104 if (CUE_SUCCESS != CU_initialize_registry())
00105 return CU_get_error();
00106
00107
00108 pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
00109 if (NULL == pSuite) {
00110 CU_cleanup_registry();
00111 return CU_get_error();
00112 }
00113
00114
00115
00116 if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
00117 (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
00118 {
00119 CU_cleanup_registry();
00120 return CU_get_error();
00121 }
00122
00123
00124 CU_basic_set_mode(CU_BRM_VERBOSE);
00125 CU_basic_run_tests();
00126 CU_cleanup_registry();
00127 return CU_get_error();
00128 }