tests/test_properties.c

changeset 1628
b15ed93f2aa7
parent 1605
55b13f583356
equal deleted inserted replaced
1627:d643fec1b2d5 1628:b15ed93f2aa7
489 } 489 }
490 490
491 cxPropertiesDestroy(&prop); 491 cxPropertiesDestroy(&prop);
492 } 492 }
493 493
494 static FILE *cx_opentmp(char *tpl) {
495 #if defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 200809L
496 strcpy(tpl, "ucxtestXXXXXX");
497 int fd = mkstemp(tpl);
498 if (fd < 0) return NULL;
499 return fdopen(fd, "w");
500 #else
501 char *fname = tmpnam(tpl);
502 return fname == NULL ? NULL : fopen(fname, "wx");
503 #endif
504 }
505
494 CX_TEST(test_properties_load) { 506 CX_TEST(test_properties_load) {
495 CxTestingAllocator talloc; 507 CxTestingAllocator talloc;
496 cx_testing_allocator_init(&talloc); 508 cx_testing_allocator_init(&talloc);
497 char fname[16] = "ucxtestXXXXXX"; 509 char fname[L_tmpnam];
498 int tmpfd = mkstemp(fname); 510 FILE *f = cx_opentmp(fname);
499 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 511 bool rmtmp = false;
500 CX_TEST_DO { 512 CX_TEST_DO {
501 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 513 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
514 rmtmp = true;
502 fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n"); 515 fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
503 fprintf(f, "\n\nkey3 = value3\n\n"); 516 fprintf(f, "\n\nkey3 = value3\n\n");
504 517
505 size_t key_len = 512; 518 size_t key_len = 512;
506 char *long_key = (char *) malloc(key_len); 519 char *long_key = (char *) malloc(key_len);
554 cxMapFree(map); 567 cxMapFree(map);
555 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 568 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
556 } 569 }
557 cx_testing_allocator_destroy(&talloc); 570 cx_testing_allocator_destroy(&talloc);
558 if (f) fclose(f); 571 if (f) fclose(f);
559 remove(fname); 572 if (rmtmp) remove(fname);
560 } 573 }
561 574
562 CX_TEST(test_properties_load_empty_file) { 575 CX_TEST(test_properties_load_empty_file) {
563 CxTestingAllocator talloc; 576 CxTestingAllocator talloc;
564 cx_testing_allocator_init(&talloc); 577 cx_testing_allocator_init(&talloc);
565 char fname[16] = "ucxtestXXXXXX"; 578 char fname[L_tmpnam];
566 int tmpfd = mkstemp(fname); 579 FILE *f = cx_opentmp(fname);
567 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 580 bool rmtmp = false;
568 CX_TEST_DO { 581 CX_TEST_DO {
569 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 582 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
583 rmtmp = true;
570 fclose(f); 584 fclose(f);
571 f = NULL; 585 f = NULL;
572 586
573 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); 587 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
574 // store something that we don't want to be deleted 588 // store something that we don't want to be deleted
584 cxMapFree(map); 598 cxMapFree(map);
585 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 599 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
586 } 600 }
587 cx_testing_allocator_destroy(&talloc); 601 cx_testing_allocator_destroy(&talloc);
588 if (f) fclose(f); 602 if (f) fclose(f);
589 remove(fname); 603 if (rmtmp) remove(fname);
590 } 604 }
591 605
592 CX_TEST(test_properties_load_only_comments) { 606 CX_TEST(test_properties_load_only_comments) {
593 CxTestingAllocator talloc; 607 CxTestingAllocator talloc;
594 cx_testing_allocator_init(&talloc); 608 cx_testing_allocator_init(&talloc);
595 char fname[16] = "ucxtestXXXXXX"; 609 char fname[L_tmpnam];
596 int tmpfd = mkstemp(fname); 610 FILE *f = cx_opentmp(fname);
597 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 611 bool rmtmp = false;
598 CX_TEST_DO { 612 CX_TEST_DO {
599 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 613 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
614 rmtmp = true;
600 fputs("# test file\n\n# contains only comments\n\n# key = value\n", f); 615 fputs("# test file\n\n# contains only comments\n\n# key = value\n", f);
601 fclose(f); 616 fclose(f);
602 f = NULL; 617 f = NULL;
603 618
604 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); 619 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
610 cxMapFree(map); 625 cxMapFree(map);
611 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 626 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
612 } 627 }
613 cx_testing_allocator_destroy(&talloc); 628 cx_testing_allocator_destroy(&talloc);
614 if (f) fclose(f); 629 if (f) fclose(f);
615 remove(fname); 630 if (rmtmp) remove(fname);
616 } 631 }
617 632
618 CX_TEST(test_properties_load_error) { 633 CX_TEST(test_properties_load_error) {
619 CxTestingAllocator talloc; 634 CxTestingAllocator talloc;
620 cx_testing_allocator_init(&talloc); 635 cx_testing_allocator_init(&talloc);
621 char fname[16] = "ucxtestXXXXXX"; 636 char fname[L_tmpnam];
622 int tmpfd = mkstemp(fname); 637 FILE *f = cx_opentmp(fname);
623 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 638 bool rmtmp = false;
624 CX_TEST_DO { 639 CX_TEST_DO {
625 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 640 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
641 rmtmp = true;
626 fputs("# test file\n\ntest = value\n = value2\n", f); 642 fputs("# test file\n\ntest = value\n = value2\n", f);
627 fclose(f); 643 fclose(f);
628 f = NULL; 644 f = NULL;
629 645
630 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); 646 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
640 cxMapFree(map); 656 cxMapFree(map);
641 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 657 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
642 } 658 }
643 cx_testing_allocator_destroy(&talloc); 659 cx_testing_allocator_destroy(&talloc);
644 if (f) fclose(f); 660 if (f) fclose(f);
645 remove(fname); 661 if (rmtmp) remove(fname);
646 } 662 }
647 663
648 CX_TEST(test_properties_load_file_not_exists) { 664 CX_TEST(test_properties_load_file_not_exists) {
649 CX_TEST_DO { 665 CX_TEST_DO {
650 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); 666 CxMap *map = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
655 } 671 }
656 672
657 CX_TEST(test_properties_load_cxmutstr_map) { 673 CX_TEST(test_properties_load_cxmutstr_map) {
658 CxTestingAllocator talloc; 674 CxTestingAllocator talloc;
659 cx_testing_allocator_init(&talloc); 675 cx_testing_allocator_init(&talloc);
660 char fname[16] = "ucxtestXXXXXX"; 676 char fname[L_tmpnam];
661 int tmpfd = mkstemp(fname); 677 FILE *f = cx_opentmp(fname);
662 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 678 bool rmtmp = false;
663 CX_TEST_DO { 679 CX_TEST_DO {
664 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 680 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
681 rmtmp = true;
665 fputs("# test file\n\ntest = value\ntest2 = value2\n", f); 682 fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
666 fclose(f); 683 fclose(f);
667 f = NULL; 684 f = NULL;
668 685
669 CxMap *map = cxHashMapCreate(NULL, sizeof(cxmutstr), 0); 686 CxMap *map = cxHashMapCreate(NULL, sizeof(cxmutstr), 0);
680 cxMapFree(map); 697 cxMapFree(map);
681 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 698 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
682 } 699 }
683 cx_testing_allocator_destroy(&talloc); 700 cx_testing_allocator_destroy(&talloc);
684 if (f) fclose(f); 701 if (f) fclose(f);
685 remove(fname); 702 if (rmtmp) remove(fname);
686 } 703 }
687 704
688 CX_TEST(test_properties_load_incompatible_map) { 705 CX_TEST(test_properties_load_incompatible_map) {
689 CxTestingAllocator talloc; 706 CxTestingAllocator talloc;
690 cx_testing_allocator_init(&talloc); 707 cx_testing_allocator_init(&talloc);
691 char fname[16] = "ucxtestXXXXXX"; 708 char fname[L_tmpnam];
692 int tmpfd = mkstemp(fname); 709 FILE *f = cx_opentmp(fname);
693 FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w"); 710 bool rmtmp = false;
694 CX_TEST_DO { 711 CX_TEST_DO {
695 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted"); 712 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
713 rmtmp = true;
696 fputs("# test file\n\ntest = value\ntest2 = value2\n", f); 714 fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
697 fclose(f); 715 fclose(f);
698 f = NULL; 716 f = NULL;
699 717
700 CxMap *map = cxHashMapCreate(NULL, sizeof(CxBuffer), 0); 718 CxMap *map = cxHashMapCreate(NULL, sizeof(CxBuffer), 0);
706 cxMapFree(map); 724 cxMapFree(map);
707 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 725 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
708 } 726 }
709 cx_testing_allocator_destroy(&talloc); 727 cx_testing_allocator_destroy(&talloc);
710 if (f) fclose(f); 728 if (f) fclose(f);
711 remove(fname); 729 if (rmtmp) remove(fname);
712 } 730 }
713 731
714 CX_TEST(test_properties_multiple_fill) { 732 CX_TEST(test_properties_multiple_fill) {
715 const char *props1 = "key1 = value1\n"; 733 const char *props1 = "key1 = value1\n";
716 const char *props2 = "key2 = value2\n"; 734 const char *props2 = "key2 = value2\n";

mercurial