Esempio n. 1
0
int main(){

ElemType userVal;

printf("Please enter a set of integers.\n");
int checker;

LIST* lst = lst_create();

while (1){

   checker = scanf("%i", &userVal);
   if (checker == EOF || checker == 0)
    break;
   lst_push_back(lst, userVal);
}
printf("The list before the quick sort: \n");
lst_print(lst);
printf("The list after the quick sort: \n");
qsort1(lst);
lst_print(lst);
lst_free(lst);

return 0;
}
Esempio n. 2
0
void			obj_print_data(const t_obj_data *data)
{
	puts("--- POSITIONS ---");
	lst_print(data->positions, print_vec3, 0);
	puts("--- END POSITIONS ---");
	puts("--- UVS ---");
	lst_print(data->uvs, print_vec2, 0);
	puts("--- END UVS ---");
	puts("--- NORMALS ---");
	lst_print(data->normals, print_vec3, 0);
	puts("--- END NORMALS ---");
	puts("--- POLYGONS ---");
	lst_print(data->polygons, print_polygons, 0);
	puts("--- END POLYGONS ---");
}
Esempio n. 3
0
/*===================================================
=		Função que realiza o exit da par-shell      =
===================================================*/
void exit_par_shell(){
	mutex_lock();
	//	Ativar a flag de exit
	exit_ative = true;
	if(pthread_cond_signal(&podeMonitorizar) != 0){
		perror("Erro no pthread_cond_signal no exit-global da par-shell: ");
	}
	mutex_unlock();

	//	Percorrer os pid's de todos os terminais, terminando-os
	while((pidterminal = remove_terminal(lista_terminais)) > 0){
		kill(pidterminal,SIGKILL);
	}	
	//	Esperar que a tarefa monitora termine
	if(pthread_join(tid,NULL) < 0){
		perror("Erro no pthread_join: ");
		exit(EXIT_FAILURE);
	}
	//	Imprimir lista de informações dos processos filhos executados
	lst_print(list);
	//	Destruir as variáveis e estruturas da par-shell
	destroy_variables();
	//	Sair da par-shell
	exit(EXIT_SUCCESS);	
}
Esempio n. 4
0
static void		print_polygons(void *data)
{
	t_lst		*components;

	puts("---");
	components = (t_lst *)((t_polygon *)data)->vertices;
	lst_print(components, print_poly_component, 0);
	puts("---");
}
Esempio n. 5
0
int main()
{
	char* argumentos[MAX_ARGS + 1];
	char buffer[BUFFER_SIZE];
	list = lst_new();
	int pid;
	pthread_t tid;
	pthread_mutex_init(&mutex, NULL);
	
	// Criar tarefa para monitorizar os filhos
	if(pthread_create(&tid, 0, tarefa_monitor,NULL) != 0) {
	  perror("Erro no pthread_create: ");
	  exit(EXIT_FAILURE);
	}
	while(1){
		if(readLineArguments(argumentos,MAX_ARGS,buffer,BUFFER_SIZE) <= 0){ 
			continue;
		}
		if(strcmp(argumentos[0],"exit")==0) {
			// exit - esperar pela tarefa monitora e imprimir as informacoes dos filhos
			pthread_mutex_lock(&mutex);
			exit_ative = true;
			pthread_mutex_unlock(&mutex);
			
			pthread_join(tid,NULL);
			lst_print(list);
			pthread_mutex_destroy(&mutex);
			exit(EXIT_SUCCESS);		
		
		}
		else {	
			pid = fork();
			time_t starttime = time(NULL);
			
			if (pid < 0) {
				perror("Erro no fork");
				continue;
			}
			
			if (pid == 0) {  // Codigo do filho
				execv(argumentos[0], argumentos);
				perror("Erro no execv:");
				exit(EXIT_FAILURE);
			}
			// Codigo do pai
			pthread_mutex_lock(&mutex);
			nfilhos++;
			insert_new_process(list,pid,starttime);
			pthread_mutex_unlock(&mutex);
		}	
	}
	return 0;
}
Esempio n. 6
0
int main(int argc, char **argv){
	char buffer[BUFFER_SIZE];
	int numArgs;
	char *arg_vector[VECTOR_SIZE];
	time_t starttime;
	
	int child_pid;
	pthread_t monitor_thread;
	pthread_t writer_thread;
	lst = lst_new();
	writing_queue = new_queue();

	/* Initialize synchronization objects */
	pthread_mutex_init_(&mutex, NULL);
	pthread_cond_init_(&write_cond, 0);
	pthread_cond_init_(&max_par, NULL);
	pthread_cond_init_(&new_child, NULL);

	if ((log_fd = fopen("./log.txt", "a+")) == NULL){
		perror("[ERROR] opening log file");
		exit(EXIT_FAILURE);
	}
	read_log(); /* assign total time and iteration values for this execution */
	pthread_create_(&monitor_thread, NULL, (void *)&monitor, NULL); /* Create Monitor Thread */  
	pthread_create_(&writer_thread, NULL, (void *)&writer, NULL); /* Create Writer Thread */
 
	while (1) {
		numArgs = readLineArguments(arg_vector, VECTOR_SIZE, buffer, BUFFER_SIZE);
		if (numArgs <= 0)continue;

		if (strcmp(arg_vector[0], EXIT_COMMAND) == 0 ) {

			/* signal exit command */
			exit_command = 1;

			/* wait for monitor thread to end */
			pthread_cond_signal_(&new_child); /* must signal to catch exit flag */
			pthread_join_(monitor_thread, NULL);
			pthread_join_(writer_thread, NULL);

			/* print all the elements in the list */
			lst_print(lst);
			
			/* terminate sync objects */
			pthread_mutex_destroy_(&mutex);
			pthread_cond_destroy_(&write_cond);
			pthread_cond_destroy_(&max_par);
			pthread_cond_destroy_(&new_child);
			fclose(log_fd);
			lst_destroy(lst);
			exit(EXIT_SUCCESS);
		}

		/* while there are child process slots available launch new child process,
		* else wait here */
		pthread_mutex_lock_(&mutex);
		while (child_count >= MAXPAR) pthread_cond_wait_(&max_par, &mutex);
		pthread_mutex_unlock_(&mutex);

		child_pid = fork();
		if (child_pid < 0){ /* test for error in fork */
			perror("[ERROR] forking process");
			continue;
		}
		if (child_pid == 0){ /* execute on child */
			if (execv(arg_vector[0], arg_vector) == -1) {
			perror("[ERROR] executing program.");
			exit(EXIT_FAILURE);
			}
		}
		else { /* execute on parent */
			/* Main thread
			 *
			 * Child processes are inserted into the process list saving their pid
			 * and their start time.
			 * Child counter is incremented.
			 * The condition variable waiting for a new child process is signaled.
 			 */
			time(&starttime); /* get start time of child process */
			pthread_mutex_lock_(&mutex);
			insert_new_process(lst, child_pid, starttime);
			child_count++;
			pthread_cond_signal_(&new_child);
			pthread_mutex_unlock_(&mutex);
		}
	}
}
Esempio n. 7
0
/*////////////////////////////////////////////
/////////////// MAIN FUNCTION ////////////////
////////////////////////////////////////////*/
int main() {
	char* arg[7];
	char buffer[100], filename[100], fnamePipeStats[100];
	int vectorSize = 7, pid, numargs, fd, fDescPIPE, fDescPIPE_S;
	pthread_t waitChild;
	pid_t pidChild;
	terminalLst* terminals = new_terminal_lst();
	l = lst_new();
	if (pthread_mutex_lock(&mutex) != 0) {
		perror(error_mutex_lock);
	}
	if (pthread_mutex_unlock(&mutex) != 0) {
		perror(error_mutex_unlock);
	}
    
	if (pthread_mutex_init(&mutex, NULL) != 0){
		perror(error_mutex);
	}
	if (pthread_cond_init(&can_wait, NULL) != 0) {
		perror(error_cond_init_wait);
	}
	if (pthread_cond_init(&can_execute, NULL) != 0) {
		perror(error_cond_init_execute);
	}
	if (pthread_create(&waitChild, NULL, waitThread, NULL) != 0) {
		perror(error_thread_create);
		exit(EXIT_FAILURE);
	}
    if (signal(SIGINT, CTRLC) == SIG_ERR) {
        perror(error_signal);
    }
	unlink(PIPE); /*-------------------------------------------------*/

	if (mkfifo(PIPE, 0777) != 0) {
		perror(error_mkfifo);
		exit(EXIT_FAILURE);
	}
	if ((fDescPIPE = open(PIPE, O_RDONLY)) == -1) {
		perror(error_pipe_open);
		exit(EXIT_FAILURE);
	}

	close(0);
	dup2(fDescPIPE, 0);

	while (1) {

		numargs = readLineArguments(arg, vectorSize, buffer, 100);

		if(flag_signal)
			break;

		if (numargs <= 1) {
			continue;
		}
		
		if (pthread_mutex_lock(&mutex) != 0) {
			perror(error_mutex_lock);
		}
		while (numChildren == MAXPAR) {
            if (pthread_cond_wait(&can_execute, &mutex) != 0) {
                perror(error_cond_wait);
            }
		}
		if (pthread_mutex_unlock(&mutex) != 0) {
			perror(error_mutex_unlock);
		}
		else if (strcmp(arg[0], EXIT) != 0) {
			if (strcmp(arg[0], starter) == 0) {
				int pid_terminal = atoi(arg[1]);
				insert_terminal(terminals, pid_terminal);
				continue;
			}
			if (strcmp(arg[0], stats) == 0) { /*tratar o PID e meter na lista*/
				sprintf(fnamePipeStats, "%s-%s", PIPE_S, arg[1]);
                
                while((fDescPIPE_S = open(fnamePipeStats, O_WRONLY)) < 0);
                if(fDescPIPE < 0) {
					perror(error_pipe_open);
					exit(EXIT_FAILURE);
				}
				if (pthread_mutex_lock(&mutex) != 0) {
                    perror(error_mutex_lock);
                }

				sprintf (buffer, "Number of active children: %d\nTotal execution time to the moment: %d", numChildren, timeTotal);
				write(fDescPIPE_S, buffer, strlen(buffer));
                if (pthread_mutex_unlock(&mutex) != 0) {
                    perror(error_mutex_unlock);
                }

				close(fDescPIPE_S);
				continue;
			}

            if (strcmp(arg[0], EXITGLOBAL) == 0) { /* SONFOILULDNILDEFNC*/
                printf("1\n");
                CTRLC(SIGINT);
            }
			else {
				pid = fork();
				if (pid < 0) {
					perror(error_fork);
				}
				if (pthread_mutex_lock(&mutex) != 0) {
					perror(error_mutex_lock);
				}
				insert_new_process(l, pid, time(NULL));
				numChildren++;
				if (pthread_cond_signal(&can_wait) != 0) {
					perror(error_cond_wait);
				}
				if (pthread_mutex_unlock(&mutex) != 0) {
					perror(error_mutex_unlock);
				}
				if (pid == 0) {
					pidChild = getpid();
					sprintf(filename, "par-shell-%d.txt", pidChild);
					fd = open(filename,  O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
					close(1);
					dup(fd);
					close(fd);
					if (execv(arg[0], arg) < 0) {
						perror(error_execv);
						exit(EXIT_FAILURE);
					}
				}
				else {
					sprintf(filename, "par-shell-%d.txt", pid);
				}
			}
        }
        else {
            break;
        }
	}
	isExit = true;
    if (pthread_cond_signal(&can_wait) != 0) {
        perror(error_cond_wait);
    }
    if (pthread_mutex_unlock(&mutex) != 0) {
        perror(error_mutex_unlock);
    }
    if (pthread_join(waitChild, NULL) != 0) {
        perror(error_thread_join);
    }
    lst_print(l);
    lst_destroy(l);
    while(remove_terminal(terminals)!= -1){

    }
    if (pthread_cond_destroy(&can_wait) != 0) {
        perror(error_cond_destroy_wait);
    }
    if (pthread_cond_destroy(&can_execute) != 0) {
        perror(error_cond_destroy_execute);
    }
    exit(EXIT_SUCCESS);
	return 0;
}
Esempio n. 8
0
int main(int argc, char **argv) {
  pid_t child_pid; /* child process id */
  pthread_t monitor_thread; /* monitor thread */
  char *args[CMD_ARGS_MAX+1], buffer_cmd[LINE_COMMAND_MAX]; /* cmd related */
  char buffer_log[LINE_LOGFILE_MAX]; /* log related */

  if((g_lst_children = lst_new()) == NULL) {
    if(fprintf(stderr, "lst_new: couldn't create list\n") < 0)
      handle_error("fprintf");
    exit(EXIT_FAILURE);
  }

  g_log_file = f_open(PATH_LOGFILE_STR, "a+");

  while(!feof(g_log_file)) {
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* NULL or iteracao # */
    if(feof(g_log_file)) break;
    if(sscanf(buffer_log, "%*s %d", &g_iterations) != 1)
      handle_error("sscanf");
    
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* PID: # time: # s */
    f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* total time: # s */
    if(sscanf(buffer_log, "%*[^0-9] %d", &g_total_time) != 1)
      handle_error("sscanf");
  }
  
  ++g_iterations;
  
  cond_init(&g_child_cv);
  cond_init(&g_monitoring_cv);
  mutex_init(&g_mutex);
  pcreate(&monitor_thread, process_monitor, NULL);
  
  while(true) {
    int numargs = readLineArguments(args,
      CMD_ARGS_MAX + 1, buffer_cmd, LINE_COMMAND_MAX);
    
    if(args[0] == NULL) continue;
    if(numargs < 0 || (numargs > 0 && !strcmp(args[0], COMMAND_EXIT_STR))) {
      
      mutex_lock(&g_mutex);
      g_monitoring = false;
      cond_signal(&g_monitoring_cv);
      mutex_unlock(&g_mutex);
      
      pjoin(monitor_thread);
      
      lst_print(g_lst_children);
      
      destroySharedResources();
      f_close(g_log_file);
      return EXIT_SUCCESS;
    }
    else {
      FILE *fp; /* To check file existance */
      if ((fp = fopen(args[0], "r")) == NULL)
        perror(args[0]);
      else {
        f_close(fp);
        
        mutex_lock(&g_mutex);
        while(g_num_children == MAXPAR)
          cond_wait(&g_child_cv, &g_mutex);
        mutex_unlock(&g_mutex);
        
        if((child_pid = fork()) < 0) perror("fork");
        else if(child_pid == 0) {
          execv(args[0],args);
          
          destroySharedResources();
          handle_error("execv");
        }
        else {
          mutex_lock(&g_mutex);
          if(insert_new_process(g_lst_children, child_pid, time(NULL)) != 0) {
            fprintf(stderr,
              "insert_new_process: failed to insert new process\n");
            exit(EXIT_FAILURE);
          }
          ++g_num_children;
          
          cond_signal(&g_monitoring_cv);
          mutex_unlock(&g_mutex);
        }
      }
    }
  }
}
Esempio n. 9
0
int main() {
  // Test values.
  LIST *empty = lst_create();
  LIST *anotherEmptyList = lst_create();
  LIST *one = lst_create();  // List of one element [ 5 ].
  LIST *medium = lst_create();  // List of ten elements [ 1 2 ... 9 10 ].
  LIST *evenList;  // Lst of five even elements [ 2 4 6 8 10 ].
  LIST *oddList;  // List of five odd elements [ 1 3 5 7 9 ].
  LIST *dupsOfevenList; // List of some even duplicate elements [ 4 8 12 16 20 ].
  LIST *resultList;

  srand(time(NULL));
  lst_push_back(one, 5);
  for(int i = 1; i <= 10; ++i) { lst_push_back(medium, i); }

  /**
  * Unit test for void lst_push_back(LIST *l, ElemType val);
  */
  printf("\n\nTest: 0A\nDescription: Testing lst_push_back with List of length 0 with elements [] and val=42\nExpected output: [ 42 ]\n");
  printf("Your output:");
  lst_push_back(empty, 42);
  lst_print(empty);
  FREE(empty);
  empty = lst_create();

  printf("\n\nTest: 0B\nDescription: Testing lst_push_back with List of length 1 with elements [ 5 ] and val=42\nExpected output: [ 5 42 ]\n");
  printf("Your output:");
  lst_push_back(one, 42);
  lst_print(one);
  FREE(one);
  one = lst_create();
  lst_push_back(one, 5); // Reset one for later tests.

  evenList = lst_create();
  for (int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); }
  printf("\n\nTest: 0C\nDescription: Testing lst_push_back with List of length 5 with elements [2 4 6 8 10] and val=42\nExpected output: [ 2 4 6 8 10 42 ]\n");
  printf("Your output:");
  lst_push_back(evenList, 42);
  lst_print(evenList);
  FREE(evenList);


  /**
   * 2. Unit tests on function: int lst_count(LIST *l, ElemType x);
   */ 
  printf("\n\nTest: 2A\nDescription: Testing lst_count with List of length 0 and x=5\nExpected output: 0\n");
  printf("Your output: %d\n",  lst_count(empty, 5));

  printf("\n\nTest: 2B\nDescription: Testing lst_count with List of length 10 and x=-5\nExpected output: 0\n");
  printf("Your output: %d\n", lst_count(medium, -5));

  printf("\n\nTest: 2C\nDescription: Testing lst_count with List of length 10 and x=5\nExpected output: 1\n");
  printf("Your output: %d\n", lst_count(medium, 5));

  lst_push_front(medium, 10);
  printf("\n\nTest: 2D\nDescription: Testing lst_count with List of length 10 and x=10\nExpected output: 2\n");
  printf("Your output: %d\n", lst_count(medium, 10));
  lst_pop_front(medium); // Reset value for later tests.


  /**
   * 4. Unit tests on function: void lst_reverse(LIST *l);
   */
  printf("\n\nTest: 4A\nDescription: Testing lst_reverse with List of length 0\nExpected output: []\n");
  printf("Your output: ");
  lst_reverse(empty);
  lst_print(empty);

  printf("\n\nTest: 4B\nDescription: Testing lst_reverse with List of length 1\nExpected output: [ 5 ]\n");
  printf("Your output: ");
  lst_reverse(one);
  lst_print(one);

  printf("\n\nTest: 4C\nDescription: Testing lst_reverse with List of length 10\nExpected output: [ 10  9  8  7  6  5  4  3  2  1 ]\n");
  printf("Your output: ");
  lst_reverse(medium);
  lst_push_back(medium, 99);
  lst_pop_back(medium); // Check for tail correctness.
  lst_print(medium);
  FREE(medium);
  medium = lst_create();
  for (int i=1; i<=10; ++i) { lst_push_back(medium, i); } // Reset value for later tests.


  /**
   * 6. Unit tests on void lst_insert_sorted(LIST *l, ElemType x);
   */
  evenList = lst_create();
  for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); }
  printf("\n\nTest: 6A\nDescription: Testing lst_insert_sorted with List of length 0 and x=5\nExpected output: [ 5 ]\n");
  printf("Your output: ");
  lst_insert_sorted(empty, 5);
  lst_print(empty);
  FREE(empty);
  empty = lst_create(); // Resets value for later tests.

  printf("\n\nTest: 6B\nDescription: Testing lst_insert_sorted with List of length 5 and x=5\nExpected output: [ 2  4  5  6  8  10 ]\n");
  printf("Your output: ");
  lst_insert_sorted(evenList, 5);
  lst_print(evenList);
  FREE(evenList);
  evenList = lst_create();
  for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); }

  printf("\n\nTest: 6C\nDescription: Testing lst_insert_sorted with List of length 5 and x=0\nExpected output: [ 0  2  4  6  8  10 ]\n");
  printf("Your output: " );
  lst_insert_sorted(evenList, 0);
  lst_print(evenList);
  FREE(evenList);
  evenList = lst_create();
  for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); }

  printf("\n\nTest: 6D\nDescription: Testing lst_insert_sorted with List of length 5 and x=12\nExpected output: [ 2  4  6  8  10  12 ]\n");
  printf("Your output: ");
  lst_insert_sorted(evenList, 12);
  lst_print(evenList);
  FREE(evenList);
  evenList = lst_create();
  for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); } // Reset value for later tests.


  /**
   * 8. Unit tests on void lst_merge_sorted(LIST *a, LIST *b);
   */
  printf("\n\nTest: 8A\nDescription: Testing lst_merge_sorted with List a of length 0 with elements [] and List b of length 0 with elements []\n");
  printf("Your output:");
  lst_merge_sorted(anotherEmptyList, empty);
  lst_print(anotherEmptyList);
  FREE(anotherEmptyList);
  FREE(empty);
  empty = lst_create();
  anotherEmptyList = lst_create();

  printf("\n\nTest: 8B\nDescription: Testing lst_merge_sorted with List a of length 1 with elements [ 5 ] and List b of length 0 with elements []\n");
  printf("\nExpected output: [ 5 ]\n");
  printf("Your output: ");
  lst_merge_sorted(one, empty);
  lst_print(one);
  FREE(one);
  FREE(empty);
  one = lst_create();
  empty = lst_create();
  lst_push_back(one, 10);

  printf("\n\nTest: 8C\nDescription: Testing lst_merge_sorted with List a of length 0 with elements [] and List b of length 1 with elements [ 10 ]\n");
  printf("\nExpected output: [ 10 ]\n");
  printf("Your output: ");
  lst_merge_sorted(empty, one);
  lst_print(empty);
  FREE(empty);
  FREE(one);
  empty = lst_create();
  one = lst_create();
  lst_push_back(one, 5);

  oddList = lst_create();
  for(int i=0; i<5; ++i) { lst_push_back(oddList, i*2+1); }
  printf("\n\nTest: 8D\nDescription: Testing lst_merge_sorted with List a of length 5 with elements [ 2  4  6  8  10 ] and List b of length 5 with elements [ 1  3  5  7  9 ]");
  printf("\nExpected output: [ 1  2  3  4  5  6  7  8  9  10 ]\n");
  printf("Your output: ");
  lst_merge_sorted(evenList, oddList);
  lst_print(evenList);
  FREE(evenList);
  FREE(oddList);

  evenList = lst_create();
  dupsOfevenList = lst_create();
  for (int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); lst_push_back(dupsOfevenList, i*4); }
  printf("\n\nTest: 8E\nDescription: Testing lst_merge_sorted with List a of length 5 with elements [ 2  4  6  8  10 ] and List b of length 5 with elements [ 4  8  12  16  20 ]\n");
  printf("\nExpected output: [ 2  4  4  6  8  8  10  12  16  20 ]\n");
  printf("Your output: ");
  lst_merge_sorted(evenList, dupsOfevenList);
  lst_print(evenList);
  FREE(evenList);
  FREE(dupsOfevenList);


  /**
   * 10. Unit tests on LIST * lst_from_array(ElemType a[], int n);
   **/
  int emptyArray[] = {};
  int oneArray[] = {5};
  int manyArray[] = {1,2,3,4,5};

  printf("\n\nTest: 10A\nDescription: Testing lst_from_array with array of length 0 with elements [] and n=0\nExpected output: []\n");
  printf("Your output:");
  resultList = lst_from_array(emptyArray, 0);
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList);}

  printf("\n\nTest: 10B\nDescription: Testing lst_from_array with Array of length 1 with elements [ 5 ] and n=1\nExpected output: [ 5 ]\n");
  printf("Your output:");
  resultList = lst_from_array(oneArray, 1);
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }

  printf("\n\nTest: 10C\nDescription: Testing lst_from_array with Array of length 5 with elements [ 1  2  3  4  5 ] and n=5\nExpected output: [ 1  2  3  4  5 ]\n");
  printf("Your output:");
  resultList = lst_from_array(manyArray, 5);
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }


  /**
   * 12. Unit tests for LIST * lst_prefix(LIST *lst, unsigned int k);
   */
  printf("\n\nTest: 12A\nDescription: Testing lst_prefix with List of length 0 with elements [] and k=0\nExpected output: []\n");
  printf("Expected return value: []");
  printf("Your output:\n");
  resultList = lst_prefix(empty, 0);
  lst_print(empty);
  printf("Your return value: ");
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }
  FREE(empty);
  empty = lst_create();

  printf("\n\nTest: 12B\nDescription: Testing lst_prefix with List of length 0 with elements [] and k=3\nExpected output: []\n");
  printf("Expected return value: []\n");
  printf("Your output:");
  resultList = lst_prefix(empty, 3);
  lst_print(empty);
  printf("Your return value: ");
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }
  FREE(empty);
  empty = lst_create();

  printf("\n\nTest: 12C\nDescription: Testing lst_prefix with List of length 1 with elements [ 5 ] and k=0\nExpected output: [ 5 ]\n");
  printf("Expected return value: []\n");
  printf("Your output:");
  resultList = lst_prefix(one, 0);
  lst_print(one);
  printf("Your return value: ");
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }
  FREE(one);
  one = lst_create();
  lst_push_back(one, 5);

  evenList = lst_create();
  for(int i=1; i<=5; i++) { lst_push_back(evenList, i*2); }
  printf("\n\nTest: 12D\nDescription: Testing lst_prefix with List of length 5 with elements [ 2  4  6  8  10 ] and k=3\nExpected output: [ 8 10 ]\n");
  printf("Expected return value: [ 2  4  6 ]\n");
  printf("Your output: ");
  resultList = lst_prefix(evenList, 3);
  lst_print(evenList);
  printf("Your return value: ");
  if (resultList) { lst_print(resultList); } else { printf("SEG FAULT. No List returned.\n"); }
  if (resultList) { FREE(resultList); }
  //FREE(evenList);


  /**
   * 14. Unit test for void lst_concat(LIST *a, LIST *b);
   */
  anotherEmptyList = lst_create();
  evenList = lst_create();
  oddList = lst_create();
  for(int i=1; i<=5; i++) { lst_push_back(evenList, i*2); lst_push_back(oddList, i*2-1); }  // 2 4 6 8 10
  printf("\n\nTest: 14A\nDescription: Testing lst_concat with List a of length 0 with elements [] and List b of length 0 with elements []\nExpected output: []\n");
  printf("Your output:");
  lst_concat(anotherEmptyList, empty);
  lst_print(anotherEmptyList);
  FREE(anotherEmptyList);
  FREE(empty);
  empty = lst_create();

  anotherEmptyList = lst_create();
  printf("\n\nTest: 14B\nDescription: Testing lst_concat with List a of length 1 with elements [ 5 ] and List b of length 0 with elements []\nExpected output: [ 5 ]\n");
  printf("Your output:");
  lst_concat(one, anotherEmptyList);
  lst_print(one);
  FREE(one);
  FREE(anotherEmptyList);
  anotherEmptyList = lst_create();
  one = lst_create();
  lst_push_back(one, 10);

  printf("\n\nTest: 14C\nDescription: Testing lst_concat with List a of length 0 with elements []");
  printf(" and List b of length 1 with elements [ 10 ]\nExpected output: [ 10 ]\n");
  printf("Your output:");
  lst_concat(anotherEmptyList, one);
  lst_print(anotherEmptyList);
  FREE(anotherEmptyList);
  FREE(one); 
  one = lst_create();
  lst_push_back(one, 5);

  printf("\n\nTest: 14D\nDescription: Testing lst_concat with List a of length 5 with elements [ 2  4  6  8  10]");
  printf(" and List b of length 5 with elements [ 1  3  5  7  9 ]\nExpected output: [ 2  4  6  8  10  1  3  5  7  9 ]\n");
  printf("Your output:");
  lst_concat(evenList, oddList);
  lst_print(evenList);
  FREE(evenList);
  FREE(oddList);

  evenList = lst_create();
  dupsOfevenList = lst_create();
  for(int i=1; i<=5; ++i) { lst_push_back(evenList, i*2); lst_push_back(dupsOfevenList, i*4); }
  printf("\n\nTest: 14E\nDescription: Testing lst_concat with List a of length 5 with elements [ 2  4  6  8  10 ] and List b of length 5 with elements [ 4  8  12  16  20 ]\n"
    "Expected output: [ 2  4  6  8  10  4  8  12  16  20 ]\n");
  printf("Your output:");
  lst_concat(evenList, dupsOfevenList);
  lst_print(evenList);
  FREE(evenList);
  FREE(dupsOfevenList);
}
Esempio n. 10
0
int main() {
    printf("REMINDER: This test suite is NOT exhaustive. Passing all of these tests does NOT mean you will get 100%% on the project.\n");
    srand(time(NULL));
    
int i;	
	/********* 0 lst_is_empty  **********/
	LIST *empty = lst_create();
	LIST *one = lst_create();
	LIST *large = lst_create();
	lst_push_back(one, 5);
	int rand_size = rand() % 20;
	for(i = 0; i < rand_size; i++) {
		lst_push_back(large, i);
	}
	printf("\n\nTest: 0A\nDescription: Testing lst_is_empty with List of length 0\nExpected output: 1\n");
	printf("Your output: %d\n", lst_is_empty(empty));
	printf("\n\nTest: 0B\nDescription: Testing lst_is_empty with List of length 1\nExpected output: 0\n");
	printf("Your output: %d\n", lst_is_empty(one));
	printf("\n\nTest: 0CnDescription: Testing lst_is_empty with List of length %d\nExpected output: %d\n", rand_size, ((rand_size == 0) ? 1 : 0));
	printf("Your output: %d\n", lst_is_empty(large));

	FREE(large);
	FREE(one);
	FREE(empty);

	/********* 1 lst_print_rev **********/
    
	LIST *rev1 = lst_create();
	LIST *rev2 = lst_create();
	for(i = 0; i < 5; i++) {
		lst_push_back(rev2, i);
	}
	printf("\n\nTest: 1A\nDescription: Testing lst_print_rev with List of length 5\nExpected output: \n[4, 3, 2, 1, 0]\n");
	printf("Your output\n");
	lst_print_rev(rev2);
	printf("\n\nTest: 1B\nDescription: Testing lst_print_rev with List of length 0\nExpected output: \n[ ]\n");
	printf("Your output\n");
	lst_print_rev(rev1);
    
    FREE(rev1);
    FREE(rev2);

    /********** 3 lst_pop_back ***********/
    
    large = lst_create();
    one = lst_create();
    empty = lst_create();
    for(i = 0; i < 8; i++) {
        lst_push_back(large, i);
    }
    lst_push_back(one, 6);
    
    printf("\n\nTest: 3A\nDescription: Testing lst_pop_back with List of length 0\nExpected output: <some arbitrary number (NO SEG FAULT!)>\n");
    printf("Your output: %d\n", lst_pop_back(empty));
    printf("\n\nTest: 3B\nDescription: Testing lst_pop_back with List of length 1\nExpected output: 6\n");
    printf("Your output: %d\n", lst_pop_back(one));
    printf("\n\nTest: 3C\nDescription: Testing lst_pop_back with List of length 8\nExpected output: 7\n");
    printf("Your output: %d\n", lst_pop_back(large));
    
    FREE(large);
    FREE(one);
    FREE(empty);
    
    /********** 5 lst_is_sorted ***********/
    empty = lst_create();
    LIST *sorted = lst_create();
    LIST *unsorted = lst_create();
    for(i = 0; i < 10; i++) {
        lst_push_back(sorted, i);
        lst_push_back(unsorted, i);
    }
    lst_push_back(unsorted, 5);
    printf("\n\nTest: 5A\nDescription: Testing lst_is_sorted with List of length 0\nExpected output: 1\n");
    printf("Your output: %d\n", lst_is_sorted(empty));
    printf("\n\nTest: 5B\nDescription: Testing lst_is_sorted with List of length 11\nExpected output: 0\n");
    printf("Your output: %d\n", lst_is_sorted(unsorted));
    printf("\n\nTest: 5C\nDescription: Testing lst_is_sorted with List of length 10\nExpected output: 1\n");
    printf("Your output: %d\n", lst_is_sorted(sorted));
    
    FREE(empty);
    FREE(sorted);
    FREE(unsorted);
    
    /********** 7 lst_length ***********/
    
    empty = lst_create();
    rand_size = rand() % 100;
    LIST *small = lst_create();
    large = lst_create();
    for(i = 0; i < rand_size; i++) {
        lst_push_back(large, rand() % 1000);
    }
    lst_push_back(small, 3);
    
    printf("\n\nTest: 7A\nDescription: Testing lst_length with List of length 0\nExpected output: 0\n");
    printf("Your output: %d\n", lst_length(empty));
    printf("\n\nTest: 7B\nDescription: Testing lst_length with List of length 1\nExpected output: 1\n");
    printf("Your output: %d\n", lst_length(small));
    printf("\n\nTest: 7C\nDescription: Testing lst_length with List of length %d\nExpected output: %d\n", rand_size, rand_size);
    printf("Your output: %d\n", lst_length(large));
    
    FREE(empty);
    FREE(small);
    FREE(large);
    
    /********** 9 lst_clone ***********/
    
    empty = lst_create();
    small = lst_create();
    large = lst_create();
    lst_push_back(small, 3);
    rand_size = rand() % 10;
    for(i = 0; i < rand_size; i++) {
        lst_push_back(large, rand() % 500);
    }
    
    printf("\n\nTest 9A\nDescription: Testing lst_clone with List of length 0\nExpected output:\n");
    lst_print(empty);
    printf("Your output: \n");
    LIST *empty_clone = lst_clone(empty);
    lst_print(empty_clone);
    printf("\n\nTest 9B\nDescription: Testing lst_clone with List of length 1\nExpected output:\n");
    lst_print(small);
    printf("Your output: \n");
    LIST *small_clone = lst_clone(small);
    lst_print(small_clone);
    printf("\n\nTest 9C\nDescription: Testing lst_clone with List of random size\nExpected output:\n");
    lst_print(large);
    printf("Your output: \n");
    LIST *large_clone = lst_clone(large);
    lst_print(large_clone);


		FREE(empty);
		FREE(small);
		FREE(large);

    /********** 11 lst_to_array ***********/
		
		large = lst_create();
		rand_size = rand() % 50;
		for(i = 0; i < rand_size; i++) {
			lst_push_back(large, rand() % 100);
		}
		printf("\n\nTest 11A\nDescription: Testing lst_to_array with List of length %d\nExpected output:\n", rand_size);
    lst_print(large);
    printf("Your output: \n");
  	int *arr = lst_to_array(large);
		print_arr(arr, lst_length(large));

		FREE(large);

    /********** 13 lst_filter_leq ***********/
		
		large = lst_create();
		empty = lst_create();
		small = lst_create();
		lst_push_back(small, 7);
		for(i = 0; i < 10; i++) {
			lst_push_back(large, i);
		}
		printf("\n\nTest 13A\nDescription: Testing lst_filter_leq with List of length 0 and cutoff of 5\nExpected output for original List after call to lst_filter_leq:\n");
		LIST *leq_empty = lst_filter_leq(empty, 5);
		printf("[ ]\n");
		printf("Your output of original List:\n");
		lst_print(empty);
		printf("Expected output for new list of leq elements:\n");
		printf("[ ]\n");
		printf("Your output of new list of leq elements:\n");
		lst_print(leq_empty);
		printf("\n\nTest 13B\nDescription: Testing lst_filter_leq with List of length 1 and cutoff of 7\nExpected output for original List after call to lst_filter_leq:\n");
		LIST *leq_small = lst_filter_leq(small, 5);
		printf("[ 7 ]\n");
		printf("Your output of original List:\n");
		lst_print(small);
		printf("Expected output for new list of leq elements:\n");
		printf("[ ]\n");
		printf("Your output of new list of leq elements:\n");
		lst_print(leq_small);
		printf("\n\nTest 13C\nDescription: Testing lst_filter_leq with List of length 0 and cutoff of 5\nExpected output for original List after call to lst_filter_leq:\n");
		LIST *leq_large = lst_filter_leq(large, 5);
		printf("[ 6  7  8  9 ]\n");
		printf("Your output of original List:\n");
		lst_print(large);
		printf("Expected output for new list of leq elements:\n");
		printf("[ 0  1  2  3  4  5 ]\n");
		printf("Your output of new list of leq elements:\n");
		lst_print(leq_large);

		FREE(leq_empty);
		FREE(leq_small);	
		FREE(leq_large);
		FREE(empty);
		FREE(small);
		FREE(large);

}