diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-06-26 17:10:12 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-06-26 17:10:12 +0200 |
| commit | 57b81eacf20423c70a19d325bffee861d8b3a882 (patch) | |
| tree | 84e5107309a398818daf2b6237ffbc2a84af4ce4 | |
| parent | 13a965954523b8ee0a9633e02697f909c3d31a0e (diff) | |
| download | libcsv-57b81eacf20423c70a19d325bffee861d8b3a882.tar.gz libcsv-57b81eacf20423c70a19d325bffee861d8b3a882.tar.bz2 libcsv-57b81eacf20423c70a19d325bffee861d8b3a882.zip | |
Testcases für erfolgreiches Fehlermanagement...
| -rw-r--r-- | csv-test.c | 157 |
1 files changed, 157 insertions, 0 deletions
| @@ -2,6 +2,7 @@ | |||
| 2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 3 | #include <string.h> | 3 | #include <string.h> |
| 4 | #include <assert.h> | 4 | #include <assert.h> |
| 5 | #include <setjmp.h> | ||
| 5 | 6 | ||
| 6 | #include "csv.h" | 7 | #include "csv.h" |
| 7 | 8 | ||
| @@ -411,6 +412,157 @@ test6(void) | |||
| 411 | FMEMCLOSE(f); | 412 | FMEMCLOSE(f); |
| 412 | } | 413 | } |
| 413 | 414 | ||
| 415 | static void | ||
| 416 | cb_error(csv_err_t err, void *cb_arg) | ||
| 417 | { | ||
| 418 | longjmp(*((jmp_buf *) cb_arg), err); | ||
| 419 | } | ||
| 420 | |||
| 421 | void | ||
| 422 | error1(void) | ||
| 423 | { | ||
| 424 | jmp_buf env; | ||
| 425 | csv_options_t csv_options = csv_default_options; | ||
| 426 | csv_options.cb_error = cb_error; | ||
| 427 | csv_options.cb_error_arg = &env; | ||
| 428 | |||
| 429 | csv_t csv; | ||
| 430 | csv_init_opt(&csv, &csv_options); | ||
| 431 | |||
| 432 | FILE *f; | ||
| 433 | char data[] = ","; | ||
| 434 | |||
| 435 | FMEMOPEN(f, data); | ||
| 436 | |||
| 437 | switch ( setjmp(env) ) { | ||
| 438 | case CSV_ERR_OK: | ||
| 439 | assert( csv_read(&csv, f) == 2 ); | ||
| 440 | assert( csv_nfields(&csv) == 2 ); | ||
| 441 | |||
| 442 | // force an "out of range"-Exception! | ||
| 443 | assert( strcmp(csv_field(&csv, 2), "will fail") == 0 ); | ||
| 444 | break; | ||
| 445 | |||
| 446 | case CSV_ERR_OUT_OF_RANGE: | ||
| 447 | csv_cleanup(&csv); | ||
| 448 | break; | ||
| 449 | |||
| 450 | default: | ||
| 451 | assert(!"this should not be happen!"); | ||
| 452 | } | ||
| 453 | |||
| 454 | FMEMCLOSE(f); | ||
| 455 | } | ||
| 456 | |||
| 457 | void | ||
| 458 | error2(void) | ||
| 459 | { | ||
| 460 | jmp_buf env; | ||
| 461 | csv_options_t csv_options = csv_default_options; | ||
| 462 | csv_options.cb_error = cb_error; | ||
| 463 | csv_options.cb_error_arg = &env; | ||
| 464 | |||
| 465 | csv_t csv; | ||
| 466 | csv_init_opt(&csv, &csv_options); | ||
| 467 | |||
| 468 | FILE *f = fopen("/dev/null", "r"); | ||
| 469 | assert(f != NULL); | ||
| 470 | fputc('T', f); // Set Error-Flag! | ||
| 471 | |||
| 472 | switch ( setjmp(env) ) { | ||
| 473 | case CSV_ERR_OK: | ||
| 474 | assert( csv_read(&csv, f) == -1 ); | ||
| 475 | break; | ||
| 476 | |||
| 477 | case CSV_ERR_IO_READ: | ||
| 478 | csv_cleanup(&csv); | ||
| 479 | break; | ||
| 480 | |||
| 481 | default: | ||
| 482 | assert(!"this should not be happen!"); | ||
| 483 | } | ||
| 484 | |||
| 485 | fclose(f); | ||
| 486 | } | ||
| 487 | |||
| 488 | void * | ||
| 489 | my_allocate(size_t n, size_t sz, void *cb_arg) | ||
| 490 | { | ||
| 491 | (void) cb_arg; | ||
| 492 | |||
| 493 | void *ptr = calloc(n, sz); | ||
| 494 | return ptr; | ||
| 495 | } | ||
| 496 | |||
| 497 | void * | ||
| 498 | my_allocate_null(size_t n, size_t sz, void *cb_arg) | ||
| 499 | { | ||
| 500 | (void) n; | ||
| 501 | (void) sz; | ||
| 502 | (void) cb_arg; | ||
| 503 | |||
| 504 | return NULL; | ||
| 505 | } | ||
| 506 | |||
| 507 | void * | ||
| 508 | my_reallocate(void *ptr, size_t n, size_t sz, void *cb_arg) | ||
| 509 | { | ||
| 510 | (void) ptr; | ||
| 511 | (void) n; | ||
| 512 | (void) sz; | ||
| 513 | (void) cb_arg; | ||
| 514 | |||
| 515 | return NULL; | ||
| 516 | } | ||
| 517 | |||
| 518 | void | ||
| 519 | my_free(void *ptr, size_t n, size_t sz, void *cb_arg) | ||
| 520 | { | ||
| 521 | (void) n; | ||
| 522 | (void) sz; | ||
| 523 | (void) cb_arg; | ||
| 524 | |||
| 525 | free(ptr); | ||
| 526 | } | ||
| 527 | |||
| 528 | void | ||
| 529 | memory_fail1(void) | ||
| 530 | { | ||
| 531 | jmp_buf env; | ||
| 532 | |||
| 533 | csv_options_t csv_options = csv_default_options; | ||
| 534 | csv_options.cb_error = cb_error; | ||
| 535 | csv_options.cb_error_arg = &env; | ||
| 536 | csv_options.cb_allocate = my_allocate; | ||
| 537 | csv_options.cb_reallocate = my_reallocate; | ||
| 538 | csv_options.cb_free = my_free; | ||
| 539 | |||
| 540 | csv_t csv; | ||
| 541 | csv_init_opt(&csv, &csv_options); | ||
| 542 | |||
| 543 | char data[] = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T"; | ||
| 544 | |||
| 545 | FILE *f; | ||
| 546 | FMEMOPEN(f, data); | ||
| 547 | assert(f != NULL); | ||
| 548 | |||
| 549 | switch ( setjmp(env) ) { | ||
| 550 | case CSV_ERR_OK: | ||
| 551 | assert( csv_read(&csv, f) == 20 ); | ||
| 552 | assert(!"this should not be happen!"); | ||
| 553 | break; | ||
| 554 | |||
| 555 | case CSV_ERR_OUT_OF_MEMORY: | ||
| 556 | csv_cleanup(&csv); | ||
| 557 | break; | ||
| 558 | |||
| 559 | default: | ||
| 560 | assert(!"this should not be happen!"); | ||
| 561 | } | ||
| 562 | |||
| 563 | FMEMCLOSE(f); | ||
| 564 | } | ||
| 565 | |||
| 414 | int | 566 | int |
| 415 | main(void) | 567 | main(void) |
| 416 | { | 568 | { |
| @@ -421,5 +573,10 @@ main(void) | |||
| 421 | test5(); | 573 | test5(); |
| 422 | test6(); | 574 | test6(); |
| 423 | 575 | ||
| 576 | error1(); | ||
| 577 | error2(); | ||
| 578 | |||
| 579 | memory_fail1(); | ||
| 580 | |||
| 424 | return EXIT_SUCCESS; | 581 | return EXIT_SUCCESS; |
| 425 | } | 582 | } |
