2008-02-27 20:56:47 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
2012-10-16 18:37:38 +04:00
|
|
|
|
|
|
|
#include "torture.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
#include "std/c_alloc.h"
|
|
|
|
#include "std/c_rbtree.h"
|
|
|
|
|
|
|
|
typedef struct test_s {
|
2012-10-16 18:37:38 +04:00
|
|
|
int key;
|
|
|
|
int number;
|
2008-02-27 20:56:47 +03:00
|
|
|
} test_t;
|
|
|
|
|
|
|
|
static int data_cmp(const void *key, const void *data) {
|
2012-10-16 18:37:38 +04:00
|
|
|
test_t *a, *b;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
a = (test_t *) key;
|
|
|
|
b = (test_t *) data;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
if (a->key < b->key) {
|
|
|
|
return -1;
|
|
|
|
} else if (a->key > b->key) {
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
return 0;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int key_cmp(const void *key, const void *data) {
|
2012-10-16 18:37:38 +04:00
|
|
|
int a;
|
|
|
|
test_t *b;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
a = POINTER_TO_INT(key);
|
|
|
|
b = (test_t *) data;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
if (a < b->key) {
|
|
|
|
return -1;
|
|
|
|
} else if (a > b->key) {
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
return 0;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int visitor(void *obj, void *data) {
|
2012-10-16 18:37:38 +04:00
|
|
|
test_t *a;
|
|
|
|
test_t *b;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
a = (test_t *) obj;
|
|
|
|
b = (test_t *) data;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
if (a->key == b->key) {
|
|
|
|
a->number = 42;
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
return 0;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destructor(void *data) {
|
2012-10-16 18:37:38 +04:00
|
|
|
test_t *freedata = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
freedata = (test_t *) data;
|
|
|
|
SAFE_FREE(freedata);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void setup(void **state) {
|
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = c_rbtree_create(&tree, key_cmp, data_cmp);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
*state = tree;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void setup_complete_tree(void **state) {
|
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
int i = 0;
|
|
|
|
int rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_create(&tree, key_cmp, data_cmp);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
test_t *testdata = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = c_malloc(sizeof(test_t));
|
|
|
|
assert_non_null(testdata);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata->key = i;
|
|
|
|
|
|
|
|
rc = c_rbtree_insert(tree, (void *) testdata);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
*state = tree;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void teardown(void **state) {
|
|
|
|
c_rbtree_t *tree = *state;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_destroy(tree, destructor);
|
|
|
|
c_rbtree_free(tree);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
*state = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_create_free(void **state)
|
2008-08-05 13:31:37 +04:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
int rc;
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
(void) state; /* unused */
|
|
|
|
|
|
|
|
rc = c_rbtree_create(&tree, key_cmp, data_cmp);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
assert_int_equal(tree->size, 0);
|
|
|
|
|
|
|
|
rc = c_rbtree_free(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_create_null(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_create(NULL, key_cmp, data_cmp);
|
|
|
|
assert_int_equal(rc, -1);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_create(&tree, NULL, data_cmp);
|
|
|
|
assert_int_equal(rc, -1);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_create(&tree, key_cmp, NULL);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_free_null(void **state)
|
|
|
|
{
|
|
|
|
int rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
(void) state; /* unused */
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_free(NULL);
|
|
|
|
assert_int_equal(rc, -1);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_insert_delete(void **state)
|
2008-08-05 13:31:37 +04:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = NULL;
|
|
|
|
c_rbnode_t *node = NULL;
|
|
|
|
test_t *testdata = NULL;
|
|
|
|
int rc;
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
(void) state; /* unused */
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_create(&tree, key_cmp, data_cmp);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = malloc(sizeof(test_t));
|
|
|
|
testdata->key = 42;
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_insert(tree, (void *) testdata);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
node = c_rbtree_head(tree);
|
|
|
|
assert_non_null(node);
|
|
|
|
|
|
|
|
testdata = c_rbtree_node_data(node);
|
|
|
|
SAFE_FREE(testdata);
|
|
|
|
rc = c_rbtree_node_delete(node);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
c_rbtree_free(tree);
|
2008-08-05 13:31:37 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_insert_random(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
int i = 0, rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
test_t *testdata = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = malloc(sizeof(test_t));
|
|
|
|
assert_non_null(testdata);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata->key = i;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_insert(tree, testdata);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
}
|
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_insert_duplicate(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
test_t *testdata;
|
|
|
|
int rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = malloc(sizeof(test_t));
|
|
|
|
assert_non_null(testdata);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata->key = 42;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_insert(tree, (void *) testdata);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
/* add again */
|
|
|
|
testdata = malloc(sizeof(test_t));
|
|
|
|
assert_non_null(testdata);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata->key = 42;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
/* check for duplicate */
|
|
|
|
rc = c_rbtree_insert(tree, (void *) testdata);
|
|
|
|
assert_int_equal(rc, 1);
|
|
|
|
|
|
|
|
free(testdata);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_find(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
int rc, i = 42;
|
|
|
|
c_rbnode_t *node;
|
|
|
|
test_t *testdata;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
/* find the node with the key 42 */
|
|
|
|
node = c_rbtree_find(tree, (void *) &i);
|
|
|
|
assert_non_null(node);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = (test_t *) c_rbtree_node_data(node);
|
|
|
|
assert_int_equal(testdata->key, 42);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_delete(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
int rc, i = 42;
|
|
|
|
c_rbnode_t *node = NULL;
|
|
|
|
test_t *freedata = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
node = c_rbtree_find(tree, (void *) &i);
|
|
|
|
assert_non_null(node);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
freedata = (test_t *) c_rbtree_node_data(node);
|
|
|
|
free(freedata);
|
|
|
|
rc = c_rbtree_node_delete(node);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_walk(void **state)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
int rc, i = 42;
|
|
|
|
test_t *testdata;
|
|
|
|
c_rbnode_t *node;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = (test_t *) c_malloc(sizeof(test_t));
|
|
|
|
testdata->key = 42;
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_walk(tree, testdata, visitor);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
/* find the node with the key 42 */
|
|
|
|
node = c_rbtree_find(tree, (void *) &i);
|
|
|
|
assert_non_null(node);
|
|
|
|
free(testdata);
|
2008-03-03 15:22:36 +03:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
testdata = (test_t *) c_rbtree_node_data(node);
|
|
|
|
assert_int_equal(testdata->number, 42);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_walk_null(void **state)
|
2008-08-05 13:31:37 +04:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
int rc, i = 42;
|
|
|
|
test_t *testdata;
|
|
|
|
c_rbnode_t *node;
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
assert_int_equal(rc, 0);
|
|
|
|
|
|
|
|
testdata = (test_t *) malloc(sizeof(test_t));
|
|
|
|
testdata->key = 42;
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_walk(NULL, testdata, visitor);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_walk(tree, NULL, visitor);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_walk(tree, testdata, NULL);
|
|
|
|
assert_int_equal(rc, -1);
|
|
|
|
assert_int_equal(errno, EINVAL);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
/* find the node with the key 42 */
|
|
|
|
node = c_rbtree_find(tree, (void *) &i);
|
|
|
|
assert_non_null(node);
|
2008-08-05 13:31:37 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
free(testdata);
|
2008-08-05 13:31:37 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_dup(void **state)
|
2008-05-09 13:10:00 +04:00
|
|
|
{
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_t *tree = *state;
|
|
|
|
c_rbtree_t *duptree = NULL;
|
|
|
|
int rc = -1;
|
2008-05-09 13:10:00 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
duptree = c_rbtree_dup(tree);
|
|
|
|
assert_non_null(duptree);
|
2008-05-09 13:10:00 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
rc = c_rbtree_check_sanity(duptree);
|
|
|
|
assert_int_equal(rc, 0);
|
2008-05-09 13:10:00 +04:00
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
c_rbtree_free(duptree);
|
2008-05-09 13:10:00 +04:00
|
|
|
}
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
#if 0
|
2012-10-16 18:37:38 +04:00
|
|
|
static void check_c_rbtree_x)
|
2008-02-27 20:56:47 +03:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
rc = c_rbtree_check_sanity(tree);
|
|
|
|
fail_unless(rc == 0, "c_rbtree_check_sanity failed with return code %d", rc);
|
|
|
|
}
|
2008-08-05 13:31:37 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-16 18:37:38 +04:00
|
|
|
int torture_run_tests(void)
|
|
|
|
{
|
|
|
|
const UnitTest tests[] = {
|
|
|
|
unit_test(check_c_rbtree_create_free),
|
|
|
|
unit_test(check_c_rbtree_create_null),
|
|
|
|
unit_test(check_c_rbtree_free_null),
|
|
|
|
unit_test(check_c_rbtree_insert_delete),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_insert_random, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_insert_duplicate, setup, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_find, setup_complete_tree, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_delete, setup_complete_tree, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_walk, setup_complete_tree, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_walk_null, setup_complete_tree, teardown),
|
|
|
|
unit_test_setup_teardown(check_c_rbtree_dup, setup_complete_tree, teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
return run_tests(tests);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|