enhance the encoding test suite and remove unsued CONFIGURE_FILE into a CMakeList
[openjpeg.git] / tests / compare_dump_files.c
1 /*
2  * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * compare_dump_files.c
29  *
30  *  Created on: 25 juil. 2011
31  *      Author: mickael
32  * BASELINE MUST BE GENERATED BY UNIX PLATFORM REGARDING TO THE CRLF PROBLEM
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include "getopt.h"
41
42 typedef struct test_cmp_parameters
43 {
44   /**  */
45   char* base_filename;
46   /**  */
47   char* test_filename;
48 } test_cmp_parameters;
49
50 /*******************************************************************************
51  * Command line help function
52  *******************************************************************************/
53 void compare_dump_files_help_display(void) {
54   fprintf(stdout,"\nList of parameters for the compare_dump_files function  \n");
55   fprintf(stdout,"\n");
56   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline dump file \n");
57   fprintf(stdout,"  -t \t REQUIRED \t filename to the test dump file image\n");
58   fprintf(stdout,"\n");
59 }
60 /*******************************************************************************
61  * Parse command line
62  *******************************************************************************/
63 int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
64 {
65   int sizemembasefile, sizememtestfile;
66   int index;
67   const char optlist[] = "b:t:";
68   int c;
69
70   // Init parameters
71   param->base_filename = NULL;
72   param->test_filename = NULL;
73
74   opterr = 0;
75
76   while ((c = getopt(argc, argv, optlist)) != -1)
77     switch (c)
78       {
79       case 'b':
80         sizemembasefile = (int)strlen(optarg)+1;
81         param->base_filename = (char*) malloc(sizemembasefile);
82         param->base_filename[0] = '\0';
83         strncpy(param->base_filename, optarg, strlen(optarg));
84         param->base_filename[strlen(optarg)] = '\0';
85         //printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );
86         break;
87       case 't':
88         sizememtestfile = (int) strlen(optarg) + 1;
89         param->test_filename = (char*) malloc(sizememtestfile);
90         param->test_filename[0] = '\0';
91         strncpy(param->test_filename, optarg, strlen(optarg));
92         param->test_filename[strlen(optarg)] = '\0';
93         //printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);
94        break;
95       case '?':
96         if ( (optopt == 'b') || (optopt == 't') )
97           fprintf(stderr, "Option -%c requires an argument.\n", optopt);
98         else
99           if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt);
100           else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
101         return 1;
102       default:
103         fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, optarg);
104         break;
105       }
106
107   if (optind != argc)
108     {
109     for (index = optind; index < argc; index++)
110       fprintf(stderr,"Non-option argument %s\n", argv[index]);
111     return EXIT_FAILURE;
112     }
113
114   return EXIT_SUCCESS;
115 }
116 /*******************************************************************************
117  * MAIN
118  *******************************************************************************/
119 int main(int argc, char **argv)
120 {
121   test_cmp_parameters inParam;
122   FILE *fbase=NULL, *ftest=NULL;
123   int chbase, chtest;
124   int same = 1;
125   unsigned long l=1, pos;
126
127   if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
128     {
129     compare_dump_files_help_display();
130     if (!inParam.base_filename) free(inParam.base_filename);
131     if (!inParam.test_filename) free(inParam.test_filename);
132     return EXIT_FAILURE;
133     }
134
135   // Display Parameters
136   printf("******Parameters********* \n");
137   printf(" base_filename = %s\n"
138           " test_filename = %s\n",
139           inParam.base_filename, inParam.test_filename);
140   printf("************************* \n");
141
142   /* open base file */
143   printf("Try to open: %s for reading ... ", inParam.base_filename);
144   if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
145     {
146     printf("Failed.\n");
147     free(inParam.base_filename);
148     free(inParam.test_filename);
149     return EXIT_FAILURE;
150     }
151   printf("Ok.\n");
152
153   /* open test file */
154   printf("Try to open: %s for reading ... ", inParam.test_filename);
155   if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
156     {
157     printf("Failed.\n");
158     fclose(fbase);
159     free(inParam.base_filename);
160     free(inParam.test_filename);
161     return EXIT_FAILURE;
162     }
163   printf("Ok.\n");
164
165   pos=ftell(fbase);
166
167   while(!feof(fbase))
168     {
169     chbase = fgetc(fbase);
170     if(ferror(fbase))
171       {
172       printf("Error reading base file.\n");
173       return EXIT_FAILURE;
174       }
175
176     chtest = fgetc(ftest);
177     if(ferror(ftest))
178       {
179       printf("Error reading test file.\n");
180       return EXIT_FAILURE;
181       }
182
183     // CRLF problem (Baseline must be always generated by unix platform)
184     if (chbase == '\n' && chtest == '\r')
185       if (fgetc(ftest) == '\n')
186         chtest = '\n';
187
188     if(chbase != chtest)
189       {
190       size_t nbytes = 2048;
191       int CRLF_shift=1;
192       char *strbase, *strtest, *strbase_d, *strtest_d;
193       char *return_value_fgets;
194
195       printf("Files differ at line %lu:\n", l);
196       fseek(fbase,pos,SEEK_SET);
197
198       // Take into account CRLF characters when we write \n into
199       // dump file when we used WIN platform
200 #ifdef _WIN32
201       CRLF_shift = 2;
202       fseek(ftest,pos + l - 1,SEEK_SET);
203 #else
204       fseek(ftest,pos,SEEK_SET);
205 #endif
206
207       strbase = (char *) malloc(nbytes + 1);
208       strtest = (char *) malloc(nbytes + 1);
209
210       return_value_fgets = fgets(strbase, nbytes, fbase);
211       if (!strcmp(return_value_fgets,strbase))
212         fprintf(stderr,"\nWARNING: fgets return a value different that the first argument");
213       free(return_value_fgets);
214
215       return_value_fgets = fgets(strtest, nbytes, ftest);
216       if (!strcmp(return_value_fgets,strtest))
217           fprintf(stderr,"\nWARNING: fgets return a value different that the first argument");
218       free(return_value_fgets);
219
220       strbase_d = (char *) malloc(strlen(strbase)+1);
221       strtest_d = (char *) malloc(strlen(strtest)+1);
222       strncpy(strbase_d, strbase, strlen(strbase)-1);
223       strncpy(strtest_d, strtest, strlen(strtest)-CRLF_shift);
224       strbase_d[strlen(strbase)-1] = '\0';
225       strtest_d[strlen(strtest)-CRLF_shift] = '\0';
226       printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
227
228       free(strbase);free(strtest);
229       free(strbase_d);free(strtest_d);
230       same = 0;
231       break;
232       }
233     else
234       {
235       if (chbase == '\n')
236         {
237         l++;
238         pos = ftell(fbase);
239         }
240       }
241     }
242
243   //Close File
244   fclose(fbase);
245   fclose(ftest);
246
247   // Free memory
248   free(inParam.base_filename);
249   free(inParam.test_filename);
250
251   if(same)
252     {
253       printf("\n***** TEST SUCCEED: Files are the same. *****\n");
254       return EXIT_SUCCESS;
255     }
256   else return EXIT_FAILURE;
257 }