On Wed, 2012-04-18 at 15:00 +1000, Peter Hutterer wrote: > Reads in all files, writes them out to a tmp directory. Re-reads them in and > compares the new database with the old database. > > Signed-off-by: Peter Hutterer <peter.hutte...@who-t.net> > --- > libwacom/libwacom.c | 28 +++++++- > libwacom/libwacom.h | 2 +- > test/Makefile.am | 3 +- > test/dbverify.c | 188 > +++++++++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 216 insertions(+), 5 deletions(-) > create mode 100644 test/dbverify.c > > diff --git a/libwacom/libwacom.c b/libwacom/libwacom.c > index 686a23e..4d9142c 100644 > --- a/libwacom/libwacom.c > +++ b/libwacom/libwacom.c > @@ -224,14 +224,36 @@ libwacom_copy(const WacomDevice *device) > > > static int > -compare_matches(const WacomDevice *a, const WacomDevice *b) > +compare_matches(WacomDevice *a, WacomDevice *b) > { > - /* FIXME: fill this in */ > + WacomMatch **ma, **mb; > + int nmatches_a, nmatches_b; > + int i, j; > + > + ma = libwacom_get_matches(a, &nmatches_a); > + mb = libwacom_get_matches(b, &nmatches_b); > + > + if (nmatches_a != nmatches_b) > + return 1; > + > + for (i = 0; i < nmatches_a; i++) { > + int found = 0; > + WacomMatch *match_a = ma[i]; > + for (j = 0; !found && j < nmatches_b; j++) { > + WacomMatch *match_b = mb[j]; > + > + if (strcmp(match_a->match, match_b->match) == 0) > + found = 1; > + } > + if (!found) > + return 1; > + } > + > return 0; > } > > int > -libwacom_compare(const WacomDevice *a, const WacomDevice *b, > WacomCompareFlags flags) > +libwacom_compare(WacomDevice *a, WacomDevice *b, WacomCompareFlags flags)
Why do you need to do that? Seems to me that const is missing from a few function declarations then. > { > if ((a && !b) || (b && !a)) > return 1; > diff --git a/libwacom/libwacom.h b/libwacom/libwacom.h > index 806510a..f5f23b2 100644 > --- a/libwacom/libwacom.h > +++ b/libwacom/libwacom.h > @@ -303,7 +303,7 @@ void libwacom_destroy(WacomDevice *device); > * > * @return 0 if the devices are identical, nonzero otherwise > */ > -int libwacom_compare(const WacomDevice *a, const WacomDevice *b, > WacomCompareFlags flags); > +int libwacom_compare(WacomDevice *a, WacomDevice *b, WacomCompareFlags > flags); > > /** > * @param device The tablet to query > diff --git a/test/Makefile.am b/test/Makefile.am > index a402022..7ef0e52 100644 > --- a/test/Makefile.am > +++ b/test/Makefile.am > @@ -1,7 +1,8 @@ > -noinst_PROGRAMS=load > +noinst_PROGRAMS=load dbverify > > TESTS=$(noinst_PROGRAMS) > > INCLUDES=-I$(top_srcdir)/libwacom -DTOPSRCDIR="\"$(top_srcdir)\"" > > load_LDADD=$(top_builddir)/libwacom/libwacom.la > +dbverify_LDADD=$(top_builddir)/libwacom/libwacom.la > diff --git a/test/dbverify.c b/test/dbverify.c > new file mode 100644 > index 0000000..fa3c34d > --- /dev/null > +++ b/test/dbverify.c > @@ -0,0 +1,188 @@ > +/* > + * Copyright © 2012 Red Hat, Inc. > + * > + * Permission to use, copy, modify, distribute, and sell this software > + udo y and its documentation for any purpose is hereby granted without > + * fee, provided that the above copyright notice appear in all copies > + * and that both that copyright notice and this permission notice > + * appear in supporting documentation, and that the name of Red Hat > + * not be used in advertising or publicity pertaining to distribution > + * of the software without specific, written prior permission. Red > + * Hat makes no representations about the suitability of this software > + * for any purpose. It is provided "as is" without express or implied > + * warranty. > + * > + * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, > + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN > + * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR > + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS > + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, > + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN > + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. > + * > + * Authors: > + * Peter Hutterer (peter.hutte...@redhat.com) > + */ > + > +#ifdef HAVE_CONFIG_H > +#include "config.h" > +#endif > + > +#define _GNU_SOURCE > +#include <stdio.h> > +#include <stdlib.h> > +#include <string.h> > +#include <dirent.h> > +#include "libwacom.h" > +#include <assert.h> > +#include <unistd.h> > + > + > +static int > +scandir_filter(const struct dirent *entry) > +{ > + return strncmp(entry->d_name, ".", 1); > +} > + > +static void > +rmtmpdir(const char *dir) > +{ > + int nfiles; > + struct dirent **files; > + char *path = NULL; > + > + nfiles = scandir(dir, &files, scandir_filter, alphasort); > + while(nfiles--) > + { > + asprintf(&path, "%s/%s", dir, files[nfiles]->d_name); > + assert(path); > + remove(path); > + free(files[nfiles]); > + free(path); > + path = NULL; > + } > + > + free(files); > + remove(dir); > +} > + > + > +static void > +compare_databases(WacomDeviceDatabase *orig, WacomDeviceDatabase *new) > +{ > + int ndevices = 0, i; > + WacomDevice **oldall, **o; > + WacomDevice **newall, **n; > + char *old_matched; > + > + oldall = libwacom_list_devices_from_database(orig, NULL); > + newall = libwacom_list_devices_from_database(new, NULL); > + > + for (o = oldall; *o; o++) > + ndevices++; > + > + old_matched = calloc(ndevices, sizeof(char)); > + assert(old_matched); > + > + for (n = newall; *n; n++) > + { > + int found = 0; > + printf("Matching %s\n", libwacom_get_name(*n)); > + for (o = oldall, i = 0; *o && !found; o++, i++) > + /* devices with multiple matches will have multiple > + * devices in the list */ > + if (old_matched[i] == 0 && > + libwacom_compare(*n, *o, WCOMPARE_MATCHES) == 0) { > + found = 1; > + old_matched[i] = 1; > + } > + > + if (!found) > + printf("Failed to match '%s'\n", libwacom_get_name(*n)); > + assert(found); > + } > + > + for (i = 0; i < ndevices; i++) { > + if (!old_matched[i]) > + printf("No match for %s\n", > + libwacom_get_name(oldall[i])); > + assert(old_matched[i]); > + } > + > +} > + > +/* write out the current db, read it back in, compare */ > +static void > +compare_written_database(WacomDeviceDatabase *db) > +{ > + char *dirname; > + WacomDeviceDatabase *db_new; > + WacomDevice **device, **devices; > + int i; > + > + devices = libwacom_list_devices_from_database(db, NULL); > + assert(devices); > + assert(*devices); > + > + dirname = strdup("tmp.dbverify.XXXXXX"); > + dirname = mkdtemp(dirname); > + assert(dirname); > + > + for (device = devices, i = 0; *device; device++, i++) { > + int i; > + FILE *fp; > + char *path = NULL; > + int nstyli, *styli; > + > + asprintf(&path, "%s/%d-%04x-%04x.tablet", dirname, > + libwacom_get_bustype(*device), > + libwacom_get_vendor_id(*device), > + libwacom_get_product_id(*device)); > + assert(path); > + fp = fopen(path, "w"); > + assert(fp); > + libwacom_print_device_description(fp, *device); > + fclose(fp); > + free(path); > + > + styli = libwacom_get_supported_styli(*device, &nstyli); > + for (i = 0; i < nstyli; i++) { > + FILE *fp_stylus; > + const WacomStylus *stylus; > + > + asprintf(&path, "%s/%#x.stylus", dirname, styli[i]); > + stylus = libwacom_stylus_get_for_id(db, styli[i]); > + assert(stylus); > + fp_stylus = fopen(path, "w"); > + assert(fp_stylus); > + libwacom_print_stylus_description(fp_stylus, stylus); > + } > + } > + > + db_new = libwacom_database_new_for_path(dirname); > + assert(db_new); > + compare_databases(db, db_new); > + libwacom_database_destroy(db_new); > + > + rmtmpdir(dirname); > + free(dirname); > +} > + > + > +int main(int argc, char **argv) > +{ > + WacomDeviceDatabase *db; > + > + db = libwacom_database_new_for_path(TOPSRCDIR"/data"); > + if (!db) > + printf("Failed to load data from %s", TOPSRCDIR"/data"); > + assert(db); > + > + > + compare_written_database(db); > + libwacom_database_destroy (db); > + > + return 0; > +} > + > +/* vim: set noexpandtab tabstop=8 shiftwidth=8: */ ------------------------------------------------------------------------------ Better than sec? Nothing is better than sec when it comes to monitoring Big Data applications. Try Boundary one-second resolution app monitoring today. Free. http://p.sf.net/sfu/Boundary-dev2dev _______________________________________________ Linuxwacom-devel mailing list Linuxwacom-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel