int main(int argc, const char **argv) { float *inputs; int i, error; memory_t memory; cache_t cache; membase_t *p_mem; float_heap heap; /* Set up the simulated memory. */ p_mem = make_cached_memory(argc, argv, NUM_ELEMS * sizeof(int)); /* Generate random floats to sort. */ printf("Generating %d random floats to sort.\n", NUM_ELEMS); inputs = malloc(NUM_ELEMS * sizeof(float)); srand48(SEED); for (i = 0; i < NUM_ELEMS; i++) inputs[i] = (float) drand48(); /* Use the heap to sort the sequence of floats. */ printf("Sorting numbers using the heap.\n"); init_heap(&heap, p_mem, NUM_ELEMS); for (i = 0; i < NUM_ELEMS; i++) add_value(&heap, inputs[i]); /* Sort the inputs so that we can check the heap's results. */ printf("Checking the results against the sorted inputs.\n"); qsort(inputs, NUM_ELEMS, sizeof(float), compare_float_ptrs); error = 0; for (i = 0; i < NUM_ELEMS; i++) { float val = get_first_value(&heap); if (val != inputs[i]) { printf("ERROR: heap and sorted array don't match at " "index %d! heap = %f, val = %f\n", i, val, inputs[i]); error = 1; } } if (error) { printf("Some values didn't match, aborting.\n"); abort(); } /* Print out the results of the heap sort. */ printf("\nMemory-Access Statistics:\n\n"); p_mem->print_stats(p_mem); printf("\n"); return 0; }
/* Test the heap. */ int main(int argc, char **argv) { float_heap h; int i; float lastval; init_heap(&h); /* Use a seed so that the sequence of random values * is always the same. */ srand(11); /* Fill up the heap with random values. */ for (i = 0; i < MAX_HEAP_ELEMS; i++) add_value(&h, rand() % 1000); /* * Print out the results. If anything is out of order, * flag it. (Pull the very first value separately so that * we don't have to set lastval to something strange just * to get the test to work...) */ lastval = get_first_value(&h); printf("Value 0 = %f\n", lastval); for (i = 1; i < MAX_HEAP_ELEMS; i++) { float val = get_first_value(&h); printf("Value %d = %f\n", i, val); if (val < lastval) printf(" ERROR: OUT OF ORDER.\n"); lastval = val; } return 0; }
FILE* be_init(int inout,url_t* u,int iszipped) { FILE* fh=NULL; long a=0; char* err=NULL; int fd; #if HAVE_FCNTL && HAVE_FTRUNCATE struct flock fl; #endif if (u==NULL) { return NULL; } switch (u->type) { case url_file : { u->value = expand_tilde(u->value); error(200,_("Opening file \"%s\" for %s\n"),u->value,inout?"r":"w+"); #if HAVE_FCNTL && HAVE_FTRUNCATE fd=open(u->value,inout?O_RDONLY:O_CREAT|O_RDWR,0666); #else fd=open(u->value,inout?O_RDONLY:O_CREAT|O_RDWR|O_TRUNC,0666); #endif error(255,"Opened file \"%s\" with fd=%i\n",u->value,fd); if(fd==-1) { error(0,_("Couldn't open file %s for %s"),u->value, inout?"reading\n":"writing\n"); return NULL; } #if HAVE_FCNTL && HAVE_FTRUNCATE if(!inout) { fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; if (fcntl(fd, F_SETLK, &fl) == -1) { if (fcntl(fd, F_SETLK, &fl) == -1) error(0,_("File %s is locked by another process.\n"),u->value); else error(0,_("Cannot get lock for file %s"),u->value); return NULL; } if(ftruncate(fd,0)==-1) error(0,_("Error truncating file %s"),u->value); } #endif #ifdef WITH_ZLIB if(iszipped && !inout){ fh=gzdopen(fd,"wb9"); if(fh==NULL){ error(0,_("Couldn't open file %s for %s"),u->value, inout?"reading\n":"writing\n"); } } else{ #endif fh=fdopen(fd,inout?"r":"w+"); if(fh==NULL){ error(0,_("Couldn't open file %s for %s"),u->value, inout?"reading\n":"writing\n"); } #ifdef WITH_ZLIB } #endif return fh; } case url_stdout : { #ifdef WITH_ZLIB if(iszipped){ return gzdopen(fileno(stdout),"wb"); } else{ #endif return stdout; #ifdef WITH_ZLIB } #endif } case url_stdin : { #ifdef WITH_ZLIB if(iszipped){ return gzdopen(fileno(stdin),"r"); } else{ #endif return stdin; #ifdef WITH_ZLIB } #endif } case url_stderr : { #ifdef WITH_ZLIB if(iszipped){ return gzdopen(fileno(stderr),"wb"); } else{ #endif return stderr; #ifdef WITH_ZLIB } #endif } case url_fd : { a=strtol(u->value,&err,10); if(*err!='\0'||errno==ERANGE){ error(0,"Illegal file descriptor value:%s\n",u->value); } #ifdef WITH_ZLIB if(iszipped && !inout){ fh=gzdopen(a,"w"); if(fh==NULL){ error(0,"Couldn't reopen file descriptor %li\n",a); } } else{ #endif fh=fdopen(a,inout?"r":"w"); if(fh==NULL){ error(0,"Couldn't reopen file descriptor %li\n",a); } #ifdef WITH_ZLIB } #endif return fh; } #ifdef WITH_PSQL case url_sql : { char *pghost, *pgport, *pgoptions, *pgtty, *dbName, *login, *pwd; char *tmp,*tmp2; psql_data* ret = (psql_data*) malloc(sizeof(psql_data)*1); if (ret==NULL) { error(0,"Not enough memory for postgres sql connection\n"); return ret; } tmp=strdup(u->value); tmp2=tmp; pgtty=NULL;pgoptions=NULL; if ((pghost=get_first_value(&tmp)) == NULL) { error(0,"Must define host for Postgres sql connection\n"); free(tmp2); return NULL; } else { error(100,"Psql host is %s\n",pghost); if ((pgport=get_first_value(&tmp)) == NULL) { error(0,"Must define port for Postgres sql connection\n"); free(tmp2); return NULL; } else { error(100,"Psql port is %s\n",pgport); if ((dbName=get_first_value(&tmp)) == NULL) { error(0,"Must define name for database for Postgres sql connection\n"); free(tmp2); return NULL; } else { error(100,"Psql db is %s\n",dbName); if ((login=get_first_value(&tmp)) == NULL) { error(0,"Must define login for Postgres sql connection\n"); free(tmp2); return NULL; } else { error(100,"Psql login is %s\n",login); if ((pwd=get_first_value(&tmp)) == NULL) { error(0,"Must define password for database for Postgres sql connection\n"); free(tmp2); return NULL; } else { error(100,"Psql passwd is %s\n",pwd); if ((ret->table=get_first_value(&tmp))==NULL) { error(0,"Must define table for sql..\n"); free(tmp2); return NULL; } else { if (ret->table[0]=='\0') { error(0,"Must define table for sql..\n"); free(tmp2); return NULL; } else { /* everything went ok.. */ } } } } } } } if (login[0] == '\0' ) { login = NULL; } if (pwd[0] == '\0' ) { pwd = NULL; } ret->conn = PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbName,login,pwd); if (PQstatus(ret->conn) == CONNECTION_BAD){ error(0,"Postgres sql error during connection\n"); free(tmp2); return NULL; } /* Otherwise we would become to situation that name of table would be freeed */ ret->table = strdup(ret->table); /* And now we have made a connection to database.. Next thing we do is to begin a new transaction block */ ret->res = PQexec(ret->conn, "BEGIN"); if (!ret->res || PQresultStatus(ret->res) != PGRES_COMMAND_OK) { error(0,"BEGIN command failed... \n"); PQclear(ret->res); free(ret); ret=NULL; } else { PQclear(ret->res); if ((inout?be_sql_readinit(ret):RETOK)!=RETOK) { error(255,"Something went wrong with sql backend init.\n"); return NULL; } } free(tmp2); return ret; } #endif #ifdef WITH_CURL case url_http: case url_https: case url_ftp: { error(200,_("Opening curl \"%s\" for %s\n"),u->value,inout?"r":"w+"); if (iszipped) { return NULL; } return url_fopen(u->value,inout?"r":"w+"); } #endif /* WITH CURL */ default:{ error(0,"Unsupported backend: %i", u->type); return NULL; } } /* Not reached */ return NULL; }