| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- /*
- * 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
- */
- #include <errno.h>
- #include <time.h>
- #include "torture.h"
- #include "std/c_alloc.h"
- #include "std/c_rbtree.h"
- typedef struct test_s {
- int key;
- int number;
- } test_t;
- static int data_cmp(const void *key, const void *data) {
- test_t *a, *b;
- a = (test_t *) key;
- b = (test_t *) data;
- if (a->key < b->key) {
- return -1;
- } else if (a->key > b->key) {
- return 1;
- }
- return 0;
- }
- static int key_cmp(const void *key, const void *data) {
- int a;
- test_t *b;
- a = POINTER_TO_INT(key);
- b = (test_t *) data;
- if (a < b->key) {
- return -1;
- } else if (a > b->key) {
- return 1;
- }
- return 0;
- }
- static int visitor(void *obj, void *data) {
- test_t *a;
- test_t *b;
- a = (test_t *) obj;
- b = (test_t *) data;
- if (a->key == b->key) {
- a->number = 42;
- }
- return 0;
- }
- static void destructor(void *data) {
- test_t *freedata = NULL;
- freedata = (test_t *) data;
- SAFE_FREE(freedata);
- }
- static void setup(void **state) {
- c_rbtree_t *tree = NULL;
- c_rbtree_create(&tree, key_cmp, data_cmp);
- *state = tree;
- }
- static void setup_complete_tree(void **state) {
- c_rbtree_t *tree = NULL;
- int i = 0;
- int rc;
- c_rbtree_create(&tree, key_cmp, data_cmp);
- for (i = 0; i < 100; i++) {
- test_t *testdata = NULL;
- testdata = c_malloc(sizeof(test_t));
- assert_non_null(testdata);
- testdata->key = i;
- rc = c_rbtree_insert(tree, (void *) testdata);
- assert_int_equal(rc, 0);
- }
- *state = tree;
- }
- static void teardown(void **state) {
- c_rbtree_t *tree = *state;
- c_rbtree_destroy(tree, destructor);
- c_rbtree_free(tree);
- *state = NULL;
- }
- 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);
- }
- static void check_c_rbtree_free_null(void **state)
- {
- int rc;
- (void) state; /* unused */
- rc = c_rbtree_free(NULL);
- assert_int_equal(rc, -1);
- }
- 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)
- {
- c_rbtree_t *tree = *state;
- int i = 0, rc;
- for (i = 0; i < 100; i++) {
- test_t *testdata = NULL;
- testdata = malloc(sizeof(test_t));
- assert_non_null(testdata);
- testdata->key = i;
- rc = c_rbtree_insert(tree, testdata);
- assert_int_equal(rc, 0);
- }
- rc = c_rbtree_check_sanity(tree);
- assert_int_equal(rc, 0);
- }
- static void check_c_rbtree_insert_duplicate(void **state)
- {
- c_rbtree_t *tree = *state;
- test_t *testdata;
- int rc;
- testdata = malloc(sizeof(test_t));
- assert_non_null(testdata);
- testdata->key = 42;
- rc = c_rbtree_insert(tree, (void *) testdata);
- assert_int_equal(rc, 0);
- /* add again */
- testdata = malloc(sizeof(test_t));
- assert_non_null(testdata);
- testdata->key = 42;
- /* check for duplicate */
- rc = c_rbtree_insert(tree, (void *) testdata);
- assert_int_equal(rc, 1);
- free(testdata);
- }
- static void check_c_rbtree_find(void **state)
- {
- c_rbtree_t *tree = *state;
- int rc, i = 42;
- c_rbnode_t *node;
- test_t *testdata;
- rc = c_rbtree_check_sanity(tree);
- assert_int_equal(rc, 0);
- /* find the node with the key 42 */
- node = c_rbtree_find(tree, (void *) &i);
- assert_non_null(node);
- testdata = (test_t *) c_rbtree_node_data(node);
- assert_int_equal(testdata->key, 42);
- }
- static void check_c_rbtree_delete(void **state)
- {
- c_rbtree_t *tree = *state;
- int rc, i = 42;
- c_rbnode_t *node = NULL;
- test_t *freedata = NULL;
- rc = c_rbtree_check_sanity(tree);
- assert_int_equal(rc, 0);
- node = c_rbtree_find(tree, (void *) &i);
- assert_non_null(node);
- freedata = (test_t *) c_rbtree_node_data(node);
- free(freedata);
- rc = c_rbtree_node_delete(node);
- assert_int_equal(rc, 0);
- rc = c_rbtree_check_sanity(tree);
- assert_int_equal(rc, 0);
- }
- static void check_c_rbtree_walk(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 *) c_malloc(sizeof(test_t));
- testdata->key = 42;
- rc = c_rbtree_walk(tree, testdata, visitor);
- assert_int_equal(rc, 0);
- /* find the node with the key 42 */
- node = c_rbtree_find(tree, (void *) &i);
- assert_non_null(node);
- free(testdata);
- testdata = (test_t *) c_rbtree_node_data(node);
- assert_int_equal(testdata->number, 42);
- }
- 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)
- {
- c_rbtree_t *tree = *state;
- c_rbtree_t *duptree = NULL;
- int rc = -1;
- duptree = c_rbtree_dup(tree);
- assert_non_null(duptree);
- rc = c_rbtree_check_sanity(duptree);
- assert_int_equal(rc, 0);
- c_rbtree_free(duptree);
- }
- #if 0
- static void check_c_rbtree_x)
- {
- 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);
- }
|