forked from scality/Droplet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9699 droplet: create a unit test framework - Add a check framework b…
…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
Showing
14 changed files
with
546 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.