void free_generations() { gen_pointer node; gen_pointer next; for(node = hist_head; node != NULL; node = next) { next = node->next; if (node->dusts) free_dust (node->dusts); if (node->planets) free_planet (node->planets); free (node); } if (dust_head != NULL) free_dust (dust_head); if (planet_head != NULL) free_planet (planet_head); dust_head = NULL; planet_head = NULL; hist_head = NULL; }
void free_wator(wator_t* pw){ /* ritono se pw non è valido */ if ( ! pw ) return; /* libero la memoria di planet */ if ( pw->plan ) free_planet ( pw->plan ) ; free( pw ); }
/** * * testa le funzioni load_planet e store_planet leggendo il pianeta da un file, riscrivendolo su un altro e confrontando i due file * * \param filename il nome del file dal quale leggere i dati del pianeta * \param formato: specifica se il file è formattato correttamente. In questo caso la funzione load_planet deve rilevare l'errore * * \return 1 se il test è superato * \return 0 altrimenti * * * */ int test_load_store (const char *filename, int formato) { FILE *fp, *fo; planet_t *pl; int ret=1; fp = fopen (filename,"r"); pl=load_planet (fp); if ( pl && formato ) { fo = fopen ("custom_test/result.dat","w"); print_planet (fo, pl); rewind (fp); fclose (fo); fo = fopen ("custom_test/result.dat","r"); //se i due file sono uguali, il test è andato a buon fine ret = confronto_file (fp, fo); fclose (fo); } else if ( (pl && !formato) || (!pl && formato) || (!pl && !formato && errno != ERANGE) ) { ret = 0; } free_planet (pl); fclose (fp); return ret; }
planet_t* load_planet (FILE* f){ planet_t *p ; char c; int nr, nc, i, j; /* la lettura di numeri negativi è considerata errata */ if ( fscanf(f,"%d\n%d\n", &nr, &nc) != 2 || nr<0 || nc <0 ){ errno = ERANGE; return NULL; } p = new_planet( nr, nc ); for (i=0; i<nr; i++) for (j=0; j<nc; j++) /* per ogni cella mi aspetto di leggere esattamente e solo il carattere o poi uno spazio o un endline*/ if ( fscanf( f, (j==nc-1) ? "%c\n" : "%c " , &c ) != 1 || ( p->w[i][j] = char_to_cell(c) ) == '?' ){ errno = ERANGE; /* libero la memoria */ free_planet( p ); return NULL; } return p; }
/** * * questo programma testa tutte le funzioni finora implementate * * esegue un test alla volta passando il risultato alla funzione print_result * **/ int main () { planet_t *pl; wator_t *wt; cell_t **copia; cell_t c; int k,l,p,g,n; FILE *f1, *f2; mtrace(); print_result ( cell_to_char(WATER) == 'W' , "cell_to_char(WATER)" ); print_result ( cell_to_char(FISH) == 'F' , "cell_to_char(FISH)" ); print_result ( cell_to_char(SHARK) == 'S' , "cell_to_char(SHARK)" ); print_result ( char_to_cell('W') == WATER , "char_to_cell('W')" ); print_result ( char_to_cell('F') == FISH , "char_to_cell('F')" ); print_result ( char_to_cell('S') == SHARK , "char_to_cell('S')" ); print_result ( char_to_cell('P') == -1 , "char_to_cell('P')" ); print_result ( char_to_cell( cell_to_char (WATER) ) == WATER , "char_to_cell( cell_to_char (WATER) )" ); print_result ( cell_to_char( char_to_cell ('F') ) == 'F' , "cell_to_char( char_to_cell ('F') )" ); test_matrice_generica (10,40); test_matrice_generica (1000,40000); pl=new_planet (10,10); print_result ( ( pl!=NULL && pl->nrow==10 && pl->ncol==10 ) || errno==ENOMEM, "new_planet (10,10)" ); free_planet (pl); contatore_pesci=contatore_squali=0; print_result ( controlla_formato_e_conta_squali_e_pesci ('W', ' ', 0, &c ) && c == WATER , "controlla_formato_e_conta_squali_e_pesci ('W', ' ', 0, &c )" ); print_result ( controlla_formato_e_conta_squali_e_pesci ('F', ' ', 0, &c ) && c == FISH && contatore_pesci==1 , "controlla_formato_e_conta_squali_e_pesci ('F', ' ', 0, &c )" ); print_result ( controlla_formato_e_conta_squali_e_pesci ('S', '\n', 1, &c ) && c == SHARK && contatore_squali==1 , "controlla_formato_e_conta_squali_e_pesci ('S', '\\n', 1, &c )" ); print_result ( ! controlla_formato_e_conta_squali_e_pesci ('M', '\n', 1, &c ), "controlla_formato_e_conta_squali_e_pesci ('M', '\\n', 1, &c )" ); print_result ( ! controlla_formato_e_conta_squali_e_pesci ('W', '\n', 0, &c ), "controlla_formato_e_conta_squali_e_pesci ('W', '\\n', 0, &c )" ); print_result ( ! controlla_formato_e_conta_squali_e_pesci ('W', 'a', 0, &c ), "controlla_formato_e_conta_squali_e_pesci ('W', 'a', 0, &c )" ); print_result ( test_load_store ("custom_test/planet1.dat", 1), "test_load_store(\"custom_test/planet1.dat\")" ); print_result ( test_load_store ("custom_test/planet2.dat", 1), "test_load_store(\"custom_test/planet2.dat\")" ); print_result ( test_load_store ("custom_test/planet3.dat", 0), "test_load_store(\"custom_test/planet3.dat\")" ); print_result ( test_load_store ("custom_test/planet4.dat", 0), "test_load_store(\"custom_test/planet4.dat\")" ); print_result ( test_load_store ("custom_test/planet5.dat", 0), "test_load_store(\"custom_test/planet5.dat\")" ); wt = malloc ( sizeof(wator_t) ); wt->plan = NULL; system ("rm -f wator.conf"); print_result ( read_wator_conf (wt) == -1, "read_wator_conf without wator.conf" ); system ("cp custom_test/wator.conf.1 wator.conf"); print_result ( read_wator_conf (wt) == 1 && wt->sd == 100 && wt->sb == 20 && wt->fb == 15 , "read_wator_conf wator.conf.1" ); system ("cp custom_test/wator.conf.2 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.2" ); system ("cp custom_test/wator.conf.3 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.3" ); system ("cp custom_test/wator.conf.4 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.4" ); system ("cp custom_test/wator.conf.5 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.5" ); system ("cp custom_test/wator.conf.6 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.6" ); system ("cp custom_test/wator.conf.7 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.7" ); system ("cp custom_test/wator.conf.8 wator.conf"); print_result ( ! read_wator_conf(wt) && errno == ERANGE , "read_wator_conf wator.conf.8" ); free_wator (wt); system ("cp custom_test/wator.conf.1 wator.conf"); wt=new_wator ("custom_test/planet1.dat"); print_result ( wt != NULL && wt->nf == 701 && wt->ns == 300 && wt->sd == 100 && wt->sb == 20 && wt->fb == 15 && wt->plan != NULL && wt->plan->nrow == 200 && wt->plan->ncol == 130 && wt->plan->w[3][7] == SHARK , "new_wator"); k=7; l=6; print_result ( ricerca_cella (wt->plan, &k, &l, FISH) && k==7 && l==7 , "ricerca_cella (wt->pl, 7, 6, FISH)" ); k=l=0; print_result ( ricerca_cella (wt->plan, &k, &l, SHARK) && k==199 && l==0 , "ricerca_cella (wt->pl, 0, 0, SHARK)" ); k=1; l=3; print_result ( ! ricerca_cella (wt->plan, &k, &l, SHARK) && k==1 && l==3 , "ricerca_cella (wt->pl, 1, 3, SHARK)" ); k=5; l=2; print_result ( ricerca_cella (wt->plan, &k, &l, FISH) && k==5 && ( l==3 || l== 1 ), "ricerca_cella (wt->pl, 5, 2, SHARK)" ); k=20; l=25; print_result ( ricerca_cella (wt->plan, &k, &l, WATER) && ( (k==19 && l==25 ) || ( k== 20 && l== 24 ) ), "ricerca_cella (wt->pl, 20, 25, WATER)" ); print_result ( shark_rule1 ( wt, 0, 2, &k, &l ) == -1 , "shark_rule_1 (0,2)" ); print_result ( shark_rule1 ( wt, 1, 14, &k, &l ) == MOVE, "shark_rule_1 (1,15)" ); print_result ( shark_rule1 ( wt, 7, 6, &k, &l ) == EAT && k==7 && l==7, "shark_rule_1 (7,7)" ); //resetto il pianeta free_wator (wt); wt=new_wator ("custom_test/planet1.dat"); n=10; wt->plan->btime[1][14] = 3; print_result ( ! riproduzione_generica (wt->plan, 1, 14, &k, &l, 4, &n ) && k==1 && l==14 && n==10, "riproduzione_generica (wt->plan, 1, 14) - nothing" ); p=1; g=14; print_result ( riproduzione_generica (wt->plan, 1, 14, &k, &l, 4, &n ) && ricerca_cella (wt->plan, &p, &g, SHARK) && p==k && g==l && n==11, "riproduzione_generica (wt->plan, 1, 14) - born" ); wt->plan->dtime[3][7] = wt->sd; print_result ( shark_rule2 ( wt, 3, 7, &k, &l ) == DEAD && wt->plan->w[3][7] == WATER, "shark_rule2 ( wt, 3, 7) - died" ); print_result ( shark_rule2 ( wt, 7, 6, &k, &l ) == ALIVE && wt->plan->btime[7][6] == 1 , "shark_rule2 ( wt, 7, 6) - nothing" ); wt->plan->btime[7][6] = wt->sb; p=7; g=6; print_result ( shark_rule2 ( wt, 7, 6, &k, &l ) == ALIVE && ricerca_cella ( wt->plan, &p, &g, SHARK ) && p==k && g==l , "shark_rule2 ( wt, 7, 6) - born" ); //resetto il pianeta free_wator (wt); wt=new_wator ("custom_test/planet1.dat"); p=0; g=12; print_result ( fish_rule3 ( wt, 0, 12, &k, &l ) == MOVE && wt->plan->w[0][12] == WATER && ricerca_cella (wt->plan,&p,&g,FISH) && p==k && g==l , "fish_rule_3 ( wt, 0, 12) - moved" ); print_result ( ! fish_rule4 (wt, 5, 70, &k, &l ) && wt->plan->btime[5][70] == 1 , "fish_rule4 (wt, 5, 70) - nothing" ); print_result ( conta_generico ( wt->plan, FISH ) == 701 , "conta_generico ( wt->plan, FISH )" ); print_result ( conta_generico ( wt->plan, SHARK ) == 300 , "conta_generico ( wt->plan, SHARK )" ); print_result ( conta_generico ( wt->plan, WATER ) == wt->plan->ncol*wt->plan->nrow - 701 - 300 , "conta_generico ( wt->plan, WATER )" ); wt->plan->btime[5][70] = wt->fb; p=5; g=70; print_result ( ! fish_rule4 (wt, 5, 70, &k, &l ) && wt->plan->btime[5][70] == 0 && ricerca_cella (wt->plan, &p,&g,FISH) && p==k && g==l , "fish_rule4 (wt, 5, 70) - born" ); //quello in più rispetto a 701 del test di prima è appena nato. Auguri! print_result ( fish_count ( wt->plan ) == wt->nf , "fish_count ( wt->plan ) == wt->nf"); print_result ( shark_count ( wt->plan ) == wt->ns , "fish_count ( wt->plan ) == wt->ns"); //copio il pianeta e lo scrivo sul file. Poi confronto i due file, per vedere se sono uguali copia = (cell_t**) alloca_matrice_generica ( wt->plan->nrow, wt->plan->ncol, sizeof (cell_t), sizeof (cell_t *) ); copia_pianeta (copia, wt->plan->w, wt->plan->nrow, wt->plan->ncol); pl = malloc ( sizeof (planet_t) ); pl->w = copia; pl->nrow = wt->plan->nrow; pl->ncol = wt->plan->ncol; pl->btime = NULL; pl->dtime = NULL; f1 = fopen ( "custom_test/result_wt.dat", "w" ); f2 = fopen ( "custom_test/result_copia.dat", "w" ); print_planet ( f1, wt->plan ); print_planet ( f2, pl ); fclose (f1); fclose (f2); print_result ( confronto_file ( f1,f2 ), "copia_pianeta ()" ); free_wator (wt); free_planet (pl); wt = new_wator ("custom_test/planet1.dat"); free_wator (wt); muntrace(); return 0; }