From e62eefb9c6543221405017134c00c97e4995db98 Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Sun, 24 Feb 2008 00:55:49 +0000 Subject: [PATCH] Add the beginnings of a test suite, based on test.c. --- Makefile.am | 2 +- configure.ac | 2 +- tests/.cvsignore | 6 ++++++ tests/Makefile.am | 12 +++++++++++ tests/gedcom.magic | 6 ++++++ tests/gedcom.result | 1 + tests/gedcom.testfile | 8 ++++++++ {src => tests}/test.c | 48 ++++++++++++++++++++++++------------------- 8 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 tests/.cvsignore create mode 100644 tests/Makefile.am create mode 100644 tests/gedcom.magic create mode 100644 tests/gedcom.result create mode 100644 tests/gedcom.testfile rename {src => tests}/test.c (69%) diff --git a/Makefile.am b/Makefile.am index f003f4b5..0300b9f4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ EXTRA_DIST = MAINT -SUBDIRS = src magic doc python +SUBDIRS = src magic tests doc python diff --git a/configure.ac b/configure.ac index 8ad8dc3e..84d998b6 100644 --- a/configure.ac +++ b/configure.ac @@ -118,5 +118,5 @@ AC_CHECK_LIB(z,gzopen) dnl See if we are cross-compiling AM_CONDITIONAL(IS_CROSS_COMPILE, test "$cross_compiling" = yes) -AC_CONFIG_FILES([Makefile src/Makefile magic/Makefile doc/Makefile python/Makefile]) +AC_CONFIG_FILES([Makefile src/Makefile magic/Makefile tests/Makefile doc/Makefile python/Makefile]) AC_OUTPUT diff --git a/tests/.cvsignore b/tests/.cvsignore new file mode 100644 index 00000000..0c9ae993 --- /dev/null +++ b/tests/.cvsignore @@ -0,0 +1,6 @@ +.deps +.libs +Makefile.in +Makefile +test + diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 00000000..3880de81 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,12 @@ +# FIXME: Actually run a test on a magic file +check_PROGRAMS = test +test_LDADD = $(top_builddir)/src/libmagic.la + +check-local: + MAGIC=$(top_builddir)/magic/magic ./test + for i in $(TEST_BASES); do MAGIC=./$$i.magic ./test ./$$i.testfile ./$$i.result; done + +TEST_BASES = gedcom + +EXTRA_DIST = \ + gedcom.magic gedcom.testfile gedcom.result diff --git a/tests/gedcom.magic b/tests/gedcom.magic new file mode 100644 index 00000000..4cfcde9d --- /dev/null +++ b/tests/gedcom.magic @@ -0,0 +1,6 @@ +# GEDCOM Geneaolgy file + +0 string/c 0\ HEAD GEDCOM genealogy data +>&0 search 1\ GEDC +>>&0 search 2\ VERS version +>>>&1 string >\0 %s diff --git a/tests/gedcom.result b/tests/gedcom.result new file mode 100644 index 00000000..e121a4ea --- /dev/null +++ b/tests/gedcom.result @@ -0,0 +1 @@ +GEDCOM genealogy data version 5.5 \ No newline at end of file diff --git a/tests/gedcom.testfile b/tests/gedcom.testfile new file mode 100644 index 00000000..3d9607e9 --- /dev/null +++ b/tests/gedcom.testfile @@ -0,0 +1,8 @@ +0 HEAD +1 SOUR GENJ +2 VERS 2.x +1 GEDC +2 VERS 5.5 +2 FORM Lineage-Linked +1 CHAR UNICODE +1 LANG Italian diff --git a/src/test.c b/tests/test.c similarity index 69% rename from src/test.c rename to tests/test.c index 94e483bf..1786bbc0 100644 --- a/src/test.c +++ b/tests/test.c @@ -30,28 +30,34 @@ int main(int argc, char **argv) { - struct magic_set *ms; - const char *m; - int i; + struct magic_set *ms; + const char *m; + int i; - ms = magic_open(MAGIC_NONE); - if (ms == NULL) { - (void) printf("ERROR opening MAGIC_NONE: out of memory\n"); - return 1; - } - if (magic_load(ms, NULL) == -1) { - (void) printf("ERROR loading with NULL file: %s\n", magic_error(ms)); - return 2; - } + ms = magic_open(MAGIC_NONE); + if (ms == NULL) { + fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n"); + return 1; + } + if (magic_load(ms, NULL) == -1) { + fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms)); + return 2; + } - for (i = 1; i < argc; i++) { - if ((m = magic_file(ms, argv[i])) == NULL) { - (void) printf("ERROR loading file %s: %s\n", argv[i], magic_error(ms)); - return 3; - } else - (void) printf("%s: %s\n", argv[i], m); - } + if (argc > 1) { + if (argc != 3) { + fprintf(stderr, "Usage: test TEST-FILE RESULT\n"); + } else { + if ((m = magic_file(ms, argv[1])) == NULL) { + fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms)); + return 3; + } else { + printf("%s: %s\n", argv[1], m); + /* Compare m with contents of argv[3] */ + } + } + } - magic_close(ms); - return 0; + magic_close(ms); + return 0; } -- 2.40.0