utils: Add regression test for C utility functions.

This commit is contained in:
Richard W.M. Jones
2014-01-18 15:56:25 +00:00
parent 89a617c07d
commit 815e739d12
4 changed files with 180 additions and 0 deletions

1
.gitignore vendored
View File

@@ -421,6 +421,7 @@ Makefile.in
/src/structs-compare.c
/src/structs-copy.c
/src/structs-free.c
/src/test-utils
/stamp-guestfs-release-notes.pod
/stamp-h1
/sysprep/.depend

View File

@@ -305,6 +305,7 @@ src/structs-cleanup.c
src/structs-compare.c
src/structs-copy.c
src/structs-free.c
src/test-utils.c
src/tmpdirs.c
src/utils.c
test-tool/test-tool.c

View File

@@ -226,6 +226,28 @@ libutils_la_SOURCES = \
libutils_la_CPPFLAGS = $(libguestfs_la_CPPFLAGS)
libutils_la_CFLAGS = $(libguestfs_la_CFLAGS)
# Tests: main tests are in tests/c-api. Here we just have some
# internal tests of utility functions.
TESTS_ENVIRONMENT = $(top_builddir)/run --test $(VG)
TESTS = test-utils
check_PROGRAMS = test-utils
test_utils_SOURCES = test-utils.c
test_utils_CPPFLAGS = \
-I$(top_srcdir)/gnulib/lib -I$(top_builddir)/gnulib/lib \
-I$(top_srcdir)/src -I.
test_utils_CFLAGS = \
$(WARN_CFLAGS) $(WERROR_CFLAGS) \
$(GPROF_CFLAGS) $(GCOV_CFLAGS)
test_utils_LDADD = \
libutils.la \
$(top_builddir)/gnulib/lib/libgnu.la
check-valgrind:
$(MAKE) VG="@VG@" check
# Pkgconfig.
pkgconfigdir = $(libdir)/pkgconfig

156
src/test-utils.c Normal file
View File

@@ -0,0 +1,156 @@
/* libguestfs
* Copyright (C) 2014 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "guestfs.h"
#include "guestfs-internal-frontend.h"
/* Test guestfs___split_string. */
static void
test_split (void)
{
char **ret;
ret = guestfs___split_string (':', "");
assert (ret);
assert (guestfs___count_strings (ret) == 0);
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', "a");
assert (ret);
assert (guestfs___count_strings (ret) == 1);
assert (STREQ (ret[0], "a"));
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', ":");
assert (ret);
assert (guestfs___count_strings (ret) == 2);
assert (STREQ (ret[0], ""));
assert (STREQ (ret[1], ""));
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', "::");
assert (ret);
assert (guestfs___count_strings (ret) == 3);
assert (STREQ (ret[0], ""));
assert (STREQ (ret[1], ""));
assert (STREQ (ret[2], ""));
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', ":a");
assert (ret);
assert (guestfs___count_strings (ret) == 2);
assert (STREQ (ret[0], ""));
assert (STREQ (ret[1], "a"));
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', "a:");
assert (ret);
assert (guestfs___count_strings (ret) == 2);
assert (STREQ (ret[0], "a"));
assert (STREQ (ret[1], ""));
guestfs___free_string_list (ret);
ret = guestfs___split_string (':', "a:b:c");
assert (ret);
assert (guestfs___count_strings (ret) == 3);
assert (STREQ (ret[0], "a"));
assert (STREQ (ret[1], "b"));
assert (STREQ (ret[2], "c"));
guestfs___free_string_list (ret);
}
/* Test guestfs___concat_strings. */
static void
test_concat (void)
{
char *ret;
const char *test1[] = { NULL };
const char *test2[] = { "", NULL };
const char *test3[] = { "a", NULL };
const char *test4[] = { "a", "", NULL };
const char *test5[] = { "a", "b", NULL };
ret = guestfs___concat_strings ((char **) test1);
assert (STREQ (ret, ""));
free (ret);
ret = guestfs___concat_strings ((char **) test2);
assert (STREQ (ret, ""));
free (ret);
ret = guestfs___concat_strings ((char **) test3);
assert (STREQ (ret, "a"));
free (ret);
ret = guestfs___concat_strings ((char **) test4);
assert (STREQ (ret, "a"));
free (ret);
ret = guestfs___concat_strings ((char **) test5);
assert (STREQ (ret, "ab"));
free (ret);
}
/* Test guestfs___join_strings. */
static void
test_join (void)
{
char *ret;
const char *test1[] = { NULL };
const char *test2[] = { "", NULL };
const char *test3[] = { "a", NULL };
const char *test4[] = { "a", "", NULL };
const char *test5[] = { "a", "b", NULL };
ret = guestfs___join_strings (":!", (char **) test1);
assert (STREQ (ret, ""));
free (ret);
ret = guestfs___join_strings (":!", (char **) test2);
assert (STREQ (ret, ""));
free (ret);
ret = guestfs___join_strings (":!", (char **) test3);
assert (STREQ (ret, "a"));
free (ret);
ret = guestfs___join_strings (":!", (char **) test4);
assert (STREQ (ret, "a:!"));
free (ret);
ret = guestfs___join_strings (":!", (char **) test5);
assert (STREQ (ret, "a:!b"));
free (ret);
}
int
main (int argc, char *argv[])
{
test_split ();
test_concat ();
test_join ();
exit (EXIT_SUCCESS);
}