comparison libtests/test_utils_lib.h @ 498:342822c2d49a

Merge api-inversion branch (-r656:771, but I don't expect to return to that branch) into the trunk. I expect there to be minor performance regressions (e.g. in the SOAP server index cacheing, which I have forcibly removed) and minor unplugged memory leaks (e.g. in audioDB::query(), where I don't free up the datum). I hope that these leaks and performance regressions can be plugged in short order. I also expect that some (but maybe not all) of the issues currently addressed in the memory-leaks branch are superseded or fixed by this merge. There remains much work to be done; go forth and do it.
author mas01cr
date Sat, 10 Jan 2009 16:47:57 +0000
parents 7e6c99481b8b
children e18843dc0aea
comparison
equal deleted inserted replaced
476:a7193678280b 498:342822c2d49a
1 void delete_dir(char * dirname); 1 #include <sys/types.h>
2 void clean_remove_db(char * dirname); 2 #include <sys/stat.h>
3 void test_status(adb_ptr d, adb_status_ptr b); 3 #include <math.h>
4 unsigned int test_insert( adb_ptr d, char * features, char * power, char * key); 4 #include <unistd.h>
5 void dump_query(adb_query_ptr adbq, adb_queryresult_ptr myadbqueryresult); 5 #include <fcntl.h>
6 int testoneresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, double Dist,double Qpos,double Spos); 6 #include <string.h>
7 double doubleabs(double foo); 7 #include <stdio.h>
8 void maketestfile(char * filename, int * ivals, double * dvals, int dvalsize); 8 #include <stdlib.h>
9 int testoneradiusresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, int count);
10 void makekeylistfile(char * filename, char * item);
11 9
10 #define TESTDB "testdb"
12 11
13 12 void clean_remove_db(char * dbname) {
14 13 unlink(dbname);
15 /* clean remove */
16 void clean_remove_db(char * dbname){
17
18 FILE* db=0;
19
20 db=fopen(dbname,"r");
21
22 if (!db){
23 return;
24 }
25
26
27 fclose(db);
28 remove(dbname);
29
30 return;
31
32 } 14 }
33 15
16 void maketestfile(const char *path, int dim, double *doubles, int ndoubles) {
17 FILE *file;
34 18
35 /* delete directory */ 19 file = fopen(path, "w");
36 void delete_dir(char * dirname){ 20 fwrite(&dim, sizeof(int), 1, file);
37 21 fwrite(doubles, sizeof(double), ndoubles, file);
38 struct dirent *d; 22 fflush(file);
39 DIR *dir; 23 fclose(file);
40 char buf[256];
41
42 printf("Deleting directory '%s' and all files\n", dirname);
43 dir = opendir(dirname);
44
45 if (dir){
46 while((d = readdir(dir))) {
47 //printf("Deleting %s in %s\n",d->d_name, dirname);
48 sprintf(buf, "%s/%s", dirname, d->d_name);
49 remove(buf);
50 }
51 }
52 closedir(dir);
53
54 rmdir(dirname);
55
56
57 return;
58
59 } 24 }
60 25
61 26 int close_enough(double a, double b, double epsilon) {
62 unsigned int test_insert( 27 return (fabs(a-b) < epsilon);
63 adb_ptr d,
64 char * features,
65 char * power,
66 char * key
67 ){
68
69 adb_insert_t myinsert={0};
70 unsigned int myerr=0;
71
72 printf("Insert:\n");
73 myinsert.features=features;
74 myinsert.power=power;
75 myinsert.key=key;
76 myerr=audiodb_insert(d,&myinsert);
77 printf("\n");
78
79 return myerr;
80
81 } 28 }
82 29
83 void test_status(adb_ptr d, adb_status_ptr b){ 30 int result_position(adb_query_results_t *r, const char *key, float dist, uint32_t qpos, uint32_t ipos) {
84 31 for(uint32_t k = 0; k < r->nresults; k++) {
85 /* get the status of the database */ 32 adb_result_t result = r->results[k];
86 audiodb_status(d,b); 33 if(close_enough(dist, result.dist, 1e-4) && (qpos == result.qpos) &&
87 34 (ipos == result.ipos) && !(strcmp(key, result.key))) {
88 /* could probably make this look a bit more clever, but it works for now */ 35 return k;
89 printf("numFiles:\t%d\n",b->numFiles); 36 }
90 printf("dim:\t%d\n",b->dim); 37 }
91 printf("length:\t%d\n",b->length); 38 return -1;
92 printf("dudCount:\t%d\n",b->dudCount);
93 printf("nullCount:\t%d\n",b->nullCount);
94 printf("flags:\t%d\n",b->flags);
95
96 return;
97 } 39 }
98 40
99 41 #define result_present_or_fail(r, k, d, q, i) \
100 void dump_query(adb_query_ptr adbq, adb_queryresult_ptr myadbqueryresult){ 42 if(result_position(r, k, d, q, i) < 0) return 1;
101
102 int size=0;
103 int i=0;
104
105 size=myadbqueryresult->sizeRlist;
106
107 printf("Dumping query:\n");
108 for(i=0; i<size; i++){
109 printf("\t'%s' query: Result %02d:%s is dist:%f qpos:%d spos:%d\n",
110 adbq->querytype,
111 i,
112 myadbqueryresult->Rlist[i],
113 myadbqueryresult->Dist[i],
114 myadbqueryresult->Qpos[i],
115 myadbqueryresult->Spos[i]
116 );
117 }
118 printf("\n");
119
120 }
121
122
123
124 int testoneresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, double Dist,double Qpos,double Spos){
125
126 int ret=0;
127 double tolerance=.0001;
128
129
130
131 if (strcmp(Rlist,myadbqueryresult->Rlist[i])){
132 ret=-1;
133 }
134
135
136 if (doubleabs((double)Dist - (double)myadbqueryresult->Dist[i]) > tolerance){
137 ret=-1;
138 }
139
140 if (doubleabs((double)Qpos - (double)myadbqueryresult->Qpos[i]) > tolerance){
141 ret=-1;
142 }
143
144 if (doubleabs((double)Spos - (double)myadbqueryresult->Spos[i]) > tolerance){
145 ret=-1;
146 }
147
148 return ret;
149 }
150
151
152 int testoneradiusresult(adb_queryresult_ptr myadbqueryresult, int i, char * Rlist, int count){
153
154 int ret=0;
155
156 if (strcmp(Rlist,myadbqueryresult->Rlist[i])){
157 ret=-1;
158 }
159
160 /* KLUDGE: at the moment, the structure returned from "sequence"
161 queries with a radius has two unused fields, Dist and Qpos, and
162 the Spos field is punned to indicate the count of hits from
163 that track. This is really ugly and needs to die. */
164 if (count != myadbqueryresult->Spos[i]) {
165 ret=-1;
166 }
167
168 return ret;
169 }
170
171
172 double doubleabs(double foo){
173
174 double retval=foo;
175
176 if (foo < 0.0) {
177 retval=foo * -1.0;
178 }
179
180 return retval;
181 }
182
183
184
185 void maketestfile(char * filename, int * ivals, double * dvals, int dvalsize) {
186
187 FILE * myfile;
188
189 myfile=fopen(filename,"w");
190 fwrite(ivals,sizeof(int),1,myfile);
191 fwrite(dvals,sizeof(double),dvalsize,myfile);
192 fflush(myfile);
193 fclose(myfile);
194
195 /* should probably test for success, but then it is a test suite already... */
196 }
197
198
199
200 void makekeylistfile(char * filename, char * item){
201
202 FILE * myfile;
203
204 myfile=fopen(filename,"w");
205 fprintf(myfile,"%s\n",item);
206 fflush(myfile);
207 fclose(myfile);
208
209 }
210
211
212
213
214