tests: update template and add pids

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.
This commit is contained in:
Craig Small 2016-04-19 21:33:02 +10:00
parent 714ea69c6d
commit aa16ab0dc1
8 changed files with 130 additions and 92 deletions

View 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 \

View 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

1
proc/.gitignore vendored
View File

@ -1,4 +1,5 @@
test_namespace
test_pids
test_sysinfo
test_uptime
test_version

View File

@ -20,68 +20,57 @@
#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);
}

58
proc/test_pids.c Normal file
View File

@ -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);
}

View File

@ -20,15 +20,12 @@
#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);
}

View 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);
}

View File

@ -26,29 +26,18 @@
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);
}