added non-regression tests for j2k_dump function and conformance data
[openjpeg.git] / tests / compare_dump_files.c
1 /*
2  * compare_dump_files.c
3  *
4  *  Created on: 25 juil. 2011
5  *      Author: mickael
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "getopt.h"
13
14 typedef struct test_cmp_parameters
15 {
16   /**  */
17   char* base_filename;
18   /**  */
19   char* test_filename;
20 } test_cmp_parameters;
21
22 /*******************************************************************************
23  * Command line help function
24  *******************************************************************************/
25 void compare_dump_files_help_display() {
26   fprintf(stdout,"\nList of parameters for the compare_dump_files function  \n");
27   fprintf(stdout,"\n");
28   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline dump file \n");
29   fprintf(stdout,"  -t \t REQUIRED \t filename to the test dump file image\n");
30   fprintf(stdout,"\n");
31 }
32 /*******************************************************************************
33  * Parse command line
34  *******************************************************************************/
35 int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
36 {
37   int sizemembasefile, sizememtestfile;
38   int index;
39   const char optlist[] = "b:t:";
40   int c;
41
42   // Init parameters
43   param->base_filename = NULL;
44   param->test_filename = NULL;
45
46   opterr = 0;
47
48   while ((c = getopt(argc, argv, optlist)) != -1)
49     switch (c)
50       {
51       case 'b':
52         sizemembasefile = (int)strlen(optarg)+1;
53         param->base_filename = (char*) malloc(sizemembasefile);
54         param->base_filename[0] = '\0';
55         strncpy(param->base_filename, optarg, strlen(optarg));
56         param->base_filename[strlen(optarg)] = '\0';
57         //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
58         break;
59       case 't':
60         sizememtestfile = (int) strlen(optarg) + 1;
61         param->test_filename = (char*) malloc(sizememtestfile);
62         param->test_filename[0] = '\0';
63         strncpy(param->test_filename, optarg, strlen(optarg));
64         param->test_filename[strlen(optarg)] = '\0';
65         //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
66        break;
67       case '?':
68         if ( (optopt == 'b') || (optopt == 't') )
69           fprintf(stderr, "Option -%c requires an argument.\n", optopt);
70         else
71           if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt);
72           else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
73         return 1;
74       default:
75         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, optarg);
76         break;
77       }
78
79   if (optind != argc)
80     {
81     for (index = optind; index < argc; index++)
82       fprintf(stderr,"Non-option argument %s\n", argv[index]);
83     return EXIT_FAILURE;
84     }
85
86   return EXIT_SUCCESS;
87 }
88 /*******************************************************************************
89  * MAIN
90  *******************************************************************************/
91 int main(int argc, char **argv)
92 {
93   test_cmp_parameters inParam;
94   FILE *fbase=NULL, *ftest=NULL;
95   char chbase, chtest;
96   int same = 1;
97   unsigned long l=1, pos;
98
99   if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
100     {
101     compare_dump_files_help_display();
102     if (!inParam.base_filename) free(inParam.base_filename);
103     if (!inParam.test_filename) free(inParam.test_filename);
104     return EXIT_FAILURE;
105     }
106
107   // Display Parameters
108   printf("******Parameters********* \n");
109   printf(" base_filename = %s\n"
110           " test_filename = %s\n",
111           inParam.base_filename, inParam.test_filename);
112   printf("************************* \n");
113
114   /* open base file */
115   printf("Try to open: %s for reading ... ", inParam.base_filename);
116   if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
117     {
118     printf("Failed.\n");
119     free(inParam.base_filename);
120     free(inParam.test_filename);
121     return EXIT_FAILURE;
122     }
123   printf("Ok.\n");
124
125   /* open test file */
126   printf("Try to open: %s for reading ... ", inParam.test_filename);
127   if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
128     {
129     printf("Failed.\n");
130     fclose(fbase);
131     free(inParam.base_filename);
132     free(inParam.test_filename);
133     return EXIT_FAILURE;
134     }
135   printf("Ok.\n");
136
137   pos=ftell(fbase);
138
139   while(!feof(fbase))
140     {
141     chbase = fgetc(fbase);
142     if(ferror(fbase))
143       {
144       printf("Error reading base file.\n");
145       return EXIT_FAILURE;
146       }
147
148     chtest = fgetc(ftest);
149     if(ferror(ftest))
150       {
151       printf("Error reading test file.\n");
152       return EXIT_FAILURE;
153       }
154
155     if(chbase != chtest)
156       {
157       size_t nbytes = 2048;
158       char* strbase, *strtest;
159       int nbytes_read_base, nbytes_read_test;
160
161       printf("Files differ at line %lu:\n", l);
162       fseek(fbase,pos,SEEK_SET);
163       fseek(ftest,pos,SEEK_SET);
164
165       strbase = (char *) malloc(nbytes + 1);
166       strtest = (char *) malloc(nbytes + 1);
167       nbytes_read_base = getline(&strbase, &nbytes, fbase);
168       nbytes_read_test = getline(&strtest, &nbytes, ftest);
169       strbase[nbytes_read_base-1] = '\0';
170       strtest[nbytes_read_test-1] = '\0';
171       printf("<%s> vs. <%s>\n", strbase, strtest);
172
173       free(strbase);
174       free(strtest);
175       same = 0;
176       break;
177       }
178     else
179       {
180       if (chbase == '\n')
181         {
182         l++;
183         pos = ftell(fbase);
184         }
185       }
186     }
187
188   //Close File
189   fclose(fbase);
190   fclose(ftest);
191
192   // Free memory
193   free(inParam.base_filename);
194   free(inParam.test_filename);
195
196   if(same)
197     {
198       printf("\n***** TEST SUCCEED: Files are the same. *****\n");
199       return EXIT_SUCCESS;
200     }
201   else return EXIT_FAILURE;
202 }