summaryrefslogtreecommitdiff
path: root/tests/compare_dump_files.c
blob: 6bc5145b29536f0a8b7e05e9d3af1acf00e35600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * compare_dump_files.c
 *
 *  Created on: 25 juil. 2011
 *      Author: mickael
 * BASELINE MUST BE GENERATED BY UNIX PLATFORM REGARDING TO THE CRLF PROBLEM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "opj_getopt.h"

typedef struct test_cmp_parameters
{
  /**  */
  char* base_filename;
  /**  */
  char* test_filename;
} test_cmp_parameters;

/*******************************************************************************
 * Command line help function
 *******************************************************************************/
static void compare_dump_files_help_display(void) {
  fprintf(stdout,"\nList of parameters for the compare_dump_files function  \n");
  fprintf(stdout,"\n");
  fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline dump file \n");
  fprintf(stdout,"  -t \t REQUIRED \t filename to the test dump file image\n");
  fprintf(stdout,"\n");
}
/*******************************************************************************
 * Parse command line
 *******************************************************************************/
static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
{
  int sizemembasefile, sizememtestfile;
  int index;
  const char optlist[] = "b:t:";
  int c;

  /* Init parameters */
  param->base_filename = NULL;
  param->test_filename = NULL;

  opj_opterr = 0;

  while ((c = opj_getopt(argc, argv, optlist)) != -1)
    switch (c)
      {
      case 'b':
        sizemembasefile = (int)strlen(opj_optarg)+1;
        param->base_filename = (char*) malloc(sizemembasefile);
        param->base_filename[0] = '\0';
        strncpy(param->base_filename, opj_optarg, strlen(opj_optarg));
        param->base_filename[strlen(opj_optarg)] = '\0';
        /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
        break;
      case 't':
        sizememtestfile = (int) strlen(opj_optarg) + 1;
        param->test_filename = (char*) malloc(sizememtestfile);
        param->test_filename[0] = '\0';
        strncpy(param->test_filename, opj_optarg, strlen(opj_optarg));
        param->test_filename[strlen(opj_optarg)] = '\0';
        /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
       break;
      case '?':
        if ( (opj_optopt == 'b') || (opj_optopt == 't') )
          fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
        else
          if (isprint(opj_optopt)) fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
          else fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
        return 1;
      default:
        fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
        break;
      }

  if (opj_optind != argc)
    {
    for (index = opj_optind; index < argc; index++)
      fprintf(stderr,"Non-option argument %s\n", argv[index]);
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}
/*******************************************************************************
 * MAIN
 *******************************************************************************/
int main(int argc, char **argv)
{
  test_cmp_parameters inParam;
  FILE *fbase=NULL, *ftest=NULL;
  int chbase, chtest;
  int same = 1;
  unsigned long l=1, pos;

  if( parse_cmdline_cmp(argc, argv, &inParam) == EXIT_FAILURE )
    {
    compare_dump_files_help_display();
    if (!inParam.base_filename) free(inParam.base_filename);
    if (!inParam.test_filename) free(inParam.test_filename);
    return EXIT_FAILURE;
    }

  /* Display Parameters*/
  printf("******Parameters********* \n");
  printf(" base_filename = %s\n"
          " test_filename = %s\n",
          inParam.base_filename, inParam.test_filename);
  printf("************************* \n");

  /* open base file */
  printf("Try to open: %s for reading ... ", inParam.base_filename);
  if((fbase = fopen(inParam.base_filename, "rb"))==NULL)
    {
    printf("Failed.\n");
    free(inParam.base_filename);
    free(inParam.test_filename);
    return EXIT_FAILURE;
    }
  printf("Ok.\n");

  /* open test file */
  printf("Try to open: %s for reading ... ", inParam.test_filename);
  if((ftest = fopen(inParam.test_filename, "rb"))==NULL)
    {
    printf("Failed.\n");
    fclose(fbase);
    free(inParam.base_filename);
    free(inParam.test_filename);
    return EXIT_FAILURE;
    }
  printf("Ok.\n");

  pos=ftell(fbase);

  while(!feof(fbase))
    {
    chbase = fgetc(fbase);
    if(ferror(fbase))
      {
      printf("Error reading base file.\n");
      return EXIT_FAILURE;
      }

    chtest = fgetc(ftest);
    if(ferror(ftest))
      {
      printf("Error reading test file.\n");
      return EXIT_FAILURE;
      }

    /* CRLF problem (Baseline must be always generated by unix platform)*/
    if (chbase == '\n' && chtest == '\r')
      if (fgetc(ftest) == '\n')
        chtest = '\n';

    if(chbase != chtest)
      {
      size_t nbytes = 2048;
      int CRLF_shift=1;
      char *strbase, *strtest, *strbase_d, *strtest_d;

      printf("Files differ at line %lu:\n", l);
      fseek(fbase,pos,SEEK_SET);

      /* Take into account CRLF characters when we write \n into
      // dump file when we used WIN platform*/
#ifdef _WIN32
      CRLF_shift = 2;
      fseek(ftest,pos + l - 1,SEEK_SET);
#else
      fseek(ftest,pos,SEEK_SET);
#endif

      strbase = (char *) malloc(nbytes + 1);
      strtest = (char *) malloc(nbytes + 1);

	if (fgets(strbase, nbytes, fbase) == NULL)
		fprintf(stderr,"\nWARNING: fgets return a NULL value");
	else
	{
		if (fgets(strtest, nbytes, ftest) == NULL)
			fprintf(stderr,"\nWARNING: fgets return a NULL value");
		else
		{
			strbase_d = (char *) malloc(strlen(strbase)+1);
			strtest_d = (char *) malloc(strlen(strtest)+1);
			strncpy(strbase_d, strbase, strlen(strbase)-1);
			strncpy(strtest_d, strtest, strlen(strtest)-CRLF_shift);
			strbase_d[strlen(strbase)-1] = '\0';
			strtest_d[strlen(strtest)-CRLF_shift] = '\0';
			printf("<%s> vs. <%s>\n", strbase_d, strtest_d);
			free(strbase_d);free(strtest_d);
		}
	}

	free(strbase);free(strtest);
	
	same = 0;

      break;
      }
    else
      {
      if (chbase == '\n')
        {
        l++;
        pos = ftell(fbase);
        }
      }
    }

  /*Close File*/
  fclose(fbase);
  fclose(ftest);

  /* Free memory*/
  free(inParam.base_filename);
  free(inParam.test_filename);

  if(same)
    {
      printf("\n***** TEST SUCCEED: Files are the same. *****\n");
      return EXIT_SUCCESS;
    }
  else return EXIT_FAILURE;
}