Пример #1
0
static node_t *
treeIterateCb(tree_t *aTree, node_t *aNode, void *data) {
    unsigned *i = (unsigned *)data;
    node_t *sNode;

    assert(aNode->magic == NODE_MAGIC);
#ifdef FORWARD_PRINT
    if (*i != 0) {
	fprintf(stderr, "-->");
    }
    fprintf(stderr, "%ld", aNode->key);
#endif

    /* Test trp_search(). */
    sNode = tree_search(aTree, aNode);
    assert(sNode == aNode);

    /* Test trp_nsearch(). */
    sNode = tree_nsearch(aTree, aNode);
    assert(sNode == aNode);

    /* Test trp_psearch(). */
    sNode = tree_psearch(aTree, aNode);
    assert(sNode == aNode);

    (*i)++;

    return NULL;
}
Пример #2
0
static void
nodeRemove(tree_t *aTree, node_t *aNode, unsigned aNNodes) {
    node_t *sNode;

#ifdef VERBOSE
    fprintf(stderr, "trp_remove(%3ld)", aNode->key);
#endif
    tree_remove(aTree, aNode);

    /* Test trp_nsearch(). */
    sNode = tree_nsearch(aTree, aNode);
    assert(sNode == NULL || sNode->key >= aNode->key);

    /* Test trp_psearch(). */
    sNode = tree_psearch(aTree, aNode);
    assert(sNode == NULL || sNode->key <= aNode->key);

    aNode->magic = 0;

#ifdef TREE_PRINT
    fprintf(stderr, "\n\t   tree: ");
#endif
#ifdef FORWARD_PRINT
    fprintf(stderr, "\n\tforward: ");
#endif
    assert(aNNodes - 1 == treeIterate(aTree));
#ifdef REVERSE_PRINT
    fprintf(stderr, "\n\treverse: ");
#endif
    assert(aNNodes - 1 == treeIterateReverse(aTree));
#ifdef VERBOSE
    fprintf(stderr, "\n");
#endif
}
Пример #3
0
static node_t *
treeReverseIterateCb(tree_t *aTree, node_t *aNode, void *data) {
    unsigned *i = (unsigned *)data;
    node_t *sNode;

    assert(aNode->magic == NODE_MAGIC);
    if (reverse_print) {
	if (*i != 0) {
	    fprintf(stderr, "<--");
	}
	fprintf(stderr, "%ld", aNode->key);
    }

    /* Test rb_search(). */
    sNode = tree_search(aTree, aNode);
    assert(sNode == aNode);

    /* Test rb_nsearch(). */
    sNode = tree_nsearch(aTree, aNode);
    assert(sNode == aNode);

    /* Test rb_psearch(). */
    sNode = tree_psearch(aTree, aNode);
    assert(sNode == aNode);

    (*i)++;

    return NULL;
}
Пример #4
0
static void
nodeRemove(tree_t *aTree, node_t *aNode, unsigned aNNodes) {
    node_t *sNode;
    unsigned blackHeight, imbalances;

    if (verbose) {
	fprintf(stderr, "rb_remove(%3ld)", aNode->key);
    }
    tree_remove(aTree, aNode);

    /* Test rb_nsearch(). */
    sNode = tree_nsearch(aTree, aNode);
    assert(sNode == NULL || sNode->key >= aNode->key);

    /* Test rb_psearch(). */
    sNode = tree_psearch(aTree, aNode);
    assert(sNode == NULL || sNode->key <= aNode->key);

    aNode->magic = 0;

    if (tree_print) {
	fprintf(stderr, "\n\t   tree: ");
    }
    rbtn_black_height(node_t, link, aTree, blackHeight);
    imbalances = treeRecurse(aTree->rbt_root, blackHeight, 0,
      &(aTree->rbt_nil));
    if (imbalances != 0) {
	fprintf(stderr, "\nTree imbalance\n");
	abort();
    }
    if (forward_print) {
	fprintf(stderr, "\n\tforward: ");
    }
    assert(aNNodes - 1 == treeIterate(aTree));
    if (reverse_print) {
	fprintf(stderr, "\n\treverse: ");
    }
    assert(aNNodes - 1 == treeIterateReverse(aTree));
    if (verbose) {
	fprintf(stderr, "\n");
    }
}
Пример #5
0
int
main(void) {
    tree_t tree;
    long set[NNODES];
    node_t nodes[NNODES], key, *sNode, *nodeA;
    unsigned i, j, k;

    fprintf(stderr, "Test begin\n");

    /* Initialize tree. */
    tree_new(&tree, 42);

    /*
     * Empty tree.
     */
    fprintf(stderr, "Empty tree:\n");

    /* trp_first(). */
    nodeA = tree_first(&tree);
    if (nodeA == NULL) {
	fprintf(stderr, "trp_first() --> nil\n");
    } else {
	fprintf(stderr, "trp_first() --> %ld\n", nodeA->key);
    }

    /* trp_last(). */
    nodeA = tree_last(&tree);
    if (nodeA == NULL) {
	fprintf(stderr, "trp_last() --> nil\n");
    } else {
	fprintf(stderr, "trp_last() --> %ld\n", nodeA->key);
    }

    /* trp_search(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_search(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "trp_search(0) --> nil\n");
    } else {
	fprintf(stderr, "trp_search(0) --> %ld\n", nodeA->key);
    }

    /* trp_nsearch(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_nsearch(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "trp_nsearch(0) --> nil\n");
    } else {
	fprintf(stderr, "trp_nsearch(0) --> %ld\n", nodeA->key);
    }

    /* trp_psearch(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_psearch(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "trp_psearch(0) --> nil\n");
    } else {
	fprintf(stderr, "trp_psearch(0) --> %ld\n", nodeA->key);
    }

    /* trp_insert(). */
    srandom(42);
    for (i = 0; i < NSETS; i++) {
	for (j = 0; j < NNODES; j++) {
	    set[j] = (long) (((double) NNODES)
	      * ((double) random() / ((double)RAND_MAX)));
	}

	for (j = 1; j <= NNODES; j++) {
#ifdef VERBOSE
	    fprintf(stderr, "Tree %u, %u node%s\n", i, j, j != 1 ? "s" : "");
#endif

	    /* Initialize tree and nodes. */
	    tree_new(&tree, 42);
	    for (k = 0; k < j; k++) {
		nodes[k].magic = NODE_MAGIC;
		nodes[k].key = set[k];
	    }

	    /* Insert nodes. */
	    for (k = 0; k < j; k++) {
#ifdef VERBOSE
		fprintf(stderr, "trp_insert(%3ld)", nodes[k].key);
#endif
		tree_insert(&tree, &nodes[k]);

#ifdef TREE_PRINT
		fprintf(stderr, "\n\t   tree: ");
#endif
#ifdef FORWARD_PRINT
		fprintf(stderr, "\n\tforward: ");
#endif
		assert(k + 1 == treeIterate(&tree));
#ifdef REVERSE_PRINT
		fprintf(stderr, "\n\treverse: ");
#endif
		assert(k + 1 == treeIterateReverse(&tree));
#ifdef VERBOSE
		fprintf(stderr, "\n");
#endif

		sNode = tree_first(&tree);
		assert(sNode != NULL);

		sNode = tree_last(&tree);
		assert(sNode != NULL);

		sNode = tree_next(&tree, &nodes[k]);
		sNode = tree_prev(&tree, &nodes[k]);
	    }

	    /* Remove nodes. */
	    switch (i % 4) {
		case 0: {
		    for (k = 0; k < j; k++) {
			nodeRemove(&tree, &nodes[k], j - k);
		    }
		    break;
		} case 1: {
		    for (k = j; k > 0; k--) {
			nodeRemove(&tree, &nodes[k-1], k);
		    }
		    break;
		} case 2: {
		    node_t *start;
		    unsigned nNodes = j;

		    start = NULL;
		    do {
			start = tree_iter(&tree, start, removeIterateCb,
			  (void *)&nNodes);
			nNodes--;
		    } while (start != NULL);
		    assert(nNodes == 0);
		    break;
		} case 3: {
		    node_t *start;
		    unsigned nNodes = j;

		    start = NULL;
		    do {
			start = tree_reverse_iter(&tree, start,
			  removeReverseIterateCb, (void *)&nNodes);
			nNodes--;
		    } while (start != NULL);
		    assert(nNodes == 0);
		    break;
		} default: {
		    assert(false);
		}
	    }
	}
    }

    fprintf(stderr, "Test end\n");
    return 0;
}
Пример #6
0
int
main(void) {
    tree_t tree;
    long set[NNODES];
    node_t nodes[NNODES], key, *sNode, *nodeA;
    unsigned i, j, k, blackHeight, imbalances;

    fprintf(stderr, "Test begin\n");

    /* Initialize tree. */
    tree_new(&tree);

    /*
     * Empty tree.
     */
    fprintf(stderr, "Empty tree:\n");

    /* rb_first(). */
    nodeA = tree_first(&tree);
    if (nodeA == NULL) {
	fprintf(stderr, "rb_first() --> nil\n");
    } else {
	fprintf(stderr, "rb_first() --> %ld\n", nodeA->key);
    }

    /* rb_last(). */
    nodeA = tree_last(&tree);
    if (nodeA == NULL) {
	fprintf(stderr, "rb_last() --> nil\n");
    } else {
	fprintf(stderr, "rb_last() --> %ld\n", nodeA->key);
    }

    /* rb_search(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_search(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "rb_search(0) --> nil\n");
    } else {
	fprintf(stderr, "rb_search(0) --> %ld\n", nodeA->key);
    }

    /* rb_nsearch(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_nsearch(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "rb_nsearch(0) --> nil\n");
    } else {
	fprintf(stderr, "rb_nsearch(0) --> %ld\n", nodeA->key);
    }

    /* rb_psearch(). */
    key.key = 0;
    key.magic = NODE_MAGIC;
    nodeA = tree_psearch(&tree, &key);
    if (nodeA == NULL) {
	fprintf(stderr, "rb_psearch(0) --> nil\n");
    } else {
	fprintf(stderr, "rb_psearch(0) --> %ld\n", nodeA->key);
    }

    /* rb_insert(). */
    srandom(42);
    for (i = 0; i < NSETS; i++) {
	if (i == 0) {
	    // Insert in order.
	    for (j = 0; j < NNODES; j++) {
		set[j] = j;
	    }
	} else if (i == 1) {
	    // Insert in reverse order.
	    for (j = 0; j < NNODES; j++) {
		set[j] = NNODES - j - 1;
	    }
	} else {
	    for (j = 0; j < NNODES; j++) {
		set[j] = (long) (((double) NNODES)
		  * ((double) random() / ((double)RAND_MAX)));
	    }
	}

	fprintf(stderr, "Tree %u\n", i);
	for (j = 1; j <= NNODES; j++) {
	    if (verbose) {
		fprintf(stderr, "Tree %u, %u node%s\n",
		  i, j, j != 1 ? "s" : "");
	    }

	    /* Initialize tree and nodes. */
	    tree_new(&tree);
	    tree.rbt_nil.magic = 0;
	    for (k = 0; k < j; k++) {
		nodes[k].magic = NODE_MAGIC;
		nodes[k].key = set[k];
	    }

	    /* Insert nodes. */
	    for (k = 0; k < j; k++) {
		if (verbose) {
		    fprintf(stderr, "rb_insert(%3ld)", nodes[k].key);
		}
		tree_insert(&tree, &nodes[k]);

		if (tree_print) {
		    fprintf(stderr, "\n\t   tree: ");
		}
		rbtn_black_height(node_t, link, &tree, blackHeight);
		imbalances = treeRecurse(tree.rbt_root, blackHeight, 0,
		  &(tree.rbt_nil));
		if (imbalances != 0) {
		    fprintf(stderr, "\nTree imbalance\n");
		    abort();
		}
		if (forward_print) {
		    fprintf(stderr, "\n\tforward: ");
		}
		assert(k + 1 == treeIterate(&tree));
		if (reverse_print) {
		    fprintf(stderr, "\n\treverse: ");
		}
		assert(k + 1 == treeIterateReverse(&tree));
		if (verbose) {
		    fprintf(stderr, "\n");
		}

		sNode = tree_first(&tree);
		assert(sNode != NULL);

		sNode = tree_last(&tree);
		assert(sNode != NULL);

		sNode = tree_next(&tree, &nodes[k]);
		sNode = tree_prev(&tree, &nodes[k]);
	    }

	    /* Remove nodes. */
	    switch (i % 4) {
		case 0: {
		    for (k = 0; k < j; k++) {
			nodeRemove(&tree, &nodes[k], j - k);
		    }
		    break;
		} case 1: {
		    for (k = j; k > 0; k--) {
			nodeRemove(&tree, &nodes[k-1], k);
		    }
		    break;
		} case 2: {
		    node_t *start;
		    unsigned nNodes = j;

		    start = NULL;
		    do {
			start = tree_iter(&tree, start, removeIterateCb,
			  (void *)&nNodes);
			nNodes--;
		    } while (start != NULL);
		    assert(nNodes == 0);
		    break;
		} case 3: {
		    node_t *start;
		    unsigned nNodes = j;

		    start = NULL;
		    do {
			start = tree_reverse_iter(&tree, start,
			  removeReverseIterateCb, (void *)&nNodes);
			nNodes--;
		    } while (start != NULL);
		    assert(nNodes == 0);
		    break;
		} default: {
		    assert(false);
		}
	    }
	}
    }

    fprintf(stderr, "Test end\n");
    return 0;
}