diff --git a/src/fmock/fmock.c b/src/fmock/fmock.c index c1cbaa4..ce3aa73 100644 --- a/src/fmock/fmock.c +++ b/src/fmock/fmock.c @@ -16,6 +16,7 @@ size_t count_expect_mock=0; pthread_mutex_t mut_count_expect_mock; struct list_base_fmock *g_list_base_fmock=NULL; +pthread_mutex_t mut_g_list_base_fmock; #define LOCK(mutex_var) pthread_mutex_lock(&mutex_var); #define UNLOCK(mutex_var) pthread_mutex_unlock(&mutex_var); @@ -47,19 +48,23 @@ int parse_count_args_(char *input){ */ void append_list_base_fmock(struct list_base_fmock **l_fmock, struct func_mock_info_struct *f_mock){ INCREMENT_(count_f_mock); + LOCK(mut_g_list_base_fmock); + PRINT_DEBUG(" append_list_base_fmock %s",f_mock->str_namefunc); if(*l_fmock){ struct list_base_fmock *tmp_l_n = *l_fmock; while(tmp_l_n->next) tmp_l_n = tmp_l_n->next; (tmp_l_n->next) = malloc(sizeof(struct list_base_fmock)); (tmp_l_n->next)->info_mock = f_mock; + (tmp_l_n->next)->next = NULL; } else{ *l_fmock = malloc(sizeof(struct list_base_fmock)); (*l_fmock)->info_mock = f_mock; } + UNLOCK(mut_g_list_base_fmock); } -struct func_mock_info_struct *f_mock_glist; +struct func_mock_info_struct *f_mock_glist = NULL; pthread_mutex_t mut_f_mock_glist; void append_variable_current(struct list_current_variable **lcurrent_var, char *current_var){ @@ -75,7 +80,7 @@ void append_variable_current(struct list_current_variable **lcurrent_var, char * } else{ - PRINTF("-- lcurrent_var NULL\n"); + PRINT_DEBUG("-- lcurrent_var %s\n","NULL"); *lcurrent_var = malloc(sizeof(struct list_current_variable)); //(*lcurrent_var)->str_current_variables = malloc(strlen(current_var)); //strcpy((*lcurrent_var)->str_current_variables,current_var); @@ -90,6 +95,7 @@ void append_fmock_to_listmock(struct func_mock_info_struct **f_mock_list, struc INCREMENT_(count_expect_mock); } LOCK(mut_f_mock_glist); + PRINT_DEBUG(" append_fmock_to_listmock %s",f_mock->str_namefunc); if(*f_mock_list){ struct func_mock_info_struct *tmp_fmock_info = *f_mock_list; while(tmp_fmock_info->next) tmp_fmock_info = tmp_fmock_info->next; @@ -124,6 +130,7 @@ __attribute__((constructor)) void init_mock_c(){ + pthread_mutex_init(&mut_g_list_base_fmock, NULL); pthread_mutex_init(&mut_f_mock_glist, NULL); pthread_mutex_init(&mut_count_f_mock_wished, NULL); pthread_mutex_init(&mut_count_f_mock, NULL); @@ -140,6 +147,7 @@ char * number_call_translate(long nb){ else if(nb == 1) sprintf(ret," to be called once"); else if(nb == 0 ) sprintf(ret," not to be executed"); else if(nb==INFINITY) sprintf(ret," to be called forever"); + else if(nb==INITSTATE) sprintf(ret," not expected"); else sprintf(ret," nothing! it's negative:%ld", nb); return ret; @@ -172,6 +180,8 @@ __attribute__((destructor)) void check_mock_excpected(){ + signal(SIGSEGV, SIG_DFL); /* restore default behaviour , */ + //int kdefault=0, kgreen=1, kred=2, kyellow=3, kblue=4, knothing; if(unicolour) { kgreen=knothing; kred=knothing; kyellow=knothing; kblue=knothing; @@ -244,7 +254,7 @@ __attribute__((destructor)) PRINTF("%s%s%s\n\n",colors_f[kblue],reader,DEFAULT_K ); while(tmp_inf_mock){ if(0==strncmp(tmp_inf_mock->str_namefunc,nameff, len_nameff)){ - if(tmp_inf_mock->expect_call){ + if(tmp_inf_mock->expect_call==1){ int success = !((tmp_inf_mock->init_times_left == tmp_inf_mock->times_left) || (tmp_inf_mock->failed_call)); if(tmp_inf_mock->l_current_var){ @@ -257,7 +267,7 @@ __attribute__((destructor)) colors_f[!unicolour*(kred - success)],tab_hk_f[hk_FL-success],colors_f[knothing*success],tmp_inf_mock->str_namefunc, number_call_translate(tmp_inf_mock->init_times_left), tmp_inf_mock->call, tmp_inf_mock->failed_call, strprint_caller_(tmp_inf_mock->str_caller), tmp_inf_mock->str_conditions , DEFAULT_K); } - }else{/* will expect */ + }else if(tmp_inf_mock->expect_call==0) {/* will expect */ int success = !(tmp_inf_mock->failed_call); if(tmp_inf_mock->l_current_var){ PRINTF("%s%s%s %s\t will %s,\t called %ld times and failed %ld times %s,\t with condition: %s,%s\n" , @@ -270,16 +280,35 @@ __attribute__((destructor)) tmp_inf_mock->failed_call, strprint_caller_(tmp_inf_mock->str_caller), tmp_inf_mock->str_conditions , DEFAULT_K); } } + else if(tmp_inf_mock->expect_call==-1){ + if(tmp_inf_mock->l_current_var){ + PRINTF("%s%s%s %s\t %s,\t called %ld times, %s\n" , + colors_f[!unicolour*(kred)],tab_hk_f[hk_FL],colors_f[kdefault],tmp_inf_mock->str_namefunc, + number_call_translate(tmp_inf_mock->init_times_left), tmp_inf_mock->call, + strprint_caller_(tmp_inf_mock->str_caller) ); + PRINT_VAR_CUR(tmp_inf_mock); + }else{ + PRINTF("%s%s%s %s\t %s,\t called %ld times, %s\n" , + colors_f[!unicolour*(kred)],tab_hk_f[hk_FL],colors_f[kdefault],tmp_inf_mock->str_namefunc, + number_call_translate(tmp_inf_mock->init_times_left), tmp_inf_mock->call, + strprint_caller_(tmp_inf_mock->str_caller)); + } + + } } tmp_inf_mock = tmp_inf_mock->next; } + PRINT_DEBUG(" end listing info mock of %s \n", nameff); tmp_list_fm = tmp_list_fm->next; } - PRINT_DEBUG("%s\n","check mock done!"); + PRINT_DEBUG("%s\n","info mock done!"); + pthread_mutex_destroy(&mut_g_list_base_fmock); pthread_mutex_destroy(&mut_f_mock_glist); pthread_mutex_destroy(&mut_count_f_mock_wished); pthread_mutex_destroy(&mut_count_f_mock); pthread_mutex_destroy(&mut_count_expect_mock); + PRINT_DEBUG("%s\n","pthread_mutex_destroy done!"); + PRINT_DEBUG("%s\n","check mock done!"); } diff --git a/src/fmock/fmock.h b/src/fmock/fmock.h index 3b7dc0c..61937df 100644 --- a/src/fmock/fmock.h +++ b/src/fmock/fmock.h @@ -8,6 +8,7 @@ #define INITSTATE -1 #define DONOTHING 0 +#define PRE_ID ___line_ /* * list of each variable called * use str_print_current_variables attibute function pointer to record variable @@ -64,6 +65,25 @@ extern struct list_base_fmock *g_list_base_fmock; #endif +#define INIT_MOCK_INFO_IF_NO__(tmp__mock, namefunction, pre_id, id) \ + (tmp__mock)->run = NULL;\ + (tmp__mock)->call_mock_condition = NULL;\ + /*(tmp__mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;*/\ + ((tmp__mock)->info_mock)->expect_call = -1;\ + ((tmp__mock)->info_mock)->call = 0;\ + ((tmp__mock)->info_mock)->failed_call = 0;\ + ((tmp__mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 32 + strlen(#pre_id));\ + sprintf(((tmp__mock)->info_mock)->str_namefunc,"%s%s%d",#namefunction,#pre_id,id);\ + ((tmp__mock)->info_mock)->str_conditions = NULL;\ + ((tmp__mock)->info_mock)->str_caller = NULL;\ + ((tmp__mock)->info_mock)->l_current_var= NULL;\ + ((tmp__mock)->info_mock)->next = NULL;\ + /*(tmp__mock)->next = NULL;*/\ + append_fmock_to_listmock(&f_mock_glist, (tmp__mock)->info_mock);\ + append_list_base_fmock( &g_list_base_fmock ,(tmp__mock)->info_mock); + +#define INIT_MOCK_INFO_IF_NO_(tmp_new_mock, namefunction, pre_id) \ + INIT_MOCK_INFO_IF_NO__(tmp_new_mock, namefunction, pre_id, __LINE__) \ #define MOCK_FUNC(returntype, namefunction, args_prototype_with_parenthesis, args_call_with_parenthesis)\ /*typedef returntype FUNC_type_ ## namefunction args_prototype_with_parenthesis ;*/\ @@ -80,33 +100,44 @@ extern struct list_base_fmock *g_list_base_fmock; list_mo_ ## namefunction.info_mock = malloc(sizeof(struct func_mock_info_struct));\ (list_mo_ ## namefunction.info_mock)->times_left = INITSTATE;\ (list_mo_ ## namefunction.info_mock)->init_times_left = INITSTATE;\ - /*list_mo_ ## namefunction.arg_count = parse_count_args_(#args_prototype_with_parenthesis);\ - list_mo_ ## namefunction.call_mock_condition = malloc(list_mo_ ## namefunction.arg_count * sizeof( int (*)(void*))) ;*/\ list_mo_ ## namefunction.str_print_current_variables = NULL;\ list_mo_ ## namefunction.next = NULL;\ }\ returntype namefunction args_prototype_with_parenthesis {\ - /*static size_t count_call_f=0;\ - PRINT_DEBUG(">>>>>>count call of %s: %ld\n",STRFY(namefunction),++count_call_f);*/\ + static size_t count_call_f=0;\ + ++count_call_f;\ + PRINT_DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>count call of %s: %ld\n",#namefunction,count_call_f);\ struct list_mock_return_ ## namefunction *tmp_mock = &list_mo_ ## namefunction;\ if( (tmp_mock->info_mock)->times_left == INITSTATE ){\ - PRINT_HK_C(YELLOW_K,HK_TR," WARNING, no EXPECT_MOCK_CALL or WILL_MOCK_CALL defined for the mock function %s.\n",#namefunction);\ - PRINT_HK_C(YELLOW_K,HK_TR," Can be defined by EXPECT_MOCK_CALL(%s,%s,%s,true,1) if call once and accept all args, the same args with WILL_MOCK_CALL \n",STRFY (returntype), STRFY(namefunction),STRFY (args_prototype_with_parenthesis) ); \ - return (returntype)0; \ + PRINT_HK_C(YELLOW_K, HK_TR," WARNING, %s, no EXPECT_MOCK_CALL or WILL_MOCK_CALL, but called %ld times.\n",#namefunction, count_call_f);\ + if(count_call_f==1){\ + PRINT_HK_C(YELLOW_K,HK_TR," For instance:\n%s EXPECT_MOCK_CALL(%s,%s,%s,true,1){\n%s\t %s ret;\n%s \t ...do something with %s;\n%s\t return ret;\n%s }\n%s if call once and accept all args, the same args with WILL_MOCK_CALL \n",\ + HK_TR, #returntype, #namefunction,#args_prototype_with_parenthesis, HK_TR,#returntype, HK_TR, #args_call_with_parenthesis, HK_TR, HK_TR, HK_TR ); \ + /*return (returntype)0;*/ \ + INIT_MOCK_INFO_IF_NO_(tmp_mock,namefunction, PRE_ID);\ + }/* to have log */\ + if(list_mo_ ## namefunction.next ) PRINT_ERROR(" %s .next SHOULD BE NULL\n",STRFY(list_mo_ ## namefunction));\ }\ while(tmp_mock->next && (tmp_mock->info_mock)->times_left == 0) tmp_mock = tmp_mock->next ;\ ++((tmp_mock->info_mock)->call);\ if(tmp_mock->str_print_current_variables)\ append_variable_current(&((tmp_mock->info_mock)->l_current_var), tmp_mock->str_print_current_variables args_call_with_parenthesis);\ - /*LOG("condition_func:%d\n", tmp_mock->call_mock_condition args_call_with_parenthesis);*/ /*LOG("%s\n","failure condition");*/\ + else if(count_call_f == 1){\ + PRINT_HK_C(YELLOW_K,HK_TR," no printer variable defined, to define it:\n%s STR_PRINT_CUR_VAR(%s,%s,%s){\n%s\t char* ret=malloc(256);/*for instance*/;\n%s \t ... sprintf(ret,...., %s);/*for instance*/ \n%s\t return ret;\n%s }\n%s same prototype as MOCK_FUNC whithout returntype which always char* \n",\ + HK_TR, #namefunction,#args_prototype_with_parenthesis, #args_call_with_parenthesis, HK_TR, HK_TR, #args_call_with_parenthesis, HK_TR, HK_TR, HK_TR ); \ + }\ + /*LOG("condition_func:%d\n", tmp_mock->call_mock_condition args_call_with_parenthesis);*/ /*LOG("%s\n","failure condition");*/\ /*EXPECT_EQ_TYPE_INT(1, tmp_mock->call_mock_condition args_call_with_parenthesis);*/ /*LOG("%s\n","failure condition");*/\ - if ((tmp_mock->info_mock)->times_left == 0) /*no longer response, default return */ \ - return (returntype)0;/* default return */\ + /*if ((tmp_mock->info_mock)->times_left == 0)*/ /*no longer response, default return */ \ + /*return (returntype)0;*//* default return */\ if( (tmp_mock->info_mock)->str_caller == NULL){ \ - /*PRINT_HK_C(YELLOW_K,HK_TR," WARNING, the caller (TEST function which call (execute) the mock function %s may be defined in TEST function by INIT_CALLER_MOCK(%s) if need to have stats\n", #namefunction,#namefunction); \ + if(count_call_f == 1){\ + PRINT_HK_C(YELLOW_K,HK_TR," WARNING, no INIT_CALLER_MOCK; you can put it like this: \n%s TEST(nametest){\n%s\t INIT_CALLER_MOCK(%s); \n%s\t %s%s; \n%s }\n%s i.e before calling %s in this TEST, to have explicit logs\n",\ + HK_TR, HK_TR, #namefunction, HK_TR,#namefunction,#args_call_with_parenthesis, HK_TR, HK_TR, #namefunction);\ + } \ /*return (returntype)0;*/ \ }\ - else{\ + else if (((tmp_mock->info_mock)->times_left != 0) && ((tmp_mock->info_mock)->times_left != INITSTATE )) {\ size_t len0 = strlen((tmp_mock->info_mock)->str_conditions);\ size_t len1 = strlen("call check condition: aa");\ char *msg_call=malloc(len0 + len1);\ @@ -118,10 +149,10 @@ extern struct list_base_fmock *g_list_base_fmock; PRINT_LOC("Failure, arguments not expected\ncondition ( %s ) not verified\n\n", (tmp_mock->info_mock)->str_conditions);\ PRINT_HK_C(RED_K,HK_TR," 1 argument check failed from %s \n",__func__); \ }*/\ - PRINT_DEBUG(" %*c VALUES: mock function:%s, conditions:%s t_left:%ld, init_left:%ld| args:%s\n",8,'^',(tmp_mock->info_mock)->str_namefunc, (tmp_mock->info_mock)->str_conditions, (tmp_mock->info_mock)->times_left,(tmp_mock->info_mock)->init_times_left, STRFY(args_call_with_parenthesis));\ + PRINT_DEBUG(" %*c VALUES: mock function:%s, conditions:%s t_left:%ld, init_left:%ld| args:%s\n",8,'^',(tmp_mock->info_mock)->str_namefunc, (tmp_mock->info_mock)->str_conditions, (tmp_mock->info_mock)->times_left,(tmp_mock->info_mock)->init_times_left, #args_call_with_parenthesis);\ if (((tmp_mock->info_mock)->times_left <= INFINITY) || ((tmp_mock->info_mock)->times_left > 0)){\ --((tmp_mock->info_mock)->times_left);\ - PRINT_DEBUG(" %*c VALUES: mock function:%s, conditions:%s t_left:%ld, init_left:%ld| args:%s\n",8,'v',(tmp_mock->info_mock)->str_namefunc, (tmp_mock->info_mock)->str_conditions, (tmp_mock->info_mock)->times_left,(tmp_mock->info_mock)->init_times_left, STRFY(args_call_with_parenthesis));\ + PRINT_DEBUG(" %*c VALUES: mock function:%s, conditions:%s t_left:%ld, init_left:%ld| args:%s\n",8,'v',(tmp_mock->info_mock)->str_namefunc, (tmp_mock->info_mock)->str_conditions, (tmp_mock->info_mock)->times_left,(tmp_mock->info_mock)->init_times_left, #args_call_with_parenthesis);\ if(1 == tmp_mock->call_mock_condition args_call_with_parenthesis){\ return tmp_mock->run args_call_with_parenthesis;\ }\ @@ -174,14 +205,13 @@ __attribute__((constructor)) void create_str_print_variables ## namefunction(){\ char* str_print_variables ## namefunction args_prototype_with_parenthesis -#define PRE_ID ___line_ #define FILL_MOCK_INFO(tmp_new_mock, namefunction, condition_on_args_expression , repeat, f_expect_call, pre_id, id, is_init) \ (tmp_new_mock)->run = CONCAT(run_ ## namefunction ## pre_id, id);\ (tmp_new_mock)->call_mock_condition = CONCAT(namefunction ## _cond_, id);\ if(!is_init)\ - tmp_new_mock->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;\ + (tmp_new_mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;\ /*(tmp_new_mock)->info_mock = malloc(sizeof(struct func_mock_info_struct));*/\ ((tmp_new_mock)->info_mock)->expect_call = f_expect_call;\ ((tmp_new_mock)->info_mock)->call = 0;\ diff --git a/src/ftest/ftest.c b/src/ftest/ftest.c index eb82711..b43884b 100644 --- a/src/ftest/ftest.c +++ b/src/ftest/ftest.c @@ -1,61 +1,44 @@ #include "ftest/ftest.h" +#include /* -#define DEFAULT_K "\033[0m" //Resets the text to default color -#define GREEN_K "\033[0;32m" -#define RED_K "\033[0;31m" + * check if dir path is empty ! + * 1 == it is empty,0 it is not empty + */ +int dir_empty(const char *path) +{ + struct dirent *ent; + int ret = 1; -#define HK_EQ "[==========]" -#define HK_TR "[----------]" -#define HK_RN "[RUN ]" -#define HK_DN "[ DONE]" -#define HK_OK "[ OK ]" -#define HK_FL "[ FAILED ]" -#define HK_PS "[ PASSED ]" + DIR *d = opendir(path); + if (!d) { + fprintf(stderr, "%s: ", path); + perror(""); + return -1; + } -*/ + while ((ent = readdir(d))) { + if (!strcmp(ent->d_name, ".") || !(strcmp(ent->d_name, ".."))) + continue; + ret = 0; + break; + } + + closedir(d); + return ret; +} /* * by default display in millisecond */ /* -#ifndef SECOND - #define SECOND 0 -#endif -#ifndef NANOSECOND - #define NANOSECOND 0 -#endif + */ #define NANOSECOND (timeunit[0]=='n') #define SECOND (timeunit[0]=='s') -#ifndef PROGRESS - #define PROGRESS 0 -#endif - -#if 0 -#ifndef SAVE_LOG -#define SAVE_LOG 0 -#else -#ifndef ORDER_LOG - #define ORDER_LOG 1 -#endif -#endif -#endif - -#if 0 -#ifndef PARALLEL - #define PARALLEL 1 - #define LOCK(mut) - #define UNLOCK(mut) -#else /*PARALLEL defined*/ - #define LOCK(mutex_var) pthread_mutex_lock(&mutex_var); - #define UNLOCK(mutex_var) pthread_mutex_unlock(&mutex_var); -#endif -#endif /* 0 */ - #define LOCK(mutex_var) pthread_mutex_lock(&mutex_var); #define UNLOCK(mutex_var) pthread_mutex_unlock(&mutex_var); @@ -241,11 +224,11 @@ void append_failed_list(struct failed_lists **fn_failed_list ,const char *name_f */ long int id_of_thread_executed(void){ size_t id_from_self = pthread_self(); - for(size_t i=0; i, --savelog , -s=file, --savelog=file\n\t\tthis option save the global ordered result in 'file',\n\t\tthis option active the option -o or --ordered. \n\n"); + printf("\t -s , --savelog , -s=file, --savelog=file\n\t\tthis option save the global ordered result in 'file',\n\t\t \n\n"); // printf("\t -w , --width , -w=WID, --savelog=WID\n\t\tthis option change the width of the progress bar to WID, by default, WID=80,\n\t\tex: -w100, or --width=100 or -wi 100\n\n"); printf("\t -n=, ... ,--numtests=,...\n\t\tthis option allow to execute only the selected numbers of tests (in the order in file test)\n\t\tex: -n=0,6,3 8 to execute the tests 0,3,6,8 (if the number is less than the count of all tests)\n\n"); @@ -1079,36 +1066,6 @@ run_all_tests() } -#if 0 -/* - * parallel run tests / div section - */ -void execute_div_test(struct func *fun, size_t num){ - size_t cur = 0; - struct timespec start_t; - struct func *tmp = fun; - while(tmp){ - if(cur %PARALLEL == num){ - PRINT_DEBUG("thread [%ld], cur = %ld , cur mod PARA = %ld , funcname = %s \n", num,cur, cur%PARALLEL, tmp->name); - begin_execute_func(tmp->name, &start_t); - tmp->run(); - end_execute_func(tmp->name, start_t); - } - tmp = tmp->next; - ++cur; - } -} - -void* -run_all_div_tests(void *id) -{ - size_t id_th=*(size_t*)id; - struct timespec start_t; - head_run(count_tests/PARALLEL, &start_t); - execute_div_test(f_beging, id_th); - stat_end_run(count_tests/PARALLEL, start_t); -} -#endif /* * begin end parallel tests */ @@ -1243,16 +1200,24 @@ run_parallel_tests(void *id) stat_end_parallel_run(count_fail_thread[id_th]+count_pass_thread[id_th], start_t, id_th); } +/* + * + */ +void final_parallel_test_(); + /* * initialisation */ void init_parallel_test_() { - if(savelog) ordered =1; + //if(savelog) ordered =1; //progress = ordered; - + + signal(SIGSEGV, final_parallel_test_); + + is_parallel_nb = 1; f_ou_th = malloc((parallel_nb + 1) *sizeof(FILE*)); @@ -1304,6 +1269,7 @@ init_parallel_test_() void final_parallel_test_() { + free(count_pass_test); free(count_fail_test); free(count_pass_thread); @@ -1355,7 +1321,13 @@ final_parallel_test_() } } - + /* if SIGSEGV */ + if(!dir_empty("/dev/shm")){ + system("rm /dev/shm/*"); + PRINT_DEBUG(" cleanup %s \n", "/dev/shm"); + } + else + PRINT_DEBUG(" %s already empty \n", "/dev/shm"); } void run_all_tests_parallel(size_t parallel /*, int max_col*/) @@ -1425,6 +1397,10 @@ __attribute__((destructor)) void purge_tests() { + /*if(savelog){ + fclose(f_savelog); + PRINT_DEBUG("%s\n","close f_savelog done"); + }*/ struct func *tmp = f_beging; clear_all_func(&tmp); PRINT_DEBUG("%s\n","purge done"); diff --git a/src/ftest/ftest.h b/src/ftest/ftest.h index 49b96f1..c94592b 100644 --- a/src/ftest/ftest.h +++ b/src/ftest/ftest.h @@ -62,6 +62,7 @@ extern FILE **f_ou_th; extern bool unicolour; extern bool ordered; extern bool log_parallel; +extern char *savelog; extern char *colors_f[]; extern char *tab_hk_f[]; @@ -77,6 +78,8 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; #endif #endif +/* + * */ #define BUF_SIZE 256 #define INIT_STREAM_(stream,buf,len)\ @@ -103,7 +106,7 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; if (stream == NULL) { fprintf(stderr," error open stream on \'tmp\' %s:%d:%s \n",__FILE__,__LINE__,__func__); exit(0); } #define BEGIN_CPY_STREAM_MEM(stream, buf)\ - rewind(stream);\ + ;rewind(stream);\ while(fgets(buf, BUF_SIZE, stream)){ @@ -111,8 +114,34 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; }\ fclose(stream);\ free(buf);\ - remove(filename); + remove(filename);\ +#define CPY_STREAM_TO_OUT_THR(stream,msg)\ + BEGIN_CPY_STREAM_MEM (stream, msg);\ + fprintf(F_OUT, "%s",msg);\ + if(id_thread >= 0){\ + fprintf(f_ou_th[id_thread], "%s",msg);\ + }\ + END_CPY_STREAM_MEM (stream, msg);\ + + +#define INIT_STREAM_MEM_SV_(stream, buf,savelog)\ + char *buf = malloc(BUF_SIZE);\ + char *filename=malloc(strlen(directory_in_memory) + strlen("tmp_")+32) ;\ + sprintf(filename,"%s/tmp_%ld",directory_in_memory,pthread_self());\ + FILE *stream = fopen(filename,"w+");\ + if (stream == NULL) { fprintf(stderr," error open stream on \'tmp\' %s:%d:%s \n",__FILE__,__LINE__,__func__); exit(0); }\ + FILE *f_savelog = fopen(savelog,"w+");\ + if (f_savelog == NULL) { fprintf(stderr," error open f_savelog on \'tmp\' %s:%d:%s \n",__FILE__,__LINE__,__func__); exit(0); } + + + +#define _CPY_STREAM_OUT_AND_SV_ (stream, f_savelog, msg)\ + BEGIN_CPY_STREAM_MEM (stream, msg)\ + fprintf(F_OUT,"%s",msg);\ + fprintf(f_savelog,"%s",msg); \ + END_CPY_STREAM_MEM (stream, msg);\ + fclose(f_savelog); #if 1 /* @@ -120,20 +149,59 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; * I've tried open_memstream but it have some bugs. * so I use normal fopen a file a memory location '/dev/shm', it is remove after use! */ + +#define PRINTF( ...) \ + do{ \ + if(is_parallel_nb){\ + long int id_thread=id_of_thread_executed();\ + if(log_parallel){\ + INIT_STREAM_MEM (stream, msg);\ + fprintf(stream, __VA_ARGS__);\ + CPY_STREAM_TO_OUT_THR(stream, msg);\ + fflush(f_ou_th[id_thread]);\ + }\ + else{\ + if(id_thread < 0){\ + fprintf(F_OUT,__VA_ARGS__);\ + }\ + else{\ + fprintf(f_ou_th[id_thread], __VA_ARGS__);\ + fflush(f_ou_th[id_thread]);\ + }\ + }\ + } \ + else{\ + if(savelog){\ + INIT_STREAM_MEM_SV_(stream,msg,savelog);\ + fprintf(stream, __VA_ARGS__);\ + _CPY_STREAM_OUT_AND_SV_(stream, f_savelog, msg);\ + }\ + else\ + fprintf(F_OUT, __VA_ARGS__);\ + }\ + }while(0) + +#define LOG(...) PRINTF(__VA_ARGS__) + +#define PRINT_LOC(fmt, ...) \ + PRINTF( "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__) + +/* +#define PRINT_HK_C(color,hk,...)\ + PRINTF(color hk DEFAULT_K __VA_ARGS__) +*/ + + #define PRINT_HK_C(color,hk,...)\ do{ \ if(is_parallel_nb){\ - size_t id_thread=id_of_thread_executed();\ + long int id_thread=id_of_thread_executed();\ if(log_parallel){\ INIT_STREAM_MEM(stream, msg);\ if(!unicolour) fprintf(stream, color hk DEFAULT_K __VA_ARGS__); \ else fprintf(stream, hk __VA_ARGS__); \ - BEGIN_CPY_STREAM_MEM (stream, msg)\ - fprintf(F_OUT,"%s",msg);\ - if(id_thread >= 0){\ - fprintf(f_ou_th[id_thread],"%s",msg); \ - }\ - END_CPY_STREAM_MEM (stream, msg);\ + CPY_STREAM_TO_OUT_THR (stream, msg)\ + fflush(f_ou_th[id_thread]);\ }\ else{\ if(id_thread < 0){\ @@ -143,31 +211,39 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; else{\ if(!unicolour) fprintf(f_ou_th[id_thread], color hk DEFAULT_K __VA_ARGS__); \ else fprintf(f_ou_th[id_thread], hk __VA_ARGS__); \ + fflush(f_ou_th[id_thread]);\ }\ }\ } \ else{\ - if(!unicolour) fprintf(F_OUT, color hk DEFAULT_K __VA_ARGS__); \ - else fprintf(F_OUT, hk __VA_ARGS__); \ + if(savelog){\ + INIT_STREAM_MEM_SV_(stream, msg, savelog);\ + if(!unicolour) fprintf(stream, color hk DEFAULT_K __VA_ARGS__); \ + else fprintf(stream, hk __VA_ARGS__);\ + _CPY_STREAM_OUT_AND_SV_ (stream, f_savelog, msg);\ + }\ + else{\ + if(!unicolour) fprintf(F_OUT, color hk DEFAULT_K __VA_ARGS__); \ + else fprintf(F_OUT, hk __VA_ARGS__); \ + }\ }\ }while(0) - + +#else + + #define PRINT_LOC(fmt, ...) \ do{ \ /*if(ordered){*/\ if(is_parallel_nb){\ - size_t id_thread=id_of_thread_executed();\ + long int id_thread=id_of_thread_executed();\ if(log_parallel){\ INIT_STREAM_MEM (stream, msg);\ fprintf(stream, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__);\ - BEGIN_CPY_STREAM_MEM (stream, msg);\ - fprintf(F_OUT, "%s",msg);\ - if(id_thread >= 0){\ - fprintf(f_ou_th[id_thread], "%s",msg);\ - }\ - END_CPY_STREAM_MEM (stream, msg);\ + CPY_STREAM_TO_OUT_THR (stream, msg);\ + fflush(f_ou_th[id_thread]);\ }\ else{\ if(id_thread < 0){\ @@ -177,48 +253,26 @@ extern int hk_EQ, hk_TR, hk_RN, hk_DN, hk_OK, hk_FL, hk_PS, hk_SK; else{\ fprintf(f_ou_th[id_thread], "%s:%d:%s(): " fmt, __FILE__, \ __LINE__, __func__, __VA_ARGS__);\ + fflush(f_ou_th[id_thread]);\ }\ }\ } \ else{\ - fprintf(F_OUT, "%s:%d:%s(): " fmt, __FILE__, \ - __LINE__, __func__, __VA_ARGS__);\ + if(savelog){\ + INIT_STREAM_MEM_SV_(stream, msg,savelog);\ + fprintf(stream, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__);\ + _CPY_STREAM_OUT_AND_SV_(stream, f_savelog, msg);\ + }\ + else\ + fprintf(F_OUT, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__);\ }\ }while(0) -#define PRINTF( ...) \ - do{ \ - if(is_parallel_nb){\ - size_t id_thread=id_of_thread_executed();\ - if(log_parallel){\ - INIT_STREAM_MEM (stream, msg);\ - fprintf(stream, __VA_ARGS__);\ - BEGIN_CPY_STREAM_MEM (stream, msg);\ - fprintf(F_OUT,"%s",msg);\ - if(id_thread >= 0){\ - fprintf(f_ou_th[id_thread],"%s",msg);\ - }\ - END_CPY_STREAM_MEM(stream, msg);\ - }\ - else{\ - if(id_thread < 0){\ - fprintf(F_OUT,__VA_ARGS__);\ - }\ - else{\ - fprintf(f_ou_th[id_thread], __VA_ARGS__);\ - }\ - }\ - } \ - else{\ - fprintf(F_OUT, __VA_ARGS__);\ - }\ - }while(0) +#endif -#define LOG(...) PRINTF(__VA_ARGS__) +#if 0 -//#endif -#else /* below alternative of above solution, but it execute twice functions called in print functions when log_parallel == 1 */ -//#if 1 +/* below old solution, but it execute twice functions called in print functions when log_parallel == 1 */ #define PRINT_LOC(fmt, ...) \ do{ \ diff --git a/test/is_good.c b/test/is_good.c index 6a0adac..5832f4c 100644 --- a/test/is_good.c +++ b/test/is_good.c @@ -14,7 +14,7 @@ #include "permutation_t/permutation_t.h" -#if 1 +#if 0 TEST(size_permutation2){ PRINTF("another size_permutation2 again\n"); @@ -155,8 +155,6 @@ TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} -TEST(sleep){sleep(1);} - TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} @@ -167,15 +165,26 @@ TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} + +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} TEST(sleep){sleep(1);} #endif +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} +TEST(sleep){sleep(1);} + MOCK_FUNC(int, f_mock, (), ()) -EXPECT_MOCK_CALL(int,f_mock, (),1, 2) { +EXPECT_MOCK_CALL(int,f_mock, (),false, 2) { EXPECT_EQ_IN_MOCKF(21,21,f_mock); EXPECT_EQ(1,3); EXPECT_EQ(4,4); @@ -186,7 +195,7 @@ EXPECT_MOCK_CALL(int,f_mock, (),1==2||2<1, 1) {return 18;} EXPECT_MOCK_CALL(int,f_mock, (),1, INFINITY) {return -18;} TEST(mockf1){ - //INIT_CALLER_MOCK(f_mock); + INIT_CALLER_MOCK(f_mock); for(int i = 0; i<8; ++i){ @@ -223,6 +232,7 @@ EXPECT_MOCK_CALL(int, f2_mock, (int a,int b), (a==b), 1){ } + MOCK_FUNC(int, f3_mock,(int a,int b),(a,b)) EXPECT_MOCK_CALL(int, f3_mock, (int a,int b), (ab),2){ + return a*b; +} + + +TEST(f4_mock_test){ + PRINTF("f7 ret: %d\n",f7_mock(1,1)); + PRINTF("second call f7 : %d\n",f7_mock(2,0)); + +} int main(int argc, char **argv){