diff options
author | Hans Ulrich Niedermann <gp@n-dimensional.de> | 2006-09-19 15:36:46 +0000 |
---|---|---|
committer | Hans Ulrich Niedermann <gp@n-dimensional.de> | 2006-09-19 15:36:46 +0000 |
commit | 8cc785fd7eccd2a3e779794de9fcf8558d698dcc (patch) | |
tree | 9100b0ee4bcf76efb20f0f20e0525a4cc4eed7fe /tests | |
parent | 0f8b1a3e047d37c35433effa05702aa2dcab5747 (diff) | |
download | libgphoto2-8cc785fd7eccd2a3e779794de9fcf8558d698dcc.tar.gz |
split ddb parser from test program
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@9191 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ddb/Makefile.am | 2 | ||||
-rw-r--r-- | tests/ddb/ddb-common.h | 3 | ||||
-rw-r--r-- | tests/ddb/ddb-txt.y | 228 | ||||
-rw-r--r-- | tests/ddb/test-ddb.c | 197 |
4 files changed, 225 insertions, 205 deletions
diff --git a/tests/ddb/Makefile.am b/tests/ddb/Makefile.am index aaacc861b..6bbdf3766 100644 --- a/tests/ddb/Makefile.am +++ b/tests/ddb/Makefile.am @@ -45,7 +45,7 @@ BUILT_SOURCES = $(flexbison_BUILT_SOURCES_) CLEANFILES = $(check_SCRIPTS) $(BUILT_SOURCES) $(flexbison_CLEANFILES_) gp2ddb.txt #if HAVE_FLEX_BISON -test_ddb_SOURCES = $(flexbison_BUILT_SOURCES_) ddb-common.c ddb-common.h +test_ddb_SOURCES = $(flexbison_BUILT_SOURCES_) test-ddb.c ddb-common.c ddb-common.h test_ddb_LDADD = \ -lfl \ $(top_builddir)/libgphoto2/libgphoto2.la \ diff --git a/tests/ddb/ddb-common.h b/tests/ddb/ddb-common.h index 0059bac93..8fcce3af8 100644 --- a/tests/ddb/ddb-common.h +++ b/tests/ddb/ddb-common.h @@ -3,6 +3,7 @@ #define _GPHOTO2_INTERNAL_CODE +#include <stdio.h> #include <gphoto2/gphoto2-abilities-list.h> typedef union { @@ -58,6 +59,8 @@ YY_DECL; // #define YYDEBUG 1 +extern FILE *yyin; + void lexer_reset(const char *_filename); #endif /* !__DDB_TXT_H__ */ diff --git a/tests/ddb/ddb-txt.y b/tests/ddb/ddb-txt.y index 9e597e96d..d79a23388 100644 --- a/tests/ddb/ddb-txt.y +++ b/tests/ddb/ddb-txt.y @@ -10,13 +10,14 @@ #include "ddb-txt.tab.h" +extern char *yytext; + + char *current_driver = NULL; char *current_device_name = NULL; CameraAbilities ca; unsigned int speed_index = 0; -extern FILE *yyin; - char *alloc_parse_string(char *s); char *error_string = NULL; @@ -343,58 +344,51 @@ interface_usb: ; interface_usb_internal: - | interface_usb_vendprod - | interface_usb_class_stuff - | interface_usb_vendprod - interface_usb_class_stuff + interface_usb_vendprod_optional + interface_usb_class_optional ; -interface_usb_vendprod: - interface_usb_vendor TOK_SEP - interface_usb_product TOK_SEP +interface_usb_vendprod_optional: + /* empty */ + | interface_usb_vendor + interface_usb_product ; interface_usb_vendor: - TOK_VENDOR TOK_NUMBER + TOK_VENDOR TOK_NUMBER TOK_SEP { ca.usb_vendor = $<ui_val>2; } ; interface_usb_product: - TOK_PRODUCT TOK_NUMBER + TOK_PRODUCT TOK_NUMBER TOK_SEP { ca.usb_product = $<ui_val>2; } ; -interface_usb_class_stuff: - | interface_usb_class TOK_SEP - | interface_usb_class TOK_SEP - interface_usb_subclass TOK_SEP - | interface_usb_class TOK_SEP - interface_usb_subclass TOK_SEP - interface_usb_protocol TOK_SEP - | interface_usb_class TOK_SEP - interface_usb_protocol TOK_SEP - ; - -interface_usb_class: - TOK_CLASS TOK_NUMBER +interface_usb_class_optional: + /* empty */ + | TOK_CLASS TOK_NUMBER TOK_SEP { ca.usb_class = $<ui_val>2; } + interface_usb_subclass_optional ; -interface_usb_subclass: - TOK_SUBCLASS TOK_NUMBER +interface_usb_subclass_optional: + /* empty */ + | TOK_SUBCLASS TOK_NUMBER TOK_SEP { ca.usb_subclass = $<ui_val>2; } + interface_usb_protocol_optional ; -interface_usb_protocol: - TOK_PROTOCOL TOK_NUMBER +interface_usb_protocol_optional: + /* empty */ + | TOK_PROTOCOL TOK_NUMBER TOK_SEP { ca.usb_protocol = $<ui_val>2; } @@ -430,6 +424,8 @@ void yyerror(const char *filename, { fprintf(stderr, "%s:%d: parse error: %s\n", filename, yylineno, str); + fprintf(stderr, "%s:%d- near: `%s'\n", + filename, yylineno, yytext); if (NULL != error_string) { fprintf(stderr, "%s:%d- <<%s>>\n", filename, yylineno, error_string); @@ -438,86 +434,6 @@ void yyerror(const char *filename, exit(4); } -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, 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; (i<n_ddb) && (i<n_clb); i++) { - CameraAbilities a, b; - gp_abilities_list_get_abilities(ddb, i, &a); - gp_abilities_list_get_abilities(clb, i, &b); - if (compare_camera_abilities(&a, &b)) { - errors++; - } - } - 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); - } -} - static void reset_ca(void) @@ -528,99 +444,3 @@ reset_ca(void) ca.usb_subclass = -1; ca.usb_protocol = -1; } - - -#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; - } -} diff --git a/tests/ddb/test-ddb.c b/tests/ddb/test-ddb.c new file mode 100644 index 000000000..b1297d4bc --- /dev/null +++ b/tests/ddb/test-ddb.c @@ -0,0 +1,197 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define _GPHOTO2_INTERNAL_CODE +#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; + } +} |