Skip to content

Commit

Permalink
#9699 droplet: create a unit test framework - Add a check framework b…
Browse files Browse the repository at this point in the history
…ased unit tests for most common struct used in droplet - The tests complete with the following coverage - File 'src/sbuf.c' Lines executed:85.19% of 54 - File 'src/dbuf.c' Lines executed:30.56% of 108 - File 'src/ntinydb.c' Lines executed:84.21% of 76 - File 'src/value.c' Lines executed:44.78% of 67 - File 'src/dict.c' Lines executed:60.11% of 188 - File 'src/task.c' Lines executed:79.47% of 151

git-svn-id: svn+ssh://svn.xe15.com/svn/bizanga/interop@37084 d0ba0a31-f0f3-0310-b3bb-b8bd854a737c
  • Loading branch information
davidbariod authored and David Bariod committed Sep 20, 2013
1 parent a9f0948 commit 66c63ae
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ACLOCAL_AMFLAGS = -I m4

SUBDIRS=libdroplet examples
SUBDIRS=libdroplet examples utests

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = droplet-3.0.pc
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ AC_CONFIG_FILES([
Makefile
examples/Makefile
libdroplet/Makefile
utests/Makefile
droplet-3.0.pc
libdroplet.spec
])
Expand Down
2 changes: 1 addition & 1 deletion libdroplet/include/droplet/ntinydb.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
typedef int (*dpl_ntinydb_func_t)(const char *key_ptr, int key_len, void *cb_arg);

/* PROTO ntinydb.c */
dpl_status_t dpl_ntinydb_set(dpl_sbuf_t *blob, const char *key, char *buf, int len);
dpl_status_t dpl_ntinydb_set(dpl_sbuf_t *blob, const char *key, const char *buf, int len);
dpl_status_t dpl_ntinydb_get(const char *buf, int len, const char *key, const char **data_returned, int *datalen_returned);
dpl_status_t dpl_ntinydb_list(const char *buf, int len, dpl_ntinydb_func_t cb_func, void *cb_arg);
#endif
3 changes: 3 additions & 0 deletions libdroplet/src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ dpl_dict_new(int n_buckets)
{
dpl_dict_t *dict;

if (0 == n_buckets)
return NULL;

dict = malloc(sizeof (*dict));
if (NULL == dict)
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion libdroplet/src/ntinydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
dpl_status_t
dpl_ntinydb_set(dpl_sbuf_t *blob,
const char *key,
char *data,
const char *data,
int datalen)
{
char flag;
Expand Down
6 changes: 6 additions & 0 deletions utests/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin_PROGRAMS = alltests

alltests_CFLAGS = -std=gnu99 -I$(top_srcdir)/libdroplet/include
alltests_LDADD = $(top_builddir)/libdroplet/libdroplet.la $(JSON_LIBS) -lcrypto -lcheck
alltests_SOURCES = taskpool_utest.c ntinydb_utest.c dbuf_utest.c sbuf_utest.c dict_utest.c vec_utest.c utest_main.c

47 changes: 47 additions & 0 deletions utests/dbuf_utest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <check.h>
#include <droplet.h>
#include <droplet/dbuf.h>

#include "utest_main.h"

START_TEST(dbuf_test)
{
dpl_dbuf_t * b = dpl_dbuf_new();
fail_if(NULL == b, NULL);
fail_unless(0 == dpl_dbuf_length(b), NULL);
dpl_dbuf_free(b);

b = dpl_dbuf_new();
fail_if(NULL == b, NULL);

const char * str = "abcdefghijklmnopqrstuvwxyz";
int ret;

ret = dpl_dbuf_add(b, str, sizeof(str));
fail_unless(1 == ret, NULL);
fail_unless(sizeof(str) == dpl_dbuf_length(b), NULL);

unsigned int i;
for (i = 0; i < sizeof(str); i++)
{
char out;
ret = dpl_dbuf_consume(b, &out, sizeof(out));
fail_unless(1 == ret, NULL);
fail_unless(sizeof(str) == dpl_dbuf_length(b) + i + 1, NULL);
fail_unless(out == str[i], NULL);
}
dpl_dbuf_free(b);
}
END_TEST


Suite *
dbuf_suite ()
{
Suite * s = suite_create("dbuf");
TCase * t = tcase_create("base");
tcase_add_test(t, dbuf_test);
suite_add_tcase(s, t);
return s;
}
120 changes: 120 additions & 0 deletions utests/dict_utest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <check.h>
#include <droplet.h>

#include "utest_main.h"


static int
utest_dpl_dict_dump_one(dpl_dict_var_t * v, void *user_data)
{
fail_unless(user_data == NULL, NULL);
fail_if(NULL == v, NULL);
fail_unless(v->val->type == DPL_VALUE_STRING, NULL);
printf("%s: %s\n", v->key, v->val->string->buf);

return 0;
}

START_TEST(dict_test)
{
dpl_dict_t * dict;
const char * const * it;
char init_value[] = "a";
dpl_dict_var_t * var;
const dpl_value_t * value;
dpl_status_t ret;
int c;

static const char * const utest_strings[] = { "Foo", "bAr", "baz", NULL };

dict = dpl_dict_new(0);
fail_unless(NULL == dict, NULL);
dict = dpl_dict_new(10);

it = utest_strings;
while (*it != NULL)
{
ret = dpl_dict_add(dict, *it, init_value, 1);
fail_unless(DPL_SUCCESS == ret, NULL);
(*init_value)++;
it++;
}
//
// new, free, dup, copy combo
//
dpl_dict_t * d2 = dpl_dict_new(5);
ret = dpl_dict_copy(d2, dict);
fail_unless(DPL_SUCCESS == ret, NULL);
dpl_dict_free(dict);
dict = dpl_dict_new(20);
ret = dpl_dict_copy(dict, d2);
fail_unless(DPL_SUCCESS == ret, NULL);
dpl_dict_free(d2);
d2 = dpl_dict_dup(dict);
fail_unless(NULL != d2, NULL);
dpl_dict_free(dict);
dict = d2;

it = utest_strings;
var = dpl_dict_get(dict, *it);
fail_unless(var == NULL, NULL);
it++;
var = dpl_dict_get(dict, *it);
fail_unless(var == NULL, NULL);
it++;
var = dpl_dict_get(dict, *it);
fail_if(NULL == var, NULL);
value = var->val;
fail_unless(DPL_VALUE_STRING == value->type, NULL);
fail_unless(0 == strcmp(value->string->buf, "c"), NULL);

it = utest_strings;
dpl_dict_get_lowered(dict, *it, &var);
fail_if(NULL == var, NULL);
value = var->val;
fail_unless(DPL_VALUE_STRING == value->type, NULL);
fail_unless(0 == strcmp(value->string->buf, "a"), NULL);
it++;
dpl_dict_get_lowered(dict, *it, &var);
fail_if(NULL == value, NULL);
value = var->val;
fail_unless(DPL_VALUE_STRING == value->type, NULL);
fail_unless(0 == strcmp(value->string->buf, "b"), NULL);
it++;
dpl_dict_get_lowered(dict, *it, &var);
fail_if(NULL == value, NULL);
value = var->val;
fail_unless(DPL_VALUE_STRING == value->type, NULL);
fail_unless(0 == strcmp(value->string->buf, "c"), NULL);

dpl_dict_iterate(dict, utest_dpl_dict_dump_one, NULL);
dpl_dict_print(dict, stdout, 0);

c = dpl_dict_count(dict);
fail_unless(3 == c, NULL);

#if 0
dpl_dict_var_free();
dpl_dict_add_value();
dpl_dict_remove();
dpl_dict_dup();
dpl_dict_filter_prefix();
dpl_dict_get_value();
#endif

dpl_dict_free(dict);
}
END_TEST


Suite *
dict_suite (void)
{
Suite * s = suite_create("dict");
TCase * d = tcase_create("base");
tcase_add_test(d, dict_test);
suite_add_tcase(s, d);
return s;
}


96 changes: 96 additions & 0 deletions utests/ntinydb_utest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <check.h>
#include <droplet.h>
#include <droplet/ntinydb.h>

#include "utest_main.h"

typedef struct {
unsigned int nelem;
const char ** keys;
bool * found;
} arg_list;

static int check_keys (const char * k,
int kl,
void * arg)
{
arg_list * a = (arg_list*) arg;
unsigned int i;
for (i = 0; i < a->nelem; i++)
{
if (0 == strncmp(k, a->keys[i], kl))
{
a->found[i] = true;
break;
}
}
return 0;
}


START_TEST(ntinydb_test)
{
dpl_sbuf_t * b = dpl_sbuf_new(1);
const char * keys[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
char * datas[sizeof(keys) / sizeof(keys[0])];
unsigned int i;
int fd;
int ret;
dpl_status_t s;

fd = open("/dev/urandom", O_RDONLY);
fail_if(-1 == ret, NULL);

for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++)
{
datas[i] = malloc(512);
fail_if(NULL == datas[i], NULL);
ret = read(fd, datas[i], 512);
fail_unless(512 == ret, NULL);
s = dpl_ntinydb_set(b, keys[i], datas[i], 512);
fail_unless(DPL_SUCCESS == s, NULL);
}
for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++)
{
const char * datas_check[sizeof(keys) / sizeof(keys[0])];
int out_len;
s = dpl_ntinydb_get(b->buf, b->len, keys[i], datas_check + i, &out_len);
fail_unless(DPL_SUCCESS == s, NULL);
fail_unless(512 == out_len);
fail_unless(0 == memcmp(datas_check[i], datas[i], 512));
}
arg_list all_keys;
all_keys.nelem = sizeof(keys) / sizeof(keys[0]);
all_keys.keys = keys;
all_keys.found = malloc(sizeof(bool) * all_keys.nelem);
for (i = 0; i < all_keys.nelem; i++)
{
all_keys.found[i] = false;
}

s = dpl_ntinydb_list(b->buf, b->len, check_keys, &all_keys);
fail_unless(DPL_SUCCESS == s, NULL);
for (i = 0; i < all_keys.nelem; i++)
{
fail_if(false == all_keys.found[i], NULL);
}
}
END_TEST


Suite *
ntinydb_suite (void)
{
Suite * s = suite_create("ntinydb");
TCase * d = tcase_create("base");
tcase_add_test(d, ntinydb_test);
suite_add_tcase(s, d);
return s;
}

60 changes: 60 additions & 0 deletions utests/sbuf_utest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <check.h>
#include <droplet.h>


START_TEST(sbuf_test)
{
dpl_sbuf_t * b;
dpl_sbuf_t * b2;

int sizes[] = { 1, 2, 4, 8, 16, 32, 65};
unsigned int i;
for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
{
b = dpl_sbuf_new(sizes[i]);
fail_if(NULL == b, NULL);
b2 = dpl_sbuf_dup(b);
fail_if(NULL == b2, NULL);
dpl_sbuf_free(b);
dpl_sbuf_free(b2);
}

const char * strs[] = { "truc", "bidule", "machin", "chose" };
for (i = 0; i < sizeof(strs) / sizeof(strs[0]); i++)
{
b = dpl_sbuf_new_from_str(strs[i]);
fail_if(NULL == b, NULL);
fail_unless(0 == strcmp(strs[i], dpl_sbuf_get_str(b)), NULL);
b2 = dpl_sbuf_dup(b);
fail_if(NULL == b2, NULL);
fail_unless(0 == strcmp(strs[i], dpl_sbuf_get_str(b2)), NULL);
dpl_sbuf_print(stdout, b);
dpl_sbuf_free(b);
dpl_sbuf_free(b2);
}

const char * full_str = "trucbidulemachinchose";
b = dpl_sbuf_new_from_str("");
for (i = 0; i < sizeof(strs) / sizeof(strs[0]); i++)
{
dpl_status_t ret;
ret = dpl_sbuf_add_str(b, strs[i]);
fail_unless(DPL_SUCCESS == ret, NULL);
}
fail_unless(0 == strcmp(full_str, dpl_sbuf_get_str(b)), NULL);
dpl_sbuf_free(b);
puts("");
}
END_TEST


Suite *
sbuf_suite ()
{
Suite * s = suite_create("sbuf");
TCase * t = tcase_create("base");
tcase_add_test(t, sbuf_test);
suite_add_tcase(s, t);
return s;
}
Loading

0 comments on commit 66c63ae

Please sign in to comment.