Debian Codegraph @

hmmer/src/p7_hmmd_search_stats.c

raw symbols SCIP
1 /* Functions for HMMD_SEARCH_STATS objects
2 * NPC, 1/10/19, Havahd, "Sparta"
3 *
4 * Contents:
5 * 1) Serialization and deserialization routines
6 * 2) Unit tests
7 * 3) Test driver
8 */
9
10 #include <p7_config.h>
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "easel.h"
17 #include "esl_ssi.h"
18 #include "hmmer.h"
19 #include "hmmpgmd.h"
20
21 /* Function: p7_hmmd_search_stats_Serialize
22 * Synopsis: Serializes a HMMD_SEARCH_STATS object into a stream of bytes
23 *. that can be reliably transmitted over internet sockets
24 *
25 * Purpose: Converts an architecture-dependent P7_SEARCH_STATS object into a contiguous stream
26 * of bytes with each field of the data structure in network byte order for transmission
27 * over sockets. The serialized byte stream may be part of a larger allocated buffer.
28 * If the provided buffer is NULL, allocates a new buffer large enough for the serialized object
29 * If the provided buffer is not large enough to hold the serialized object and its existing data, re-allocates
30 * a larger buffer
31 *
32 * Inputs: obj: A pointer to the HMMD_SEARCH_STATS object to be serialized
33 * buf: Handle to the buffer that the object should be serialized into. If *buf is NULL,
34 * a new buffer will be allocated. Passing a NULL buf is an error.
35 * n: Offset (in bytes) from the start of the buffer to where the serialized object should start.
36 * nalloc: size (in bytes) of the buffer passed in buf
37 *
38 *Returns: On success: returns eslOK, sets *buf to the base of the buffer containing the object
39 * if allocation or re-allocation was requried, sets *n to the offset from the start of the buffer
40 * to the first position after the serialized object and sets *nalloc to the new size of the buffer
41 * if allocation or re-allocation was required.
42 *
43 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if obj == NULL, if buf == NULL, if
44 * n == NULL, or
45 * if an unknown enum value is found in obj
46 */
47 extern int p7_hmmd_search_stats_Serialize(const HMMD_SEARCH_STATS *obj, uint8_t **buf, uint32_t *n, uint32_t *nalloc){
48
49 int status; // error variable used by ESL_ALLOC
50 int ser_size; // size of the structure when serialized
51 uint8_t *ptr; // current ptrition within the buffer
52 uint64_t network_64bit; // hold 64-bit fields after conversion to network order
53 // check to make sure we were passed a valid pointer
54 if((obj == NULL) || (n == NULL)){
55 return(eslEINVAL);
56 }
57
58 ser_size = HMMD_SEARCH_STATS_SERIAL_BASE;
59
60 if(obj->hit_offsets != NULL){
61 ser_size += obj->nhits * sizeof(uint64_t);
62 }
63 else{
64 ser_size += sizeof(uint64_t);
65 }
66
67 if(buf == NULL){ // Can't proceed, don't have any place to put the pointer to the buffer
68 return eslEINVAL;
69 }
70
71 // make sure we have enough space in our buffer
72 if (*buf == NULL){ // need to allocate buffer space
73 ESL_ALLOC(*buf, ser_size);
74 *n = 0; //always start at the beginning of the buffer if we have to allocate a new one.
75 *nalloc = ser_size;
76 }
77 else{
78 if(*n + ser_size > *nalloc){
79 ESL_REALLOC(*buf, *n + ser_size);
80 *nalloc = *n + ser_size;
81 }
82 }
83
84 // now that we have the buffer, do the serialization
85 ptr = *buf + *n;
86
87
88 // First field: elapsed
89 network_64bit = esl_hton64(*((uint64_t *) &(obj->elapsed))); // ow, this hurts, but is probably the best way to get the
90 // bits that represent a double out of a structure. Boy, will it fail horribly on any architecture that doesn't use
91 // 64-bit doubles
92 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->elapsed)); //Use memcpy here because it does the right thing with unaligned copies
93 ptr += sizeof(obj->elapsed);
94
95
96 // Second field: user
97 network_64bit = esl_hton64(*((uint64_t *) &(obj->user)));
98 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->user));
99 ptr += sizeof(obj->user);
100
101
102 // Third field: sys
103 network_64bit = esl_hton64(*((uint64_t *) &(obj->sys)));
104 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->sys));
105 ptr += sizeof(obj->sys);
106
107
108 // Fourth field: Z
109 network_64bit = esl_hton64(*((uint64_t *) &(obj->Z)));
110 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->Z));
111 ptr += sizeof(obj->Z);
112
113
114 // Fifth field: domZ
115 network_64bit = esl_hton64(*((uint64_t *) &(obj->domZ)));
116 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->domZ));
117 ptr += sizeof(obj->domZ);
118
119
120 // Enum sizes are non-portable across computers and compilers, so use a switch statement so that we can be sure of how
121 // much space we use
122 switch(obj->Z_setby){
123 case p7_ZSETBY_NTARGETS:
124 *ptr = 0;
125 break;
126 case p7_ZSETBY_OPTION:
127 *ptr = 1;
128 break;
129 case p7_ZSETBY_FILEINFO:
130 *ptr = 2;
131 break;
132 default:
133 ESL_EXCEPTION(eslEINVAL,"Error: unknown enum type found in HMMD_SEARCH_STATS_Serialize");
134 }
135 ptr += 1;
136
137 switch(obj->domZ_setby){
138 case p7_ZSETBY_NTARGETS:
139 *ptr = 0;
140 break;
141 case p7_ZSETBY_OPTION:
142 *ptr = 1;
143 break;
144 case p7_ZSETBY_FILEINFO:
145 *ptr = 2;
146 break;
147 default:
148 ESL_EXCEPTION(eslEINVAL, "Error: unknown enum type found in HMMD_SEARCH_STATS_Serialize");
149 }
150 ptr += 1;
151
152
153 // Eighth field: nmodels
154 network_64bit = esl_hton64(obj->nmodels);
155 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->nmodels));
156 ptr += sizeof(obj->nmodels);
157
158 // Ninth field: nseqs
159 network_64bit = esl_hton64(obj->nseqs);
160 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->nseqs));
161 ptr += sizeof(obj->nseqs);
162
163 // Tenth field: n_past_msv
164 network_64bit = esl_hton64(obj->n_past_msv);
165 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->n_past_msv));
166 ptr += sizeof(obj->n_past_msv);
167
168 // Eleventh field: n_past_bias
169 network_64bit = esl_hton64(obj->n_past_bias);
170 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->n_past_bias));
171 ptr += sizeof(obj->n_past_bias);
172
173 // Twelfth field: n_past_vit
174 network_64bit = esl_hton64(obj->n_past_vit);
175 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->n_past_vit));
176 ptr += sizeof(obj->n_past_vit);
177
178 // Thirteenth field: n_past_fwd
179 network_64bit = esl_hton64(obj->n_past_fwd);
180 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->n_past_fwd));
181 ptr += sizeof(obj->n_past_fwd);
182
183 // Fourteenth field: nhits
184 network_64bit = esl_hton64(obj->nhits);
185 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->nhits));
186 ptr += sizeof(obj->nhits);
187
188 // Fifteenth field: nreported
189 network_64bit = esl_hton64(obj->nreported);
190 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->nreported));
191 ptr += sizeof(obj->nreported);
192
193 // Sixteenth field: nincluded
194 network_64bit = esl_hton64(obj->nincluded);
195 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->nincluded));
196 ptr += sizeof(obj->nincluded);
197
198 if(obj->hit_offsets == NULL){ // no hit_offsets array
199 network_64bit = esl_hton64(-1);
200 memcpy((void *) ptr, (void *) &network_64bit, sizeof(uint64_t));
201 ptr += sizeof(uint64_t);
202 }
203 else{
204 int i;
205 for (i = 0; i < obj->nhits; i++)
206 {
207 network_64bit = esl_hton64(obj->hit_offsets[i]);
208 memcpy((void *) ptr, (void *) &network_64bit, sizeof(uint64_t));
209 ptr += sizeof(uint64_t);
210 }
211 }
212
213 // Ok, we've serialized the data structure. Just need to update n
214 *n = ptr - *buf; // works because ptr and *buf point to uint8_t arrays
215 return(eslOK);
216
217 ERROR: // We only get here if memory (re)allocation failed, so no cleanup required.
218 return(eslEMEM);
219
220 }
221
222
223 /* Function: HMMD_SEARCH_STATS_Deserialize
224 * Synopsis: Derializes a HMMD_SEARCH_STATS object from a stream of bytes in network order into
225 * a valid data structure
226 *
227 * Purpose: Deserializes a serialized HMMD_SEARCH_STATS object from
228 *. buf starting at position position *pos.
229 *
230 * Inputs: buf: the buffer that the object should be de-serialized from
231 * pos: a pointer to the offset from the start of buf to the beginning of the object
232 * ret_obj: a HMMD_SEARCH_STATS structure to deserialize the object into. May not be NULL.
233 *
234 * Returns: On success: returns eslOK, deserializes the HMMD_SEARCH_STATS object into ret_object, and updates
235 *. pos to point to the position after the end of the HMMD_SEARCH_STATS object.
236 *
237 * Throws: Returns eslEINVAL if ret_obj == NULL or an unknown enum
238 *. value is found in one of the fields of the serialized object.
239 * Returns eslEMEM if unable to allocate memory
240 */
241 extern int p7_hmmd_search_stats_Deserialize(const uint8_t *buf, uint32_t *n, HMMD_SEARCH_STATS *ret_obj){
242 uint8_t *ptr;
243 uint64_t network_64bit; // holds 64-bit values in network order
244 uint64_t host_64bit; //variable to hold 64-bit values after conversion to host order
245 int status;
246
247
248 if ((buf == NULL) || (ret_obj == NULL)|| (n == NULL)){ // check to make sure we've been passed valid objects
249 return(eslEINVAL);
250 }
251
252 ptr = (uint8_t *) buf + *n; // Get pointer to start of object
253
254 //First field: elapsed. Copy out of buffer into scalar variable to deal with memory alignment, convert to
255 // host machine order, and then copy into correct field to deal with type issues
256 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
257 host_64bit = esl_ntoh64(network_64bit);
258 ret_obj->elapsed = *((double *) &host_64bit);
259 ptr += sizeof(uint64_t);
260
261 //Second field: user
262 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
263 host_64bit = esl_ntoh64(network_64bit);
264 ret_obj->user = *((double *) &host_64bit);
265 ptr += sizeof(uint64_t);
266
267 //Third field: sys
268 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
269 host_64bit = esl_ntoh64(network_64bit);
270 ret_obj->sys = *((double *) &host_64bit);
271 ptr += sizeof(uint64_t);
272
273 //Fourth field: Z
274 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
275 host_64bit = esl_ntoh64(network_64bit);
276 ret_obj->Z = *((double *) &host_64bit);
277 ptr += sizeof(uint64_t);
278
279 //Fifth field: domZ
280 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
281 host_64bit = esl_ntoh64(network_64bit);
282 ret_obj->domZ = *((double *) &host_64bit);
283 ptr += sizeof(uint64_t);
284
285 //Sith and seventh fields: the enums
286 switch(*ptr){
287 case 0:
288 ret_obj->Z_setby = p7_ZSETBY_NTARGETS;
289 break;
290 case 1:
291 ret_obj->Z_setby = p7_ZSETBY_OPTION;
292 break;
293 case 2:
294 ret_obj->Z_setby = p7_ZSETBY_FILEINFO;
295 break;
296 default:
297 ESL_EXCEPTION(eslEINVAL, "Error: unknown enum type found in HMMD_SEARCH_STATS_Deserialize");
298 }
299 ptr++;
300
301 switch(*ptr){
302 case 0:
303 ret_obj->domZ_setby = p7_ZSETBY_NTARGETS;
304 break;
305 case 1:
306 ret_obj->domZ_setby = p7_ZSETBY_OPTION;
307 break;
308 case 2:
309 ret_obj->domZ_setby = p7_ZSETBY_FILEINFO;
310 break;
311 default:
312 ESL_EXCEPTION(eslEINVAL,"Error: unknown enum type found in HMMD_SEARCH_STATS_Deserialize");
313 }
314 ptr++;
315
316 //Eighth field: nmodels
317 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
318 ret_obj->nmodels = esl_ntoh64(network_64bit); // Can just do assignment to uint64_t field
319 ptr += sizeof(uint64_t);
320
321 //Ninth field: nseqs
322 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
323 ret_obj->nseqs = esl_ntoh64(network_64bit);
324 ptr += sizeof(uint64_t);
325
326 //Tenth field: n_past_msv
327 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
328 ret_obj->n_past_msv = esl_ntoh64(network_64bit);
329 ptr += sizeof(uint64_t);
330
331 //Eleventh field: n_past_bias
332 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
333 ret_obj->n_past_bias = esl_ntoh64(network_64bit);
334 ptr += sizeof(uint64_t);
335
336 //Twelfth field: n_past_vit
337 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
338 ret_obj->n_past_vit = esl_ntoh64(network_64bit);
339 ptr += sizeof(uint64_t);
340
341 //Thirteenth field: n_past_fwd
342 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
343 ret_obj->n_past_fwd = esl_ntoh64(network_64bit);
344 ptr += sizeof(uint64_t);
345
346 //Fourteenth field: nhits
347 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
348 ret_obj->nhits = esl_ntoh64(network_64bit);
349 ptr += sizeof(uint64_t);
350
351 //Fifteenth field: nreported
352 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
353 ret_obj->nreported = esl_ntoh64(network_64bit);
354 ptr += sizeof(uint64_t);
355
356 //Sixteenth field: nincluded
357 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
358 ret_obj->nincluded = esl_ntoh64(network_64bit);
359 ptr += sizeof(uint64_t);
360
361 // seventh field: hit_offsets array, if any
362 memcpy(&network_64bit, ptr, sizeof(uint64_t));
363 ptr += sizeof(uint64_t);
364 if(esl_ntoh64(network_64bit) == (uint64_t) -1){ // no hit_offsets array
365 if(ret_obj->hit_offsets != NULL){
366 free(ret_obj->hit_offsets);
367 ret_obj->hit_offsets = NULL;
368 }
369 }
370 else{ // there's a hit_offsets array
371 if(ret_obj->hit_offsets != NULL){
372 ESL_REALLOC(ret_obj->hit_offsets, ret_obj->nhits * sizeof(uint64_t));
373 }
374 else{
375 ESL_ALLOC(ret_obj->hit_offsets, ret_obj->nhits * sizeof(uint64_t));
376 }
377
378 ret_obj->hit_offsets[0] = esl_ntoh64(network_64bit); //already have the first offset read out of the buffer
379 int i;
380 for (i = 1; i < ret_obj->nhits; i++)
381 {
382 memcpy(&network_64bit, ptr, sizeof(uint64_t));
383 ptr += sizeof(uint64_t);
384 ret_obj->hit_offsets[i] = esl_ntoh64(network_64bit);
385 }
386 }
387
388 *n = ptr - buf; // update position counter to just past the object we just deserialized
389 return eslOK; // If we make it here, we've finished successfully
390 ERROR:
391 return eslEMEM;
392 }
393
394 /*****************************************************************
395 * 2. Unit tests
396 *****************************************************************/
397 #ifdef p7HMMD_SEARCH_STATS_TESTDRIVE
398
399 /* compares two HMMD_SEARCH_STATS objects, and returns eslOK if their contents are the same, eslFAIL if they are different
400 *or if either of them are NULL
401 * NOTE: this function compares double-precision values for exact equality. It is thus only useful when checking that
402 * two HMMD_SEARCH_STATS objects have the exact same binary contents, as should be the case when we serialize/deserialize them
403 * it is not useful for checking HMMD_SEARCH_STATS objects for general equivalence, due to the standard problems involved in
404 * comparing floating-point numbers for equality. That is why it is a static function and not part of the general API for
405 * HMMD_SEARCH_STATS objects.
406 */
407 static int hmmd_search_stats_Same(const HMMD_SEARCH_STATS *first, const HMMD_SEARCH_STATS *second){
408 if((first == NULL)||(second==NULL)){ // we've been passed bad pointers
409 return eslFAIL;
410 }
411
412 // Now, compare all the sub-fields
413 if(first->elapsed != second->elapsed){
414 return eslFAIL;
415 }
416
417 if(first->user != second->user){
418 return eslFAIL;
419 }
420
421 if(first->sys != second->sys){
422 return eslFAIL;
423 }
424
425 if(first->Z != second->Z){
426 return eslFAIL;
427 }
428
429 if(first->domZ != second->domZ){
430 return eslFAIL;
431 }
432
433 if(first->Z_setby != second->Z_setby){
434 return eslFAIL;
435 }
436
437 if(first->domZ_setby != second->domZ_setby){
438 return eslFAIL;
439 }
440
441 if(first->nmodels != second->nmodels){
442 return eslFAIL;
443 }
444
445 if(first->nseqs != second->nseqs){
446 return eslFAIL;
447 }
448
449 if(first->n_past_msv != second->n_past_msv){
450 return eslFAIL;
451 }
452
453 if(first->n_past_bias != second->n_past_bias){
454 return eslFAIL;
455 }
456
457 if(first->n_past_fwd != second->n_past_fwd){
458 return eslFAIL;
459 }
460
461 if(first->n_past_vit != second->n_past_vit){
462 return eslFAIL;
463 }
464
465 if(first->nhits != second->nhits){
466 return eslFAIL;
467 }
468
469 if(first->nreported != second->nreported){
470 return eslFAIL;
471 }
472
473 if(first->nincluded != second->nincluded){
474 return eslFAIL;
475 }
476
477 if(((first->hit_offsets != NULL) && (second->hit_offsets == NULL)) ||
478 ((first->hit_offsets == NULL) && (second->hit_offsets != NULL))){ // one object has a hit_offsets array and the other doesn't
479 return eslFAIL;
480 }
481
482 if(first->hit_offsets != NULL){ // both must have a hit_offsets array, since we'd already have failed if only one did
483 int i;
484 for (i = 0; i < first->nhits; i++)
485 { // we've already checked that both objects have the same value for nhits
486 if(first->hit_offsets[i] != second->hit_offsets[i]){
487 return eslFAIL;
488 }
489 }
490 }
491
492 return eslOK; // If we make it this far, everything matched
493 }
494
495
496
497 // Function to generate a random double-precision value using C rand() function.
498 // based on example at http://www.cs.utsa.edu/~wagner/CS2073/random/random.html
499 // Because this uses the base rand() function, the results it generates aren't
500 // good enough for publication-quality research, but we're just going to use
501 // it to generate some random values for structures that we'll serialize and deserialize
502
503 double random_double(double min, double max){
504
505 double zero_to_one = rand()/(double) RAND_MAX;
506
507 return (max-min) * zero_to_one + min;
508 }
509
510
511 // serializes and deserializes 100 random HMMD_SEARCH_STATS structures
512 // returns eslOK if all of the deserialized structures matched the
513 // ones they were serialized from, eslFAIL if not, eslEMEM if unable to allocate memory
514 static void
515 utest_serialize()
516 {
517 char msg[] = "serialize utest failed";
518 HMMD_SEARCH_STATS *serial=NULL, *deserial=NULL;
519 uint8_t **buffer = NULL;
520 double low = -1000.0;
521 double high = 1000.0;
522 uint32_t pos, buffer_size;
523 int i,j;
524 int status;
525
526 srand(time(0)); // reseed randomness
527 ESL_ALLOC(serial, 100 * sizeof(HMMD_SEARCH_STATS));
528 ESL_ALLOC(deserial, sizeof(HMMD_SEARCH_STATS));
529 deserial->hit_offsets = NULL;
530 ESL_ALLOC(buffer, sizeof(uint8_t *));
531 *buffer = NULL; //force Serialize to allocate the first buffer
532 pos = 0;
533 buffer_size = 0;
534
535 for (i = 0; i < 100; i++)
536 {
537 // assign random values to each field in the data structure and then serialize
538 serial[i].elapsed = random_double(low, high);
539 serial[i].user = random_double(low, high);
540 serial[i].sys = random_double(low, high);
541 serial[i].Z = random_double(low, high);
542 serial[i].domZ = random_double(low, high);
543 serial[i].Z_setby = (enum p7_zsetby_e) rand() % p7_ZSETBY_FILEINFO;
544 serial[i].domZ_setby = (enum p7_zsetby_e) rand() % p7_ZSETBY_FILEINFO;
545 serial[i].nmodels = rand();
546 serial[i].nseqs = rand();
547 serial[i].n_past_msv = rand();
548 serial[i].n_past_bias = rand();
549 serial[i].n_past_vit = rand();
550 serial[i].n_past_fwd = rand();
551 serial[i].nhits = rand() % 10000; // keep the size of the hit_offsets array reasonable
552 serial[i].nreported = rand();
553 serial[i].nincluded = rand();
554
555 if ((rand() % 2) == 0){ // 50% chance of hit_offsets array
556 ESL_ALLOC(serial[i].hit_offsets, serial[i].nhits * sizeof(uint64_t));
557 for(j = 0; j < serial[i].nhits; j++){
558 serial[i].hit_offsets[j] = (((uint64_t) rand()) << 32) + ((uint64_t) rand());
559 }
560 }
561 else serial[i].hit_offsets = NULL;
562
563 if (p7_hmmd_search_stats_Serialize(&(serial[i]), buffer, &pos, &buffer_size) != eslOK) esl_fatal(msg);
564 }
565
566 // Should now have 100 serialized structures in buffer
567 // Go through, deserialize each one, and compare to the structure it was serialized from
568 pos = 0;
569 for (i = 0; i < 100; i++)
570 {
571 if (p7_hmmd_search_stats_Deserialize(*buffer, &pos, deserial) != eslOK) esl_fatal(msg);
572 if (hmmd_search_stats_Same(&(serial[i]), deserial) != eslOK) esl_fatal(msg);
573 }
574
575 // If we make it this far without failing out, we've succeeded
576 for (i = 0; i < 100; i++)
577 free(serial[i].hit_offsets);
578 free(serial);
579 free(deserial->hit_offsets);
580 free(deserial);
581 free(*buffer);
582 free(buffer);
583 return;
584
585 ERROR:
586 esl_fatal(msg);
587 }
588
589 // Test that the _Serialize() function generates the correct errors when passed invalid arguments
590 static void
591 utest_serialize_error_conditions()
592 {
593 char msg[] = "serialize_error_conditions utest failed";
594 uint8_t **buf = NULL;
595 uint32_t n = 0;
596 uint32_t nalloc = 0;
597 HMMD_SEARCH_STATS foo;
598 int status;
599
600 // Start out with foo initialized to valid values
601 foo.elapsed = 1.0;
602 foo.user = 2.0;
603 foo.sys = 3.0;
604 foo.Z = 4.0;
605 foo.domZ = 5.0;
606 foo.Z_setby = p7_ZSETBY_NTARGETS;
607 foo.domZ_setby = p7_ZSETBY_NTARGETS;
608 foo.nmodels = 1;
609 foo.nseqs = 2;
610 foo.n_past_msv = 3;
611 foo.n_past_bias = 4;
612 foo.n_past_vit = 5;
613 foo.n_past_fwd = 6;
614 foo.nhits = 7;
615 foo.nreported = 8;
616 foo.nincluded = 9;
617 foo.hit_offsets = NULL;
618
619 // Test 1: _Serialize returns error if passed NULL buffer
620 buf = NULL;
621 if (p7_hmmd_search_stats_Serialize(&foo, buf, &n, &nalloc) != eslEINVAL) esl_fatal(msg);
622
623 // Test 2: error on NULL n ptr
624 ESL_ALLOC(buf, sizeof(uint8_t *));
625 *buf = NULL;
626 if (p7_hmmd_search_stats_Serialize(&foo, buf, NULL, &nalloc) != eslEINVAL) esl_fatal(msg);
627
628 // Test 3: error on NULL object ptr
629 if (p7_hmmd_search_stats_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL) esl_fatal(msg);
630
631 /* Comment these tests out now that Serialize() has been debugged because invalid enums terminate the utest
632 // Test 4: invalid enum in Z_setby field
633 foo.Z_setby = 255; // set Z_setby to invalid value
634 buf = &buf_ptr;
635 if(p7_hmmd_search_stats_Serialize(&foo, buf, &n, &nalloc) != eslEINVAL){
636 return eslFAIL;
637 }
638 else{
639 //printf("invalid Z_setby check passed\n");
640 }
641
642 // Test 5: invalid enum in domZ_setby field
643 foo.Z_setby = p7_ZSETBY_FILEINFO; // reset this field to valid value
644 foo.domZ_setby = 255; // set this one to invalid
645 if(p7_hmmd_search_stats_Serialize(&foo, buf, &n, &nalloc) != eslEINVAL){
646 return eslFAIL;
647 }
648 else{
649 //printf("invalid domZ_setby check passed\n");
650 }
651 */
652
653 if (buf) free(*buf);
654 free(buf);
655 return;
656
657 ERROR:
658 esl_fatal(msg);
659 }
660
661 // test that the _Deserialize() function returns the correct errors when passed invalid data
662 static void
663 utest_deserialize_error_conditions()
664 {
665 char msg[] = "deserialize_error_conditions utest failed";
666 uint8_t **buf = NULL;
667 uint32_t n = 0;
668 uint32_t nalloc = 0;
669 HMMD_SEARCH_STATS foo;
670 int status;
671
672 // Start out with foo initialized to valid values
673 foo.elapsed = 1.0;
674 foo.user = 2.0;
675 foo.sys = 3.0;
676 foo.Z = 4.0;
677 foo.domZ = 5.0;
678 foo.Z_setby = p7_ZSETBY_NTARGETS;
679 foo.domZ_setby = p7_ZSETBY_NTARGETS;
680 foo.nmodels = 1;
681 foo.nseqs = 2;
682 foo.n_past_msv = 3;
683 foo.n_past_bias = 4;
684 foo.n_past_vit = 5;
685 foo.n_past_fwd = 6;
686 foo.nhits = 7;
687 foo.nreported = 8;
688 foo.nincluded = 9;
689 foo.hit_offsets = NULL;
690
691 // Test 1: should return eslEINVAL if buf == NULL
692 ESL_ALLOC(buf, sizeof(uint8_t *));
693 *buf = NULL;
694
695 if (p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslEINVAL) esl_fatal(msg);
696
697 if (buf) free(*buf);
698 free(buf);
699
700 // Test 2: invalid Z_setby enum in serialized buffer
701 buf = malloc(sizeof(uint8_t *));
702 *buf = NULL;
703
704 if (p7_hmmd_search_stats_Serialize(&foo, buf, &n, &nalloc) != eslOK) esl_fatal(msg);
705 n = 0; // reset to beginning of buffer
706
707 // sanity check: make sure we can deserialize the base serialized buffer before we muck with it
708 if (p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslOK) esl_fatal(msg);
709
710 /* comment these tests out because invalid enum values cause the utest to terminate
711 //Now, put invalid value in the buffer at the position of the Z_setby enum
712 (*buf)[40] = 255;
713 n = 0; // reset to start of buffer
714 if(p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslEINVAL){
715 free(*buf);
716 free(buf);
717 return eslFAIL;
718 }
719 else{
720 //printf("Invalid Z_setby field check passed\n");
721 }
722
723 // Test 3: invalid domZ_setby enum in serialized buffer
724 (*buf)[40] = p7_ZSETBY_FILEINFO; // reset Z_setby to valid value
725 n = 0; // reset to beginning of buffer
726
727 // sanity check: make sure we've reset the buffer to a valid state
728 if(p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslOK){
729 free(*buf);
730 free(buf);
731 //printf("Didn't reset Z_setby to valid value\n");
732 return eslFAIL;
733 }
734
735 (*buf)[41] = 255; // set domZ_setby to invalid value
736
737 n = 0; // reset to start of buffer
738 if(p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslEINVAL){
739 free(*buf);
740 free(buf);
741 return eslFAIL;
742 }
743 else{
744 //printf("Invalid domZ_setby field check passed\n");
745 }
746 */
747
748 //test 4: NULL n
749 (*buf)[41] = p7_ZSETBY_FILEINFO; // reset domZ_setby to valid value
750 n = 0; // reset to beginning of buffer
751
752 // sanity check: make sure we've reset the buffer to a valid state
753 if (p7_hmmd_search_stats_Deserialize(*buf, &n, &foo) != eslOK) esl_fatal(msg);
754
755 n = 0; // reset to start of buffer
756 if (p7_hmmd_search_stats_Deserialize(*buf, NULL, &foo) != eslEINVAL) esl_fatal(msg);
757
758
759 // end. If we get here, we've passed
760 if (buf) free(*buf);
761 free(buf);
762 return;
763
764 ERROR:
765 esl_fatal(msg);
766 }
767 #endif
768
769 /*****************************************************************
770 * 3. Test driver
771 *****************************************************************/
772 #ifdef p7HMMD_SEARCH_STATS_TESTDRIVE
773
774 int
775 main(int argc, char **argv)
776 {
777 utest_serialize_error_conditions();
778 utest_deserialize_error_conditions();
779 utest_serialize();
780
781 return eslOK;
782 }
783 #endif