| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <esl_config.h> |
| 13 |
|
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "easel.h" |
| 17 |
#include "esl_recorder.h" |
| 18 |
|
| 19 |
static void linearray_reverse(ESL_RECORDER *rc, int pos, int n); |
| 20 |
static int recorder_new_baseline(ESL_RECORDER *rc, int newbase); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
ESL_RECORDER * |
| 39 |
esl_recorder_Create(FILE *fp, int maxlines) |
| 40 |
{ |
| 41 |
ESL_RECORDER *rc = NULL; |
| 42 |
int i; |
| 43 |
int status; |
| 44 |
|
| 45 |
ESL_ALLOC(rc, sizeof(ESL_RECORDER)); |
| 46 |
rc->fp = fp; |
| 47 |
rc->line = NULL; |
| 48 |
rc->nalloc = maxlines; |
| 49 |
rc->lalloc = NULL; |
| 50 |
rc->offset = NULL; |
| 51 |
rc->nread = 0; |
| 52 |
rc->ncurr = 0; |
| 53 |
rc->baseline = 0; |
| 54 |
rc->markline = -1; |
| 55 |
|
| 56 |
ESL_ALLOC(rc->line, sizeof(char *) * rc->nalloc); |
| 57 |
for (i = 0; i < rc->nalloc; i++) rc->line[i] = NULL; |
| 58 |
|
| 59 |
ESL_ALLOC(rc->lalloc, sizeof(int) * rc->nalloc); |
| 60 |
for (i = 0; i < rc->nalloc; i++) rc->lalloc[i] = 0; |
| 61 |
|
| 62 |
ESL_ALLOC(rc->offset, sizeof(off_t) * rc->nalloc); |
| 63 |
for (i = 0; i < rc->nalloc; i++) rc->offset[i] = 0; |
| 64 |
|
| 65 |
return rc; |
| 66 |
|
| 67 |
ERROR: |
| 68 |
esl_recorder_Destroy(rc); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
int |
| 113 |
esl_recorder_ResizeTo(ESL_RECORDER *rc, int new_maxlines) |
| 114 |
{ |
| 115 |
int idx; |
| 116 |
int newbase; |
| 117 |
void *tmp; |
| 118 |
int minlines; |
| 119 |
int status; |
| 120 |
|
| 121 |
if (new_maxlines == rc->nalloc) return eslOK; |
| 122 |
|
| 123 |
if (new_maxlines > rc->nalloc) |
| 124 |
{ |
| 125 |
if ((rc->nread - rc->baseline) / rc->nalloc != 0) |
| 126 |
{ |
| 127 |
newbase = ESL_MAX(rc->baseline, rc->nread - rc->nalloc); |
| 128 |
status = recorder_new_baseline(rc, newbase); |
| 129 |
if (status) ESL_EXCEPTION(eslEINCONCEIVABLE, "baseline reset failed unexpectedly"); |
| 130 |
} |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
|
| 135 |
if (rc->markline >= 0) |
| 136 |
{ |
| 137 |
minlines = rc->nread - rc->markline; |
| 138 |
if (new_maxlines < minlines) |
| 139 |
ESL_EXCEPTION(eslEINVAL, "can't shrink that far without losing marked line"); |
| 140 |
} |
| 141 |
|
| 142 |
minlines = rc->nread - rc->ncurr + 1; |
| 143 |
if (new_maxlines < minlines) |
| 144 |
ESL_EXCEPTION(eslEINVAL, "can't shrink that far without losing current line"); |
| 145 |
|
| 146 |
if (rc->nread - rc->baseline > new_maxlines) |
| 147 |
{ |
| 148 |
newbase = rc->nread - new_maxlines; |
| 149 |
status = recorder_new_baseline(rc, newbase); |
| 150 |
if (status) ESL_EXCEPTION(eslEINCONCEIVABLE, "baseline reset failed unexpectedly"); |
| 151 |
} |
| 152 |
|
| 153 |
for (idx = new_maxlines; idx < rc->nalloc; idx++) |
| 154 |
if (rc->line[idx]) free(rc->line[idx]); |
| 155 |
} |
| 156 |
|
| 157 |
ESL_RALLOC(rc->line, tmp, sizeof(char *) * new_maxlines); |
| 158 |
ESL_RALLOC(rc->lalloc, tmp, sizeof(int) * new_maxlines); |
| 159 |
ESL_RALLOC(rc->offset, tmp, sizeof(off_t) * new_maxlines); |
| 160 |
for (idx = rc->nalloc; idx < new_maxlines; idx++) |
| 161 |
{ |
| 162 |
rc->line[idx] = NULL; |
| 163 |
rc->lalloc[idx] = 0; |
| 164 |
rc->offset[idx] = 0; |
| 165 |
} |
| 166 |
rc->nalloc = new_maxlines; |
| 167 |
return eslOK; |
| 168 |
|
| 169 |
ERROR: |
| 170 |
return status; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
int |
| 181 |
esl_recorder_GetFirst(ESL_RECORDER *rc) |
| 182 |
{ |
| 183 |
return (ESL_MAX(rc->baseline, rc->nread-rc->nalloc)); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
int |
| 197 |
esl_recorder_GetLast(ESL_RECORDER *rc) |
| 198 |
{ |
| 199 |
return (rc->nread-1); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
int |
| 211 |
esl_recorder_GetCurrent(ESL_RECORDER *rc) |
| 212 |
{ |
| 213 |
return (rc->ncurr-1); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
int |
| 224 |
esl_recorder_GetNext(ESL_RECORDER *rc) |
| 225 |
{ |
| 226 |
return (rc->ncurr); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
void |
| 241 |
esl_recorder_Destroy(ESL_RECORDER *rc) |
| 242 |
{ |
| 243 |
int i; |
| 244 |
|
| 245 |
if (rc == NULL) return; |
| 246 |
|
| 247 |
if (rc->offset) free(rc->offset); |
| 248 |
if (rc->lalloc) free(rc->lalloc); |
| 249 |
if (rc->line) { |
| 250 |
for (i = 0; i < rc->nalloc; i++) |
| 251 |
if (rc->line[i]) free(rc->line[i]); |
| 252 |
free(rc->line); |
| 253 |
} |
| 254 |
free(rc); |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
int |
| 285 |
esl_recorder_Read(ESL_RECORDER *rc, char **opt_line) |
| 286 |
{ |
| 287 |
int idx = (rc->ncurr - rc->baseline) % rc->nalloc; |
| 288 |
int status; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
if (rc->ncurr >= rc->nread) |
| 293 |
{ |
| 294 |
|
| 295 |
if ( rc->markline >= 0 && |
| 296 |
((rc->ncurr - rc->baseline) % rc->nalloc == ((rc->markline - rc->baseline) % rc->nalloc))) |
| 297 |
{ |
| 298 |
int xtra = ESL_MAX(3, (rc->nalloc / 3)); |
| 299 |
status = esl_recorder_ResizeTo(rc, rc->nalloc + xtra); |
| 300 |
if (status) goto ERROR; |
| 301 |
idx = (rc->ncurr - rc->baseline) % rc->nalloc; |
| 302 |
} |
| 303 |
|
| 304 |
rc->offset[idx] = ftello(rc->fp); |
| 305 |
status = esl_fgets(&(rc->line[idx]), &(rc->lalloc[idx]), rc->fp); |
| 306 |
if (status) goto ERROR; |
| 307 |
rc->nread++; |
| 308 |
} |
| 309 |
|
| 310 |
rc->ncurr++; |
| 311 |
if (opt_line) *opt_line = rc->line[idx]; |
| 312 |
return eslOK; |
| 313 |
|
| 314 |
ERROR: |
| 315 |
if (opt_line) *opt_line = NULL; |
| 316 |
return status; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
int |
| 349 |
esl_recorder_Position(ESL_RECORDER *rc, int linenumber) |
| 350 |
{ |
| 351 |
|
| 352 |
int line0 = ESL_MAX(rc->baseline, rc->nread - rc->nalloc); |
| 353 |
int status; |
| 354 |
|
| 355 |
if (linenumber < line0) |
| 356 |
ESL_EXCEPTION(eslEINVAL, "recorder's window is past that line"); |
| 357 |
|
| 358 |
if (linenumber >= rc->nread) { |
| 359 |
while (rc->nread < linenumber) |
| 360 |
if ((status = esl_recorder_Read(rc, NULL)) != eslOK) return status; |
| 361 |
} |
| 362 |
|
| 363 |
rc->ncurr = linenumber; |
| 364 |
return eslOK; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
int |
| 388 |
esl_recorder_MarkBlock(ESL_RECORDER *rc, int markline) |
| 389 |
{ |
| 390 |
int line0 = ESL_MAX(rc->baseline, rc->nread - rc->nalloc); |
| 391 |
|
| 392 |
if (markline < line0) ESL_EXCEPTION(eslEINVAL, "recorder window already passed marked line"); |
| 393 |
rc->markline = markline; |
| 394 |
return eslOK; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
int |
| 412 |
esl_recorder_UnmarkBlock(ESL_RECORDER *rc) |
| 413 |
{ |
| 414 |
rc->markline = -1; |
| 415 |
return eslOK; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
int |
| 455 |
esl_recorder_GetBlock(ESL_RECORDER *rc, char ***opt_lines, int **opt_lalloc, off_t **opt_offset, int *opt_nlines) |
| 456 |
{ |
| 457 |
int idx0, idx1; |
| 458 |
int status; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
idx0 = (rc->markline - rc->baseline) % rc->nalloc; |
| 465 |
idx1 = (rc->ncurr-1 - rc->baseline) % rc->nalloc; |
| 466 |
if (idx0 > idx1) |
| 467 |
{ |
| 468 |
if ((status = recorder_new_baseline(rc, rc->ncurr-rc->nalloc)) != eslOK) goto ERROR; |
| 469 |
idx0 = (rc->markline - rc->baseline) % rc->nalloc; |
| 470 |
} |
| 471 |
|
| 472 |
if (opt_lines) *opt_lines = rc->line + idx0; |
| 473 |
if (opt_lalloc) *opt_lalloc = rc->lalloc + idx0; |
| 474 |
if (opt_offset) *opt_offset = rc->offset + idx0; |
| 475 |
if (opt_nlines) *opt_nlines = rc->ncurr - rc->markline; |
| 476 |
return eslOK; |
| 477 |
|
| 478 |
ERROR: |
| 479 |
if (opt_lines) *opt_lines = NULL; |
| 480 |
if (opt_lalloc) *opt_lalloc = NULL; |
| 481 |
if (opt_offset) *opt_offset = NULL; |
| 482 |
if (opt_nlines) *opt_nlines = 0; |
| 483 |
return status; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
static void |
| 499 |
linearray_reverse(ESL_RECORDER *rc, int pos, int n) |
| 500 |
{ |
| 501 |
int i; |
| 502 |
char *tmps; |
| 503 |
int tmpi; |
| 504 |
off_t tmpo; |
| 505 |
|
| 506 |
|
| 507 |
for (i = 0; i < n/2; i++) |
| 508 |
{ |
| 509 |
tmps = rc->line[pos+n-i-1]; |
| 510 |
rc->line[pos+n-i-1] = rc->line[pos+i]; |
| 511 |
rc->line[pos+i] = tmps; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
for (i = 0; i < n/2; i++) |
| 516 |
{ |
| 517 |
tmpi = rc->lalloc[pos+n-i-1]; |
| 518 |
rc->lalloc[pos+n-i-1] = rc->lalloc[pos+i]; |
| 519 |
rc->lalloc[pos+i] = tmpi; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
for (i = 0; i < n/2; i++) |
| 524 |
{ |
| 525 |
tmpo = rc->offset[pos+n-i-1]; |
| 526 |
rc->offset[pos+n-i-1] = rc->offset[pos+i]; |
| 527 |
rc->offset[pos+i] = tmpo; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
static int |
| 585 |
recorder_new_baseline(ESL_RECORDER *rc, int newbase) |
| 586 |
{ |
| 587 |
int n1, n2; |
| 588 |
|
| 589 |
if (newbase < rc->baseline) ESL_EXCEPTION(eslEINVAL, "new baseline must be > old one"); |
| 590 |
if (newbase == rc->baseline) return eslOK; |
| 591 |
|
| 592 |
n1 = (rc->nread - rc->baseline) % rc->nalloc; |
| 593 |
n2 = ESL_MIN(rc->nread-rc->baseline, rc->nalloc) - n1; |
| 594 |
|
| 595 |
if (n1>1) linearray_reverse(rc, 0, n1); |
| 596 |
if (n2>1) linearray_reverse(rc, n1, n2); |
| 597 |
|
| 598 |
n1 = (rc->nread - newbase) % rc->nalloc; |
| 599 |
n2 = ESL_MIN(rc->nread - newbase, rc->nalloc) - n1; |
| 600 |
|
| 601 |
if (n1>1) linearray_reverse(rc, 0, n1); |
| 602 |
if (n2>1) linearray_reverse(rc, n1, n2); |
| 603 |
|
| 604 |
rc->baseline = newbase; |
| 605 |
return eslOK; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
#ifdef eslRECORDER_BENCHMARK |
| 617 |
|
| 618 |
|
| 619 |
#include <stdio.h> |
| 620 |
|
| 621 |
#include "easel.h" |
| 622 |
#include "esl_getopts.h" |
| 623 |
#include "esl_recorder.h" |
| 624 |
#include "esl_stopwatch.h" |
| 625 |
|
| 626 |
static ESL_OPTIONS options[] = { |
| 627 |
|
| 628 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 629 |
{ "-N", eslARG_INT, "1000", NULL, "n>0", NULL, NULL, NULL, "set recorder window size in lines", 0 }, |
| 630 |
{ 0,0,0,0,0,0,0,0 }, |
| 631 |
}; |
| 632 |
static char usage[] = "[-options] <filename>"; |
| 633 |
static char banner[] = "benchmarking speed of ESL_RECORDER reading"; |
| 634 |
|
| 635 |
int |
| 636 |
main(int argc, char **argv) |
| 637 |
{ |
| 638 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 639 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 640 |
ESL_RECORDER *rc = NULL; |
| 641 |
char *filename = esl_opt_GetArg(go, 1); |
| 642 |
int N = esl_opt_GetInteger(go, "-N"); |
| 643 |
FILE *fp = NULL; |
| 644 |
char *buf = NULL; |
| 645 |
int balloc = 0; |
| 646 |
int status; |
| 647 |
|
| 648 |
if ((fp = fopen(filename, "r")) == NULL) esl_fatal("no such file %s\n", filename); |
| 649 |
rc = esl_recorder_Create(fp, N); |
| 650 |
esl_stopwatch_Start(w); |
| 651 |
while ((status = esl_recorder_Read(rc, &buf)) == eslOK); |
| 652 |
esl_recorder_Destroy(rc); |
| 653 |
fclose(fp); |
| 654 |
|
| 655 |
esl_stopwatch_Stop(w); |
| 656 |
esl_stopwatch_Display(stdout, w, "recorder time: "); |
| 657 |
|
| 658 |
if ((fp = fopen(filename, "r")) == NULL) esl_fatal("no such file %s\n", filename); |
| 659 |
esl_stopwatch_Start(w); |
| 660 |
while ((status = esl_fgets(&buf, &balloc, fp)) == eslOK); |
| 661 |
free(buf); |
| 662 |
fclose(fp); |
| 663 |
|
| 664 |
esl_stopwatch_Stop(w); |
| 665 |
esl_stopwatch_Display(stdout, w, "esl_fgets() time: "); |
| 666 |
|
| 667 |
esl_stopwatch_Destroy(w); |
| 668 |
esl_getopts_Destroy(go); |
| 669 |
return 0; |
| 670 |
} |
| 671 |
#endif |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
#ifdef eslRECORDER_TESTDRIVE |
| 680 |
#include "esl_random.h" |
| 681 |
|
| 682 |
static void |
| 683 |
generate_testfile(ESL_RANDOMNESS *rng, char *tmpfile, int *is_data, int nlines) |
| 684 |
{ |
| 685 |
char *msg = "esl_recorder:: test file generator failed"; |
| 686 |
FILE *fp = NULL; |
| 687 |
int in_block = esl_rnd_Roll(rng, 2); |
| 688 |
int nblock = 1 + esl_rnd_Roll(rng, 10); |
| 689 |
int i; |
| 690 |
|
| 691 |
if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg); |
| 692 |
for (i = 0; i < nlines; i++) |
| 693 |
{ |
| 694 |
is_data[i] = in_block ? TRUE : FALSE; |
| 695 |
fprintf(fp, "%c%d\n", (in_block ? '#' : ' '), i); |
| 696 |
if (--nblock == 0) { |
| 697 |
in_block = ! in_block; |
| 698 |
nblock = 1 + esl_rnd_Roll(rng, 10); |
| 699 |
} |
| 700 |
} |
| 701 |
fclose(fp); |
| 702 |
} |
| 703 |
|
| 704 |
static void |
| 705 |
utest_basic(char *tmpfile, int N) |
| 706 |
{ |
| 707 |
char *msg = "esl_recorder:: basic unit test failed"; |
| 708 |
ESL_RECORDER *rc = NULL; |
| 709 |
FILE *fp = NULL; |
| 710 |
int i; |
| 711 |
char *buf; |
| 712 |
|
| 713 |
if ((fp = fopen(tmpfile, "r")) == NULL) esl_fatal(msg); |
| 714 |
if ((rc = esl_recorder_Create(fp, N)) == NULL) esl_fatal(msg); |
| 715 |
for (i = 0; i < N; i++) |
| 716 |
{ |
| 717 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 718 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 719 |
} |
| 720 |
if (esl_recorder_Read(rc, &buf) != eslEOF) esl_fatal(msg); |
| 721 |
|
| 722 |
if (buf != NULL) esl_fatal(msg); |
| 723 |
|
| 724 |
if (esl_recorder_Position(rc, 0) != eslOK) esl_fatal(msg); |
| 725 |
for (i = 0; i < N; i++) |
| 726 |
{ |
| 727 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 728 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 729 |
} |
| 730 |
if (esl_recorder_Read(rc, &buf) != eslEOF) esl_fatal(msg); |
| 731 |
|
| 732 |
fclose(fp); |
| 733 |
esl_recorder_Destroy(rc); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
static void |
| 738 |
utest_grow(char *tmpfile, int N) |
| 739 |
{ |
| 740 |
char *msg = "esl_recorder:: grow unit test failed"; |
| 741 |
ESL_RECORDER *rc = NULL; |
| 742 |
FILE *fp = NULL; |
| 743 |
int i; |
| 744 |
char *buf; |
| 745 |
|
| 746 |
if ((fp = fopen(tmpfile, "r")) == NULL) esl_fatal(msg); |
| 747 |
if ((rc = esl_recorder_Create(fp, 3)) == NULL) esl_fatal(msg); |
| 748 |
for (i = 0; i < 4; i++) |
| 749 |
{ |
| 750 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 751 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
if (esl_recorder_Position(rc, 0) != eslEINVAL) esl_fatal(msg); |
| 756 |
if (esl_recorder_Position(rc, 1) != eslOK) esl_fatal(msg); |
| 757 |
if (esl_recorder_ResizeTo(rc, 6) != eslOK) esl_fatal(msg); |
| 758 |
|
| 759 |
|
| 760 |
for (i = 1; i < N; i++) |
| 761 |
{ |
| 762 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 763 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 764 |
} |
| 765 |
if (esl_recorder_Read(rc, &buf) != eslEOF) esl_fatal(msg); |
| 766 |
|
| 767 |
fclose(fp); |
| 768 |
esl_recorder_Destroy(rc); |
| 769 |
} |
| 770 |
|
| 771 |
static void |
| 772 |
utest_grow2(char *tmpfile, int N) |
| 773 |
{ |
| 774 |
char *msg = "esl_recorder:: grow2 unit test failed"; |
| 775 |
ESL_RECORDER *rc = NULL; |
| 776 |
FILE *fp = NULL; |
| 777 |
int i; |
| 778 |
char *buf; |
| 779 |
|
| 780 |
if (N < 5) esl_fatal(msg); |
| 781 |
|
| 782 |
if ((fp = fopen(tmpfile, "r")) == NULL) esl_fatal(msg); |
| 783 |
if ((rc = esl_recorder_Create(fp, 3)) == NULL) esl_fatal(msg); |
| 784 |
for (i = 0; i < 4; i++) |
| 785 |
{ |
| 786 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 787 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
if (esl_recorder_ResizeTo(rc, 6) != eslOK) esl_fatal(msg); |
| 792 |
|
| 793 |
|
| 794 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 795 |
if (atoi(buf+1) != 4) esl_fatal(msg); |
| 796 |
|
| 797 |
|
| 798 |
if (esl_recorder_Position(rc, 0) != eslEINVAL) esl_fatal(msg); |
| 799 |
if (esl_recorder_Position(rc, 1) != eslOK) esl_fatal(msg); |
| 800 |
for (i = 1; i < N; i++) |
| 801 |
{ |
| 802 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 803 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 804 |
} |
| 805 |
if (esl_recorder_Read(rc, &buf) != eslEOF) esl_fatal(msg); |
| 806 |
|
| 807 |
fclose(fp); |
| 808 |
esl_recorder_Destroy(rc); |
| 809 |
} |
| 810 |
|
| 811 |
static void |
| 812 |
utest_shrink(char *tmpfile, int N) |
| 813 |
{ |
| 814 |
char *msg = "esl_recorder:: shrink unit test failed"; |
| 815 |
ESL_RECORDER *rc = NULL; |
| 816 |
FILE *fp = NULL; |
| 817 |
int i; |
| 818 |
char *buf; |
| 819 |
|
| 820 |
if ((fp = fopen(tmpfile, "r")) == NULL) esl_fatal(msg); |
| 821 |
if ((rc = esl_recorder_Create(fp, 6)) == NULL) esl_fatal(msg); |
| 822 |
for (i = 0; i < 7; i++) |
| 823 |
{ |
| 824 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 825 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
if (esl_recorder_ResizeTo(rc, 3) != eslOK) esl_fatal(msg); |
| 830 |
|
| 831 |
if (esl_recorder_Position(rc, 4) != eslOK) esl_fatal(msg); |
| 832 |
|
| 833 |
for (i = 4; i < N; i++) |
| 834 |
{ |
| 835 |
if (esl_recorder_Read(rc, &buf) != eslOK) esl_fatal(msg); |
| 836 |
if (atoi(buf+1) != i) esl_fatal(msg); |
| 837 |
} |
| 838 |
if (esl_recorder_Read(rc, &buf) != eslEOF) esl_fatal(msg); |
| 839 |
|
| 840 |
fclose(fp); |
| 841 |
esl_recorder_Destroy(rc); |
| 842 |
} |
| 843 |
|
| 844 |
static void |
| 845 |
utest_block(ESL_RANDOMNESS *rng, char *tmpfile, int *is_data, int N) |
| 846 |
{ |
| 847 |
char *msg = "esl_recorder:: block unit test failed"; |
| 848 |
ESL_RECORDER *rc = NULL; |
| 849 |
FILE *fp = NULL; |
| 850 |
int linenumber = 0; |
| 851 |
int max_reposition = 2; |
| 852 |
int max_realloc = 2; |
| 853 |
int *nseen1 = NULL; |
| 854 |
int *nseen2 = NULL; |
| 855 |
int minalloc; |
| 856 |
int roll; |
| 857 |
char *buf; |
| 858 |
char **block; |
| 859 |
int from; |
| 860 |
int n,i; |
| 861 |
int status = eslOK; |
| 862 |
|
| 863 |
if ((fp = fopen(tmpfile, "r")) == NULL) esl_fatal(msg); |
| 864 |
roll = 1+esl_rnd_Roll(rng, N+1); |
| 865 |
if ((rc = esl_recorder_Create(fp, roll)) == NULL) esl_fatal(msg); |
| 866 |
|
| 867 |
if ((nseen1 = malloc(sizeof(int) * N)) == NULL) esl_fatal(msg); |
| 868 |
if ((nseen2 = malloc(sizeof(int) * N)) == NULL) esl_fatal(msg); |
| 869 |
for (i = 0; i < N; i++) nseen1[i] = 0; |
| 870 |
for (i = 0; i < N; i++) nseen2[i] = 0; |
| 871 |
|
| 872 |
while (status == eslOK) |
| 873 |
{ |
| 874 |
|
| 875 |
do { |
| 876 |
if (esl_recorder_Read(rc, &buf) == eslEOF) goto DONE; |
| 877 |
if (atoi(buf+1) != linenumber) esl_fatal(msg); |
| 878 |
if (esl_recorder_GetCurrent(rc) != linenumber) esl_fatal(msg); |
| 879 |
nseen1[linenumber]++; |
| 880 |
linenumber++; |
| 881 |
} while (*buf != '#'); |
| 882 |
|
| 883 |
|
| 884 |
from = esl_recorder_GetCurrent(rc); |
| 885 |
esl_recorder_MarkBlock(rc, from); |
| 886 |
do { |
| 887 |
if ((status = esl_recorder_Read(rc, &buf)) == eslEOF) break; |
| 888 |
if (atoi(buf+1) != linenumber) esl_fatal(msg); |
| 889 |
if (esl_recorder_GetCurrent(rc) != linenumber) esl_fatal(msg); |
| 890 |
nseen1[linenumber]++; |
| 891 |
linenumber++; |
| 892 |
} while (*buf == '#'); |
| 893 |
|
| 894 |
|
| 895 |
esl_recorder_GetBlock(rc, &block, NULL, NULL, &n); |
| 896 |
if (status == eslOK) n--; |
| 897 |
|
| 898 |
|
| 899 |
for (i = 0; i < n; i++) |
| 900 |
{ |
| 901 |
if (atoi(block[i]+1) != from+i) esl_fatal(msg); |
| 902 |
nseen2[from+i]++; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
esl_recorder_UnmarkBlock(rc); |
| 907 |
|
| 908 |
|
| 909 |
if (status == eslOK && max_reposition && (roll = esl_rnd_Roll(rng, 5)) == 0) |
| 910 |
{ |
| 911 |
linenumber = esl_recorder_GetFirst(rc) + |
| 912 |
esl_rnd_Roll(rng, esl_recorder_GetLast(rc) - esl_recorder_GetFirst(rc) + 1); |
| 913 |
if (esl_recorder_Position(rc, linenumber) != eslOK) esl_fatal(msg); |
| 914 |
max_reposition--; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
if (status == eslOK && max_realloc && (roll = esl_rnd_Roll(rng, 5)) == 0) |
| 919 |
{ |
| 920 |
|
| 921 |
minalloc = rc->nread-rc->ncurr+1; |
| 922 |
if (minalloc <= rc->nalloc) |
| 923 |
{ |
| 924 |
roll = minalloc + esl_rnd_Roll(rng, rc->nalloc-minalloc+1); |
| 925 |
if (esl_recorder_ResizeTo(rc, roll) != eslOK) esl_fatal(msg); |
| 926 |
max_realloc--; |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
DONE: |
| 932 |
|
| 933 |
if (esl_recorder_GetCurrent(rc) != N-1) esl_fatal(msg); |
| 934 |
|
| 935 |
|
| 936 |
for (i = 0; i < N; i++) |
| 937 |
if (! nseen1[i]) esl_fatal(msg); |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
for (i = 0; i < N; i++) { |
| 943 |
if ( is_data[i] && ! nseen2[i]) esl_fatal(msg); |
| 944 |
if (! is_data[i] && nseen2[i]) esl_fatal(msg); |
| 945 |
} |
| 946 |
|
| 947 |
fclose(fp); |
| 948 |
esl_recorder_Destroy(rc); |
| 949 |
free(nseen1); |
| 950 |
free(nseen2); |
| 951 |
} |
| 952 |
|
| 953 |
#endif |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
#ifdef eslRECORDER_TESTDRIVE |
| 961 |
|
| 962 |
|
| 963 |
#include <stdio.h> |
| 964 |
|
| 965 |
#include "easel.h" |
| 966 |
#include "esl_getopts.h" |
| 967 |
#include "esl_random.h" |
| 968 |
#include "esl_recorder.h" |
| 969 |
|
| 970 |
static ESL_OPTIONS options[] = { |
| 971 |
|
| 972 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 973 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 974 |
{ "-v", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "more verbose output", 0 }, |
| 975 |
{ "-N", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "number of lines per test file", 0 }, |
| 976 |
{ "-F", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of test files", 0 }, |
| 977 |
|
| 978 |
{ 0,0,0,0,0,0,0,0 }, |
| 979 |
}; |
| 980 |
static char usage[] = "[-options] <filename>"; |
| 981 |
static char banner[] = "test driver for ESL_RECORDER"; |
| 982 |
|
| 983 |
int |
| 984 |
main(int argc, char **argv) |
| 985 |
{ |
| 986 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 987 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 988 |
char template[13]= "esltmpXXXXXX"; |
| 989 |
char tmpfile[13]; |
| 990 |
int N = esl_opt_GetInteger(go, "-N"); |
| 991 |
int nfiles = esl_opt_GetInteger(go, "-F"); |
| 992 |
int *is_data = malloc(sizeof(int) * N); |
| 993 |
|
| 994 |
esl_exception_SetHandler(&esl_nonfatal_handler); |
| 995 |
|
| 996 |
if (esl_opt_GetBoolean(go, "-v")) { |
| 997 |
printf("random number seed: %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 998 |
} |
| 999 |
|
| 1000 |
while (nfiles--) |
| 1001 |
{ |
| 1002 |
strcpy(tmpfile, template); |
| 1003 |
generate_testfile(rng, tmpfile, is_data, N); |
| 1004 |
|
| 1005 |
utest_basic (tmpfile, N); |
| 1006 |
utest_grow (tmpfile, N); |
| 1007 |
utest_grow2 (tmpfile, N); |
| 1008 |
utest_shrink(tmpfile, N); |
| 1009 |
utest_block (rng, tmpfile, is_data, N); |
| 1010 |
|
| 1011 |
remove(tmpfile); |
| 1012 |
} |
| 1013 |
|
| 1014 |
free(is_data); |
| 1015 |
esl_getopts_Destroy(go); |
| 1016 |
esl_randomness_Destroy(rng); |
| 1017 |
|
| 1018 |
printf("ok\n"); |
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
#endif |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
#ifdef eslRECORDER_EXAMPLE |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
#include "easel.h" |
| 1034 |
#include "esl_recorder.h" |
| 1035 |
|
| 1036 |
int |
| 1037 |
main(int argc, char **argv) |
| 1038 |
{ |
| 1039 |
FILE *fp = stdin; |
| 1040 |
ESL_RECORDER *rc = esl_recorder_Create(fp, 10); |
| 1041 |
char *line; |
| 1042 |
int i; |
| 1043 |
int status; |
| 1044 |
|
| 1045 |
printf("\nFirst time:\n"); |
| 1046 |
for (i = 0; i < 10; i++) |
| 1047 |
{ |
| 1048 |
if ((status = esl_recorder_Read(rc, &line)) != eslOK) break; |
| 1049 |
fputs(line, stdout); |
| 1050 |
} |
| 1051 |
|
| 1052 |
esl_recorder_Position(rc, 0); |
| 1053 |
|
| 1054 |
printf("\nOne more time:\n"); |
| 1055 |
for (i = 0; i < 10; i++) |
| 1056 |
{ |
| 1057 |
if ((status = esl_recorder_Read(rc, &line)) != eslOK) break; |
| 1058 |
fputs(line, stdout); |
| 1059 |
} |
| 1060 |
|
| 1061 |
esl_recorder_Destroy(rc); |
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
#endif |
| 1065 |
|
| 1066 |
|
| 1067 |
#ifdef eslRECORDER_EXAMPLE2 |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
#include <stdio.h> |
| 1072 |
#include <ctype.h> |
| 1073 |
|
| 1074 |
#include "easel.h" |
| 1075 |
#include "esl_recorder.h" |
| 1076 |
|
| 1077 |
static int |
| 1078 |
is_data(char *s) |
| 1079 |
{ |
| 1080 |
if (*s == '#') return TRUE; |
| 1081 |
|
| 1082 |
return FALSE; |
| 1083 |
} |
| 1084 |
|
| 1085 |
int |
| 1086 |
main(int argc, char **argv) |
| 1087 |
{ |
| 1088 |
FILE *fp = fopen(argv[1], "r"); |
| 1089 |
ESL_RECORDER *rc = esl_recorder_Create(fp, 10); |
| 1090 |
char *line; |
| 1091 |
char **block; |
| 1092 |
int n; |
| 1093 |
int nblocks=0; |
| 1094 |
int i; |
| 1095 |
int status = eslOK; |
| 1096 |
|
| 1097 |
while (status == eslOK) |
| 1098 |
{ |
| 1099 |
|
| 1100 |
do { |
| 1101 |
if (esl_recorder_Read(rc, &line) == eslEOF) goto DONE; |
| 1102 |
} while (! is_data(line)); |
| 1103 |
|
| 1104 |
|
| 1105 |
esl_recorder_MarkBlock(rc, esl_recorder_GetCurrent(rc)); |
| 1106 |
do { |
| 1107 |
status = esl_recorder_Read(rc, &line); |
| 1108 |
} while (status == eslOK && is_data(line)); |
| 1109 |
|
| 1110 |
|
| 1111 |
esl_recorder_GetBlock(rc, &block, NULL, NULL, &n); |
| 1112 |
nblocks++; |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
if (status == eslOK) n--; |
| 1118 |
|
| 1119 |
|
| 1120 |
printf("BLOCK %d\n", nblocks); |
| 1121 |
for (i = 0; i < n; i++) |
| 1122 |
printf("line %4d: %s", i+1, block[i]); |
| 1123 |
printf("\n\n"); |
| 1124 |
|
| 1125 |
|
| 1126 |
esl_recorder_UnmarkBlock(rc); |
| 1127 |
} |
| 1128 |
|
| 1129 |
DONE: |
| 1130 |
esl_recorder_Destroy(rc); |
| 1131 |
fclose(fp); |
| 1132 |
return 0; |
| 1133 |
} |
| 1134 |
#endif |
| 1135 |
|
| 1136 |
|