コード例 #1
0
ファイル: test-02-rotate.c プロジェクト: cleure/C-bstree
int main(int argc, char **argv)
{
    int i;
    struct bstree *tree = bstree_new(&copyfn, &freefn, &int_cmp, 0);
    
    int test[] = {20, 15, 32, 40, 25};
    for (i = 0; i < sizeof(test)/sizeof(test[0]); i++) {
        bstree_insert(tree, &test[i], NULL);
    }
    
    test_start(argv);
    test_case1(tree);
    
    assert(bstree_rotate_left(NULL, NULL) == 0);
    assert(bstree_rotate_left(NULL, tree->root) == 0);
    assert(bstree_rotate_left(tree, NULL) == 0);
    
    assert(bstree_rotate_left(tree, tree->root) == 1);
    test_case2(tree);
    
    assert(bstree_rotate_right(NULL, NULL) == 0);
    assert(bstree_rotate_right(NULL, tree->root) == 0);
    assert(bstree_rotate_right(tree, NULL) == 0);
    
    assert(bstree_rotate_right(tree, tree->root) == 1);
    test_case1(tree);
    
    test_end();
    bstree_delete(tree);

    return 0;
}
コード例 #2
0
ファイル: test_bstree.c プロジェクト: rodolf0/natools
void generate_test_tree(int allow_dups) {
    size_t n = 0, i;
    int sum = 0, *e=NULL;
    bstree_t *t = bstree_init(free, (cmp_func_t)intcmp, allow_dups);
    bst_node_t *node;

    int inserted = 0, removed = 0;
#ifndef _BALANCED_TREE_
    int rightrot = 0, leftrot = 0;
#endif

    size_t q = random() % 7000;
    for (i = 0; i < q; i++) {
#ifdef _BALANCED_TREE_
        switch (random() % 3) {
#else
        switch (random() % 5) {
#endif
        case 0:
        case 1:
            e = (int*)zmalloc(sizeof(int));
            *e = random() % 512;
            n++;
            sum += *e;
            bstree_insert(t, e);
            inserted++;
            break;
        case 2:
            /* try n find a possibly inserted number to remove it */
            e = (int*)zmalloc(sizeof(int));
            *e = random() % 512;
            node = bstree_find(t, e);
            if (node) {
                n--;
                sum -= *(int*)node->data;
                bstree_remove(t, node);
                removed++;
            }
            free(e);
            break;
#ifndef _BALANCED_TREE_
        case 3:
            /* try n find a possibly inserted number to rotate it's node */
            e = zmalloc(sizeof(int));
            *e = random() % 512;
            node = bstree_find(t, e);
            if (node) {
                bstree_rotate_left(t, node);
                leftrot++;
            }
            free(e);
            break;
        case 4:
            /* try n find a possibly inserted number to rotate it's node */
            e = zmalloc(sizeof(int));
            *e = random() % 512;
            node = bstree_find(t, e);
            if (node) {
                bstree_rotate_right(t, node);
                rightrot++;
            }
            free(e);
            break;
#endif
        }
#ifdef _BALANCED_TREE_
        /* check RB properties */
        if (t->root)
            assert(t->root->color == black);
        check_red_node_childs(t->root);
        current_height = 0;
        black_height = -1;
        check_black_height(t->root);
#endif
    }
#ifndef _BALANCED_TREE_
    fprintf(stderr, "    i: %d, r: %d, rl: %d, rr:%d     ",
            inserted, removed, leftrot, rightrot);
#endif

    /* check all elements are correct */
    if (allow_dups) {
        assert(t->size == n);
        totalsum = 0;
        bstree_foreach(t, (void (*)(void*))acumulate, inorder);
        assert(totalsum == sum);
    } else {
        while (t->size) {
            bst_node_t *r = t->root;
            int x = *(int*)r->data;
            bstree_remove(t, r);
            assert(bstree_find(t, &x) == NULL);
        }
    }

    /* check propper sorting of elements*/
    minimum = 0;
    bstree_foreach(t, (void (*)(void*))check_set_min, inorder);

    bstree_destroy(t);
}


#define ITERATIONS 50
int main(void) {
    int i;
    for (i = 1; i <= ITERATIONS; i++) {
        fprintf(stderr, "\rtesting ... %d%%", 100*i/ITERATIONS);
        generate_test_tree(1);
        generate_test_tree(0);
    }
    fprintf(stderr, "\n");
    return 0;
}