nextcloud-desktop/test/csync/std_tests/check_std_c_rbtree.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

364 lines
8 KiB
C
Raw Normal View History

/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
*
* 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
*/
2008-02-27 20:56:47 +03:00
#include <errno.h>
#include <time.h>
#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 {
int key;
int number;
2008-02-27 20:56:47 +03:00
} test_t;
static int data_cmp(const void *key, const void *data) {
test_t *a, *b;
2008-02-27 20:56:47 +03:00
a = (test_t *) key;
b = (test_t *) data;
2008-02-27 20:56:47 +03:00
if (a->key < b->key) {
return -1;
} else if (a->key > b->key) {
return 1;
}
2008-02-27 20:56:47 +03:00
return 0;
2008-02-27 20:56:47 +03:00
}
static int key_cmp(const void *key, const void *data) {
int a;
test_t *b;
2008-02-27 20:56:47 +03:00
a = POINTER_TO_INT(key);
b = (test_t *) data;
2008-02-27 20:56:47 +03:00
if (a < b->key) {
return -1;
} else if (a > b->key) {
return 1;
}
2008-02-27 20:56:47 +03:00
return 0;
2008-02-27 20:56:47 +03:00
}
static int visitor(void *obj, void *data) {
test_t *a;
test_t *b;
2008-02-27 20:56:47 +03:00
a = (test_t *) obj;
b = (test_t *) data;
2008-02-27 20:56:47 +03:00
if (a->key == b->key) {
a->number = 42;
}
2008-02-27 20:56:47 +03:00
return 0;
2008-02-27 20:56:47 +03:00
}
static void destructor(void *data) {
test_t *freedata = NULL;
2008-02-27 20:56:47 +03:00
freedata = (test_t *) data;
SAFE_FREE(freedata);
2008-02-27 20:56:47 +03:00
}
static void setup(void **state) {
c_rbtree_t *tree = NULL;
c_rbtree_create(&tree, key_cmp, data_cmp);
*state = tree;
2008-02-27 20:56:47 +03: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
c_rbtree_create(&tree, key_cmp, data_cmp);
2008-02-27 20:56:47 +03:00
for (i = 0; i < 100; i++) {
test_t *testdata = NULL;
2008-02-27 20:56:47 +03:00
testdata = c_malloc(sizeof(test_t));
assert_non_null(testdata);
2008-02-27 20:56:47 +03:00
testdata->key = i;
rc = c_rbtree_insert(tree, (void *) testdata);
assert_int_equal(rc, 0);
}
2008-02-27 20:56:47 +03:00
*state = tree;
2008-02-27 20:56:47 +03:00
}
static void teardown(void **state) {
c_rbtree_t *tree = *state;
2008-02-27 20:56:47 +03:00
c_rbtree_destroy(tree, destructor);
c_rbtree_free(tree);
2008-02-27 20:56:47 +03:00
*state = NULL;
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_create_free(void **state)
{
c_rbtree_t *tree = NULL;
int rc;
(void) state; /* unused */
c_rbtree_create(&tree, key_cmp, data_cmp);
assert_int_equal(tree->size, 0);
rc = c_rbtree_free(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_free_null(void **state)
{
int rc;
2008-02-27 20:56:47 +03:00
(void) state; /* unused */
2008-02-27 20:56:47 +03:00
rc = c_rbtree_free(NULL);
assert_int_equal(rc, -1);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_insert_delete(void **state)
{
c_rbtree_t *tree = NULL;
c_rbnode_t *node = NULL;
test_t *testdata = NULL;
int rc;
(void) state; /* unused */
c_rbtree_create(&tree, key_cmp, data_cmp);
testdata = malloc(sizeof(test_t));
testdata->key = 42;
rc = c_rbtree_insert(tree, (void *) testdata);
assert_int_equal(rc, 0);
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);
}
static void check_c_rbtree_insert_random(void **state)
2008-02-27 20:56:47 +03:00
{
c_rbtree_t *tree = *state;
int i = 0, rc;
2008-02-27 20:56:47 +03:00
for (i = 0; i < 100; i++) {
test_t *testdata = NULL;
2008-02-27 20:56:47 +03:00
testdata = malloc(sizeof(test_t));
assert_non_null(testdata);
2008-02-27 20:56:47 +03:00
testdata->key = i;
2008-02-27 20:56:47 +03:00
rc = c_rbtree_insert(tree, testdata);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_insert_duplicate(void **state)
2008-02-27 20:56:47 +03:00
{
c_rbtree_t *tree = *state;
test_t *testdata;
int rc;
2008-02-27 20:56:47 +03:00
testdata = malloc(sizeof(test_t));
assert_non_null(testdata);
2008-02-27 20:56:47 +03:00
testdata->key = 42;
2008-02-27 20:56:47 +03:00
rc = c_rbtree_insert(tree, (void *) testdata);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
/* add again */
testdata = malloc(sizeof(test_t));
assert_non_null(testdata);
2008-02-27 20:56:47 +03:00
testdata->key = 42;
2008-02-27 20:56:47 +03: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
}
static void check_c_rbtree_find(void **state)
2008-02-27 20:56:47 +03: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
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03: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
testdata = (test_t *) c_rbtree_node_data(node);
assert_int_equal(testdata->key, 42);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_delete(void **state)
2008-02-27 20:56:47 +03: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
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
node = c_rbtree_find(tree, (void *) &i);
assert_non_null(node);
2008-02-27 20:56:47 +03: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
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_walk(void **state)
2008-02-27 20:56:47 +03: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
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03:00
testdata = (test_t *) c_malloc(sizeof(test_t));
testdata->key = 42;
2008-02-27 20:56:47 +03:00
rc = c_rbtree_walk(tree, testdata, visitor);
assert_int_equal(rc, 0);
2008-02-27 20:56:47 +03: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
testdata = (test_t *) c_rbtree_node_data(node);
assert_int_equal(testdata->number, 42);
2008-02-27 20:56:47 +03:00
}
static void check_c_rbtree_walk_null(void **state)
{
c_rbtree_t *tree = *state;
int rc, i = 42;
test_t *testdata;
c_rbnode_t *node;
rc = c_rbtree_check_sanity(tree);
assert_int_equal(rc, 0);
testdata = (test_t *) malloc(sizeof(test_t));
testdata->key = 42;
rc = c_rbtree_walk(NULL, testdata, visitor);
assert_int_equal(rc, -1);
assert_int_equal(errno, EINVAL);
rc = c_rbtree_walk(tree, NULL, visitor);
assert_int_equal(rc, -1);
assert_int_equal(errno, EINVAL);
rc = c_rbtree_walk(tree, testdata, NULL);
assert_int_equal(rc, -1);
assert_int_equal(errno, EINVAL);
/* find the node with the key 42 */
node = c_rbtree_find(tree, (void *) &i);
assert_non_null(node);
free(testdata);
}
static void check_c_rbtree_dup(void **state)
2008-05-09 13:10:00 +04:00
{
c_rbtree_t *tree = *state;
c_rbtree_t *duptree = NULL;
int rc = -1;
2008-05-09 13:10:00 +04:00
duptree = c_rbtree_dup(tree);
assert_non_null(duptree);
2008-05-09 13:10:00 +04:00
rc = c_rbtree_check_sanity(duptree);
assert_int_equal(rc, 0);
2008-05-09 13:10:00 +04:00
c_rbtree_free(duptree);
2008-05-09 13:10:00 +04:00
}
2008-02-27 20:56:47 +03:00
#if 0
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);
}
#endif
int torture_run_tests(void)
{
const UnitTest tests[] = {
unit_test(check_c_rbtree_create_free),
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
}