Esempio n. 1
0
void compute_path( Path *p ){
    int i;

    if(p->num + 1 == numCities){
        for(i = 0; i < numCities; i++){
            if(p->visited[i] == 0){
                add_city(p, i);
            }
        }
        if(check_best_path(p) == 1){
            Path* curr_best_path;

            curr_best_path = copy_paths(p);
            
            #pragma omp critical
            best_path = curr_best_path;
        }

        remove_last(p);
        remove_last(p);

    } else {
        for( i = 0; i < numCities; i++){
            if(p->visited[i] == 0){
                add_city(p, i);
                compute_path(p);
            }
        }
        remove_last(p);
    }
}
Esempio n. 2
0
static void
allocate_objects(PMEMobjpool *pop, size_t size_min, size_t size_max)
{
	size_t allocated_total = 0;

	size_t sstart = 0;

	PMEMoid oid = pmemobj_root(pop, 1);
	uint64_t uuid_lo = oid.pool_uuid_lo;

	while (allocated_total < ALLOC_TOTAL) {
		size_t s = RRAND(seed, size_max, size_min);
		pmemobj_alloc(pop, &oid, s, 0, NULL, NULL);
		s = pmemobj_alloc_usable_size(oid);

		UT_ASSERTeq(OID_IS_NULL(oid), 0);
		objects[nobjects++] = oid.off;
		UT_ASSERT(nobjects < MAX_OBJECTS);
		allocated_total += s;
		allocated_current += s;

		if (allocated_current > ALLOC_CURR) {
			shuffle_objects(sstart, nobjects);
			for (int i = 0; i < FREES_P; ++i) {
				oid.pool_uuid_lo = uuid_lo;
				oid.off = remove_last();
				allocated_current -=
					pmemobj_alloc_usable_size(oid);
				pmemobj_free(&oid);
			}
			sstart = nobjects;
		}
	}
}
Esempio n. 3
0
// Removes an element from the array without preserving the order of the
// elements.
Value unordered_remove(Array* a, size_t index)
{
    // assumes swap(Value*, Value*) has been defined. Hopefully, swapping will
    // be a compiler builtin.
 
    swap(index(a, i), index(a, length(a) - 1));
    return remove_last(a);
}
Esempio n. 4
0
int main(void) {
	int id, sim, sim_r, max_sim, max_sim_r;
	int real_id, real_id_r;
	char tmp[PER_AUNT][MAXL + 1];
	int count[PER_AUNT];

	/* first input belongings and their numbers */
	while (scanf("%s", belongings[n].name) == 1) {
		if (!strcmp("Sue", belongings[n].name))
			break;
		scanf("%d", &belongings[n].count);
		remove_last(belongings[n].name);
		n++;
	}
	
	n--;
	
	scanf("%d%*c %s %d, %s %d, %s %d, %s %d", 
		&id, tmp, count, tmp+1, count+1, tmp+2 , count+2, tmp+3, count+3);
		
	remove_last_all(tmp[0], PER_AUNT, MAXL + 1);
	max_sim = check_aunt_sim(tmp[0], count, !CHECK_RANGES);
	max_sim_r = check_aunt_sim(tmp[0], count, CHECK_RANGES);
	real_id = real_id_r = 1;
	
	while (scanf("%*s %d%*c %s %d, %s %d, %s %d, %s %d", 
		&id, tmp, count, tmp+1, count+1, tmp+2 , count+2, tmp+3, count+3) != EOF) {
		
		remove_last_all(tmp[0], PER_AUNT, MAXL + 1);
		
		sim = check_aunt_sim(tmp[0], count, !CHECK_RANGES);
		sim_r = check_aunt_sim(tmp[0], count, CHECK_RANGES);
		
		if (sim > max_sim) {
			max_sim = sim;
			real_id = id;
		}
		
		if (sim_r > max_sim_r) {
			max_sim_r = sim_r;
			real_id_r = id;
		}
	}
	
	printf("Real Aunt Sue without ranges: %d\n", real_id);
	printf("Real Aunt Sue with ranges: %d\n", real_id_r);
	
	return 0;
}
Esempio n. 5
0
int main(void)
{
	setup_locale();
	setup_tmpctx();

	sanity();
	remove_one();
	remove_first();
	remove_last();
	remove_multiple();
	remove_all();
	remove_complex();

	tal_free(tmpctx);
	printf("run-json_remove ok\n");
}
Esempio n. 6
0
static void
delete_objects(PMEMobjpool *pop, float pct)
{
	size_t nfree = (float)nobjects * pct;

	PMEMoid oid = pmemobj_root(pop, 1);
	uint64_t uuid_lo = oid.pool_uuid_lo;

	shuffle_objects(0, nobjects);
	while (nfree--) {
		oid.off = remove_last();
		oid.pool_uuid_lo = uuid_lo;

		allocated_current -= pmemobj_alloc_usable_size(oid);

		pmemobj_free(&oid);
	}
}
Esempio n. 7
0
template <class T> int List<T>::remove_last()
{
	// Empty List
	if (head == NULL)
		return 0;
		
	// One-item List
	else if (head->next == NULL)
	{
		delete head;
		head = NULL;
		count--;
		return 1;
	}
		
	// Multi-item List
	else {
		remove_last(head->next);
		return 1;
	}
}
Esempio n. 8
0
void scheduler()
{
	unsigned int cpu_id = CPUID; 
	struct scheduler_info *si = &(cpu[ cpu_id ].sched);
	struct thread *tr;
	int idle;
	int flip = 0;
	uint64_t requested_time = TIMESLICE;

	// We will never go back.
	set_cpu_flags(  cpu_flags() & ~EFLAG_NESTED_TASK );

	// Now we're working.
	acquire_spinlock( &(si->lock_scheduler) );

	// Start timing the scheduler
	stats_time_start( &(cpu[ cpu_id ].st_schedulerTime) );

	assert( (cpu_flags() & EFLAG_INTERRUPT) == 0 );

	ack_apic();

	while (1==1)
	{
		// Just show the world that we're still alive.
		((char*)0xB8000)[158 - cpu_id * 2] ++ ;		/// \todo remove one day


		// If the garbage collector has work to do, let it run.
		if ( gc_has_work( cpu_id ) == 0 )
		{
			tr = smk_gc[ cpu_id ];
			exec_thread( cpu_id, tr, TIMESLICE, 0 );
			stats_time( cpu_id, &(cpu[ cpu_id ].st_systemTime) ); 
		}

		
		// Find out when the next timed event is.
		requested_time = remove_timers( si, cpu[ cpu_id ].st_systemTime.usage );
		idle = 0;
			
		// Fast queue support
		if ( flip == 0 )
		{
			tr = get_fast( si );
			if ( tr != NULL )
			{
				exec_thread( cpu_id, tr, TIMESLICE, 0 );
				stats_time( cpu_id, &(cpu[ cpu_id ].st_systemTime) ); 
														// Maintain CPU time.

				if ( si->sched_count != 0 ) flip = 1;	// Ensure others run
				continue;
			}
		}


		flip = 0;
		// Reset of fast queue
		
		// If there's nothing to do, do idle.
		if ( si->sched_count == 0 ) 
		{
			if ( gc_has_work( cpu_id ) == 0 ) continue;	// Al-e-oop!
				
			tr = smk_idle[ cpu_id ];
			idle = 1;		// Safe to wake up when required.
		}
		else
		{
			tr = si->sched_queue[ si->position ].tr;
			si->position = (si->position + 1) % si->sched_count;

			requested_time = TIMESLICE;
			idle = 0;		// Don't interrupt until timeslice is over
		}


		// And run the selected thread.
		exec_thread( cpu_id, tr, requested_time, idle );
		stats_time( cpu_id, &(cpu[ cpu_id ].st_systemTime) ); 
														// Maintain CPU time.
														//
		// If the previous thread requested a state change, honour it.
		remove_last( si );

	}

}
Esempio n. 9
0
int main()
{
	t_node *head;
	t_node *n1;
	t_node *n2;
	int *i1;
	int *i2;
	int *i3;
	int i;
	int j;
	int k;
	char *c1;
	char *c2;
	char *c3;
	char a;
	char b;
	char c;

	head = (t_node*)xmalloc(10*sizeof(t_node));
	
	/*new_node*/
	n1 = new_node("one\n", NULL);
	my_str(n1->elem);/*prints one*/
	n1->elem = NULL;
	free(n1);
	n1 = new_node(NULL, NULL);
	if(!n1->elem)
		my_str("create NULL node ok\n");
	else
		my_str("create NULL node FAIL!\n");
	free(n1);
	n1 = new_node("a", NULL);
	n2 = new_node("b", n1);
	my_str(n2->elem);
	my_str((n2->next)->elem);/*prints ba*/
	my_char('\n');
	my_str("---------------------------------------------------------------------\n");

	/*add_node*/
	add_node(n1, &head);
	my_str((*head).elem);/*prints a*/
	my_char('\n');
	add_node(n2, &head);
	my_str((*head).elem);/*prints b*/
	my_char('\n');
	add_node(NULL, &head);
	if(strcmp((*head).elem, n2->elem) == 0)
		my_str("add NULL ok\n");
	else
		my_str("add NULL FAIL!\n");
	add_node(new_node(NULL, NULL), &head);
	if(strcmp((*head).elem, n2->elem) == 0)
		my_str("add NULL node ok\n");
	else
		my_str("add NULL node FAIL!\n");
	add_node(new_node("something", NULL), NULL);/*if this line doesn't segfault then we're good!*/
	my_str("---------------------------------------------------------------------\n");

	/*traversals*/
	empty_list(&head);
	i = 3;
	i1 = &i;
	add_node(new_node(i1, NULL), &head);
	j = 2;
	i2 = &j;
	add_node(new_node(i2, NULL), &head);
	k = 1;
	i3 = &k;
	add_node(new_node(i3, NULL), &head);
	traverse_int(head);/*prints 1 2 3 */
	my_char('\n');
	head->next->elem = NULL;
	traverse_int(head);/*prints 1 NULL 3*/
	my_char('\n');
	traverse_int(NULL);/*prints The list is empty!*/

	empty_list(&head);
	c = 'c';
	c1 = &c;
	add_node(new_node(c1, NULL), &head);
	b = 'b';
	c2 = &b;
	add_node(new_node(c2, NULL), &head);
	a = 'a';
	c3 = &a;
	add_node(new_node(c3, NULL), &head);
	traverse_char(head);/*prints a b c */
	my_char('\n');
	head->elem = NULL;
	traverse_char(head);/*prints NULL b c*/
	my_char('\n');
	traverse_char(NULL);/*prints The list is empty!*/

	empty_list(&head);
	add_node(new_node("third", NULL), &head);
	add_node(new_node("second", NULL), &head);
	add_node(new_node("first", NULL), &head);
	traverse_string(head);/*prints first second third */
	my_char('\n');
	head->next->next->elem = NULL;
	traverse_string(head);/*prints first second NULL*/
	my_char('\n');
	traverse_string(NULL);/*prints The list is empty!*/
	empty_list(&head);
	my_str("---------------------------------------------------------------------\n");

	/*add_elem*/
	add_elem("a", &head);
	add_elem("b", &head);
	add_elem("c", &head);
	my_str(head->elem);/*prints c*/
	my_char('\n');
	add_elem(NULL, &head);
	if(strcmp(head->elem, "c") == 0)
		my_str("add NULL elem ok\n");
	else
		my_str("add NULL elem FAIL!\n");	
	my_str("---------------------------------------------------------------------\n");
	
	/*append*/
	append(new_node("z", NULL), &head);
	traverse_string(head);/*prints c b a z*/
	my_char('\n');
	append(NULL, &head);
	traverse_string(head);/*prints c b a z*/
	my_char('\n');
	append(new_node("stuff", NULL), NULL);/*if this line doesn't segfault then we're good!*/
	my_str("---------------------------------------------------------------------\n");

	/*add_node_at*/
	add_node_at(new_node("d", NULL), &head, 0);
	traverse_string(head);/*prints d c b a z*/
	my_char('\n');
	add_node_at(new_node("y", NULL), &head, 42);
	traverse_string(head);/*prints d c b a z y*/
	my_char('\n');
	add_node_at(new_node("0", NULL), &head, 4);
	traverse_string(head);/*prints d c b a 0 z y*/
	my_char('\n');
	add_node_at(NULL, &head, 2);
	traverse_string(head);/*prints d c b a 0 z y*/
	my_char('\n');
	add_node_at(new_node(NULL, NULL), &head, 1);
	traverse_string(head);/*prints d c b a 0 z y*/
	my_char('\n');
	add_node_at(new_node("something", NULL), NULL, 7);/*if this line doesn't segfault then we're good!*/
	my_str("---------------------------------------------------------------------\n");

	/*remove_node*/
	my_str(remove_node(&head));/*prints d*/
	my_str(": ");
	traverse_string(head);
	my_char('\n');
	if(!remove_node(NULL))
		my_str("remove node from NULL ok\n");
	else
		my_str("remove node from NULL FAIL!\n");
	my_str("---------------------------------------------------------------------\n");

	/*remove_node_at*/
	my_str(remove_node_at(&head, 0));/*prints c*/
	my_str(": ");
	traverse_string(head);
	my_char('\n');
	my_str(remove_node_at(&head, 42));/*prints y*/
	my_str(": ");
	traverse_string(head);
	my_char('\n');
	my_str(remove_node_at(&head, 2));/*prints 0*/
	my_str(": ");
	traverse_string(head);
	my_char('\n');
	if(!remove_node_at(NULL, 100))
		my_str("remove node from NULL ok\n");
	else
		my_str("remove node from NULL FAIL!\n");
	my_str("---------------------------------------------------------------------\n");

	/*remove_last*/
	my_str(remove_last(&head));/*prints z*/
	my_str(": ");
	traverse_string(head);
	my_char('\n');
	if(!remove_last(NULL))
		my_str("remove last from NULL ok\n");
	else
		my_str("remove last from NULL FAIL!\n");
	my_str("---------------------------------------------------------------------\n");

	/*count_nodes*/
	my_int(count_nodes(head));/*prints 2*/
	my_char('\n');
	my_int(count_nodes(NULL));/*prints 0*/
	my_char('\n');
	my_str("---------------------------------------------------------------------\n");

	/*node_at*/
	my_str((node_at(head, 1))->elem);/*prints a*/
	my_char('\n');
	my_str((node_at(head, 0))->elem);/*prints b*/
	my_char('\n');
	my_str((node_at(head, 42))->elem);/*prints a*/
	my_char('\n');
	if(!node_at(NULL, 12))
		my_str("node at with NULL ok\n");
	else
		my_str("node at with NULL FAIL!\n");
	my_str("---------------------------------------------------------------------\n");

	/*elem_at*/
	my_str(elem_at(head, 0));/*prints b*/
	my_char('\n');
	my_str(elem_at(head, 1));/*prints a*/
	my_char('\n');
	my_str(elem_at(head, 42));/*prints a*/
	my_char('\n');
	if(!elem_at(NULL, 3))
		my_str("elem at with NULL ok\n");
	else
		my_str("elem at with NULL FAIL!\n");
	my_str("---------------------------------------------------------------------\n");

	/*empty_list*/
	my_int(count_nodes(head));/*prints 2*/
	my_char('\n');
	empty_list(&head);
	my_int(count_nodes(head));/*prints 0*/
	my_char('\n');
	empty_list(NULL);/*if this doesn't segfault then we're good!*/
	my_str("---------------------------------------------------------------------\n");
	
	free(head);

	return 0;
}
Esempio n. 10
0
void remove_last_all(char *s, int rows, int cols) {
	int i;
	
	for (i = 0; i < rows; i++)
		remove_last(s + i * cols);
}
Esempio n. 11
0
/** OTFUR algorithm **/
otfur_result*
otfur(antichain *safety_game_PO, antichain *safety_game_PI, GNode* cfinfo, alphabet_info *alphabet, char starting_player, int dimension, int* max_credit) {
	clock_t start_time = clock();

	// Structures initialization
	GHashTable *succ_to_visit = g_hash_table_new_full(hash_key, compare_keys, free, NULL);
	GHashTable *passed = g_hash_table_new_full(hash_key, compare_keys, (GDestroyNotify)free_hash_table_key, free);
	GHashTable *depend = g_hash_table_new_full(hash_key, compare_keys, free, NULL);
	//GList *trash = NULL;

	GList *waiting = NULL;
	antichain *losing_PO = new_antichain();
	antichain *losing_PI = new_antichain();

	// Build initial tuple
	tuple *s_ini = build_initial_tuple(cfinfo, dimension, max_credit);

	// if s_ini belongs to the system and all its successors are unsafe, s_ini is losing
	if((starting_player == P_O && has_successor_in_safety_game(s_ini, alphabet, safety_game_PI) == FALSE)/* ||
			(starting_player == P_I && has_successor_not_in_safety_game(s_ini, alphabet, safety_game_PO) == TRUE)*/) { // To avoid k differences between forward and backward algorithms
		add_to_losing(s_ini, losing_PO, losing_PI);
	} // else, explore its successors
	else {
		waiting = add_to_waiting(s_ini, alphabet, safety_game_PO, safety_game_PI, losing_PO, losing_PI, waiting, succ_to_visit, passed);
		add_to_passed(s_ini, passed);
	}

	safety_game_edge *cur_edge;
	char cur_losing;
	int nb_cf_passed = 1;
	int nb_iter = 0;
	antichain* cur_safety_game;
	while(waiting != NULL && is_losing(s_ini, losing_PO, losing_PI) == FALSE) {
		nb_iter++;
		GList *last_link = g_list_last(waiting);
		cur_edge = (safety_game_edge*)last_link->data;
		waiting = remove_last(waiting);
		if(is_losing(cur_edge->from, losing_PO, losing_PI) == FALSE) { // If s is not losing
			if(is_passed(cur_edge->to, passed) == FALSE) { // If s' is not passed
				add_to_passed(cur_edge->to, passed);
				nb_cf_passed++;
				if(is_losing(cur_edge->to, losing_PO, losing_PI) == TRUE) { // If s' is losing -> add e for reevaluation
					//waiting = g_list_append(waiting, clone_safety_game_edge(cur_edge));
					waiting = g_list_append(waiting, cur_edge);
				}
				// Basic case: if s' belongs to P_O and has no successor safe non losing, add it to losing
				// Put the same test for P_I to avoid k differences between forward and backward algorithms
				else if(cur_edge->to->cf->player == P_O && has_successor_non_losing_in_safety_game(cur_edge->to, alphabet, losing_PO, losing_PI, safety_game_PI) == FALSE) {
					//waiting = g_list_append(waiting, clone_safety_game_edge(cur_edge));
					waiting = g_list_append(waiting, cur_edge);
					add_to_losing(cur_edge->to, losing_PO, losing_PI);
				}
				else { // else, explore its successors
					add_to_depend(cur_edge->to, cur_edge, depend);
					waiting = add_to_waiting(cur_edge->to, alphabet, safety_game_PO, safety_game_PI, losing_PO, losing_PI, waiting, succ_to_visit, passed);
				}
			}
			else { // s' is passed
				if(cur_edge->from->cf->player == P_O) { // s belongs to P_O
					if(is_losing(cur_edge->to, losing_PO, losing_PI) == FALSE) { // if s' is not losing, add e to the dependencies of s'
						add_to_depend(cur_edge->to, cur_edge, depend);
					}
					else { // if s' is losing, and if there is no more successor of s non losing to visit, s is losing -> add its dependencies to waiting
						if(has_one_succ_to_visit_non_losing(cur_edge->from, succ_to_visit, losing_PO, losing_PI) == FALSE) {
							add_to_losing(cur_edge->from, losing_PO, losing_PI);
							waiting = g_list_concat(waiting, get_dependencies(cur_edge->from, depend));
						}
						else { // if there is still at least a successor non passed non losing, add it to waiting
							waiting = g_list_append(waiting, get_first_successor_passed_non_losing(cur_edge->from, succ_to_visit, passed, losing_PO, losing_PI));
						}
					}
				}
				else { // s belongs to P_I
					// reevaluation
					if(is_losing(cur_edge->to, losing_PO, losing_PI) == TRUE) {
						cur_losing = TRUE;
					}
					else {
						cur_losing = reevaluation(cur_edge->from, alphabet, losing_PO, losing_PI);
					}

					// if s is losing, add its dependencies to waiting
					if(cur_losing == TRUE) {
						add_to_losing(cur_edge->from, losing_PO, losing_PI);
						waiting = g_list_concat(waiting, get_dependencies(cur_edge->from, depend));
					}

					// if s' is not losing, add e to its dependencies list
					if(is_losing(cur_edge->to, losing_PO, losing_PI) == FALSE) {
						add_to_depend(cur_edge->to, cur_edge, depend);
					}
				}
				//trash = g_list_append(trash, cur_edge->to);
			}
		}
		else { // if s is losing, add its dependencies to waiting
			//free_tuple_full(cur_edge->to);
			waiting = g_list_concat(waiting, get_dependencies(cur_edge->from, depend));
		}
	}
	float otfur_time = (clock() - start_time) * 1e-6;

	// Extract the solution from passed and losing
	start_time = clock();
	safety_game* sg = compute_winning_positions(losing_PO, losing_PI, passed, build_initial_tuple(cfinfo, dimension, max_credit), alphabet, nb_cf_passed);
	float winning_positions_computation_time = (clock() - start_time) * 1e-6;

	// Free memory
	GList *curlink = waiting;
	//while(curlink != NULL) {
	//	free_safety_game_edge(curlink->data);
	//	curlink = curlink->next;
	//}
	g_list_free(waiting);
	/*curlink = trash;
	while(curlink != NULL) {
		free_tuple_full(curlink->data);
		curlink = curlink->next;
	}
	g_list_free(trash);*/
	//g_hash_table_destroy(passed); // this one is not correct !
	g_hash_table_foreach(depend, free_depend, NULL);
	g_hash_table_destroy(depend);
	g_hash_table_foreach(succ_to_visit, free_succ_to_visit, NULL);
	g_hash_table_destroy(succ_to_visit);

	// Build result
	otfur_result *res = (otfur_result*)malloc(sizeof(otfur_result));
	res->winning_positions = sg;
	res->otfur_time = otfur_time;
	res->winning_positions_computation_time = winning_positions_computation_time;
	res->nb_cf_passed = nb_cf_passed;
	res->nb_iter = nb_iter;

	return res;
}
Esempio n. 12
0
File: main.c Progetto: TristanDuck/C
int main(void)
{
	IntNode *head = NULL;  // An empty linked list.
    IntNode *empty = NULL; // Another empty linked list.

    printf("=== Testing insert_front ===\n\n");
	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 3.\n");
	head = insert_front(head, 3);
	printf("Expected list: 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 2.\n");
	head = insert_front(head, 2);
	printf("Expected list: 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling insert_front with list: ");
    print_linked_list(head);
    printf("\nInserting 1.\n");
	head = insert_front(head, 1);
	printf("Expected list: 1 -> 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing contains ===\n\n");

	_Bool found;

	printf("Calling contains with list: ");
    print_linked_list(empty);
    printf("\nSearching for 1.\n");
	found = contains(empty, 1);
	printf("Expected result: false\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 1.\n");
	found = contains(head, 1);
	printf("Expected result: true\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 3.\n");
	found = contains(head, 3);
	printf("Expected result: true\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

	printf("Calling contains with list: ");
    print_linked_list(head);
    printf("\nSearching for 6.\n");
	found = contains(head, 6);
	printf("Expected result: false\n");
    printf("Actual result: ");
    print_boolean(found);
	printf("\n\n");

    printf("=== Testing append_rear ===\n\n");

	printf("Calling append_rear with list: ");
    print_linked_list(head);
    printf("\nAppending 4.\n");
	head = append_rear(head, 4);
	printf("Expected list: 1 -> 2 -> 3 -> 4\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing remove_first ===\n\n");

	printf("Calling remove_first with list: ");
    print_linked_list(head);
	head = remove_first(head);
	printf("\nExpected list: 2 -> 3 -> 4\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    printf("=== Testing remove_last ===\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: 2\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

	printf("Calling remove_last with list: ");
    print_linked_list(head);
	head = remove_last(head);
	printf("\nExpected list: empty list\n");
    printf("Actual list: ");
    print_linked_list(head);
	printf("\n\n");

    /* Tests for Exercise 1. */
    
    printf("Building linked list 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 5 -> 5\n\n");

    head = NULL;
    head = intnode_construct(5, head);
    head = intnode_construct(5, head);
    head = intnode_construct(5, head);
    head = intnode_construct(4, head);
    head = intnode_construct(3, head);
    head = intnode_construct(3, head);
    head = intnode_construct(2, head);
    head = intnode_construct(1, head);
    head = intnode_construct(1, head);
    // print_linked_list(head);

    printf("=== Testing count ===\n\n");

    int occurrences;

	printf("Calling count with list: ");
    print_linked_list(empty);
    printf("\nCounting 1's.\n");
	occurrences = count(empty, 1);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(empty);
    printf("\nCounting 7's.\n");
	occurrences = count(empty, 7);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 1's.\n");
	occurrences = count(head, 1);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 2's.\n");
	occurrences = count(head, 2);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 3's.\n");
	occurrences = count(head, 3);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 4's.\n");
	occurrences = count(head, 4);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 5's.\n");
	occurrences = count(head, 5);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", occurrences);

	printf("Calling count with list: ");
    print_linked_list(head);
    printf("\nCounting 7's.\n");
	occurrences = count(head, 7);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", occurrences);

    /* Tests for Exercise 2. */

    printf("=== Testing index ===\n\n");

    int posn;

	printf("Calling index with list: ");
    print_linked_list(empty);
    printf("\nSearching for 1.\n");
	posn = index(empty, 1);
	printf("Expected result: -1\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 1.\n");
	posn = index(head, 1);
	printf("Expected result: 0\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 2.\n");
	posn = index(head, 2);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 3.\n");
	posn = index(head, 3);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 4.\n");
	posn = index(head, 4);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 5.\n");
	posn = index(head, 5);
	printf("Expected result: 6\n");
    printf("Actual result: %d\n\n", posn);

	printf("Calling index with list: ");
    print_linked_list(head);
    printf("\nSearching for 7.\n");
	posn = index(head, 7);
	printf("Expected result: -1\n");
    printf("Actual result: %d\n\n", posn);

    /* Tests for Exercise 31. */

    printf("=== Testing fetch ===\n\n");

    /* We can't test these cases, because they should cause the function
     * to terminate via assert. 
     *
     * 1. The list is empty; terminate via assert.
     * 2. index < 0 or index >= # of nodes; terminate via assert.
     */

    int value;

	printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 0.\n");
	value = fetch(head, 0);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", value);

	printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 1.\n");
	value = fetch(head, 1);
	printf("Expected result: 1\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 2.\n");
	value = fetch(head, 2);
	printf("Expected result: 2\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 3.\n");
	value = fetch(head, 3);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 4.\n");
	value = fetch(head, 4);
	printf("Expected result: 3\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 5.\n");
	value = fetch(head, 5);
	printf("Expected result: 4\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 6.\n");
	value = fetch(head, 6);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 7.\n");
	value = fetch(head, 7);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);	
    
    printf("Calling fetch with list: ");
    print_linked_list(head);
    printf("\nFetching value at index 8.\n");
	value = fetch(head, 8);
	printf("Expected result: 5\n");
    printf("Actual result: %d\n\n", value);

    /* Tests for Exercise 4. */

    printf("Building linked list 1 -> 2 -> 3 -> 4\n\n");

    IntNode *list = NULL;
    list = intnode_construct(4, list);
    list = intnode_construct(3, list);
    list = intnode_construct(2, list);
    list = intnode_construct(1, list);

    printf("=== Testing remove_last_one_pointer ===\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1 -> 2 -> 3\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1 -> 2\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: 1\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");

	printf("Calling remove_last_one_pointer with list: ");
    print_linked_list(list);
	list = remove_last_one_pointer(list);
	printf("\nExpected list: empty list\n");
    printf("Actual list: ");
    print_linked_list(list);
	printf("\n\n");
}
Esempio n. 13
0
void MoveArray::remove_move() {
    if (length() > 0) {
       remove_last();
    }
}