| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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; |
| 54 |
uint8_t *ptr; |
| 55 |
uint32_t network_32bit; |
| 56 |
uint64_t network_64bit; |
| 57 |
|
| 58 |
|
| 59 |
if(obj == NULL || buf == NULL || n == NULL || ((*buf == NULL) && ((*n != 0) || (*nalloc != 0)))) { |
| 60 |
|
| 61 |
return(eslEINVAL); |
| 62 |
} |
| 63 |
|
| 64 |
int ser_size = HMMD_SEARCH_STATUS_SERIAL_SIZE; |
| 65 |
if(*buf == NULL){ |
| 66 |
ESL_ALLOC(*buf, ser_size); |
| 67 |
*nalloc = ser_size; |
| 68 |
} |
| 69 |
|
| 70 |
if((*n + ser_size) > *nalloc){ |
| 71 |
ESL_REALLOC(*buf, (*n + ser_size)); |
| 72 |
*nalloc = *n + ser_size; |
| 73 |
} |
| 74 |
|
| 75 |
ptr = *buf + *n; |
| 76 |
|
| 77 |
|
| 78 |
network_32bit = esl_hton32(obj->status); |
| 79 |
memcpy(ptr, &network_32bit, sizeof(int32_t)); |
| 80 |
ptr += sizeof(int32_t); |
| 81 |
|
| 82 |
|
| 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; |
| 88 |
return eslOK; |
| 89 |
|
| 90 |
ERROR: |
| 91 |
return eslEMEM; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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; |
| 113 |
uint32_t network_32bit; |
| 114 |
|
| 115 |
if(ret_obj == NULL || buf == NULL || n == NULL){ |
| 116 |
return eslEINVAL; |
| 117 |
} |
| 118 |
|
| 119 |
ptr = (uint8_t *) buf + *n; |
| 120 |
|
| 121 |
|
| 122 |
memcpy(&network_32bit, ptr, sizeof(uint32_t)); |
| 123 |
ret_obj->status = esl_ntoh32(network_32bit); |
| 124 |
ptr += sizeof(uint32_t); |
| 125 |
|
| 126 |
|
| 127 |
memcpy(&network_64bit, ptr, sizeof(uint64_t)); |
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 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 |
|
| 199 |
|
| 200 |
#ifdef p7HMMD_SEARCH_STATUS_TESTDRIVE |
| 201 |
|
| 202 |
|
| 203 |
static void utest_Serialize_error_conditions(){ |
| 204 |
int status; |
| 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 |
|
| 217 |
buf = NULL; |
| 218 |
|
| 219 |
if(hmmd_search_status_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){ |
| 220 |
esl_fatal(msg); |
| 221 |
} |
| 222 |
else{ |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
ESL_ALLOC(buf, sizeof(uint8_t *)); |
| 227 |
*buf = NULL; |
| 228 |
|
| 229 |
|
| 230 |
if(hmmd_search_status_Serialize(foo, buf, NULL, &nalloc) != eslEINVAL){ |
| 231 |
esl_fatal(msg); |
| 232 |
} |
| 233 |
else{ |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if(hmmd_search_status_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL){ |
| 239 |
esl_fatal(msg); |
| 240 |
} |
| 241 |
else{ |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
n = 3; |
| 247 |
if(hmmd_search_status_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){ |
| 248 |
esl_fatal(msg); |
| 249 |
} |
| 250 |
else{ |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
|
| 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 |
|
| 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; |
| 291 |
HMMD_SEARCH_STATUS *deserial = NULL; |
| 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){ |
| 307 |
esl_fatal(msg); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if(hmmd_search_status_Deserialize(NULL, &n, deserial) != eslEINVAL){ |
| 312 |
esl_fatal(msg); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if(hmmd_search_status_Deserialize(buf, NULL, deserial) != eslEINVAL){ |
| 318 |
esl_fatal(msg); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if(hmmd_search_status_Deserialize(buf, &n, NULL) != eslEINVAL){ |
| 324 |
esl_fatal(msg); |
| 325 |
} |
| 326 |
|
| 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; |
| 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){ |
| 378 |
esl_fatal(msg); |
| 379 |
} |
| 380 |
|
| 381 |
} |
| 382 |
|
| 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 |
|
| 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; |
| 430 |
} |
| 431 |
|
| 432 |
#endif |