[pacman-dev] [PATCH] add unit tests

Andrew Gregory andrew.gregory.8 at gmail.com
Fri Apr 12 14:52:15 EDT 2013


---
 test/alpm/unit/Makefile         | 16 +++++++++
 test/alpm/unit/alpm_list_test.c | 76 +++++++++++++++++++++++++++++++++++++++++
 test/alpm/unit/runner.c         | 47 +++++++++++++++++++++++++
 test/alpm/unit/util_test.c      | 34 ++++++++++++++++++
 4 files changed, 173 insertions(+)
 create mode 100644 test/alpm/unit/Makefile
 create mode 100644 test/alpm/unit/alpm_list_test.c
 create mode 100644 test/alpm/unit/runner.c
 create mode 100644 test/alpm/unit/util_test.c

diff --git a/test/alpm/unit/Makefile b/test/alpm/unit/Makefile
new file mode 100644
index 0000000..9436238
--- /dev/null
+++ b/test/alpm/unit/Makefile
@@ -0,0 +1,16 @@
+CFLAGS += -Wall -I../../../lib/libalpm -fPIC -DLDCONFIG=\"ldconfig\"
+LDFLAGS += -lcheck -ldl
+
+tests = alpm_list_test.so \
+		util_test.so
+
+%.so: %.c
+	$(CC) $(CFLAGS) -shared $< -o $@ $(LDFLAGS)
+
+.PHONY: clean check
+
+check: runner $(tests)
+	./runner
+
+clean:
+	rm -rf *.so *.o runner
diff --git a/test/alpm/unit/alpm_list_test.c b/test/alpm/unit/alpm_list_test.c
new file mode 100644
index 0000000..1f08ae0
--- /dev/null
+++ b/test/alpm/unit/alpm_list_test.c
@@ -0,0 +1,76 @@
+#include <check.h>
+
+#include "alpm_list.c"
+
+static void check_list(alpm_list_t *list, char **expected)
+{
+    int i = 0;
+    for(; list; list = list->next, i++) {
+        ck_assert_str_eq(list->data, expected[i]);
+    }
+}
+
+START_TEST (check_alpm_list_add)
+{
+    alpm_list_t *list = NULL;
+
+    list = alpm_list_add(list, "1");
+    ck_assert_str_eq(list->data, "1");
+
+    list = alpm_list_add(list, "2");
+    ck_assert_str_eq(list->next->data, "2");
+
+    alpm_list_free(list);
+}
+END_TEST
+
+START_TEST (check_alpm_list_count)
+{
+    alpm_list_t *list = NULL;
+    ck_assert_int_eq(alpm_list_count(list), 0);
+
+    list = alpm_list_add(list, "1");
+    ck_assert_int_eq(alpm_list_count(list), 1);
+
+    list = alpm_list_add(list, "2");
+    ck_assert_int_eq(alpm_list_count(list), 2);
+
+    list = alpm_list_add(list, "3");
+    ck_assert_int_eq(alpm_list_count(list), 3);
+
+    alpm_list_free(list);
+}
+END_TEST
+
+START_TEST (check_alpm_list_join) {
+    char *expected[5] = {"1", "3", "5", "2", "4"};
+
+    alpm_list_t *list = NULL;
+    list = alpm_list_add(list, "1");
+    list = alpm_list_add(list, "3");
+    list = alpm_list_add(list, "5");
+
+    alpm_list_t *list2 = NULL;
+    list2 = alpm_list_add(list2, "2");
+    list2 = alpm_list_add(list2, "4");
+
+    list = alpm_list_join(list, list2);
+
+    check_list(list, expected);
+
+    alpm_list_free(list);
+}
+END_TEST
+
+Suite *build_suite(void) {
+    Suite *s = suite_create("alpm_list");
+    TCase *tc_core = tcase_create("Core");
+
+    tcase_add_test(tc_core, check_alpm_list_add);
+    tcase_add_test(tc_core, check_alpm_list_count);
+    tcase_add_test(tc_core, check_alpm_list_join);
+
+    suite_add_tcase(s, tc_core);
+
+    return s;
+}
diff --git a/test/alpm/unit/runner.c b/test/alpm/unit/runner.c
new file mode 100644
index 0000000..472c17f
--- /dev/null
+++ b/test/alpm/unit/runner.c
@@ -0,0 +1,47 @@
+#include <dlfcn.h>
+#include <stdio.h>
+#include <check.h>
+
+#include <glob.h>
+
+void add_suite(SRunner *sr, const char *dl)
+{
+    Suite *(*build_suite)(void);
+    char *error;
+    void *handle;
+
+    handle = dlopen(dl, RTLD_LAZY);
+    if(!handle) {
+        fputs(dlerror(), stderr);
+        return;
+    }
+
+    dlerror();
+
+    *(void **) (&build_suite) = dlsym(handle, "build_suite");
+    if((error = dlerror()) != NULL) {
+        fputs(error, stderr);
+        return;
+    }
+
+    srunner_add_suite(sr, (*build_suite)());
+}
+
+int main(void) {
+    int failed;
+    glob_t tests;
+    int i;
+    Suite *s = suite_create("alpm");
+    SRunner *sr = srunner_create(s);
+
+    glob("./*_test.so", 0, NULL, &tests);
+    for(i = 0; i < tests.gl_pathc; i++) {
+        add_suite(sr, tests.gl_pathv[i]);
+    }
+
+    srunner_run_all(sr, CK_ENV);
+    failed = srunner_ntests_failed(sr);
+    srunner_free(sr);
+
+    return failed;
+}
diff --git a/test/alpm/unit/util_test.c b/test/alpm/unit/util_test.c
new file mode 100644
index 0000000..cb06630
--- /dev/null
+++ b/test/alpm/unit/util_test.c
@@ -0,0 +1,34 @@
+#include <check.h>
+#include <unistd.h>
+
+#include "util.c"
+
+START_TEST (check_alpm_strip_newline)
+{
+    char tests[][2][30] = {
+        {"test\n", "test"},
+        {"test\n\n", "test"},
+        {"test", "this will fail"},
+        {"test", "test"}
+    };
+    size_t len, i, test_count = sizeof(tests)/sizeof(tests[0]);
+
+    for(i = 0; i < test_count; i++) {
+        char *output = tests[i][0], *expected = tests[i][1];
+        size_t expected_len = strlen(expected);
+
+        len = _alpm_strip_newline(output, strlen(output));
+
+        ck_assert_str_eq(output, expected);
+        ck_assert_int_eq(len, expected_len);
+    }
+}
+END_TEST
+
+Suite *build_suite(void) {
+    Suite *s = suite_create("util.c");
+    TCase *tc_core = tcase_create("Core");
+    tcase_add_test(tc_core, check_alpm_strip_newline);
+    suite_add_tcase(s, tc_core);
+    return s;
+}
-- 
1.8.2.1



More information about the pacman-dev mailing list