summaryrefslogtreecommitdiff
path: root/tests/ddb/test-ddb.c
blob: b07caa62951f74c12490f04b32989a63239d17c3 (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
/** \file test-ddb.c
 * \author Copyright (C) 2006 Hans Ulrich Niedermann
 *
 * \note
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * \note
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details. 
 *
 * \note
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

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

#include <gphoto2/gphoto2-abilities-list.h>

#include "ddb-common.h"
#include "ddb-txt.tab.h"

static int
compare_camera_abilities(const CameraAbilities *a, 
			 const CameraAbilities *b);

CameraAbilitiesList *
read_abilities_list(const char *filename);
CameraAbilitiesList *
read_abilities_list(const char *filename)
{
  CameraAbilitiesList *al = NULL;
  int retval;
  if (0!=(retval = gp_abilities_list_new(&al))) { return NULL; }
  if (0!=(retval = yyparse(filename, al))) { goto error; }
  return al;
 error:
  if (al!=NULL)
    gp_abilities_list_free(al);
  return NULL;
}


static int
compare_lists (void)
{
  unsigned int errors = 0;
  CameraAbilitiesList *ddb = read_abilities_list(NULL);
  CameraAbilitiesList *clb;
  unsigned int i, j, n_ddb, n_clb;
  gp_abilities_list_new(&clb);
  gp_abilities_list_load(clb, NULL);
  n_ddb = gp_abilities_list_count(ddb);
  n_clb = gp_abilities_list_count(clb);
  fprintf(stderr, "## Comparing ddb with clb ##\n");
  for (i=0, j=0; (i<n_ddb) && (j<n_clb); ) {
    CameraAbilities a, b;
    gp_abilities_list_get_abilities(ddb, i, &a);
    gp_abilities_list_get_abilities(clb, j, &b);
    if ((b.port & GP_PORT_USB) && (b.usb_class == 666)) {
      /* ignore this camera */
      j++;
      continue;
    }
    if (compare_camera_abilities(&a, &b)) {
      errors++;
    }
    i++, j++;
  }
  gp_abilities_list_free(clb);
  gp_abilities_list_free(ddb);
  return (errors == 0)? 0 : 1;
}


int main(int argc, char *argv[])
{
  int i;
  int retval;
  CameraAbilitiesList *al;
#if YYDEBUG
  yydebug = 1;
#endif
  if (0!=(retval=gp_abilities_list_new(&al))) {
    fprintf(stderr, "Could not create CameraAbilitiesList\n");
    return 6;
  }
  if (argc > 1) {
    for (i=1; i<argc; i++) {
      if (0==strcmp("--compare", argv[i])) {
	return compare_lists();
      } else {
	const char *filename = argv[i];
	yyin = fopen(filename, "r");
	if (NULL == yyin) {
	  fprintf(stderr, "File %s does not exist. Aborting.\n", filename);
	  return 5;
	}
	retval = yyparse(filename, al);
	fclose(yyin);
	if (retval != 0) {
	  return retval;
	}
      }
    }
    return 0;
  } else {
    yyin = stdin;
    return yyparse("<stdin>", al);
  }
}




#define CMP_RET_S(cmp_func, field)					\
  do {									\
    int retval = cmp_func(a->field, b->field);				\
    if (retval != 0) {							\
      fprintf(stderr, "    difference in .%s: <<%s>> vs <<%s>>\n",		\
	      #field, a->field, b->field);				\
      errors++;								\
    }									\
  } while (0)

#define CMP_RET_UI(cmp_func, field)					\
  do {									\
    int retval = cmp_func((unsigned int) (a->field),			\
			  (unsigned int) (b->field));			\
    if (retval != 0) {							\
      fprintf(stderr, "    difference in .%s: 0x%x vs 0x%x\n",		\
	      #field, (unsigned int) (a->field),			\
	      (unsigned int) (b->field));				\
      errors++;								\
    }									\
  } while (0)


static int
uicmp(unsigned int a, unsigned int b)
{
  if (a < b) return -1;
  else if (a > b) return 1;
  else return 0;
}

static int
strcmp_colon(const char *a, const char *b)
{
  unsigned int i = 0;
  while (1) {
    if ((a[i] == '\0') && (b[i] == '\0'))
      return 0;
    if (a[i] == '\0')
      return -1;
    if (b[i] == '\0')
      return 1;
    if (a[i] == b[i])
      goto next;
    if (((a[i] == ' ') && (b[i] == ':')) ||
	((a[i] == ':') && (a[i] == ' ')))
      goto next;
    if (a[i] < b[i])
      return -1;
    if (a[i] > b[i])
      return 1;
    fprintf(stderr, "Internal error in strcmp_colon\n");
    exit(13);
  next:
    ++i;
  }
}


static int
compare_camera_abilities(const CameraAbilities *a, 
			 const CameraAbilities *b)
{
  unsigned int errors = 0;
  int i;
  CMP_RET_S(strcmp_colon, model);
  CMP_RET_S(strcmp, library);
  /* CMP_RET_S(strcmp, id); */
  CMP_RET_UI(uicmp, port);
  if ((a->port & GP_PORT_SERIAL)) {
    for (i=0; (a->speed[i] != 0) && (a->speed[i] != 0); i++) {
      CMP_RET_UI(uicmp, speed[i]);
    }
  }
  CMP_RET_UI(uicmp, operations);
  CMP_RET_UI(uicmp, file_operations);
  CMP_RET_UI(uicmp, folder_operations);
  if ((a->port & GP_PORT_USB)) {
    CMP_RET_UI(uicmp, usb_vendor);
    CMP_RET_UI(uicmp, usb_product);
    CMP_RET_UI(uicmp, usb_class);
    if (a->usb_class != 0) {
      CMP_RET_UI(uicmp, usb_subclass);
      CMP_RET_UI(uicmp, usb_protocol);
    }
  }
  CMP_RET_UI(uicmp, device_type);
  if (errors == 0) {
    return 0;
  } else {
    fprintf(stderr, "Difference for <<%s>>\n", a->model);
    return 1;
  }
}