Merge pull request #1164 from sebras/master
[openjpeg.git] / tests / compare_dump_files.c
1 /*
2  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
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  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <assert.h>
39
40 #include "opj_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 static 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 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
64 {
65   size_t 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   opj_opterr = 0;
75
76   while ((c = opj_getopt(argc, argv, optlist)) != -1)
77     switch (c)
78       {
79     case 'b':
80       sizemembasefile = strlen(opj_optarg) + 1;
81       param->base_filename = (char*) malloc(sizemembasefile);
82       strcpy(param->base_filename, opj_optarg);
83       /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
84       break;
85     case 't':
86       sizememtestfile = strlen(opj_optarg) + 1;
87       param->test_filename = (char*) malloc(sizememtestfile);
88       strcpy(param->test_filename, opj_optarg);
89       /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
90       break;
91     case '?':
92       if ( (opj_optopt == 'b') || (opj_optopt == 't') )
93         fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
94       else
95         if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
96         else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
97       return 1;
98     default:
99       fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
100       break;
101       }
102
103   if (opj_optind != argc)
104     {
105     for (index = opj_optind; index < argc; index++)
106       fprintf(stderr,"Non-option argument %s\n", argv[index]);
107     return 1;
108     }
109
110   return 0;
111 }
112
113 /*******************************************************************************
114  * MAIN
115  *******************************************************************************/
116 int main(int argc, char **argv)
117 {
118   test_cmp_parameters inParam;
119   FILE *fbase=NULL, *ftest=NULL;
120   int same = 0;
121   char lbase[512];
122   char strbase[512];
123   char ltest[512];
124   char strtest[512];
125
126   if( parse_cmdline_cmp(argc, argv, &inParam) == 1 )
127     {
128     compare_dump_files_help_display();
129     goto cleanup;
130     }
131
132   /* Display Parameters*/
133   printf("******Parameters********* \n");
134   printf(" base_filename = %s\n"
135     " test_filename = %s\n",
136     inParam.base_filename, inParam.test_filename);
137   printf("************************* \n");
138
139   /* open base file */
140   printf("Try to open: %s for reading ... ", inParam.base_filename);
141   if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
142     {
143     goto cleanup;
144     }
145   printf("Ok.\n");
146
147   /* open test file */
148   printf("Try to open: %s for reading ... ", inParam.test_filename);
149   if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
150     {
151     goto cleanup;
152     }
153   printf("Ok.\n");
154
155   while (fgets(lbase, sizeof(lbase), fbase) && fgets(ltest,sizeof(ltest),ftest))
156     {
157     int nbase = sscanf(lbase, "%511[^\r\n]", strbase);
158     int ntest = sscanf(ltest, "%511[^\r\n]", strtest);
159     assert( nbase != 511 && ntest != 511 );
160     if( nbase != 1 || ntest != 1 )
161       {
162       fprintf(stderr, "could not parse line from files\n" );
163       goto cleanup;
164       }
165     if( strcmp( strbase, strtest ) != 0 )
166       {
167       fprintf(stderr,"<%s> vs. <%s>\n", strbase, strtest);
168       goto cleanup;
169       }
170     }
171
172   same = 1;
173   printf("\n***** TEST SUCCEED: Files are the same. *****\n");
174 cleanup:
175   /*Close File*/
176   if(fbase) fclose(fbase);
177   if(ftest) fclose(ftest);
178
179   /* Free memory*/
180   free(inParam.base_filename);
181   free(inParam.test_filename);
182
183   return same ? EXIT_SUCCESS : EXIT_FAILURE;
184 }