static void test_contains() { jnx_list *l = jnx_list_create(); char *text = "Text"; char *another = "Another"; char *fake = "Fake"; jnx_list_add(l,text); jnx_list_add(l,another); JNXCHECK(jnx_list_contains(l,text,test_contains_comparator) == 1); JNXCHECK(jnx_list_contains(l,another,test_contains_comparator) == 1); JNXCHECK(jnx_list_contains(l,fake,test_contains_comparator) == 0); }
jnx_list *cartographer_get_at(sfView *view) { /*----------------------------------------------------------------------------- * Again a temporary solution to return the entire list *-----------------------------------------------------------------------------*/ if(object_list == NULL) { return NULL; } sfVector2f position = sfView_getCenter(view); int view_max_size = sfView_getSize(view).x; jnx_node *head = object_list->head; jnx_node *head_reset = head; jnx_list *temp_list = jnx_list_create(); int count = 0; while(head) { game_object *obj = head->_data; sfVector2f obj_pos = sfSprite_getPosition(obj->sprite); if(obj_pos.x < position.x + view_max_size && obj_pos.x > position.x - view_max_size && obj_pos.y < position.y + view_max_size && obj_pos.y > position.y - view_max_size) { jnx_list_add(temp_list,obj); ++count; } head = head->next_node; } head = head_reset; /*----------------------------------------------------------------------------- * Note, that you probably don't want to return the original list ever as any manipulation outside of this functionality could cause error *-----------------------------------------------------------------------------*/ return temp_list; }
static void test_removal_from() { JNXLOG(LDEBUG,"- test_removal_from\n"); jnx_list *list = jnx_list_create(); // Test the empty list removal should succeed without blowing up jnx_list_remove_from(&list, 5, compare_ints); int i; for (i = 1; i < 10; i++) jnx_list_add(list, (void *)i); // Remove from the middle jnx_list_remove_from(&list, 3, compare_ints); // Remove the head jnx_list_remove_from(&list, 1, compare_ints); // Remove the tail jnx_list_remove_from(&list, 9, compare_ints); // Check the remaining elements are 245678 char digit[2]; char leftover[10]; leftover[0] = '\0'; jnx_node *current = list->head; while (current != NULL) { sprintf(digit, "%d", (int) current->_data); strcat(leftover, digit); current = current->next_node; } JNXCHECK(strcmp(leftover, "245678") == 0); }
jnx_int32 jnx_hash_put(jnx_hashmap* hashmap, const jnx_char* key, void* value) { JNXCHECK(hashmap); JNXCHECK(key); JNXCHECK(value); jnx_int32 index = jnx_hash_string(key, hashmap->size); if (hashmap->data[index].used == 0) { // we need to setup the bucket hashmap->data[index].bucket = jnx_list_create(); hashmap->data[index].bucket_len = 0; //okay so bucket is ready to get given a KVP entry, so we'll use our bucket struct jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el)); current_bucket_el->origin_key = malloc(strlen(key) + 1); strncpy(current_bucket_el->origin_key,key,strlen(key) + 1); current_bucket_el->origin_value = value;//insert our bucket element... jnx_list_add(hashmap->data[index].bucket, current_bucket_el); hashmap->data[index].bucket_len++; hashmap->data[index].used++; hashmap->used_up++; } else { // So we are here assuming that the bucket exists so we're going to append to the existing bucket... //check to see if it already exists! jnx_node* head = hashmap->data[index].bucket->head; jnx_node* rewind_head = head; while (head) { jnx_hash_bucket_el* bucketel = head->_data; if (strcmp(bucketel->origin_key, key) == 0) { bucketel->origin_value = value; hashmap->data[index].bucket->head = rewind_head; return 0; } head = head->next_node; } hashmap->data[index].bucket->head = rewind_head; //If the list has been searched and the entry does not exist, we'll do a new insert jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el)); current_bucket_el->origin_key = malloc(strlen(key) + 1); strncpy(current_bucket_el->origin_key,key,strlen(key) + 1); current_bucket_el->origin_value = value; jnx_list_add(hashmap->data[index].bucket, current_bucket_el); hashmap->data[index].bucket_len++; hashmap->data[index].used = 1; hashmap->used_up++; //so the linked list length should be incremented.... } return 0; }
void cartographer_add(game_object *obj) { if(object_list == NULL) { object_list = jnx_list_create(); } /*----------------------------------------------------------------------------- * Temporary solution whilst I create an algorithm for search *-----------------------------------------------------------------------------*/ jnx_list_add(object_list,obj); }
static void collect_keys_from_node(jnx_btree_node *node, jnx_list *keys) { if (node == NULL) return; if ( node->is_leaf ) { append_keys_from_node(node, keys); return; } jnx_int32 i; for (i = 0; i <= node->count; i++ ) { collect_keys_from_node(node->children[i], keys); if ( i < node->count ) jnx_list_add(keys, node->records[i]->key); } }
static void test_removal_returns_correct_data() { JNXLOG(LDEBUG,"- test_removal_returns_correct_data\n"); char *data = malloc(strlen("data item") + 1); strcpy(data, "data item"); char *lookup = malloc(strlen("data item") + 1); strcpy(lookup, "data item"); jnx_list *ls = jnx_list_create(); jnx_list_add(ls, (void *) data); void *result = jnx_list_remove_from(&ls, (void *) lookup, (ordering) strcmp); JNXCHECK(result == data); JNXCHECK(result != lookup); JNXCHECK(ls->head == NULL); }
static void test_data_removal() { JNXLOG(LDEBUG,"- test_data_removal"); char *test_string = "AABBCC"; jnx_list *list = jnx_list_create(); int count = 10; int x; for(x = 0; x < count; ++x) { char *dynamic_string = malloc(strlen(test_string)); jnx_list_add(list,dynamic_string); } char *ret; while((ret = jnx_list_remove(&list)) != NULL) { free(ret); } jnx_list_destroy(&list); JNXCHECK(list == NULL); JNXLOG(LDEBUG,"OK"); }
static void test_list_index() { JNXLOG(LDEBUG,"- test_list_sequence"); jnx_list *j = jnx_list_create(); int count = 6; char *ar[] = { "A", "B", "C", "D", "E", "F" }; int y; for(y = 0; y < count; ++y) { jnx_list_add(j,ar[y]); } int x; for(x = count - 1; x >= 0; x--) { char *current = jnx_list_remove(&j); JNXCHECK(strcmp(current,ar[x]) == 0); } JNXLOG(LDEBUG,"OK"); jnx_list_destroy(&j); JNXCHECK(j == NULL); }
static void test_list_tail() { jnx_list *l = jnx_list_create(); int count = 3; char *ar[] = { "A", "B", "C" }; int x; for(x = 0; x < count; ++x) { jnx_list_add(l,ar[x]); } int c=count; while(l->tail != NULL) { JNXCHECK(strcmp((char*)l->tail->_data,ar[c-1]) == 0); --c; l->tail = l->tail->prev_node; } jnx_list_destroy(&l); }
static void test_list_creation() { JNXLOG(LDEBUG,"- test_list_creation"); jnx_list *secondlist = jnx_list_create(); JNXCHECK(secondlist != NULL); JNXLOG(LDEBUG,"OK"); struct foo *f = malloc(sizeof(foo)); f->number = 10; JNXLOG(LDEBUG,"- test_list_insertion"); jnx_list_add(secondlist,(void*)f); JNXLOG(LDEBUG,"OK"); JNXLOG(LDEBUG,"- test_list_removal"); struct foo *output = (struct foo*)jnx_list_remove(&secondlist); JNXCHECK(output->number == 10); free(output); JNXLOG(LDEBUG,"OK"); JNXLOG(LDEBUG,"- test_list_deletion"); jnx_list_destroy(&secondlist); JNXLOG(LDEBUG,"OK"); }
void cartographer_update() { /*----------------------------------------------------------------------------- * Warning, may slow down game loop if there are alot of objects here *-----------------------------------------------------------------------------*/ jnx_node *head = object_list->head; jnx_list *new_draw = jnx_list_create(); while(head) { game_object *current = head->_data; if(current->health > 0) { //still an active object jnx_list_add(new_draw,current); } else { if(strcmp(current->object_type,"player") == 0) { /*----------------------------------------------------------------------------- * If the player has been killed we'll call a clean up function to end the game *-----------------------------------------------------------------------------*/ game_end(); } JNX_LOG(NULL,"Object %s at %g %g has been removed as health was %d\n", current->object_type,current->position.x, current->position.y, current->health); //remove the object play_sound(sound_lexplosion); /*----------------------------------------------------------------------------- * Add our soon to be lost object to the score board *-----------------------------------------------------------------------------*/ printf("adding object for destruction %s with health %d\n",current->object_type,current->health); //Possible bug here if the player starts in reach of enemy ships and they start blowing up before game time score_add_destroyed_obj(current); sfSprite_destroy(current->sprite); free(current); } head = head->next_node; } object_list = new_draw; }
static void test_removal_front() { JNXLOG(LDEBUG,"- test_removal_front\n"); jnx_list *l = jnx_list_create(); int count = 3; char *ar[] = { "A", "B", "C" }; int x; for(x = 0; x < count; x++) { jnx_list_add(l,ar[x]); } JNXCHECK(l->counter == 3); void *data = jnx_list_remove_front(&l); JNXCHECK(strcmp(data,"A") == 0); JNXCHECK(l->counter == 2); data = jnx_list_remove_front(&l); JNXCHECK(strcmp(data, "B") == 0); JNXCHECK(l->counter == 1); data = jnx_list_remove_front(&l); JNXCHECK(strcmp(data, "C") == 0); JNXCHECK(l->counter == 0); }
static void append_keys_from_node(jnx_btree_node *node, jnx_list *keys) { jnx_int32 i; for (i = 0; i < node->count; i++ ) jnx_list_add(keys, node->records[i]->key); }