]> sourceware.org Git - lvm2.git/commitdiff
Unit testing for some string libdm functions
authorZdenek Kabelac <zkabelac@redhat.com>
Thu, 23 Feb 2012 22:47:17 +0000 (22:47 +0000)
committerZdenek Kabelac <zkabelac@redhat.com>
Thu, 23 Feb 2012 22:47:17 +0000 (22:47 +0000)
TODO: more functions need to be unit tested.

test/unit/Makefile.in
test/unit/run.c
test/unit/string_t.c [new file with mode: 0644]

index 604ea1aef453d6584418722c78a6e695cc12b4ce..65e8bec8c2d688a541c7debc0c8d69d29869e2fa 100644 (file)
@@ -15,7 +15,7 @@ top_srcdir = @top_srcdir@
 top_builddir = @top_builddir@
 
 VPATH = $(srcdir)
-SOURCES = bitset_t.c matcher_t.c config_t.c run.c
+SOURCES = bitset_t.c matcher_t.c config_t.c string_t.c run.c
 ifeq ("@TESTING@", "yes")
 TARGETS = run
 endif
index 734385587453acfd8548b9a2e57940c9d8749150..482498ae5e53c5d0107090476e0341a0e99ad24b 100644 (file)
 DECL(bitset);
 DECL(regex);
 DECL(config);
+DECL(string);
 
 CU_SuiteInfo suites[] = {
        USE(bitset),
        USE(regex),
        USE(config),
+       USE(string),
        CU_SUITE_INFO_NULL
 };
 
diff --git a/test/unit/string_t.c b/test/unit/string_t.c
new file mode 100644 (file)
index 0000000..df72505
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "libdevmapper.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include <CUnit/CUnit.h>
+
+int string_init(void);
+int string_fini(void);
+
+static struct dm_pool *mem = NULL;
+
+int string_init(void)
+{
+       mem = dm_pool_create("string test", 1024);
+
+       return (mem == NULL);
+}
+
+int string_fini(void)
+{
+       dm_pool_destroy(mem);
+
+       return 0;
+}
+
+/* TODO: Add more string unit tests here */
+
+static void test_strncpy(void)
+{
+       const char st[] = "1234567890";
+       char buf[sizeof(st)];
+
+       CU_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf)), 1);
+       CU_ASSERT_EQUAL(strcmp(buf, st), 0);
+
+       CU_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf) - 1), 0);
+       CU_ASSERT_EQUAL(strlen(buf) + 1, sizeof(buf) - 1);
+}
+
+static void test_asprint(void)
+{
+       const char st0[] = "";
+       const char st1[] = "12345678901";
+       const char st2[] = "1234567890123456789012345678901234567890123456789012345678901234567";
+       char *buf;
+       int a;
+
+       a = dm_asprintf(&buf, "%s", st0);
+       CU_ASSERT_EQUAL(strcmp(buf, st0), 0);
+       CU_ASSERT_EQUAL(a, sizeof(st0));
+       free(buf);
+
+       a = dm_asprintf(&buf, "%s", st1);
+       CU_ASSERT_EQUAL(strcmp(buf, st1), 0);
+       CU_ASSERT_EQUAL(a, sizeof(st1));
+       free(buf);
+
+       a = dm_asprintf(&buf, "%s", st2);
+       CU_ASSERT_EQUAL(a, sizeof(st2));
+       CU_ASSERT_EQUAL(strcmp(buf, st2), 0);
+       free(buf);
+}
+
+CU_TestInfo string_list[] = {
+       { (char*)"asprint", test_asprint },
+       { (char*)"strncpy", test_strncpy },
+       CU_TEST_INFO_NULL
+};
This page took 0.036617 seconds and 5 git commands to generate.