diff options
author | Karel Zak | 2013-01-28 10:09:03 +0100 |
---|---|---|
committer | Karel Zak | 2013-03-11 13:00:54 +0100 |
commit | 58d62d2f7e3058b7c2ae92f98bbdc1688a2d12ed (patch) | |
tree | 6817303bcd0560d8e459097513388140fb122e4c /libfdisk | |
parent | libfdisk: (gpt) fix compiler warnings [-Wformat] (diff) | |
download | kernel-qcow2-util-linux-58d62d2f7e3058b7c2ae92f98bbdc1688a2d12ed.tar.gz kernel-qcow2-util-linux-58d62d2f7e3058b7c2ae92f98bbdc1688a2d12ed.tar.xz kernel-qcow2-util-linux-58d62d2f7e3058b7c2ae92f98bbdc1688a2d12ed.zip |
libfdisk: add stuff for unit tests
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk')
-rw-r--r-- | libfdisk/src/Makemodule.am | 15 | ||||
-rw-r--r-- | libfdisk/src/fdiskP.h | 14 | ||||
-rw-r--r-- | libfdisk/src/test.c | 59 |
3 files changed, 87 insertions, 1 deletions
diff --git a/libfdisk/src/Makemodule.am b/libfdisk/src/Makemodule.am index 20d4e4e66..eb40a3f1a 100644 --- a/libfdisk/src/Makemodule.am +++ b/libfdisk/src/Makemodule.am @@ -7,8 +7,10 @@ noinst_LTLIBRARIES += libfdisk.la libfdisk_la_SOURCES = \ libfdisk/src/libfdisk.h \ + libfdisk/src/fdiskP.h \ \ libfdisk/src/init.c \ + libfdisk/src/test.c \ libfdisk/src/alignment.c \ libfdisk/src/label.c \ libfdisk/src/utils.c \ @@ -35,5 +37,16 @@ libfdisk_la_LIBADD += libuuid.la libfdisk_la_CFLAGS += -I$(ul_libuuid_incdir) endif - libfdisk_la_DEPENDENCIES = $(libfdisk_la_LIBADD) + +libfdisk_tests_cflags = -DTEST_PROGRAM $(libfdisk_la_CFLAGS) +libfdisk_tests_ldflags = -static +libfdisk_tests_ldadd = libfdisk.la + +if BUILD_LIBBLKID +libfdisk_tests_ldflags += libblkid.la +endif + +if BUILD_LIBUUID +libfdisk_tests_ldflags += libuuid.la +endif diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index cef03fc20..3a40bb449 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -86,6 +86,19 @@ extern int fdisk_debug_mask; # define DBG_FLUSH do { ; } while(0) #endif + +#ifdef TEST_PROGRAM +struct fdisk_test { + const char *name; + int (*body)(struct fdisk_test *ts, int argc, char *argv[]); + const char *usage; +}; + +/* test.c */ +extern int fdisk_run_test(struct fdisk_test *tests, int argc, char *argv[]); +#endif + + typedef unsigned long long sector_t; /* @@ -221,6 +234,7 @@ struct fdisk_context { * FIXME: use any enum rather than hardcoded number */ }; + /* context.c */ extern int __fdisk_context_switch_label(struct fdisk_context *cxt, struct fdisk_label *lb); diff --git a/libfdisk/src/test.c b/libfdisk/src/test.c new file mode 100644 index 000000000..31ed7e03a --- /dev/null +++ b/libfdisk/src/test.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com> + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * + * Routines for TEST_PROGRAMs + */ + +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#ifndef TEST_PROGRAM +#define TEST_PROGRAM +#endif + +#include "fdiskP.h" + +int fdisk_run_test(struct fdisk_test *tests, int argc, char *argv[]) +{ + int rc = -1; + struct fdisk_test *ts; + + assert(tests); + assert(argc); + assert(argv); + + if (argc < 2 || + strcmp(argv[1], "--help") == 0 || + strcmp(argv[1], "-h") == 0) + goto usage; + + fdisk_init_debug(0); + + for (ts = tests; ts->name; ts++) { + if (strcmp(ts->name, argv[1]) == 0) { + rc = ts->body(ts, argc - 1, argv + 1); + if (rc) + printf("FAILED [rc=%d]", rc); + break; + } + } + + if (rc < 0 && ts->name == NULL) + goto usage; + + return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +usage: + printf("\nUsage:\n\t%s <test> [testoptions]\nTests:\n", + program_invocation_short_name); + for (ts = tests; ts->name; ts++) { + printf("\t%-15s", ts->name); + if (ts->usage) + printf(" %s\n", ts->usage); + } + printf("\n"); + return EXIT_FAILURE; +} |