]> granicus.if.org Git - procps-ng/commitdiff
tests: update template and add pids
authorCraig Small <csmall@enc.com.au>
Tue, 19 Apr 2016 11:33:02 +0000 (21:33 +1000)
committerCraig Small <csmall@enc.com.au>
Tue, 19 Apr 2016 11:33:02 +0000 (21:33 +1000)
Generalised the library API tests and created a common
test-runner. Instead of copy and pasting the same code in
each time.

First cut of pids API test, for _new so far.

Makefile.am
include/tests.h
proc/.gitignore
proc/test_namespace.c
proc/test_pids.c [new file with mode: 0644]
proc/test_sysinfo.c
proc/test_uptime.c
proc/test_version.c

index 42a67e5fbc8305fe9ea045f77fa688b347eabbc5..77e34f34b39ef8ac7ca25ea80f2f2ef9f4ebb250 100644 (file)
@@ -269,6 +269,8 @@ proc_test_sysinfo_SOURCES = proc/test_sysinfo.c
 proc_test_sysinfo_LDADD = proc/libprocps.la
 proc_test_namespace_SOURCES = proc/test_namespace.c
 proc_test_namespace_LDADD = proc/libprocps.la
+proc_test_pids_SOURCES = proc/test_pids.c
+proc_test_pids_LDADD = proc/libprocps.la
 proc_test_uptime_SOURCES = include/tests.h proc/test_uptime.c
 proc_test_uptime_LDADD = proc/libprocps.la
 proc_test_version_SOURCES = include/tests.h proc/test_version.c
@@ -283,6 +285,7 @@ endif
 BUILT_SOURCES = $(top_srcdir)/.version
 
 TESTS = proc/test_sysinfo \
+       proc/test_pids \
        proc/test_namespace \
        proc/test_uptime \
        proc/test_version \
index c65e0ff67b3c1bc452b0bd3d17424dde7262e036..1774f52d96965a6d550ed85495b741d327f013ec 100644 (file)
@@ -2,9 +2,26 @@
 #ifndef PROCPS_NG_TESTS_H
 #define PROCPS_NG_TESTS_H
 
-struct test_func {
-    int (*func)(void *data);
-    char *name;
-};
+typedef int (*TestFunction)(void *data);
 
+char *testname;
+
+
+static inline int run_tests(TestFunction *list, void *data)
+{
+    int i;
+    TestFunction current;
+
+    for (i=0; list[i] != NULL; i++) {
+        testname = NULL;
+        current = list[i];
+        if (!current(data)) {
+            fprintf(stderr, "FAIL: %s\n", testname);
+            return EXIT_FAILURE;
+        } else {
+            fprintf(stderr, "PASS: %s\n", testname);
+        }
+    }
+    return EXIT_SUCCESS;
+}
 #endif
index 7f596aa575941126da8cabc4e5e0c674a622b29a..d3b428d97c9c720f7bb917a0e2e9be8c868130e7 100644 (file)
@@ -1,4 +1,5 @@
 test_namespace
+test_pids
 test_sysinfo
 test_uptime
 test_version
index ec9c58626f2f7c46ce91b995a937065fd9c1f598..d39f03d836af571aa2e99be24089de7a10e18a5c 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#include <proc/namespace.h>
-
-struct test_func {
-    int (*func)(void *data);
-    char *name;
-};
+#include <proc/procps.h>
+#include "tests.h"
 
 int check_name_minus(void *data)
 {
+    testname = "procps_ns_get_name() negative id";
     return (procps_ns_get_name(-1) == NULL);
 }
 
 int check_name_over(void *data)
 {
+    testname = "procps_ns_get_name() id over limit";
     return (procps_ns_get_name(999) == NULL);
 }
 
 int check_name_ipc(void *data)
 {
+    testname = "procps_ns_get_name() ipc";
     return (strcmp(procps_ns_get_name(PROCPS_NS_IPC),"ipc")==0);
 }
 
 int check_id_null(void *data)
 {
+    testname = "procps_ns_get_id(NULL)";
     return (procps_ns_get_id(NULL) < 0);
 }
 
 int check_id_unfound(void *data)
 {
+    testname = "procps_ns_get_id(unknown)";
     return (procps_ns_get_id("foobar") < 0);
 }
 
 int check_id_mnt(void *data)
 {
+    testname = "procps_ns_get_id(mnt)";
     return (procps_ns_get_id("mnt") == PROCPS_NS_MNT);
 }
 
-struct test_func tests[] = {
-    { check_name_minus, "procps_ns_get_name() negative id"},
-    { check_name_over, "procps_ns_get_name() id over limit"},
-    { check_name_ipc, "procps_ns_get_name() ipc"},
-    { check_id_null, "procps_ns_get_id(NULL)"},
-    { check_id_unfound, "procps_ns_get_id(unknown)"},
-    { check_id_mnt, "procps_ns_get_id(mnt)"},
-    { NULL, NULL}
+TestFunction test_funcs[] = {
+    check_name_minus,
+    check_name_over,
+    check_name_ipc,
+    check_id_null,
+    check_id_unfound,
+    check_id_mnt,
+    NULL
 };
 
 int main(int argc, char *argv[])
 {
-    int i;
-    struct test_func *current;
-
-    for(i=0; tests[i].func != NULL; i++) {
-        current = &tests[i];
-        if (!current->func(NULL)) {
-            fprintf(stderr, "FAIL: %s\n", current->name);
-            return EXIT_FAILURE;
-        } else {
-            fprintf(stderr, "PASS: %s\n", current->name);
-        }
-    }
-    return EXIT_SUCCESS;
+    return run_tests(test_funcs, NULL);
 }
 
-
diff --git a/proc/test_pids.c b/proc/test_pids.c
new file mode 100644 (file)
index 0000000..ad37eab
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * libprocps - Library to read proc filesystem
+ * Tests for pids library calls
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include <proc/procps.h>
+#include "tests.h"
+
+int check_pids_new_nullinfo(void *data)
+{
+    testname = "procps_pids_new() info=NULL returns -EINVAL";
+    return (procps_pids_new(NULL, 0, NULL) == -EINVAL);
+}
+
+int check_pids_new_enum(void *data)
+{
+    testname = "procps_pids_new() items=enum returns -EINVAL";
+    struct procps_pidsinfo *info;
+    return (procps_pids_new(&info, 3, PROCPS_PIDS_noop) == -EINVAL);
+}
+
+int check_pids_new_toomany(void *data)
+{
+    struct procps_pidsinfo *info;
+    enum pids_item items[] = { PROCPS_PIDS_ID_PID, PROCPS_PIDS_ID_PID };
+    testname = "procps_pids_new() too many items returns -EINVAL";
+    return (procps_pids_new(&info, 1, items) == -EINVAL);
+}
+
+TestFunction test_funcs[] = {
+    check_pids_new_nullinfo,
+    check_pids_new_enum,
+    check_pids_new_toomany,
+    NULL };
+
+int main(int argc, char *argv[])
+{
+    return run_tests(test_funcs, NULL);
+}
+
+
index 9597d50da959a0aa269dcd8e94024b9e34da7bcb..80f627d3df5f7f7be5f6e29439970bd65c50edb5 100644 (file)
 #include <stdio.h>
 
 #include <proc/sysinfo.h>
-
-struct test_func {
-    int (*func)(void *data);
-    char *name;
-};
+#include "tests.h"
 
 int check_hertz(void *data)
 {
     long hz;
+    testname = "procps_hertz_get()";
 
     hz =  procps_hertz_get();
     return (hz > 0);
@@ -37,6 +34,7 @@ int check_hertz(void *data)
 int check_loadavg(void *data)
 {
     double a,b,c;
+    testname = "procps_loadavg()";
 
     if (procps_loadavg(&a, &b, &c) == 0)
         return 1;
@@ -45,33 +43,22 @@ int check_loadavg(void *data)
 
 int check_loadavg_null(void *data)
 {
+    testname = "procps_loadavg() with NULLs";
     if (procps_loadavg(NULL, NULL, NULL) == 0)
         return 1;
     return 0;
 }
 
-struct test_func tests[] = {
-    { check_hertz, "procps_hertz_get()"},
-    { check_loadavg, "procps_loadavg()"},
-    { check_loadavg, "procps_loadavg() with NULLs"},
-    { NULL, NULL}
+TestFunction test_funcs[] = {
+    check_hertz,
+    check_loadavg,
+    check_loadavg_null,
+    NULL,
 };
 
 int main(int argc, char *argv[])
 {
-    int i;
-    struct test_func *current;
-
-    for(i=0; tests[i].func != NULL; i++) {
-        current = &tests[i];
-        if (!current->func(NULL)) {
-            fprintf(stderr, "FAIL: %s\n", current->name);
-            return EXIT_FAILURE;
-        } else {
-            fprintf(stderr, "PASS: %s\n", current->name);
-        }
-    }
-    return EXIT_SUCCESS;
+    return run_tests(test_funcs, NULL);
 }
 
 
index 92fc5fdb9e10179282244f4c57335cfd49a1b29f..5058294b0474960aea0a142cc1ad61ccd8b9593e 100644 (file)
@@ -26,6 +26,7 @@
 
 int check_uptime(void *data)
 {
+    testname = "procps_uptime()";
     double up=0, idle=0;
     int rc;
     rc = procps_uptime(&up, &idle);
@@ -36,6 +37,7 @@ int check_uptime_nullup(void *data)
 {
     double idle=0;
     int rc;
+    testname = "procps_uptime() (up=NULL)";
     rc = procps_uptime(NULL, &idle);
     return (rc > 0 && idle > 0);
 }
@@ -44,6 +46,7 @@ int check_uptime_nullidle(void *data)
 {
     double up=0;
     int rc;
+    testname = "procps_uptime() (idle=NULL)";
     rc = procps_uptime(&up, NULL);
     return (rc > 0 && up > 0);
 }
@@ -51,6 +54,7 @@ int check_uptime_nullidle(void *data)
 int check_uptime_nullall(void *data)
 {
     int rc;
+    testname = "procps_uptime() (up,idle=NULL)";
     rc = procps_uptime(NULL, NULL);
     return (rc > 0);
 }
@@ -58,6 +62,7 @@ int check_uptime_nullall(void *data)
 int check_uptime_sprint(void *data)
 {
     char *str;
+    testname = "procps_uptime_sprint()";
 
     str = procps_uptime_sprint();
 
@@ -67,37 +72,26 @@ int check_uptime_sprint(void *data)
 int check_uptime_sprint_short(void *data)
 {
     char *str;
+    testname = "procps_uptime_sprint_short()";
 
     str = procps_uptime_sprint_short();
 
     return (str != NULL && str[0] != '\0');
 }
 
-struct test_func tests[] = {
-    { check_uptime, "procps_uptime()"},
-    { check_uptime_nullup, "procps_uptime() (up=NULL)"},
-    { check_uptime_nullidle, "procps_uptime() (idle=NULL)"},
-    { check_uptime_nullall, "procps_uptime() (up,idle=NULL)"},
-    { check_uptime_sprint, "procps_uptime_sprint()"},
-    { check_uptime_sprint_short, "procps_uptime_sprint_short()"},
-    { NULL, NULL}
+TestFunction test_funcs[] = {
+    check_uptime,
+    check_uptime_nullup,
+    check_uptime_nullidle,
+    check_uptime_nullall,
+    check_uptime_sprint,
+    check_uptime_sprint_short,
+    NULL,
 };
 
 int main(int argc, char *argv[])
 {
-    int i;
-    struct test_func *current;
-
-    for(i=0; tests[i].func != NULL; i++) {
-        current = &tests[i];
-        if (!current->func(NULL)) {
-            fprintf(stderr, "FAIL: %s\n", current->name);
-            return EXIT_FAILURE;
-        } else {
-            fprintf(stderr, "PASS: %s\n", current->name);
-        }
-    }
-    return EXIT_SUCCESS;
+    return run_tests(test_funcs, NULL);
 }
 
 
index c55dae54bc770e8db00a9a5d75cd67605cbf7f13..ba01925ce2b413d0425d500e8addb77b9dc279fb 100644 (file)
 
 int check_linux_version(void *data)
 {
+    testname = "procps_linux_version()";
     return (procps_linux_version() > 0);
 }
 
-struct test_func tests[] = {
-    { check_linux_version, "procps_linux_version()"},
-    { NULL, NULL}
+TestFunction test_funcs[] = {
+    check_linux_version,
+    NULL
 };
 
 int main(int argc, char *argv[])
 {
-    int i;
-    struct test_func *current;
-
-    for(i=0; tests[i].func != NULL; i++) {
-        current = &tests[i];
-        if (!current->func(NULL)) {
-            fprintf(stderr, "FAIL: %s\n", current->name);
-            return EXIT_FAILURE;
-        } else {
-            fprintf(stderr, "PASS: %s\n", current->name);
-        }
-    }
-    return EXIT_SUCCESS;
+    return run_tests(test_funcs, NULL);
 }