Debian Codegraph @

hmmer/src/hmmd_search_status.c

raw symbols SCIP
1 /* Functions to manipulate HMMD_SEARCH_STATUS objects.
2 * Contents: 1) Serialization and Deserialization routines
3 * 2) Debugging/Utility Functions
4 * 3) Unit Tests
5 * 4) Test Driver
6 * NPC 2/20/19 [Sparta]
7 */
8 #include <p7_config.h>
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <string.h>
15
16 #include "easel.h"
17
18 #include "hmmer.h"
19 #include "hmmpgmd.h"
20
21
22 /*****************************************************************
23 * 1. Functions to manipulate HMMD_SEARCH_STATUS objects
24 *****************************************************************/
25
26 /* Function: hmmd_search_status_Serialize
27 * Synopsis: Serializes a HMMD_SEARCH_STATUS object into a stream of bytes
28 *. that can be reliably transmitted over internet sockets
29 *
30 * Purpose: Converts an architecture-dependent HMMD_SEARCH_STATUS object into a contiguous stream
31 * of bytes with each field of the data structure in network byte order for transmission
32 * over sockets. The serialized byte stream may be part of a larger allocated buffer.
33 * If the provided buffer is NULL, allocates a new buffer large enough for the serialized object
34 * If the provided buffer is not large enough to hold the serialized object and its existing data, re-allocates
35 * a larger buffer
36 *
37 * Inputs: obj: A pointer to the HMMD_SEARCH_STATUS object to be serialized
38 * buf: Handle to the buffer that the object should be serialized into. If *buf is NULL,
39 * a new buffer will be allocated. buf == NULL is not allowed.
40 * n: Offset (in bytes) from the start of the buffer to where the serialized object should start.
41 * nalloc: size (in bytes) of the buffer passed in buf
42 *
43 *Returns: On success: returns eslOK, sets *buf to the base of the buffer containing the object
44 * if allocation or re-allocation was requried, sets *n to the offset from the start of the buffer
45 * to the first position after the serialized object and sets *nalloc to the new size of the buffer
46 * if allocation or re-allocation was required.
47 *
48 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if obj == NULL, n == NULL, buf == NULL,
49 *. or if *buf = NULL and either *n != 0 or *nalloc != 0
50 */
51
52 extern int hmmd_search_status_Serialize(const HMMD_SEARCH_STATUS *obj, uint8_t **buf, uint32_t *n, uint32_t *nalloc){
53 int status; // error variable used by ESL_ALLOC
54 uint8_t *ptr; // current position within the buffer
55 uint32_t network_32bit; // hold 32-bit fields after conversion to network order
56 uint64_t network_64bit; // hold 64-bit fields after conversion to network order
57
58 // check to make sure we were passed a valid pointer
59 if(obj == NULL || buf == NULL || n == NULL || ((*buf == NULL) && ((*n != 0) || (*nalloc != 0)))) {
60 // no object to serialize, nowhere to put a buffer pointer, or NULL buffer and non-zero offset or buffer size
61 return(eslEINVAL);
62 }
63
64 int ser_size = HMMD_SEARCH_STATUS_SERIAL_SIZE; // No variable-length fields to worry about
65 if(*buf == NULL){ // have no buffer, so allocate one
66 ESL_ALLOC(*buf, ser_size);
67 *nalloc = ser_size;
68 }
69
70 if((*n + ser_size) > *nalloc){ //have a buffer, but it's not big enough
71 ESL_REALLOC(*buf, (*n + ser_size));
72 *nalloc = *n + ser_size;
73 }
74
75 ptr = *buf + *n; // pointer to start of region we'll serialize to
76
77 // Field 1: status field
78 network_32bit = esl_hton32(obj->status);
79 memcpy(ptr, &network_32bit, sizeof(int32_t));
80 ptr += sizeof(int32_t);
81
82 // Field 2: msg_size
83 network_64bit = esl_hton64(obj->msg_size);
84 memcpy(ptr, &network_64bit, sizeof(int64_t));
85 ptr += sizeof(int64_t);
86
87 *n = ptr - *buf; // update n to point to end of serialized region
88 return eslOK;
89
90 ERROR:
91 return eslEMEM;
92 }
93
94 /* Function: hmmd_search_status_Deserialize
95 * Synopsis: Derializes a HMMD_SEARCH_STATUS object from a stream of bytes in network order into
96 * a valid data structure
97 *
98 * Purpose: Deserializes a serialized HMMD_SEARCH_STATUS object from
99 *. buf starting at position *n.
100 *
101 * Inputs: buf: the buffer that the object should be de-serialized from
102 * pos: a pointer to the offset from the start of buf to the beginning of the object
103 * ret_obj: a HMMD_SEARCH_STATUS structure to deserialize the object into. May not be NULL.
104 *
105 * Returns: On success: returns eslOK, deserializes the HMMD_SEARCH_STATUS object into ret_object, and updates
106 *. n to point to the position after the end of the HMMD_SEARCH_STATUS object.
107 *
108 * Throws: Returns eslEINVAL if ret_obj == NULL, buf == NULL, or n == NULL.
109 */
110 extern int hmmd_search_status_Deserialize(const uint8_t *buf, uint32_t *n, HMMD_SEARCH_STATUS *ret_obj){
111 uint8_t *ptr;
112 uint64_t network_64bit; // holds 64-bit values in network order
113 uint32_t network_32bit; // holds 64-bit values in network order
114
115 if(ret_obj == NULL || buf == NULL || n == NULL){
116 return eslEINVAL;
117 }
118
119 ptr = (uint8_t *) buf + *n;
120
121 //First field: status
122 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
123 ret_obj->status = esl_ntoh32(network_32bit);
124 ptr += sizeof(uint32_t);
125
126 //Field 2: msg_size
127 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
128 ret_obj->msg_size = esl_ntoh64(network_64bit);
129 ptr += sizeof(uint64_t);
130
131 *n = ptr - buf;
132 return eslOK;
133 }
134
135 /*****************************************************************
136 * 2. Debugging Functions
137 *****************************************************************/
138
139 /* Function: hmmd_search_status_TestSample
140 * Synopsis: Creates a HMMD_SEARCH_STATUS object that contains random data.
141 *
142 * Purpose: Creates a HMMD_SEARCH_STATUS object that contains random data. This data will be syntactically correct,
143 * but is not intended to be in any way a "reasonable" hit. For example, the number of P7_DOMAIN
144 * objects in the HMMD_SEARCH_STATUS objects will match the value of the object's ndom field, but the vales of the
145 * object's score, pre_score, and sum_score fields may not be consistent with each other.
146 *
147 * Inputs: rng: the random-number generator to use in creating this object.
148 * ret_obj: pointer that is used to return the created object
149 *
150 * Returns: eslOK, and the created object in ret_obj
151 *
152 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if ret_obj == NULL
153 */
154 extern int hmmd_search_status_TestSample(ESL_RAND64 *rng, HMMD_SEARCH_STATUS **ret_obj){
155 int status;
156
157 if(ret_obj == NULL){
158 return eslEINVAL;
159 }
160
161 if(*ret_obj == NULL){
162 ESL_ALLOC(*ret_obj, sizeof(HMMD_SEARCH_STATUS));
163 }
164
165 (*ret_obj)->status = (uint32_t) esl_rand64(rng);
166 (*ret_obj)->msg_size = esl_rand64(rng);
167 return eslOK;
168
169 ERROR:
170 return eslEMEM;
171 }
172
173 /* Function: hmmd_search_status_Compare
174 * Synopsis: Compares two HMMD_SEARCH_STATUS objects for equality
175 *
176 * Purpose: Compares two HMMD_SEARCH_STATUS objects for equality
177 *
178 * Inputs: first: The first object to be compared
179 * second: The second object to be compared
180
181 *
182 * Returns: eslOK if the HMMD_SEARCH_STATUS inputs match, eslFAIL otherwise
183 *
184 * Throws: Nothing
185 */
186 extern int hmmd_search_status_Compare(HMMD_SEARCH_STATUS *first, HMMD_SEARCH_STATUS *second){
187
188 if((first->status == second->status) && (first->msg_size == second->msg_size)){
189 return eslOK;
190 }
191 else{
192 return eslFAIL;
193 }
194 }
195
196
197 /*****************************************************************
198 * 3. Unit tests
199 *****************************************************************/
200 #ifdef p7HMMD_SEARCH_STATUS_TESTDRIVE
201
202 // Test that the _Serialize() function generates the correct errors when passed invalid arguments
203 static void utest_Serialize_error_conditions(){
204 int status; // Easel error code variable
205 HMMD_SEARCH_STATUS *foo=NULL;
206 uint8_t **buf;
207 uint32_t n;
208 uint32_t nalloc;
209 ESL_RAND64 *rng;
210
211 char msg[] = "utest_Serialize_error_conditions failed";
212 rng = esl_rand64_Create(0);
213 n = 0;
214 nalloc = 0;
215 hmmd_search_status_TestSample(rng, &foo);
216 // Test 1: _Serialize returns error if passed NULL buffer
217 buf = NULL;
218
219 if(hmmd_search_status_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
220 esl_fatal(msg);
221 }
222 else{
223 // printf("null buffer check passed\n");
224 }
225
226 ESL_ALLOC(buf, sizeof(uint8_t *)); // set buf to valid value
227 *buf = NULL;
228
229 // Test 2: error on NULL n ptr
230 if(hmmd_search_status_Serialize(foo, buf, NULL, &nalloc) != eslEINVAL){
231 esl_fatal(msg);
232 }
233 else{
234 //printf("invalid n check passed\n");
235 }
236
237 // Test 3: error on NULL object ptr
238 if(hmmd_search_status_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL){
239 esl_fatal(msg);
240 }
241 else{
242 //printf("invalid object check passed\n");
243 }
244
245 // Test 4: n != 0 and *buf == NULL
246 n = 3;
247 if(hmmd_search_status_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
248 esl_fatal(msg);
249 }
250 else{
251 // printf("Non-zero n with NULL buffer check passed\n");
252 }
253
254 // Test 5: Nalloc != 0 and *buf == NULL
255 n = 0;
256 nalloc = 10;
257 if(hmmd_search_status_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
258 esl_fatal(msg);
259 }
260 else{
261 // printf("Non-zero nalloc with NULL buffer check passed\n");
262 }
263
264 if(buf !=NULL && *buf != NULL){
265 free(*buf);
266 }
267 if(buf != NULL){
268 free(buf);
269 }
270
271 free(foo);
272 esl_rand64_Destroy(rng);
273 return;
274
275 ERROR:
276 if(foo != NULL){
277 free(foo);
278 }
279
280 if(buf !=NULL && *buf != NULL){
281 free(*buf);
282 }
283 if(buf != NULL){
284 free(buf);
285 }
286 esl_fatal(msg);
287 }
288
289 static void utest_Deserialize_error_conditions(){
290 HMMD_SEARCH_STATUS *sampled = NULL; // sampled alidisplay that we'll serialze
291 HMMD_SEARCH_STATUS *deserial = NULL; // alidisplay to hold the deserialized object
292 char msg[]="utest_Deserialize_error_conditions failed";
293 uint8_t *buf = NULL;
294 uint32_t n = 0, nalloc = 0;
295 ESL_RAND64 *rng = esl_rand64_Create(0);
296 hmmd_search_status_TestSample(rng, &deserial);
297 if(deserial == NULL){
298 esl_fatal(msg);
299 }
300
301 hmmd_search_status_TestSample(rng, &sampled);
302 if(sampled == NULL){
303 esl_fatal(msg);
304 }
305
306 if(hmmd_search_status_Serialize(sampled, &buf, &n, &nalloc) != eslOK){ // serialize an object to deserialize
307 esl_fatal(msg);
308 }
309
310 // Test 1: error on buf == NULL;
311 if(hmmd_search_status_Deserialize(NULL, &n, deserial) != eslEINVAL){
312 esl_fatal(msg);
313 }
314 //printf("Test 1 passed\n");
315
316 // Test 2: error on n == NULL
317 if(hmmd_search_status_Deserialize(buf, NULL, deserial) != eslEINVAL){
318 esl_fatal(msg);
319 }
320 // printf("Test 2 passed\n");
321
322 // Test 3: error on serialized object == NULL
323 if(hmmd_search_status_Deserialize(buf, &n, NULL) != eslEINVAL){
324 esl_fatal(msg);
325 }
326 //printf("Test 3 passed\n");
327
328 free(deserial);
329 free(sampled);
330 free(buf);
331 esl_rand64_Destroy(rng);
332 return;
333 }
334
335 static void utest_Serialize(int ntrials){
336 int i;
337 uint8_t **buf=NULL;
338 uint32_t n;
339 uint32_t nalloc;
340 HMMD_SEARCH_STATUS **serial=NULL, *deserial=NULL;
341 int status;
342 char msg[] = "utest_Serialize failed";
343
344 if(ntrials <=0){
345 esl_fatal("utest_Serialize requires that ntrials be >= 1\n");
346 }
347
348 ESL_ALLOC(buf, sizeof(uint8_t *));
349 *buf = NULL;
350 n = 0;
351 nalloc = 0;
352
353 ESL_ALLOC(serial, ntrials * sizeof(HMMD_SEARCH_STATUS *));
354 for(i = 0; i< ntrials; i++){
355 serial[i] = NULL;
356 }
357
358 ESL_RAND64 *rng = esl_rand64_Create(0);
359
360 for(i = 0; i < ntrials; i++){
361 if(hmmd_search_status_TestSample(rng, &(serial[i])) != eslOK){
362 esl_fatal(msg);
363 }
364 if(hmmd_search_status_Serialize(serial[i], buf, &n, &nalloc) != eslOK){
365 esl_fatal(msg);
366 }
367 }
368
369 n = 0; // reset to start of buffer
370
371 ESL_ALLOC(deserial, sizeof(HMMD_SEARCH_STATUS));
372
373 for(i = 0; i < ntrials; i++){
374 if(hmmd_search_status_Deserialize(*buf, &n, deserial) != eslOK){
375 esl_fatal(msg);
376 }
377 if(hmmd_search_status_Compare(serial[i], deserial) != eslOK){ // deserialized structure didn't match serialized
378 esl_fatal(msg);
379 }
380
381 }
382 // haven't failed yet, so we've succeeded. Clean up and exit
383 free(*buf);
384 free(buf);
385 for(i = 0; i < ntrials; i++){
386 free(serial[i]);
387 }
388 free(serial);
389 free(deserial);
390 esl_rand64_Destroy(rng);
391 return;
392
393 ERROR:
394 if(buf != NULL){
395 if(*buf != NULL){
396 free(*buf);
397 }
398 free(buf);
399 }
400
401 if(serial != NULL){
402 for(i = 0; i < ntrials; i++){
403 if(serial[i] != NULL){
404 free(serial[i]);
405 }
406 }
407 free(serial);
408 }
409
410 if(deserial == NULL){
411 free(deserial);
412 }
413
414 esl_fatal(msg);
415
416 }
417 #endif
418 /*****************************************************************
419 * 3. Test Driver
420 *****************************************************************/
421 #ifdef p7HMMD_SEARCH_STATUS_TESTDRIVE
422
423 int
424 main(int argc, char **argv)
425 {
426 utest_Serialize_error_conditions();
427 utest_Deserialize_error_conditions();
428 utest_Serialize(100);
429 return eslOK; // If we get here, test passed
430 }
431
432 #endif