Ejemplo n.º 1
0
static void vector_4(void) {
    vector_t v;

    if (vector_init(&v, NULL, 0) < 0) {
        err_t * err = err_get();
        fprintf(stderr, "%s(%d): UNEXPECTED: %.*s\n",
                err_file(err), err_line(err), err_size(err),
		(const char *)err_data(err));
    }

    if (vector_init(&v, "my_vector", 0) < 0) {
	err_t * err = err_get();
        fprintf(stderr, "%s(%d): UNEXPECTED: %.*s\n",
                err_file(err), err_line(err), err_size(err),
		(const char *)err_data(err));
    }

    for (size_t i=VECTOR_ELEM_MIN; i<=VECTOR_ELEM_MAX; i+=3) {
        vector_init(&v, "my_vector", i);

        CU_ASSERT(vector_size(&v) == 0);
        CU_ASSERT(vector_pages(&v) == 0);
        CU_ASSERT(vector_capacity(&v) == 0);
        CU_ASSERT(vector_elem_size(&v) == i);
        CU_ASSERT(vector_elem_size(&v) * v.hdr.elem_num <= v.hdr.page_size);

        vector_size(&v, COUNT);

        CU_ASSERT(vector_size(&v) == COUNT);
        CU_ASSERT(vector_size(&v) <= vector_capacity(&v));
        CU_ASSERT(0 < vector_pages(&v));

        vector_delete(&v);
    }
}
Ejemplo n.º 2
0
static void vector_2(void) {
    vector_t v;

    CU_ASSERT(vector_init(&v, "my_vector.bin", 1) == 0);
    CU_ASSERT(vector_size(&v) == 0);
    CU_ASSERT(vector_pages(&v) == 0);
    CU_ASSERT(vector_capacity(&v) == 0);
    CU_ASSERT(vector_elem_size(&v) == 1);

    CU_ASSERT(vector_size(&v, COUNT) == COUNT);
    CU_ASSERT(COUNT <= vector_capacity(&v));
    CU_ASSERT(COUNT <= vector_size(&v));
    CU_ASSERT(3 <= vector_pages(&v));

    for (int i=0; i<COUNT; i++) {
        unsigned char c = (unsigned char)i;
        crc2 = clib_crc32(c, crc2);
        CU_ASSERT(vector_put(&v, i, &c) == 0);
    }
    CU_ASSERT_FATAL(crc2 != 0);

    FILE * f = fopen(v.hdr.name, "w");
    CU_ASSERT_FATAL(f != NULL);
    CU_ASSERT(COUNT < vector_save(&v, f));
    fclose(f), f = NULL;

    CU_ASSERT(vector_delete(&v) == 0);
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
	int *a, *b, *c, *d, *e, *f, *g;
	a = (int *)malloc(sizeof(int));
	b = (int *)malloc(sizeof(int));
	c = (int *)malloc(sizeof(int));
	d = (int *)malloc(sizeof(int));
	e = (int *)malloc(sizeof(int));
	f = (int *)malloc(sizeof(int));
	g = (int *)malloc(sizeof(int));
	*a = 6;
	*b = 1;
	*c = 99;
	*d = -17;
	*e = 22;
	*f = 9;
	*g = 6;

	struct Vector *iv = create_vector(2, free);
	vector_push_back(iv, a);
	vector_push_back(iv, b);
	vector_push_back(iv, c);
	vector_push_back(iv, d);
	vector_push_front(iv, e);
	vector_push_front(iv, f);
	vector_insert(iv, g, 5);

	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	printf("%d\n", *f);
	vector_remove(iv, f, compare, TRUE);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	vector_sort(iv, compare);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	printf("\nBsearch: %d, Lower: %d, Upper: %d\n", vector_bsearch(iv, a, compare), 
			vector_lower(iv, a, compare), vector_upper(iv, a, compare));

	vector_shuffle(iv);
	vector_foreach(iv, print);
	printf("\nCount: %d, Capacity: %d--------------\n", vector_count(iv), vector_capacity(iv));

	destroy_vector(iv, TRUE);
}
Ejemplo n.º 4
0
/*
 * Specifies a new size of a vector.
 */
void vector_resize(vector_t* pvec_vector, size_t t_resize)
{
    vector_iterator_t it_cutpos;     /* the cut position */
    size_t            t_expsize = 0;
    _byte_t*          pby_oldfinish = NULL;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    if(t_resize == vector_size(pvec_vector))
    {
        return;
    }
    else if(t_resize < vector_size(pvec_vector))
    {
        it_cutpos = vector_begin(pvec_vector);
        it_cutpos = iterator_next_n(it_cutpos, t_resize);
        vector_erase_range(pvec_vector, it_cutpos, vector_end(pvec_vector));
    }
    else
    {
        t_expsize = t_resize - vector_size(pvec_vector);
        if(t_resize > vector_capacity(pvec_vector))
        {
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_expsize));
        }

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        pvec_vector->_pby_finish += t_expsize * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
    }
}
Ejemplo n.º 5
0
static void vector_3(void) {
    vector_t v;
    vector_init(&v, __func__, 1);

    CU_ASSERT(vector_size(&v) == 0);
    CU_ASSERT(vector_pages(&v) == 0);
    CU_ASSERT(vector_capacity(&v) == 0);
    CU_ASSERT(vector_elem_size(&v) == 1);

    FILE * f = fopen("my_vector.bin", "r");
    CU_ASSERT_FATAL(f != NULL);
    CU_ASSERT(COUNT < vector_load(&v, f));
    fclose(f), f = NULL;

    crc3 = 0;

    for (int i=0; i<COUNT; i++) {
        unsigned char c;
        vector_get(&v, i, &c);
        crc3 = clib_crc32(c, crc3);
    }
    CU_ASSERT_FATAL(crc3 != 0);

    CU_ASSERT_FATAL(crc2 == crc3);

    vector_delete(&v);
}
Ejemplo n.º 6
0
void test4(void)
{
    vector_t vec = vector_from_string_list(NULL_VECTOR, "1920x1080,1280x720,720x576,720x480,704x576", NULL);
    vector_foreach(vec, test4_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_clear(vec);
    vector_destroy(vec);
}
Ejemplo n.º 7
0
/**
 * Set vector capacity.
 */
void vector_reserve(vector_t* pvec_vector, size_t t_reservesize)
{
    _byte_t* pby_reservemem = NULL; /* new memory for reserve */
    _byte_t* pby_newstart = NULL;
    _byte_t* pby_newfinish = NULL;
    _byte_t* pby_newendofstorage = NULL;
    _byte_t* pby_newpos = NULL;
    _byte_t* pby_oldpos = NULL;
    size_t   t_oldsize = 0;
    size_t   t_oldcapacity = 0;
    bool_t   b_result = false;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    if(vector_capacity(pvec_vector) < t_reservesize)
    {
        /* allocate the new vector with reserve size */
        pby_reservemem = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), t_reservesize);
        assert(pby_reservemem != NULL);
        /* get the new position */
        t_oldsize = pvec_vector->_pby_finish - pvec_vector->_pby_start;
        t_oldcapacity = pvec_vector->_pby_endofstorage - pvec_vector->_pby_start;
        pby_newstart = pby_reservemem;
        pby_newfinish = pby_reservemem + t_oldsize;
        pby_newendofstorage = pby_reservemem + _GET_VECTOR_TYPE_SIZE(pvec_vector) * t_reservesize;

        /* initialize new elements */
        _vector_init_elem_range_auxiliary(pvec_vector, pby_newstart, pby_newfinish);

        /* copy elements from old memory and destroy those */
        for(pby_newpos = pby_newstart, pby_oldpos = pvec_vector->_pby_start;
            pby_newpos < pby_newfinish && pby_oldpos < pvec_vector->_pby_finish;
            pby_newpos += _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_oldpos += _GET_VECTOR_TYPE_SIZE(pvec_vector))
        {
            /* copy from old vector_t memory */
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_newpos, pby_oldpos, &b_result);
            assert(b_result);
            /* destroy old vector_t memory */
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_DESTROY_FUNCTION(pvec_vector)(pby_oldpos, &b_result);
            assert(b_result);
        }
        assert(pby_newpos == pby_newfinish && pby_oldpos == pvec_vector->_pby_finish);

        /* free the old vector element */
        if(pvec_vector->_pby_start != NULL)
        {
            _alloc_deallocate(&pvec_vector->_t_allocator, pvec_vector->_pby_start,
                _GET_VECTOR_TYPE_SIZE(pvec_vector), t_oldcapacity / _GET_VECTOR_TYPE_SIZE(pvec_vector));
        }
        pvec_vector->_pby_start = pby_newstart;
        pvec_vector->_pby_finish = pby_newfinish;
        pvec_vector->_pby_endofstorage = pby_newendofstorage;
    }
}
Ejemplo n.º 8
0
void test_vector_reserve(void) {
  vector* v = vector_new();

  printf("vector size = %lu\n", vector_size(v));
  printf("vector capacity = %lu\n", vector_capacity(v));

  vector_reserve(v, 50);

  for(int i = 0; i < 100; ++i) {
    int* data = malloc(sizeof(int));
    *data = i;
    vector_push_back(v, data);
  }

  printf("vector size = %lu\n", vector_size(v));
  printf("vector capacity = %lu\n", vector_capacity(v));

  vector_free(v, NULL);
}
Ejemplo n.º 9
0
void test2(void)
{
    vector_t vec = vector_create2(0, 256);
    vector_set_option(vec, VECTOR_ELEMENT_COPY, (void*)strncpy);
    for (int i = 0; i < 10000; i++)
        vector_push_back(vec, "Hello, vector");
    vector_foreach(vec, test2_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_add_flags(vec, VECTOR_FLAG_FASTCLEAR);
    vector_clear(vec);
    vector_destroy(vec);
}
Ejemplo n.º 10
0
void test3(void)
{
    char buffer[8192];
    vector_t vec = vector_create_on_buffer(20, buffer, sizeof(buffer));
    vector_set_option(vec, VECTOR_ELEMENT_COPY, (void*)strncpy);
    for (int i = 0; i < 10000; i++)
        vector_push_back(vec, "Hello, vector on buffer");
    vector_foreach(vec, test2_walker, NULL);
    printf("size: %d, capacity: %d\n", vector_size(vec), vector_capacity(vec));
    vector_add_flags(vec, VECTOR_FLAG_FASTCLEAR);
    vector_clear(vec);
    vector_destroy(vec);
}
Ejemplo n.º 11
0
/**
 * Reset the size of vector elements, and filled element is from variable argument list.
 */
void _vector_resize_elem_varg(vector_t* pvec_vector, size_t t_resize, va_list val_elemlist)
{
    vector_iterator_t t_cutpos;     /* the cut position */
    size_t            t_expsize = 0;
    size_t            i = 0;
    void*             pv_varg = NULL;
    _byte_t*          pby_oldfinish = NULL;
    bool_t            b_result = false;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    if(t_resize == vector_size(pvec_vector))
    {
        return;
    }
    else if(t_resize < vector_size(pvec_vector))
    {
        t_cutpos = vector_begin(pvec_vector);
        t_cutpos = iterator_next_n(t_cutpos, t_resize);
        vector_erase_range(pvec_vector, t_cutpos, vector_end(pvec_vector));
    }
    else
    {
        t_expsize = t_resize - vector_size(pvec_vector);
        if(t_resize > vector_capacity(pvec_vector))
        {
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_expsize));
        }

        /* get varg value only once */
        pv_varg = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
        assert(pv_varg != NULL);
        _vector_get_varg_value_auxiliary(pvec_vector, val_elemlist, pv_varg);

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        pvec_vector->_pby_finish += t_expsize * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
        /* copy value from varg to new elements */
        for(i = 0; i < t_expsize; ++i)
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_oldfinish + i * _GET_VECTOR_TYPE_SIZE(pvec_vector), pv_varg, &b_result);
            assert(b_result);
        }
        /* destroy varg */
        _vector_destroy_varg_value_auxiliary(pvec_vector, pv_varg);
        _alloc_deallocate(&pvec_vector->_t_allocator, pv_varg, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
    }
}
Ejemplo n.º 12
0
const void *vector_iter_elem(vector_iter_t * self)
{
    assert(self != NULL);

    if (vector_capacity(self->vector) <= self->idx) {
        UNEXPECTED("'%d' index out-of-range", self->idx);
        return NULL;
    }

    if (vector_size(self->vector) <= self->idx)
        return NULL;

    return vector_at(self->vector, self->idx);
}
Ejemplo n.º 13
0
/**
 * Add specificed element from variable argument list at the end of vector container. 
 */
void _vector_push_back_varg(vector_t* pvec_vector, va_list val_elemlist)
{
    _byte_t*  pby_last = NULL;

    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));

    /* if the remain capacity is less then the element count */
    if (vector_capacity(pvec_vector) == vector_size(pvec_vector)) {
        vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), 1));
    }

    /* initialize the last element */
    pby_last = pvec_vector->_pby_finish;
    assert(pby_last != NULL);
    pvec_vector->_pby_finish += _GET_VECTOR_TYPE_SIZE(pvec_vector);
    _vector_init_elem_auxiliary(pvec_vector, pby_last);
    /* copy value from varg */
    _type_get_varg_value(&pvec_vector->_t_typeinfo, val_elemlist, pby_last);
}
Ejemplo n.º 14
0
static void vector_1(void) {
    vector_t v;

    CU_ASSERT(vector_init(&v, "my_vector", 0) == -1)
    err_t * err = err_get();
    fprintf(stderr, "%s(%d): %.*s\n",
            err_file(err), err_line(err), err_size(err),
            (const char *)err_data(err));

    for (size_t i=VECTOR_ELEM_MIN; i<=VECTOR_ELEM_MAX; i++) {
        CU_ASSERT(vector_init(&v, "my_vector", i) == 0);

        CU_ASSERT(vector_size(&v) == 0);
        CU_ASSERT(vector_pages(&v) == 0);
        CU_ASSERT(vector_capacity(&v) == 0);
        CU_ASSERT(vector_elem_size(&v) == i);
        CU_ASSERT(vector_elem_size(&v) * v.hdr.elem_num <= v.hdr.page_size);

        CU_ASSERT(vector_delete(&v) == 0);
    }
}
Ejemplo n.º 15
0
void fit_praxis(void) {
	extern Symbol* hoc_lookup();
	extern char* gargstr();
	char* after_quad;	
	int i;
	double err, fmin;
	double* px;
	/* allow nested calls to fit_praxis. I.e. store all the needed
	   statics specific to this invocation with proper reference
	   counting and then unref/destoy on exit from this invocation.
	   Before the prax call save the statics from earlier invocation
	   without increasing the
	   ref count and on exit restore without decreasing the ref count.
	*/
	   
	/* save before setting statics, restore after prax */
	double minerrsav, *minargsav, maxstepsizesav, tolerancesav;
	long int printmodesav, nvarsav;
	Symbol* efun_sym_sav;
	Object* efun_py_save, *efun_py_arg_save;
	void* vec_py_save_save;
	
	/* store statics specified by this invocation */
	/* will be copied just before calling prax */
	double minerr_, *minarg_;
	long int nvar_;
	Symbol* efun_sym_;
	Object* efun_py_, *efun_py_arg_;
	void* vec_py_save_;

	minerr_ = 0.0;
	nvar_ = 0;
	minarg_ = NULL;
	efun_sym_ = NULL;
	efun_py_ = NULL;
	efun_py_arg_ = NULL;
	vec_py_save_ = NULL;
	
	fmin = 0.;

    if (hoc_is_object_arg(1)) {
	assert(nrnpy_praxis_efun);
	efun_py_ = *hoc_objgetarg(1);
	hoc_obj_ref(efun_py_);
	efun_py_arg_ = *vector_pobj(vector_arg(2));
	hoc_obj_ref(efun_py_arg_);
	vec_py_save_ = vector_new2(efun_py_arg_->u.this_pointer);
	nvar_ = vector_capacity(vec_py_save_);
	px = vector_vec(vec_py_save_);
    }else{
	nvar_ = (int)chkarg(1, 0., 1e6);
	efun_sym_ = hoc_lookup(gargstr(2));
	if (!efun_sym_
	   || (efun_sym_->type != FUNCTION
	      && efun_sym_->type != FUN_BLTIN)) {
		hoc_execerror(gargstr(2), "not a function name");
	}
	
	if (!hoc_is_pdouble_arg(3)) {
		void* vec = vector_arg(3);
		if (vector_capacity(vec) != nvar_) {
			hoc_execerror("first arg not equal to size of Vector",0);
		}
		px = vector_vec(vec);
	}else{
		px = hoc_pgetarg(3);
	}
    }
	minarg_ = (double*)ecalloc(nvar_, sizeof(double));

	if (maxstepsize == 0.) {
		hoc_execerror("call attr_praxis first to set attributes", 0);
	}
	machep = 1e-15;
		
	if (ifarg(4)) {
		after_quad = gargstr(4);
	}else{
		after_quad = (char*)0;
	}

	/* save the values set by earlier invocation */
	minerrsav = minerr;
	minargsav = minarg;
	tolerancesav = tolerance;
	maxstepsizesav = maxstepsize;
	printmodesav = printmode;
	nvarsav = nvar;
	efun_sym_sav = hoc_efun_sym;
	efun_py_save = efun_py;
	efun_py_arg_save = efun_py_arg;
	vec_py_save_save = vec_py_save;


	/* copy this invocation values to the statics */
	minerr = minerr_;
	minarg = minarg_;
	nvar = nvar_;
	hoc_efun_sym = efun_sym_;
	efun_py = efun_py_;
	efun_py_arg = efun_py_arg_;
	vec_py_save = vec_py_save_;
	

	minerr=1e9;
	err = praxis(&tolerance, &machep, &maxstepsize,	nvar, &printmode,
		px, efun, &fmin, after_quad);
	err = minerr;
	if (minerr < 1e9) {
		for (i=0; i<nvar; ++i) { px[i] = minarg[i]; }
	}

	/* restore the values set by earlier invocation */
	minerr = minerrsav;
	minarg = minargsav;
	tolerance = tolerancesav;
	maxstepsize = maxstepsizesav;
	printmode = printmodesav;
	nvar = nvar_; /* in case one calls prax_pval */
	hoc_efun_sym = efun_sym_sav;
	efun_py = efun_py_save;
	efun_py_arg = efun_py_arg_save;
	vec_py_save = vec_py_save_save;

	if (efun_py_) {
		double* px = vector_vec(efun_py_arg_->u.this_pointer);
		for (i=0; i < nvar_; ++i) {
			px[i] = minarg_[i];
		}
		hoc_obj_unref(efun_py_);
		hoc_obj_unref(efun_py_arg_);
		vector_delete(vec_py_save_);
	}
	if (minarg_) {
		free(minarg_);
	}
	hoc_retpushx(err);
}
Ejemplo n.º 16
0
void test__vector_init_elem__vector_init_elem_varg__non_created(void** state)
{
    vector_t vec;
    vec._pby_start = (_byte_t*)0x89;

    expect_assert_failure(_vector_init_elem(&vec, 10, 100));
}

void test__vector_init_elem__vector_init_elem_varg__successfully_0_count(void** state)
{
    vector_t* pvec = create_vector(int);

    _vector_init_elem(pvec, 0, 100);
    assert_true(vector_size(pvec) == 0);
    assert_true(vector_capacity(pvec) == 0);

    vector_destroy(pvec);
}

void test__vector_init_elem__vector_init_elem_varg__successfully(void** state)
{
    size_t i = 0;
    vector_t* pvec = create_vector(int);

    _vector_init_elem(pvec, 8, 100);
    assert_true(vector_size(pvec) == 8);
    for(i = 0; i < vector_size(pvec); ++i)
    {
        assert_true(*(int*)vector_at(pvec, i) == 100);
    }
Ejemplo n.º 17
0
/**
 * Insert multiple copys of element befor specificed position, the element is from variable argument list.
 */
vector_iterator_t _vector_insert_n_varg(vector_t* pvec_vector, vector_iterator_t it_pos, size_t t_count, va_list val_elemlist)
{
    void*  pv_varg = NULL;
    bool_t b_result = false;

    /* test the vector and iterator is valid */
    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_pos));

    if (t_count > 0) {
        size_t i = 0;
        _byte_t*  pby_oldfinish = NULL;
        _byte_t*  pby_pos = NULL;   /* for initialize elments and insert elements */
        _byte_t*  pby_destpos = NULL;

        /* if the remain capacity is less then the element count */
        if (vector_size(pvec_vector) + t_count > vector_capacity(pvec_vector)) {
            size_t t_insertpos = iterator_distance(vector_begin(pvec_vector), it_pos);
            /* reserve the new size */
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_count));
            it_pos = iterator_next_n(vector_begin(pvec_vector), t_insertpos);
        }

        /* insert the element counts of element to the position */
        /* 
         * move the elements from pos to the finish of vector to the 
         * memory that after the pos element count.
         * +-----------------------------------------+
         * |          |             |                |
         * +-----------------------------------------+
         * ^          ^             ^
         * |          |             |
         * start     it_pos         finish
         *                           _t_count__
         *                          /          \
         * +-----------------------------------------+
         * |          |             |new element|    |
         * +-----------------------------------------+
         * ^          ^             ^           ^
         * |          |             |           |
         * start     it_pos       pby_oldfinish  finish
         */
        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        assert(pby_oldfinish != NULL);
        pvec_vector->_pby_finish += t_count * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);
        /* move element from old finish to new finish */
        for (pby_pos = pby_oldfinish - _GET_VECTOR_TYPE_SIZE(pvec_vector),
             pby_destpos = pvec_vector->_pby_finish - _GET_VECTOR_TYPE_SIZE(pvec_vector);
             pby_pos >= _VECTOR_ITERATOR_COREPOS(it_pos);
             pby_pos -= _GET_VECTOR_TYPE_SIZE(pvec_vector),
             pby_destpos -= _GET_VECTOR_TYPE_SIZE(pvec_vector)) {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_destpos, pby_pos, &b_result);
            assert(b_result);
        }

        /* get varg value only once */
        pv_varg = _alloc_allocate(&pvec_vector->_t_allocator, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
        assert(pv_varg != NULL);
        _vector_get_varg_value_auxiliary(pvec_vector, val_elemlist, pv_varg);
        /* copy value for varg */
        for (i = 0; i < t_count; ++i) {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_pos) + i * _GET_VECTOR_TYPE_SIZE(pvec_vector), pv_varg, &b_result);
            assert(b_result);
        }
        /* destroy varg and free memory */
        _vector_destroy_varg_value_auxiliary(pvec_vector, pv_varg);
        _alloc_deallocate(&pvec_vector->_t_allocator, pv_varg, _GET_VECTOR_TYPE_SIZE(pvec_vector), 1);
    }

    return it_pos;
}
Ejemplo n.º 18
0
TEST(VectorTest, AccessorsManipulators) {
	status_t err = NO_ERROR;
	vector_handle_t vector = NULL;
	size_t count = 0;
	size_t capacity = 0;
	char element = 0;
	char* p_element = NULL;

	err = vector_create(1, 1, &vector);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(vector != NULL);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(capacity, (size_t)1);


	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_append(vector, &element);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)1);

	err = vector_pop(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	for (element = 0; element < 10; element++) {
		err = vector_append(vector, &element);
		ASSERT_EQ(err, NO_ERROR);
	}

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)10);

	err = vector_capacity(vector, &capacity);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_GT(capacity, (size_t)1);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)5);

	err = vector_remove(vector, 5);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_element_address(vector, 5, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);
	ASSERT_EQ(*p_element, (char)6);

	err = vector_count(vector, &count);
	while ((err == NO_ERROR) && (count > 4)) {
		err = vector_pop(vector);
		ASSERT_EQ(err, NO_ERROR);
		err = vector_count(vector, &count);
	}

	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)4);

	err = vector_remove(vector, 6);
	ASSERT_NE(err, NO_ERROR);

	err = vector_array(vector, (void**)&p_element);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_TRUE(NULL != p_element);

	element = 0;
	while (element < 4) {
		ASSERT_EQ(*p_element, element);
		p_element++;
		element++;
	}

	err = vector_clear(vector);
	ASSERT_EQ(err, NO_ERROR);

	err = vector_count(vector, &count);
	ASSERT_EQ(err, NO_ERROR);
	ASSERT_EQ(count, (size_t)0);

	err = vector_pop(vector);
	ASSERT_NE(err, NO_ERROR);

	vector_release(vector);
}
Ejemplo n.º 19
0
/**
 * Insert a range of elements into vector at a specificed position.
 */
void vector_insert_range(vector_t* pvec_vector, vector_iterator_t it_pos, vector_iterator_t it_begin, vector_iterator_t it_end)
{
    size_t            t_count = 0; /* the element count */
    bool_t            b_result = false;
    vector_iterator_t it_first;
    vector_iterator_t it_second;
    _byte_t*          pby_oldfinish = NULL;
    _byte_t*          pby_pos = NULL;
    _byte_t*          pby_destpos = NULL;

    /* test the vector and iterator is valid */
    assert(pvec_vector != NULL);
    assert(_vector_is_inited(pvec_vector));
    assert(_vector_iterator_belong_to_vector(pvec_vector, it_pos));
    /*assert(!_vector_iterator_belong_to_vector(pvec_vector, it_begin));*/
    /*assert(!_vector_iterator_belong_to_vector(pvec_vector, it_end));*/
    assert(_vector_same_vector_iterator_type(pvec_vector, it_begin));
    assert(_vector_same_vector_iterator_type(pvec_vector, it_end));
    assert(iterator_equal(it_begin, it_end) || _vector_iterator_before(it_begin, it_end));

    t_count = iterator_distance(it_begin, it_end);
    if(t_count > 0)
    {
        /* if the remain capacity is less then the element count */
        if(vector_size(pvec_vector) + t_count > vector_capacity(pvec_vector))
        {
            size_t t_distance = _VECTOR_ITERATOR_COREPOS(it_pos) - pvec_vector->_pby_start;
            /* reserve the new size */
            vector_reserve(pvec_vector, _vector_calculate_new_capacity(vector_size(pvec_vector), t_count));
            _VECTOR_ITERATOR_COREPOS(it_pos) = pvec_vector->_pby_start + t_distance;
        }

        /* initialize new elements */
        pby_oldfinish = pvec_vector->_pby_finish;
        assert(pby_oldfinish != NULL);
        pvec_vector->_pby_finish += t_count * _GET_VECTOR_TYPE_SIZE(pvec_vector);
        _vector_init_elem_range_auxiliary(pvec_vector, pby_oldfinish, pvec_vector->_pby_finish);

        /* move element from old finish to new finish */
        for(pby_pos = pby_oldfinish - _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_destpos = pvec_vector->_pby_finish - _GET_VECTOR_TYPE_SIZE(pvec_vector);
            pby_pos >= _VECTOR_ITERATOR_COREPOS(it_pos);
            pby_pos -= _GET_VECTOR_TYPE_SIZE(pvec_vector),
            pby_destpos -= _GET_VECTOR_TYPE_SIZE(pvec_vector))
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(pby_destpos, pby_pos, &b_result);
            assert(b_result);
        }

        /* insert element counts copys to the pos */
        for(it_first = it_pos, it_second = it_begin;
            !iterator_equal(it_second, it_end);
            it_first = iterator_next(it_first), it_second = iterator_next(it_second))
        {
            b_result = _GET_VECTOR_TYPE_SIZE(pvec_vector);
            _GET_VECTOR_TYPE_COPY_FUNCTION(pvec_vector)(_VECTOR_ITERATOR_COREPOS(it_first), _VECTOR_ITERATOR_COREPOS(it_second), &b_result);
            assert(b_result);
        }
        assert(_VECTOR_ITERATOR_COREPOS(it_first) == _VECTOR_ITERATOR_COREPOS(it_pos) + 
               (_VECTOR_ITERATOR_COREPOS(it_end) - _VECTOR_ITERATOR_COREPOS(it_begin)));
    }
}