aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--csv-test.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/csv-test.c b/csv-test.c
index 5fd0e15..ecf55cd 100644
--- a/csv-test.c
+++ b/csv-test.c
@@ -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
415static void
416cb_error(csv_err_t err, void *cb_arg)
417{
418 longjmp(*((jmp_buf *) cb_arg), err);
419}
420
421void
422error1(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
457void
458error2(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
488void *
489my_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
497void *
498my_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
507void *
508my_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
518void
519my_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
528void
529memory_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
414int 566int
415main(void) 567main(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}