void test__rb_tree_iterator_before__invalid_second(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;

    _rb_tree_init(pt_rb_tree, NULL);
    it_first = _rb_tree_begin(pt_rb_tree);
    it_second = _rb_tree_begin(pt_rb_tree);
    it_second._t_pos._t_treepos._pby_corepos = NULL;
    expect_assert_failure(_rb_tree_iterator_before(it_first, it_second));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_before__equal(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;
    int elem = 100;

    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_first = _rb_tree_begin(pt_rb_tree);
    it_second = _rb_tree_begin(pt_rb_tree);
    assert_false(_rb_tree_iterator_before(it_first, it_second));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_distance__0(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;
    int elem = 100;

    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_first = _rb_tree_begin(pt_rb_tree);
    it_second = _rb_tree_begin(pt_rb_tree);
    assert_true(_rb_tree_iterator_distance(it_first, it_second) == 0);

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_before__not_same_rb_tree(void** state)
{
    _rb_tree_t* pt_first = _create_rb_tree("int");
    _rb_tree_t* pt_second = _create_rb_tree("int");
    _rb_tree_iterator_t it_first;
    _rb_tree_iterator_t it_second;

    _rb_tree_init(pt_first, NULL);
    _rb_tree_init(pt_second, NULL);
    it_first = _rb_tree_begin(pt_first);
    it_second = _rb_tree_begin(pt_second);
    expect_assert_failure(_rb_tree_iterator_before(it_first, it_second));

    _rb_tree_destroy(pt_first);
    _rb_tree_destroy(pt_second);
}
void test__rb_tree_iterator_get_pointer__c_builtin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 12;
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(*(int*)_rb_tree_iterator_get_pointer(it_iter) == 12);

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_prev__null_tree(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    _rb_tree_init(pt_rb_tree, NULL);

    it_iter = _rb_tree_begin(pt_rb_tree);

    it_iter._t_pos._t_treepos._pt_tree = NULL;
    expect_assert_failure(_rb_tree_iterator_prev(it_iter));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_get_value__null_tree(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 0;
    _rb_tree_init(pt_rb_tree, NULL);

    it_iter = _rb_tree_begin(pt_rb_tree);

    it_iter._t_pos._t_treepos._pt_tree = NULL;
    expect_assert_failure(_rb_tree_iterator_get_value(it_iter, &elem));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_prev__begin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 100;
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);

    expect_assert_failure(_rb_tree_iterator_prev(it_iter));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_get_pointer__libcstl_builtin(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("vector_t<int>");
    _rb_tree_iterator_t it_iter;
    vector_t* pvec = create_vector(int);
    _rb_tree_init(pt_rb_tree, NULL);
    vector_init_n(pvec, 10);
    _rb_tree_insert_unique(pt_rb_tree, pvec);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(vector_size((vector_t*)_rb_tree_iterator_get_pointer(it_iter)) == 10);

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_get_pointer__cstr(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("char*");
    _rb_tree_iterator_t it_iter;
    string_t* pstr = create_string();
    string_init_cstr(pstr, "abc");
    _rb_tree_init(pt_rb_tree, NULL);
    _rb_tree_insert_unique(pt_rb_tree, pstr);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(strcmp((char*)_rb_tree_iterator_get_pointer(it_iter), "abc") == 0);

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_prev__end(void** state)
{
    _rb_tree_t* pt_rb_tree = _create_rb_tree("int");
    _rb_tree_iterator_t it_iter;
    int elem = 10;
    _rb_tree_init(pt_rb_tree, NULL);

    _rb_tree_insert_unique(pt_rb_tree, &elem);
    it_iter = _rb_tree_end(pt_rb_tree);

    it_iter = _rb_tree_iterator_prev(it_iter);
    assert_true(_rb_tree_iterator_equal(it_iter, _rb_tree_begin(pt_rb_tree)));

    _rb_tree_destroy(pt_rb_tree);
}
void test__rb_tree_iterator_get_pointer__user_define(void** state)
{
    _rb_tree_t* pt_rb_tree = NULL;
    _rb_tree_iterator_t it_iter;
    _test__rb_tree_iterator_get_pointer__user_define_t elem;

    type_register(_test__rb_tree_iterator_get_pointer__user_define_t, NULL, NULL, NULL, NULL);
    pt_rb_tree = _create_rb_tree("_test__rb_tree_iterator_get_pointer__user_define_t");
    _rb_tree_init(pt_rb_tree, NULL);
    elem.elem = 100;
    _rb_tree_insert_unique(pt_rb_tree, &elem);

    it_iter = _rb_tree_begin(pt_rb_tree);
    assert_true(((_test__rb_tree_iterator_get_pointer__user_define_t*)_rb_tree_iterator_get_pointer(it_iter))->elem == 100);

    _rb_tree_destroy(pt_rb_tree);
}
Esempio n. 13
0
multimap_iterator_t multimap_begin(const multimap_t* cpt_multimap)
{
    multimap_iterator_t t_newiterator;

    assert(cpt_multimap != NULL);

#ifdef CSTL_MULTIMAP_AVL_TREE
    t_newiterator = _avl_tree_begin(_GET_MULTIMAP_AVL_TREE(cpt_multimap));
#else
    t_newiterator = _rb_tree_begin(_GET_MULTIMAP_RB_TREE(cpt_multimap));
#endif

    _GET_CONTAINER(&t_newiterator) = (multimap_t*)cpt_multimap;
    _GET_MULTIMAP_CONTAINER_TYPE(&t_newiterator) = _MULTIMAP_CONTAINER;
    _GET_MULTIMAP_ITERATOR_TYPE(&t_newiterator) = _BIDIRECTIONAL_ITERATOR;

    return t_newiterator;
}
Esempio n. 14
0
/**
 * Return an iterator that addresses the first element in the set.
 */
set_iterator_t set_begin(const set_t* cpset_set)
{
    set_iterator_t it_begin;

    assert(cpset_set != NULL);

#ifdef CSTL_SET_AVL_TREE
    it_begin = _avl_tree_begin(&cpset_set->_t_tree);
#else
    it_begin = _rb_tree_begin(&cpset_set->_t_tree);
#endif

    _ITERATOR_CONTAINER(it_begin) = (set_t*)cpset_set;
    _SET_ITERATOR_CONTAINER_TYPE(it_begin) = _SET_CONTAINER;
    _SET_ITERATOR_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;

    return it_begin;
}
Esempio n. 15
0
/**
 * Return iterator reference previous element.
 */
_rb_tree_iterator_t _rb_tree_iterator_prev(_rb_tree_iterator_t it_iter)
{
    _rbnode_t* pt_prev = NULL;
    _rbnode_t* pt_cur = (_rbnode_t*)_RB_TREE_ITERATOR_COREPOS(it_iter);

    assert(_rb_tree_iterator_belong_to_rb_tree(_RB_TREE_ITERATOR_TREE(it_iter), it_iter));
    assert(!_rb_tree_iterator_equal(it_iter, _rb_tree_begin(_RB_TREE_ITERATOR_TREE(it_iter))));

    /* previous end */
    if(_rb_tree_iterator_equal(it_iter, _rb_tree_end(_RB_TREE_ITERATOR_TREE(it_iter))))
    {
        _RB_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)_RB_TREE_ITERATOR_TREE(it_iter)->_t_rbroot._pt_right;
    }
    else
    {
        if(pt_cur->_pt_left != NULL)
        {
            pt_prev = pt_cur->_pt_left;
            while(pt_prev->_pt_right != NULL)
            {
                pt_prev = pt_prev->_pt_right;
            }

            _RB_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_prev;
        }
        else
        {
            pt_prev = pt_cur->_pt_parent;
            while(pt_cur == pt_prev->_pt_left)
            {
                pt_cur = pt_prev;
                pt_prev = pt_prev->_pt_parent;
            }

            /* 
             * there is no same special condition as next, because the begin 
             * iterator is the valid iterator 
             */
            _RB_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)pt_prev;
        }
    }

    return it_iter;
}
Esempio n. 16
0
/**
 * Return an iterator that addresses the first element in the map.
 */
map_iterator_t map_begin(const map_t* cpmap_map)
{
    map_iterator_t it_begin;

    assert(cpmap_map != NULL);
    assert(_pair_is_inited(&cpmap_map->_pair_temp));

#ifdef CSTL_MAP_AVL_TREE
    it_begin = _avl_tree_begin(&cpmap_map->_t_tree);
#else
    it_begin = _rb_tree_begin(&cpmap_map->_t_tree);
#endif

    _GET_CONTAINER(it_begin) = (map_t*)cpmap_map;
    _GET_MAP_CONTAINER_TYPE(it_begin) = _MAP_CONTAINER;
    _GET_MAP_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;

    return it_begin;
}