Example #1
0
static inline void itree_free(itnode *n) {
	if (n) {
		itree_free(n->left);
		itree_free(n->right);
		free(n);
	}
}
END_TEST

START_TEST(test_insert_into_empty)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(holes == 0);
    CHK_NODE(root, == 10, == 10, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
END_TEST

START_TEST(test_insert_not_adjacentr)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(itree_insert(12, &root, &holes) == 0);
    fail_unless(holes == 0);
    CHK_NODE(root, == 10, == 10, == 1, == NULL, != NULL, == 1);
    itree_t *r = root->r;
    CHK_NODE(r, == 12, == 12, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
END_TEST

START_TEST(test_insert_not_adjacentl)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(itree_insert(8, &root, &holes) == 0);
    fail_unless(holes == 1);
    CHK_NODE(root, == 10, == 10, == 0, != NULL, == NULL, == 1);
    itree_t *l = root->l;
    CHK_NODE(l, == 8, == 8, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
int
stackdist_process_trace(const uint64_t *p,
                        const uint64_t n,
                        const stackdist_cb fn)
{
    int ret = 0;
    hash_t *hash = NULL;
    itree_t *itree = NULL;
    
    hash = hash_init();
    if (hash == NULL) {
        goto out;
    }

    for(uint64_t i = 0; i < n; i++) {
        const uint64_t addr = p[i];

        /* Search. */

        uint64_t last;
        if ((ret = hash_insert(hash, addr, i, &last)) != 0) {
            goto out;
        }

        if (last == HASH_NOT_FOUND) {
            fn(i, STACKDIST_INFINITE);
            continue;
        }
        
        uint64_t holes;
        if ((ret = itree_insert(last, &itree, &holes)) != 0) {
            goto out;
        }

        const uint64_t dist = i - last - holes - 1;
        fn(i, dist);
    }
out:
    if (hash != NULL) {
        hash_free(hash);
    }

    if (itree != NULL) {
        itree_free(itree);
    }

    return ret;
}
END_TEST

START_TEST(test_merge_l_in_l_subtree)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(itree_insert(8, &root, &holes) == 0);
    fail_unless(itree_insert(12, &root, &holes) == 0);
    fail_unless(itree_insert(6, &root, &holes) == 0);
    fail_unless(itree_insert(9, &root, &holes) == 0);
    fail_unless(holes == 2);
    CHK_NODE(root, == 8, == 10, == 1, != NULL, != NULL, == 1);
    CHK_NODE(root->l, == 6, == 6, == 0, == NULL, == NULL, == 0);
    CHK_NODE(root->r, == 12, == 12, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
END_TEST

START_TEST(test_insert_balance2)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(15, &root, &holes) == 0);
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(itree_insert(13, &root, &holes) == 0);
    fail_unless(holes == 1);
    CHK_NODE(root, == 13, == 13, == 1, != NULL, != NULL, == 1);
    itree_t *l = root->l;
    CHK_NODE(l, == 10, == 10, == 0, == NULL, == NULL, == 0);
    itree_t *r = root->r;
    CHK_NODE(r, == 15, == 15, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
END_TEST

START_TEST(test_insert_adjacent2_balance1)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(15, &root, &holes) == 0);
    fail_unless(itree_insert(9, &root, &holes) == 0);
    fail_unless(itree_insert(13, &root, &holes) == 0);
    fail_unless(itree_insert(11, &root, &holes) == 0);
    fail_unless(holes == 2);
    CHK_NODE(root, == 13, == 13, == 1, != NULL, != NULL, == 2);
    itree_t *l = root->l;
    CHK_NODE(l, == 9, == 9, == 1, == NULL, != NULL, == 1);
    itree_t *lr = l->r;
    CHK_NODE(lr, == 11, == 11, == 0, == NULL, == NULL, == 0);
    itree_t *r = root->r;
    CHK_NODE(r, == 15, == 15, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
END_TEST

START_TEST(test_merge_l_in_r_subtree)
{
    itree_t *root = NULL;
    uint64_t holes;
    fail_unless(itree_insert(10, &root, &holes) == 0);
    fail_unless(itree_insert(5, &root, &holes) == 0);
    fail_unless(itree_insert(16, &root, &holes) == 0);
    fail_unless(itree_insert(3, &root, &holes) == 0);
    fail_unless(itree_insert(12, &root, &holes) == 0);
    fail_unless(itree_insert(18, &root, &holes) == 0);
    fail_unless(itree_insert(14, &root, &holes) == 0);
    fail_unless(itree_insert(11, &root, &holes) == 0);
    fail_unless(holes == 4);
    CHK_NODE(root, == 10, == 12, == 3, != NULL, != NULL, == 2);
    CHK_NODE(root->l, == 5, == 5, == 0, != NULL, == NULL, == 1);
    CHK_NODE(root->l->l, == 3, == 3, == 0, == NULL, == NULL, == 0);
    CHK_NODE(root->r, == 16, == 16, == 1, != NULL, != NULL, == 1);
    CHK_NODE(root->r->l, == 14, == 14, == 0, == NULL, == NULL, == 0);
    CHK_NODE(root->r->r, == 18, == 18, == 0, == NULL, == NULL, == 0);
    itree_free(root);
}
Example #10
0
void itree_freeall(void *o) {
	itree_free((itnode*)o);
}