comparison tests.c @ 760:1cb9454ec569

Add some simple tests for linked lists.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Feb 2023 04:00:50 +0200
parents feadd3678d2e
children 0abba6091bd1
comparison
equal deleted inserted replaced
759:618c7fa3a4f8 760:1cb9454ec569
488 out: 488 out:
489 th_io_close(fh); 489 th_io_close(fh);
490 } 490 }
491 491
492 492
493 void test_config(void) 493 void test_config_file(void)
494 { 494 {
495 static const char *filename = "cfg.temp"; 495 static const char *filename = "cfg.temp";
496 test_ctx ctx; 496 test_ctx ctx;
497 th_ioctx *fh = NULL; 497 th_ioctx *fh = NULL;
498 th_cfgitem_t *sect1, *sect2, *cfg = NULL, *item; 498 th_cfgitem_t *sect1, *sect2, *cfg = NULL, *item;
568 out: 568 out:
569 // Free the data for v_str_list 569 // Free the data for v_str_list
570 th_io_close(fh); 570 th_io_close(fh);
571 th_llist_free_func_data(v_str_list, th_free); 571 th_llist_free_func_data(v_str_list, th_free);
572 th_cfg_free(cfg, test_config_free); 572 th_cfg_free(cfg, test_config_free);
573 }
574
575
576 bool test_linked_list_length(th_llist_t *list, const size_t n)
577 {
578 test_ctx ctx;
579 bool ok = th_llist_length(list) == n;
580
581 test_start(&ctx, "Test list length matching");
582 test_result(&ctx, ok);
583 test_end(&ctx);
584
585 return ok;
586 }
587
588
589 bool test_linked_list_validity(th_llist_t *list, const size_t nstart, const ssize_t ndelta)
590 {
591 test_ctx ctx;
592 th_llist_t *node;
593 size_t n;
594 bool ok = true;
595
596 for (n = nstart, node = list; node != NULL; node = node->next, n += ndelta)
597 {
598 if ((size_t) node->data != n)
599 {
600 ok = false;
601 break;
602 }
603 }
604
605 test_start(&ctx, "Test list data contents");
606 test_result_msg(&ctx, ok, "failure at index #%d", n);
607 test_end(&ctx);
608
609 return ok;
610 }
611
612
613 int test_llist_sortfunc(const th_llist_t *lnode, const th_llist_t *rnode, void *userdata)
614 {
615 (void) userdata;
616 return lnode->data <= rnode->data;
617 }
618
619
620 void test_linked_lists(void)
621 {
622 th_llist_t *list = NULL, *node;
623 size_t nnodes = 128, n;
624
625 tprint(2, "Creating linked list\n");
626 for (n = 0; n < nnodes; n++)
627 {
628 if ((node = th_llist_append(&list, (void *) n)) == NULL)
629 {
630 THERR("Error allocating memory.\n");
631 goto out;
632 }
633 }
634
635 // Test
636 test_linked_list_length(list, nnodes);
637 test_linked_list_validity(list, 0, 1);
638
639 // Reverse
640 th_llist_reverse(&list);
641
642 test_linked_list_length(list, nnodes);
643 test_linked_list_validity(list, nnodes - 1, -1);
644
645 // Sort
646 th_llist_mergesort(&list, test_llist_sortfunc, NULL);
647
648 // Test count
649 test_linked_list_length(list, nnodes);
650 test_linked_list_validity(list, 0, 1);
651
652 // Prepend
653 tprint(2, "Prepend node to list\n");
654 for (size_t k = 0; k < 16; k++)
655 {
656 if ((node = th_llist_prepend(&list, (void *) n)) == NULL)
657 {
658 THERR("Error allocating memory.\n");
659 goto out;
660 }
661 nnodes++;
662 n++;
663 }
664
665 // Test count
666 test_linked_list_length(list, nnodes);
667
668 // Sort
669 th_llist_mergesort(&list, test_llist_sortfunc, NULL);
670 test_linked_list_validity(list, 0, 1);
671
672 // Delete
673 th_llist_delete_node(&list, list);
674 test_linked_list_length(list, nnodes - 1);
675 test_linked_list_validity(list, 1, 1);
676
677 out:
678 th_llist_free(list);
573 } 679 }
574 680
575 681
576 #ifdef TH_EXPERIMENTAL_REGEX 682 #ifdef TH_EXPERIMENTAL_REGEX
577 683
1054 // 1160 //
1055 // Configuration file handling 1161 // Configuration file handling
1056 // 1162 //
1057 if (test_set_start("Configuration file handling")) 1163 if (test_set_start("Configuration file handling"))
1058 { 1164 {
1059 test_config(); 1165 test_config_file();
1060 } 1166 }
1061 1167
1168 //
1169 // Linked lists
1170 //
1171 if (test_set_start("Linked lists"))
1172 {
1173 test_linked_lists();
1174 }
1062 // 1175 //
1063 // String functions 1176 // String functions
1064 // 1177 //
1065 if (test_set_start("String functions")) 1178 if (test_set_start("String functions"))
1066 { 1179 {