Update test code for macro refactoring 80/222980/2 submit/tizen/20200122.230050
authorjiyong.min <jiyong.min@samsung.com>
Wed, 22 Jan 2020 01:20:21 +0000 (10:20 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Wed, 22 Jan 2020 04:31:22 +0000 (13:31 +0900)
Change-Id: I4bc0fd5b84d0f97fdf924722477bb505cd7bdeb5

tests/mm_file_test.c

index 8ca21a5..9dbc4f8 100755 (executable)
 #include "mm_file_traverse.h"
 #include "mm_file_debug.h"
 
-#define MM_TIME_CHECK_START \
-       { FILE *msg_tmp_fp = fopen("time_check.txt", "a+"); struct timeval start, finish; gettimeofday(&start, NULL);
-#define MM_TIME_CHECK_FINISH(title) \
-       gettimeofday(&finish, NULL); \
-       double end_time = (finish.tv_sec + 1e-6*finish.tv_usec); \
-       double start_time = (start.tv_sec + 1e-6*start.tv_usec); \
-       if (msg_tmp_fp != NULL) { \
-               fprintf(msg_tmp_fp, "%s\n", title); \
-               fprintf(msg_tmp_fp, " - start_time:   %3.5lf sec\n", start_time); \
-               fprintf(msg_tmp_fp, " - finish_time:  %3.5lf sec\n", end_time); \
-               fprintf(msg_tmp_fp, " - elapsed time: %3.5lf sec\n", end_time - start_time); \
-               fflush(msg_tmp_fp); fclose(msg_tmp_fp); } }
+static FILE *msg_tmp_fp = NULL;
+static struct timeval start, finish;
+#define MM_TIME_CHECK_START do { \
+                       msg_tmp_fp = fopen("time_check.txt", "a+"); \
+                       gettimeofday(&start, NULL); \
+               } while(0)
+#define MM_TIME_CHECK_FINISH(title) do { \
+                       gettimeofday(&finish, NULL); \
+                       double end_time = (finish.tv_sec + 1e-6*finish.tv_usec); \
+                       double start_time = (start.tv_sec + 1e-6*start.tv_usec); \
+                       if (msg_tmp_fp != NULL) { \
+                               fprintf(msg_tmp_fp, "%s\n", title); \
+                               fprintf(msg_tmp_fp, " - start_time:   %3.5lf sec\n", start_time); \
+                               fprintf(msg_tmp_fp, " - finish_time:  %3.5lf sec\n", end_time); \
+                               fprintf(msg_tmp_fp, " - elapsed time: %3.5lf sec\n", end_time - start_time); \
+                               fflush(msg_tmp_fp); fclose(msg_tmp_fp); \
+                       } \
+               } while(0)
+#define MM_TIME_CHECK_STOP_BY_ERR do { \
+                       if (msg_tmp_fp) { /* opened by MM_TIME_CHECK_START */ \
+                               fclose(msg_tmp_fp); \
+                               msg_tmp_fp = NULL; \
+                       } \
+               } while(0)
 
 #define SAFE_FREE(src)                 { if (src) {free(src); src = NULL; } }
 
@@ -167,43 +179,73 @@ FILE *fpFailList = NULL;
 static int mmfile_get_file_infomation(void *data, void *user_data, bool file_test);
 static int mmfile_get_video_frame(void *data, void *accurate, bool file_test);
 
-
-inline static int mm_file_is_little_endian(void)
+static bool __read_file(const char *path, unsigned char **data, unsigned int *size)
 {
-       int i = 0x00000001;
-       return ((char *)&i)[0];
+       FILE *fp = NULL;
+       unsigned char *_data = NULL;
+       long _size = 0;
+
+       if (!path || !data || !size) {
+               printf("Invalid parameter\n");
+               return false;
+       }
+
+       fp = fopen(path, "r");
+       if (!fp) {
+               printf("fopen failed (%d)\n", errno);
+               return false;
+       }
+
+       if (fseek(fp, 0, SEEK_END) < 0) {
+               printf("fseek failed\n");
+               goto ERROR;
+       }
+
+       _size = ftell(fp);
+       if (_size <= 0) {
+               printf("ftell failed\n");
+               goto ERROR;
+       }
+
+       if (fseek(fp, 0, SEEK_SET) < 0) {
+               printf("fseek failed\n");
+               goto ERROR;
+       }
+
+       _data = calloc(1, _size);
+       if (!_data) {
+               printf("calloc failed\n");
+               goto ERROR;
+       }
+       if (fread(_data, sizeof(char), _size, fp) != (size_t)_size) {
+               printf("fread error\n");
+               goto ERROR;
+       }
+
+       fclose(fp);
+       *data = _data;
+       *size = (unsigned int)_size;
+       printf("file data = %p size = %u\n", *data, *size);
+
+       return true;
+
+ERROR:
+       SAFE_FREE(_data);
+       fclose(fp);
+
+       return false;
 }
 
-#define READ_FROM_FILE(FILE_PATH, data, size) \
-       do {    \
-               FILE *fp = fopen(FILE_PATH, "r");       \
-               long lsize = 0; \
-               if (fp) {       \
-                       if (fseek(fp, 0, SEEK_END) < 0) { printf("fseek failed\n"); fclose(fp); break; }        \
-                       lsize = ftell(fp);      \
-                       if (lsize <= 0) { printf("ftell failed\n"); fclose(fp); break; }        \
-                       if (fseek(fp, 0, SEEK_SET) < 0) { printf("fseek failed\n"); fclose(fp); break; }        \
-                       data = malloc(lsize);   \
-                       if (!data) { printf("ftell failed\n"); fclose(fp); break; }     \
-                       if (fread(data, sizeof(char), lsize, fp) != (size_t)lsize) { printf("fread error\n"); SAFE_FREE(data); fclose(fp); break; }     \
-                       fclose(fp);     \
-                       size = (unsigned int)lsize; \
-                       printf("file size = %d\n", size);       \
-               }       \
-       } while (0)
-
-static int
-_is_file_exist(const char *filename)
+static bool __is_file_exist(const char *filename)
 {
-       int ret = 1;
        if (filename) {
                const char *to_access = (strstr(filename, "file://") != NULL) ? filename + 7 : filename;
-               ret = access(to_access, R_OK);
-               if (ret != 0) {
+               if (access(to_access, R_OK) < 0) {
                        printf("file [%s] not found.\n", to_access);
+                       return false;
                }
        }
-       return !ret;
+       return true;
 }
 
 
@@ -212,7 +254,7 @@ int main(int argc, char **argv)
        struct stat statbuf;
        bool file_test = true;          /*if you want to test mm_file_create_content_XXX_from_memory() set file_test to false */
 
-       if (_is_file_exist(argv[1])) {
+       if (__is_file_exist(argv[1])) {
                int ret = lstat(argv[1], &statbuf);
                if (ret < 0) {
                        printf("lstat error[%d]\n", ret);
@@ -253,7 +295,7 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes
        memset(filename, 0x00, sizeof(filename));
        SAFE_STRLCPY(filename, (char *)data, sizeof(filename));
 
-       MM_TIME_CHECK_START
+       MM_TIME_CHECK_START;
 
        printf("Extracting information for [%s] \n", filename);
        /* get track info */
@@ -271,7 +313,12 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes
                unsigned int file_size = 0;
                unsigned char *buffer = NULL;
                /* Read file */
-               READ_FROM_FILE(filename, buffer, file_size);
+               if (!__read_file(filename, &buffer, &file_size)) {
+                       printf("Failed to __read_file()\n");
+
+                       MM_TIME_CHECK_STOP_BY_ERR;
+                       return -1;
+               }
 
                ret = mm_file_create_content_attrs_from_memory(&content_attrs, buffer, file_size, MM_FILE_FORMAT_3GP);
                SAFE_FREE(buffer);
@@ -350,7 +397,12 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes
                unsigned int file_size = 0;
                unsigned char *buffer = NULL;
                /* Read file */
-               READ_FROM_FILE(filename, buffer, file_size);
+               if (!__read_file(filename, &buffer, &file_size)) {
+                       printf("Failed to __read_file()\n");
+
+                       MM_TIME_CHECK_STOP_BY_ERR;
+                       return -1;
+               }
 
                ret = mm_file_create_tag_attrs_from_memory(&tag_attrs, buffer, file_size, MM_FILE_FORMAT_3GP);
                SAFE_FREE(buffer);
@@ -422,10 +474,7 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes
                if (ret != FILEINFO_ERROR_NONE) {
                        printf("failed to get attrs\n");
 
-                       if (msg_tmp_fp) { /* opened by MM_TIME_CHECK_START */
-                               fclose(msg_tmp_fp);
-                               msg_tmp_fp = NULL;
-                       }
+                       MM_TIME_CHECK_STOP_BY_ERR;
                        mm_file_destroy_tag_attrs(tag_attrs);
                        return -1;
                }
@@ -525,10 +574,7 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes
                ret = mm_file_destroy_tag_attrs(tag_attrs);
                if (ret != FILEINFO_ERROR_NONE) {
                        printf("Error mm_file_destroy_tag_attrs: %d", ret);
-                       if (msg_tmp_fp) {
-                               fclose(msg_tmp_fp);
-                               msg_tmp_fp = NULL;
-                       }
+                       MM_TIME_CHECK_STOP_BY_ERR;
                        return -1;
                }
        } else {
@@ -574,7 +620,10 @@ static int mmfile_get_video_frame(void *data, void *accurate, bool file_test)
                unsigned int file_size = 0;
                unsigned char *buffer = NULL;
                /* Read file */
-               READ_FROM_FILE(filename, buffer, file_size);
+               if (!__read_file(filename, &buffer, &file_size)) {
+                       printf("Failed to __read_file()\n");
+                       return -1;
+               }
 
                ret = mm_file_get_video_frame_from_memory(buffer, file_size, time_stamp, is_accurate, (unsigned char **)&_frame, &_frame_size, &width, &height);
                SAFE_FREE(buffer);