#include "unittest.h"
#include "util.h"
#include "out.h"
#include <inttypes.h>
#include "plugin.h"
struct dummy_plugin {
int (*foo)(int a);
int (*bar)(int a);
};
static int loaded;
static int
foo(int a)
{
return a;
}
static int
bar(int a)
{
return a;
}
static struct dummy_plugin plugin_static_funcs = {
foo, bar
};
static void
pmem_plugin_desc(const char **module_name, const char **name,
unsigned *version, void **funcs)
{
*module_name = "dummy";
*name = "plugin_static";
*version = 1;
*funcs = &plugin_static_funcs;
}
static int
pmem_plugin_load(void)
{
loaded = 1;
return 0;
}
static void
pmem_plugin_unload(void)
{
loaded = 2;
}
struct plugin_ops plugin_static = {
pmem_plugin_desc, pmem_plugin_load, pmem_plugin_unload,
};
int nplugins;
static void
plugin_traverse(const char *name, void *funcs, void *arg)
{
UT_ASSERTne(funcs, NULL);
UT_ASSERTeq(arg, &nplugins);
struct dummy_plugin *p = funcs;
if (strcmp(name, "plugin0") == 0) {
UT_ASSERTeq(p->foo(1), 4);
UT_ASSERTeq(p->bar(1), 6);
} else if (strcmp(name, "plugin1") == 0) {
UT_ASSERTeq(p->foo(1), 6);
UT_ASSERTeq(p->bar(1), 4);
} else if (strcmp(name, "plugin_static") == 0) {
UT_ASSERTeq(p->foo(1), 1);
UT_ASSERTeq(p->bar(1), 1);
} else {
UT_ASSERT(0);
}
nplugins++;
}
int
main(int argc, char *argv[])
{
START(argc, argv, "util_plugin");
int ret = plugin_init(".");
UT_ASSERTeq(ret, 0);
ret = plugin_add(&plugin_static);
UT_ASSERTeq(ret, 0);
UT_ASSERTeq(loaded, 0);
plugin_load("dummy", 1, plugin_traverse, &nplugins);
UT_ASSERTeq(loaded, 1);
plugin_fini();
UT_ASSERTeq(loaded, 2);
UT_ASSERTeq(nplugins, 3);
DONE(NULL);
}