Exemple #1
0
END_TEST

START_TEST (append_should_grown_if_needed)
{

  int num1 = 10, num2 = 20, num3 = 30, num4 = 40;
  int *found1, *found2, *found3, *found4;

  vector *v = vector_new(sizeof(int *), NULL, 2);
  vector_append(v, &num1);
  vector_append(v, &num2);
  vector_append(v, &num3);   /* grow before append */
  vector_append(v, &num4);

  found1 = vector_get(v, 0);
  found2 = vector_get(v, 1);
  found3 = vector_get(v, 2);
  found4 = vector_get(v, 3);

  fail_unless(vector_length(v) == 4, "length should be 4");
  fail_unless(v->alloc_length == 4, ".alloc_length should be 10");
  fail_unless(*found1 == 10, "element 0 should be 10 not %d", *found1);
  fail_unless(*found2 == 20, "element 1 should be 20 not %d", *found2);
  fail_unless(*found3 == 30, "element 2 should be 30 not %d", *found3);
  fail_unless(*found4 == 40, "element 3 should be 40 not %d", *found4);

  vector_free(v);
}
void generate_debug_geometry(vector_buffer *vertices, vector_buffer *normals, geometry* out)
{
    float temp[3];

    vector_buffer debug;
    vector_init(&debug);

    for(int i = 0; i < out->vertex_count; ++i)
    {
        vec3_scale(vector_get(normals, i), 0.05f, temp);
        vec3_add(vector_get(vertices, i), temp, temp);

        vector_append(&debug, vector_get(vertices, i));
        vector_append(&debug, temp);
    }

    out->debug_geometry.attributes.position = 3;
    make_buffer(out->debug_geometry.attributes.position, &out->debug_geometry.vertex_buffer, debug.data , (GLsizei) (out->vertex_count * 3 * 2 * sizeof(float)));

    out->debug_geometry.vertex_shader = make_shader(GL_VERTEX_SHADER, "shaders/debug_shader.v.glsl");
    out->debug_geometry.fragment_shader = make_shader(GL_FRAGMENT_SHADER, "shaders/debug_shader.f.glsl");

    out->debug_geometry.program = make_program(out->debug_geometry.vertex_shader, out->debug_geometry.fragment_shader);

    glBindAttribLocation(out->debug_geometry.program, out->debug_geometry.attributes.position, "position");

    glLinkProgram(out->debug_geometry.program);

    out->debug_geometry.uniform.mvp_matrix = glGetUniformLocation(out->program, "mvp_matrix");
}
Exemple #3
0
END_TEST

START_TEST (delete_element_should_shift_elements)
{

  char *sport1 = strdup("winsurf");
  char *sport2 = strdup("kitesurf");
  char *sport3 = strdup("motocross");
  char *sport4 = strdup("surf");

  vector *v = vector_new(sizeof(char *), free_string, 5);
  vector_append(v, &sport1);
  vector_append(v, &sport2);
  vector_append(v, &sport3);
  vector_append(v, &sport4);

  fail_unless(vector_delete(v, 1) == VECT_OK);
  fail_unless(vector_length(v) == 3);

  char *elem0 = *(char **)vector_get(v, 0);
  char *elem1 = *(char **)vector_get(v, 1);
  char *elem2 = *(char **)vector_get(v, 2);

  fail_unless(elem0 == sport1, "element on `0' should continue there. found '%s'", elem0);
  fail_unless(elem1 == sport3, "element on `2' should go to `1'. found '%s'", elem1);
  fail_unless(elem2 == sport4, "element on `3' should go to `2'. found '%s'", elem2);

  vector_free(v);
}
void hashmap_enter(hashmap *hm, void *key, void *value)
{
	assert (key != NULL && value != NULL);
	int pos = hm->hash_fun(key, hm->num_buckets) + 1;
	assert (pos > 0 && pos <= hm->num_buckets);
	vector* target = (struct vector*) (hm->pairs + pos);

	pair* p;
	if ((p = malloc(sizeof(pair))) == NULL) {
		printf("memory allocation error in pair creation.\n");
		exit(0);
	}
	p->key = key;
	p->value = value;

	if (target->len == 0) {
		vector_append(target, p);
		vector_append(hm->used_buckets, &pos);
	} else {
		int exists = vector_search(target, key, hm->compare_fun, 0, false);
		if (exists == -1) {
			vector_append(target, p);
		} else {
			printf("The key %d already exists.\n", *(int*) p->key);
		}
	}
	if (hm->free_fun != NULL)
		hm->free_fun(p);
	free(p);
	p = NULL;
}
Exemple #5
0
/* qloop takes a vector (or heap) of regions to check (checking) if they don't intersect
 * anything. If a region does intersect something, it is broken into
 * pieces that don't intersect that thing (if possible) which are
 * put back into the vector/heap of regions to check.
 * qloop returns false when it finds the first empty region
 * it returns true if it has exhausted the region vector/heap and never
 * found an empty area.
 */
static void
qloop (struct query_closure *qc, rtree_t * tree, heap_or_vector res, bool is_vec)
{
  BoxType *cbox;
#ifndef NDEBUG
  int n;
#endif
  while (!(qc->desired ? heap_is_empty (qc->checking.h) : vector_is_empty (qc->checking.v)))
    {
      cbox = qc->desired ? (BoxTypePtr)heap_remove_smallest (qc->checking.h) : (BoxTypePtr)vector_remove_last (qc->checking.v);
      if (setjmp (qc->env) == 0)
	{
	  assert (box_is_good (cbox));
	  qc->cbox = cbox;
#ifndef NDEBUG
	  n =
#endif
	    r_search (tree, cbox, NULL, query_one, qc);
	  assert (n == 0);
	  /* nothing intersected with this tree, put it in the result vector */
          if (is_vec)
	    vector_append (res.v, cbox);
          else
            {
              if (qc->desired)
                heap_append (res.h, qc->desired, cbox);
              else
	        vector_append (res.v, cbox);
            }
	  return;		/* found one - perhaps one answer is good enough */
	}
    }
}
Exemple #6
0
void animation_create(animation *ani, void *src, int id) {
    sd_animation *sdani = (sd_animation*)src;

    // Copy simple stuff
    ani->id = id;
    ani->start_pos = vec2i_create(sdani->start_x, sdani->start_y);
    str_create_from_cstr(&ani->animation_string, sdani->anim_string);

    // Copy collision coordinates
    vector_create(&ani->collision_coords, sizeof(collision_coord));
    collision_coord tmp_coord;
    for(int i = 0; i < sdani->col_coord_count; i++) {
        tmp_coord.pos = vec2i_create(sdani->col_coord_table[i].x, sdani->col_coord_table[i].y);
        tmp_coord.frame_index = sdani->col_coord_table[i].y_ext;
        vector_append(&ani->collision_coords, &tmp_coord);
    }

    // Copy extra strings
    vector_create(&ani->extra_strings, sizeof(str));
    str tmp_string;
    for(int i = 0; i < sdani->extra_string_count; i++) {
        str_create_from_cstr(&tmp_string, sdani->extra_strings[i]);
        vector_append(&ani->extra_strings, &tmp_string);
        // don't str_free tmp_str here because it will free the pointers
        // inside it, which vector_append does not copy
    }

    // Handle sprites
    vector_create(&ani->sprites, sizeof(sprite));
    sprite tmp_sprite;
    for(int i = 0; i < sdani->frame_count; i++) {
        sprite_create(&tmp_sprite, (void*)sdani->sprites[i], i);
        vector_append(&ani->sprites, &tmp_sprite);
    }
}
Exemple #7
0
void expand_word_state(search_prefix_t *search, prefix_fifo_t * prefix_info,
                       const struct words_state_t *word_state,
                       const symbol_t *ip, const symbol_t *op, float bo)
{
  prefix_fifo_key_t key = { word_state->state_next, ip, op };
  prefix_fifo_t *pf = (prefix_fifo_t *)hash_search((void *)&key, sizeof(prefix_fifo_key_t), search->hash);
  if (pf == NULL) {
    pf = (prefix_fifo_t *) malloc(sizeof(prefix_fifo_t));
    pf->base_state = word_state->state_next;
    pf->input = ip;
    pf->output = op;

    pf->state = state_grammar_create();
    pf->state->name = (symbol_t*) realloc(pf->state->name, sizeof(symbol_t) * (symlen(pf->base_state->name) + 1));
    symcpy(pf->state->name, pf->base_state->name);
    pf->initial_prob = LOG_ZERO;
    pf->is_accesible = false;
    pf->incoming = vector_create();

    vector_append(search->fifo, pf);
    hash_insert((void *)&key, sizeof(prefix_fifo_key_t), pf, search->hash);
  }

  incoming_t *incoming = (incoming_t *) malloc(sizeof(incoming_t));
  MEMTEST(incoming);
  incoming->prev_pf = prefix_info;
  incoming->word_idx = state_grammar_append(prefix_info->state, word_state->word, word_state->prob + bo, pf->state);
  vector_append(pf->incoming, incoming);
}
Exemple #8
0
void main(int argc, char* argv[])
{
    realVector* v = newVector();
    vector_append(v, 1.0);
    vector_append(v, 2.0);
    printf("v[0]=%f\n", vector_get(v, 0));
    printf("v[0]=%f\n", vector_get(v,1));
    
}
void test_vector_insert() {
    array *arr = vector_create();
    vector_append(arr, 5);
    vector_append(arr, 4);
    vector_append(arr, 3);
    vector_insert(arr, 100, 2);
    ASSERT(arr->array[2] == 100);

    vector_free(arr);
}
void test_vector_pop() {
    array *arr = vector_create();
    vector_append(arr, 5);
    vector_append(arr, 4);
    vector_append(arr, 3);
    data_t popped_el = 0;
    vector_pop(arr, &popped_el);
    ASSERT(popped_el == 3);
    ASSERT(arr->size == 2);

    vector_free(arr);
}
void test_vector_copy() {
    array *arr = vector_create();
    vector_append(arr, 0);
    vector_append(arr, 3);
    vector_append(arr, 5);
    array *copy_arr = vector_copy(arr);
    ASSERT(copy_arr->array[0] == 0);
    ASSERT(copy_arr->array[1] == 3);
    ASSERT(copy_arr->array[2] == 5);
    ASSERT(copy_arr->size == 3);
    ASSERT(copy_arr->capacity == 4);

    vector_free(arr);
    vector_free(copy_arr);
}
Exemple #12
0
//==============================================================================
void CC_UnitDriver::write_sfr(uint8_t address, uint8_t value)
{
    log_info("write sfr at %02Xh, value: %02Xh", address, value);

    uint8_t header[] 		= { 0x40, 0x55, 0x00, };
    uint8_t mov_a_direct[] 	= { 0xBE, 0x57, 0x75, address, value };
    uint8_t footer[] 		= { 0x90, 0x56, 0x74 };

    ByteVector command;
    vector_append(command, header, sizeof(header));
    vector_append(command, mov_a_direct, sizeof(mov_a_direct));
    vector_append(command, footer, sizeof(footer));

    usb_device_.bulk_write(endpoint_out_, command.size(), &command[0]);
}
Exemple #13
0
/*
 * Unit test for the vector_append function. Runs some example
 * operations and checks that results are as expected, writing
 * messages to the terminal so that errors can be detected and
 * pinpointed.
 *
 * \return zero on success.
 */
int test_append (void)
{
  Vector vec;
  int ii;
  
  printf ("\nrunning test_append...\n");
  
  vector_init (&vec);
  printf ("initialized: ");
  vector_dump (&vec);
  
  for (ii = -3; ii < 10; ++ii) {
    if (0 != vector_append (&vec, ii)) {
      printf ("test_append ERROR: could not append %d\n", ii);
      vector_destroy (&vec);
      return -1;
    }
    printf ("appended %2d: ", ii);
    vector_dump (&vec);
  }
  
  for (ii = -3; ii < 10; ++ii)
    if (vec.arr[ii+3] != ii) {
      printf ("test_append ERROR: arr[%d] should be %d but is %d\n", ii+3, ii, vec.arr[ii+3]);
      vector_destroy (&vec);
      return -1;
    }
  
  printf ("test_append SUCCESS\n");
  
  vector_destroy (&vec);
  return 0;
}
Exemple #14
0
static void load_syl(vector_t * v, const char * dir, const char * file)
{
	FILE * f;
	char str[11];
	char * cpy;
	char * path = malloc(strlen(dir) + 12);

	str[10] = 0;
	strcpy(path, dir);
	strcat(path, "/");
	strcat(path, file);

	vector_init(v);

	f = fopen(path, "rb");
	if (f != NULL) {
		for (;;) {
			fgets(str, 10, f);
			if (feof(f)) break;

			if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = 0;

			cpy = malloc(strlen(str) + 1);
			strcpy(cpy, str);
			vector_append(v, cpy);
		}

		fclose(f);
	} else {
		warning("Could not open file: %s", path);
	}
}
Exemple #15
0
void ticktimer_add(ticktimer *tt, int ticks, ticktimer_cb cb, void *userdata) {
    ticktimer_unit unit;
    unit.callback = cb;
    unit.ticks = ticks;
    unit.userdata = userdata;
    vector_append(&tt->units, &unit);
}
Exemple #16
0
void bk_create(bk *b, void *src) {
    sd_bk_file *sdbk = (sd_bk_file*)src;

    // File ID
    b->file_id = sdbk->file_id;

    // Copy VGA image
    surface_create_from_data(
        &b->background,
        SURFACE_TYPE_PALETTE,
        sdbk->background->w,
        sdbk->background->h,
        sdbk->background->data);

    // Copy sound translation table
    memcpy(b->sound_translation_table, sdbk->soundtable, 30);

    // Copy palettes
    vector_create(&b->palettes, sizeof(palette));
    for(int i = 0; i < sdbk->palette_count; i++) {
        vector_append(&b->palettes, (palette*)sdbk->palettes[i]);
    }

    // Copy info structs
    hashmap_create(&b->infos, 7);
    bk_info tmp_bk_info;
    for(int i = 0; i < 50; i++) {
        if(sdbk->anims[i] != NULL) {
            bk_info_create(&tmp_bk_info, (void*)sdbk->anims[i], i);
            hashmap_iput(&b->infos, i, &tmp_bk_info, sizeof(bk_info));
        }
    }
}
Exemple #17
0
END_TEST

START_TEST (search_should_fail_if_element_not_found)
{

  int num1 = 12, num2 = 22, num3 = 42;

  vector *v = vector_new(sizeof(int *), NULL, 5);
  vector_append(v, &num1);
  vector_append(v, &num2);

  fail_unless(vector_search(v, &num3, compare_ints, 0, false) == VECT_SEARCH_NOT_FOUND);
  fail_unless(vector_search(v, &num3, compare_ints, 0, true) == VECT_SEARCH_NOT_FOUND);

  vector_free(v);
}
Exemple #18
0
void
call_group_add(sip_call_group_t *group, sip_call_t *call)
{
    if (!call_group_exists(group, call)) {
        vector_append(group->calls, call);
    }
}
Exemple #19
0
bool vector_insert(vector_t vec, const void *object, size_t index)
{
    // Insert an object.
    void *ptr = NULL;
    void *ptrnext = NULL;
    
    assert(vec);
    assert(object);
    assert(index <= vec -> count);
    
    if (index == vec -> count)
        return vector_append(vec, object);
    
    if (vec -> count >= vec -> alloc_size / vec -> element_size)
        if (!vector_expand(vec, vec -> count + 1)) // Out of room. Expand it.
            return false;
    
    ptr = vec -> data + vec -> element_size * index;
    ptrnext = ptr + vec -> element_size;

    memmove(ptrnext, ptr, vector_max_pointer(vec) - ptr);
    memcpy(ptr, object, vec -> element_size);
    
    return true;
}
Exemple #20
0
static HANDLE_FUNC (handle_listen)
{
        char *arg = get_string_arg (line, &match[2]);

        if (arg == NULL) {
                return -1;
        }

        if (conf->listen_addrs == NULL) {
               conf->listen_addrs = vector_create();
               if (conf->listen_addrs == NULL) {
                       log_message(LOG_WARNING, "Could not create a list "
                                   "of listen addresses.");
                       safefree(arg);
                       return -1;
               }
        }

        vector_append (conf->listen_addrs, arg, strlen(arg) + 1);

        log_message(LOG_INFO, "Added address [%s] to listen addresses.", arg);

        safefree (arg);

        return 0;
}
Exemple #21
0
void menu_attach(menu *menu, component *c, int h) {
    c->layout(c, menu->x, menu->y + menu_get_ypos(menu), menu->w, h);
    if(vector_size(&menu->objs) == 0) {
        c->selected = 1;
    }
    vector_append(&menu->objs, &c);
}
Exemple #22
0
hash_elem_t * hashtbl_insert(hashtbl_t *h, const char *key, void *data) {
  if (!h) return NULL;
  if (!h->hash) {
    fprintf(stderr, "hashtbl insert error: no hash function\n");
    return NULL;
  }
  hashtbl_rehash(h);
  size_t bnum = h->hash(key) % h->bktnum;
  vector_t *b = h->buckets[bnum];
  if (!b) b = vector_init((free_func_t)hashtbl_elem_free, (cmp_func_t)hashtbl_keycmp);
  ssize_t idx;
  hash_elem_t *e = NULL;
  if (!h->allow_dups && (idx = vector_find(b, key)) >= 0) {
    /* no dups: reuse hash_elem replacing data */
    e = (hash_elem_t*)vector_get(b, idx);
    if (h->free) h->free(e->data);
    e->data = data;
  } else {
    /* initialize the new element */
    size_t keylen = strlen(key);
    e = (hash_elem_t*)zmalloc(sizeof(hash_elem_t) + keylen + 1);
    e->h = h;
    memcpy(e->key, key, keylen);
    e->key[keylen] = '\0';
    e->data = data;
    b = h->buckets[bnum] = vector_append(b, e);
    h->size++;
  }
  return e;
}
Exemple #23
0
/* Load a built-in or dynamically linked module.  If the `reg_fn'
 * argument is non-NULL, assume a built-in module and use reg_fn to
 * register it.  Otherwise, search for a dynamic NSS module.
 */
static void
nss_load_module(const char *source, nss_module_register_fn reg_fn)
{
	char		 buf[PATH_MAX];
	ns_mod		 mod;
	nss_module_register_fn fn;

	memset(&mod, 0, sizeof(mod));
	mod.name = strdup(source);
	if (mod.name == NULL) {
		nss_log_simple(LOG_ERR, "memory allocation failure");
		return;
	}
	if (reg_fn != NULL) {
		/* The placeholder is required, as a NULL handle
		 * represents an invalid module.
		 */
		mod.handle = nss_builtin_handle;
		fn = reg_fn;
	} else if (!is_dynamic())
		goto fin;
	else {
		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
			goto fin;
		mod.handle = libc_dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
		if (mod.handle == NULL) {
#ifdef _NSS_DEBUG
			/* This gets pretty annoying since the built-in
			 * sources aren't modules yet.
			 */
			nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
#endif
			goto fin;
		}
		fn = (nss_module_register_fn)dlfunc(mod.handle,
		    "nss_module_register");
		if (fn == NULL) {
			(void)dlclose(mod.handle);
			mod.handle = NULL;
			nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
			goto fin;
		}
	}
	mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
	if (mod.mtab == NULL || mod.mtabsize == 0) {
		if (mod.handle != nss_builtin_handle)
			(void)dlclose(mod.handle);
		mod.handle = NULL;
		nss_log(LOG_ERR, "%s, registration failed", mod.name);
		goto fin;
	}
	if (mod.mtabsize > 1)
		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
		    mtab_compare);
fin:
	_nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
	vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
}
void test_vector_append() {
    array *arr = vector_create();
    vector_append(arr, 5);
    ASSERT(arr->array[0] == 5);
    ASSERT(arr->size == 1);

    vector_free(arr);
}
Exemple #25
0
static inline void
append (struct query_closure * qc, BoxType *newone)
{ 
  if (qc->desired)
    heap_append (qc->checking.h, qc->desired, newone);
  else
    vector_append (qc->checking.v, newone);
}
Exemple #26
0
void vector_set(Vector *vector, int index, int value) {
	//zero fill up to desired index
	while (index >= vector->size) {
		vector_append(vector, 0);
	}
	
	vector->data[index] = value;
}
Exemple #27
0
void
call_add_stream(sip_call_t *call, rtp_stream_t *stream)
{
    // Store stream
    vector_append(call->streams, stream);
    // Flag this call as changed
    call->changed = true;
}
Exemple #28
0
void
call_add_rtp_packet(sip_call_t *call, packet_t *packet)
{
    // Store packet
    vector_append(call->rtp_packets, packet);
    // Flag this call as changed
    call->changed = true;
}
Exemple #29
0
//==============================================================================
static void create_read_proc(size_t count, ByteVector &proc)
{
    uint8_t clr_a[]			= { 0x5E, 0x55, 0xE4 };
    uint8_t mov_c_a_dptr_a[]= { 0x4E, 0x55, 0x93 };
    uint8_t inc_dptr[]		= { 0x5E, 0x55, 0xA3 };

    proc.clear();
    for (size_t i = 0; i < count; i++)
    {
        vector_append(proc, clr_a, sizeof(clr_a));
        if (!((i + 1) % 64) || i == (count - 1))
            mov_c_a_dptr_a[0] |= 0x01;
        vector_append(proc, mov_c_a_dptr_a, sizeof(mov_c_a_dptr_a));
        mov_c_a_dptr_a[0] &= ~0x01;
        vector_append(proc, inc_dptr, sizeof(inc_dptr));
    }
}
Exemple #30
0
int main(void)
{

  vector v;
  int num = 23;

  vector_new(&v, sizeof(int *), NULL, 2);

  vector_append(&v, &num);
  vector_append(&v, &num);
  vector_append(&v, &num);
  vector_append(&v, &num);

  vector_dispose(&v);

  return 0;
}