char *check_tree(struct tree *t, char *spec) //@ requires tree(t, ?v) &*& [_]string(spec, ?cs); /*@ ensures switch (check_tree_pure(v, cs)) { case check_fail: return result == 0; case check_success(cs0): return result != 0 &*& [_]string(result, cs0); }; @*/ { bool b = tree_is_leaf(t); //@ string_limits(spec); //@ open string(spec, _); if (*spec == 'L') { tree_dispose(t); if (!b) return 0; return spec + 1; } else if (*spec == 'N') { struct tree *l; struct tree *r; if (b) { tree_dispose(t); return 0; } tree_destruct_node(t, &l, &r); spec = check_tree(l, spec + 1); if (spec == 0) { tree_dispose(r); return 0; } return check_tree(r, spec); } else { tree_dispose(t); return 0; } }
int main() //@ requires emp; //@ ensures emp; { struct node *node0 = create_tree(); struct node *node = node0; node = tree_add_left(node); node = tree_add_right(node); node = tree_get_parent(node); node = tree_add_left(node); node = tree_get_parent(node); node = tree_get_parent(node); //@ assert(node == node0); tree_dispose(node); return 0; }
bool build(struct list *s, struct tree **t) //@ requires list(s, ?vs) &*& pointer(t, _); /*@ ensures list(s, ?rvs) &*& pointer(t, ?rt) &*& switch (build0(vs)) { case build_fail: return result == false; case build_success(rvt): return result == true &*& tree(rt, rvt) &*& rvs == nil; }; @*/ { if (!build_rec(0, s, t)) return false; if (!list_is_empty(s)) { tree_dispose(*t); return false; } return true; }
bool build_rec(int d, struct list *s, struct tree **t) //@ requires list(s, ?vs) &*& pointer(t, _); /*@ ensures list(s, ?rvs) &*& pointer(t, ?rt) &*& switch (build_rec1(d, vs)) { case fail: return result == false; case success(rvt, rvs0): return result == true &*& rvs == rvs0 &*& tree(rt, rvt); }; @*/ // decreases max_func(0, fold_left(0, max_func, vs) - d); // Not yet checked by VeriFast. { //@ build_rec1_eq(d, vs); struct tree *l; struct tree *r; if (list_is_empty(s)) return false; int h = list_head(s); if (h < d) return false; if (h == d) { list_pop(s); struct tree *leaf = create_leaf(); *t = leaf; return true; } if (!build_rec(d+1, s, &l)) return false; if (!build_rec(d+1, s, &r)) { tree_dispose(l); return false; } struct tree *node = create_node(l, r); *t = node; return true; }
int deserialize( MainTreePt *camioes, int (*comparaCamioes[DIM])(void*,void*), MainTreePt *clientes, int (*comparaClientes[DIM])(void*,void*), TabelaHashPTR *localidades, int (*comparaLocalidades)(void*,void*), int(*hash_function)(void*,int) ){ short int strl; char *str; char *locOrigem; int nelem, nelems, tmp; int nAdj, nAdjs; double dArr[2]; int teste; FILE *file; fpos_t file_pos; apagaTabelaHash(*localidades); tree_dispose(camioes); tree_dispose(clientes); file = fopen("dados.sav", "r"); if( file == NULL ) return 0; fread( &tmp, sizeof(int), 1, file ); *localidades = criaTabelaHash(hash_function, tmp, comparaLocalidades); fread( &nelems, sizeof(int), 1, file ); fgetpos( file, &file_pos ); //guardar a posição actual // inserir as localidades for( nelem=0; nelem<nelems; nelem++ ){ fread( &strl, sizeof(short int), 1, file); str = novaString(strl); *(str+strl) = '\0'; fread( str, sizeof(char), strl, file ); teste = inserelocalidade( *localidades, str ); // localidade inserida if( teste != 1 ) printf("."); fread( &nAdjs, sizeof(int), 1, file ); for( nAdj = 0; nAdj < nAdjs; nAdj++ ){ fread( &strl, sizeof(short int), 1, file ); fseek( file, strl*sizeof(char) + 2*sizeof(double) , SEEK_CUR ); } } //agora que já tem as localidades, volta atrás e insere as adjacencias fsetpos( file, &file_pos ); for( nelem=0; nelem<nelems; nelem++ ){ fread( &strl, sizeof(short int), 1, file); locOrigem = novaString(strl); *(locOrigem+strl) = '\0'; fread( locOrigem, sizeof(char), strl, file ); fread( &nAdjs, sizeof(int), 1, file ); for( nAdj = 0; nAdj < nAdjs; nAdj++ ){ fread( &strl, sizeof(short int), 1, file ); free(str); str = novaString(strl); *(str+strl) = '\0'; fread( str, sizeof(char), strl, file ); fread( &dArr, sizeof(double), 2, file ); inserirligacao(*localidades, locOrigem, str, dArr[0], dArr[1]); } } free(locOrigem); //camiões unsigned int id; char *matricula=NULL; char *local=NULL; unsigned int ncamiao, ncamioes; *camioes = tree_new( comparaCamioes ); fread( &ncamioes, sizeof(unsigned int), 1, file ); for( ncamiao=0; ncamiao<ncamioes; ncamiao++ ){ fread( &id, sizeof(unsigned int), 1, file); fread( &dArr, sizeof(double), 2, file ); fread( &strl, sizeof(short int), 1, file ); matricula = novaString(strl); *(matricula+strl) = '\0'; fread( matricula, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); local = novaString(strl); *(local+strl) = '\0'; fread( local, sizeof(char), strl, file ); tree_insert( *camioes, camiao_novo(id, matricula, dArr[0], dArr[1], local)); } //clientes ClientePt clt=NULL; unsigned int nif; char *nome; char *morada; unsigned int ncliente, nclientes; int nservico, nservicos; char *datahora; char *cmatricula; char *origem; char *carga; char *destino; *clientes = tree_new( comparaClientes ); fread( &nclientes, sizeof(unsigned int), 1, file); for( ncliente=0; ncliente<nclientes; ncliente++ ){ fread( &nif, sizeof(unsigned int), 1, file); fread( &strl, sizeof(short int), 1, file ); nome = novaString(strl); *(nome+strl) = '\0'; fread( nome, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); morada = novaString(strl); *(morada+strl) = '\0'; fread( morada, sizeof(char), strl, file ); clt = cliente_novo( nif, nome, morada, criaListaLigada( cliente_comparaServico )); tree_insert( *clientes, clt ); fread( &nservicos, sizeof(int), 1, file ); for( nservico=0; nservico<nservicos; nservico++){ fread( &dArr, sizeof(double), 2, file ); fread( &strl, sizeof(short int), 1, file ); datahora = novaString(strl); *(datahora+strl) = '\0'; fread( datahora, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); cmatricula = novaString(strl); *(cmatricula+strl) = '\0'; fread( cmatricula, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); origem = novaString(strl); *(origem+strl) = '\0'; fread( origem, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); carga = novaString(strl); *(carga+strl) = '\0'; fread( carga, sizeof(char), strl, file ); fread( &strl, sizeof(short int), 1, file ); destino = novaString(strl); *(destino+strl) = '\0'; fread( destino, sizeof(char), strl, file ); cliente_insereServico(*clientes, nif, matricula, dArr[0], dArr[1], origem, carga, destino ); } } return 1; }