Mercurial > hg > audiodb
view libtests/test_utils_lib.h @ 408:f0a69693eaef api-inversion
The lesser of two evils, part 1.
Most of the body of audiodb_insert_datum() will apply to
"LARGE_ADB"-type insertions: checking for the right flags, checking for
enough space free, synchronizing the header. Wouldn't it be nice if we
could reuse all that code (or at least the bits that apply) without one
horrible almost-identical cut-and-paste job (see
batchinsert_large_adb(), or if that's not compelling enough, the four
almost-identical query loops from before the Great Refactoring).
Well, yes, it would. Sadly C makes it mildly difficult, because its
functions are explicitly typed (so we can't pass arbitrary arguments of
other types, even if they're ABI-compatible), while its macros are
textual (which makes writing and maintaining them horrible). The
thought of a union argument was briefly entertained and then discarded
as being just Too Weird.
So, instead, (ab)use the oldest trick in the book: void *. Define an
adb_datum_internal_t which has void * instead of double *; the intention
is that this internal data type can be constructed both from an
adb_datum_t and some notional adb_reference_t (which looks very much
like an adb_insert_t at the time of writing, with char * structure
entries representing filenames). This adb_datum_internal_t structure is
very much an internals-only thing, so put its definition in the
internals header.
Call what was previously audiodb_insert_datum() a new function
audiodb_insert_datum_internal(), made static so that really no-one is
tempted to call it other than ourselves. audiodb_insert_datum() is then
trivial in terms of this new function, if stupidly tedious. (If we were
playing dangerously, we could just perform a cast, but relying on the
fact that sizeof(double *) = sizeof(void *) would almost certainly end
up biting when we least expect.
Incidental inclusion in this patch, since I noticed it at the time:
actually check for the O2_FLAG_L2NORM before scribbling all over the
l2norm table. Somewhat unsurprisingly, there are as yet no tests to
defend against this (harmless, as it turns out) erroneous behaviour.
author | mas01cr |
---|---|
date | Tue, 09 Dec 2008 20:53:39 +0000 |
parents | 7e6c99481b8b |
children | e072aa1611f5 342822c2d49a |
line wrap: on
line source
void delete_dir(char * dirname); void clean_remove_db(char * dirname); void test_status(adb_ptr d, adb_status_ptr b); unsigned int test_insert( adb_ptr d, char * features, char * power, char * key); void dump_query(adb_query_ptr adbq, adb_queryresult_ptr myadbqueryresult); int testoneresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, double Dist,double Qpos,double Spos); double doubleabs(double foo); void maketestfile(char * filename, int * ivals, double * dvals, int dvalsize); int testoneradiusresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, int count); void makekeylistfile(char * filename, char * item); /* clean remove */ void clean_remove_db(char * dbname){ FILE* db=0; db=fopen(dbname,"r"); if (!db){ return; } fclose(db); remove(dbname); return; } /* delete directory */ void delete_dir(char * dirname){ struct dirent *d; DIR *dir; char buf[256]; printf("Deleting directory '%s' and all files\n", dirname); dir = opendir(dirname); if (dir){ while((d = readdir(dir))) { //printf("Deleting %s in %s\n",d->d_name, dirname); sprintf(buf, "%s/%s", dirname, d->d_name); remove(buf); } } closedir(dir); rmdir(dirname); return; } unsigned int test_insert( adb_ptr d, char * features, char * power, char * key ){ adb_insert_t myinsert={0}; unsigned int myerr=0; printf("Insert:\n"); myinsert.features=features; myinsert.power=power; myinsert.key=key; myerr=audiodb_insert(d,&myinsert); printf("\n"); return myerr; } void test_status(adb_ptr d, adb_status_ptr b){ /* get the status of the database */ audiodb_status(d,b); /* could probably make this look a bit more clever, but it works for now */ printf("numFiles:\t%d\n",b->numFiles); printf("dim:\t%d\n",b->dim); printf("length:\t%d\n",b->length); printf("dudCount:\t%d\n",b->dudCount); printf("nullCount:\t%d\n",b->nullCount); printf("flags:\t%d\n",b->flags); return; } void dump_query(adb_query_ptr adbq, adb_queryresult_ptr myadbqueryresult){ int size=0; int i=0; size=myadbqueryresult->sizeRlist; printf("Dumping query:\n"); for(i=0; i<size; i++){ printf("\t'%s' query: Result %02d:%s is dist:%f qpos:%d spos:%d\n", adbq->querytype, i, myadbqueryresult->Rlist[i], myadbqueryresult->Dist[i], myadbqueryresult->Qpos[i], myadbqueryresult->Spos[i] ); } printf("\n"); } int testoneresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, double Dist,double Qpos,double Spos){ int ret=0; double tolerance=.0001; if (strcmp(Rlist,myadbqueryresult->Rlist[i])){ ret=-1; } if (doubleabs((double)Dist - (double)myadbqueryresult->Dist[i]) > tolerance){ ret=-1; } if (doubleabs((double)Qpos - (double)myadbqueryresult->Qpos[i]) > tolerance){ ret=-1; } if (doubleabs((double)Spos - (double)myadbqueryresult->Spos[i]) > tolerance){ ret=-1; } return ret; } int testoneradiusresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, int count){ int ret=0; if (strcmp(Rlist,myadbqueryresult->Rlist[i])){ ret=-1; } /* KLUDGE: at the moment, the structure returned from "sequence" queries with a radius has two unused fields, Dist and Qpos, and the Spos field is punned to indicate the count of hits from that track. This is really ugly and needs to die. */ if (count != myadbqueryresult->Spos[i]) { ret=-1; } return ret; } double doubleabs(double foo){ double retval=foo; if (foo < 0.0) { retval=foo * -1.0; } return retval; } void maketestfile(char * filename, int * ivals, double * dvals, int dvalsize) { FILE * myfile; myfile=fopen(filename,"w"); fwrite(ivals,sizeof(int),1,myfile); fwrite(dvals,sizeof(double),dvalsize,myfile); fflush(myfile); fclose(myfile); /* should probably test for success, but then it is a test suite already... */ } void makekeylistfile(char * filename, char * item){ FILE * myfile; myfile=fopen(filename,"w"); fprintf(myfile,"%s\n",item); fflush(myfile); fclose(myfile); }