Advertisement
Guest User

check_list.c

a guest
Feb 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.97 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <check.h>
  6.  
  7. #include "list.h"
  8.  
  9. // For older versions of the check library
  10. #ifndef ck_assert_ptr_nonnull
  11. #define ck_assert_ptr_nonnull(X) _ck_assert_ptr(X, !=,NULL)
  12. #endif
  13. #ifndef ck_assert_ptr_null
  14. #define ck_assert_ptr_null(X) _ck_assert_ptr(X, ==,NULL);
  15. #endif
  16.  
  17. #define LEN 10
  18.  
  19. /* test init & cleanup */
  20. START_TEST (test_list_init)
  21. {
  22.     struct list* l = list_init();
  23.     ck_assert_ptr_nonnull(l);
  24.     list_cleanup(l);
  25. }
  26. END_TEST
  27.  
  28. /* test add & head */
  29. START_TEST (test_add_front)
  30. {
  31.     struct list* l = list_init();
  32.    
  33.     struct node* n = list_new_node(5);
  34.     ck_assert_ptr_nonnull(n);  
  35.     ck_assert_int_eq(list_add_front(l, n), 0);
  36.    
  37.     struct node* m = list_head(l);
  38.     ck_assert_ptr_eq(m, n);
  39.    
  40.     list_cleanup(l);
  41. }
  42. END_TEST
  43.  
  44. /* test add multiple & next */
  45. START_TEST (test_next_multiple)
  46. {
  47.     struct list* l = list_init();    
  48.     struct node* n;
  49.    
  50.     for (int i = 0; i < LEN; i++) {
  51.         n = list_new_node(i);
  52.         ck_assert_int_eq(list_add_front(l, n), 0);
  53.     }
  54.    
  55.     struct node* m = list_head(l);
  56.     ck_assert_ptr_eq(m, n);
  57.  
  58.     for (int i = 0; i < LEN-1; i++) {
  59.         n = list_next(m);
  60.         ck_assert_ptr_nonnull(n);
  61.         ck_assert_ptr_ne(m, n);
  62.        
  63.         m = n;
  64.     }
  65.    
  66.     ck_assert_ptr_null(list_next(m));
  67.     list_cleanup(l);
  68. }
  69. END_TEST
  70.  
  71. /* test append & tail */
  72. START_TEST (test_add_back)
  73. {
  74.     struct list* l = list_init();
  75.    
  76.     struct node* n = list_new_node(5);
  77.     ck_assert_ptr_nonnull(n);  
  78.     ck_assert_int_eq(list_add_back(l, n), 0);
  79.    
  80.     struct node* m = list_tail(l);
  81.     ck_assert_ptr_eq(m, n);
  82.    
  83.     list_cleanup(l);
  84. }
  85. END_TEST
  86.  
  87. /* test append multiple & prev */
  88. START_TEST (test_prev_multiple)
  89. {
  90.     struct list* l = list_init();    
  91.     struct node* n;
  92.    
  93.     for (int i = 0; i < LEN; i++) {
  94.         n = list_new_node(i);
  95.         ck_assert_int_eq(list_add_back(l, n), 0);
  96.     }
  97.    
  98.     struct node* m = list_tail(l);
  99.     ck_assert_ptr_eq(m, n);
  100.  
  101.     for (int i = 0; i < LEN-1; i++) {
  102.         n = list_prev(l, m);
  103.         ck_assert_ptr_nonnull(n);
  104.         ck_assert_ptr_ne(m, n);
  105.        
  106.         m = n;
  107.     }
  108.    
  109.     ck_assert_ptr_null(list_prev(l, m));
  110.     list_cleanup(l);
  111. }
  112. END_TEST
  113.  
  114. /* test node value */
  115. START_TEST (test_node_value)
  116. {
  117.     struct list* l = list_init();
  118.     struct node* n = list_new_node(123);
  119.    
  120.     ck_assert_int_eq(list_node_value(n), 123);
  121.     list_add_front(l, n);
  122.     ck_assert_int_eq(list_node_value(list_head(l)), 123);
  123.    
  124.     list_cleanup(l);
  125. }
  126. END_TEST
  127.  
  128. /* test value multiple */
  129. START_TEST (test_value_multiple)
  130. {
  131.     struct list* l = list_init();
  132.     struct node* n;
  133.    
  134.     for (int i = LEN-1; i >= 0; i--) {
  135.         n = list_new_node(i);
  136.         list_add_front(l, n);
  137.  
  138.         n = list_new_node(i);
  139.         list_add_front(l, n);
  140.     }
  141.  
  142.     n = list_head(l);
  143.     for (int i = 0; i < LEN; i++) {
  144.         ck_assert_int_eq(list_node_value(n), i);
  145.         n = list_next(n);
  146.        
  147.         ck_assert_int_eq(list_node_value(n), i);
  148.         n = list_next(n);
  149.     }
  150.    
  151.     n = list_tail(l);
  152.     for (int i = LEN-1; i >= 0; i--) {
  153.         ck_assert_int_eq(list_node_value(n), i);
  154.         n = list_prev(l, n);
  155.        
  156.         ck_assert_int_eq(list_node_value(n), i);
  157.         n = list_prev(l, n);
  158.     }
  159.  
  160.     list_cleanup(l);
  161. }
  162. END_TEST
  163.  
  164. /* test unlink head */
  165. START_TEST (test_unlink_head)
  166. {
  167.     struct list* l = list_init();
  168.     struct node* n;
  169.    
  170.     for (int i = 0; i < LEN; i++) {
  171.         n = list_new_node(i);
  172.         list_add_front(l, n);
  173.     }
  174.    
  175.     for (int i = 0; i < LEN; i++) {
  176.         n = list_head(l);
  177.         ck_assert_int_eq(list_unlink_node(l, n), 0);
  178.         list_free_node(n);
  179.     }
  180.  
  181.     ck_assert_ptr_null(list_head(l));
  182.     list_cleanup(l);
  183. }
  184. END_TEST
  185.  
  186. /* test unlink any */
  187. START_TEST (test_unlink_multiple)
  188. {
  189.     struct list* l = list_init();
  190.     struct node* n;
  191.     struct node* m;
  192.    
  193.     for (int i = 0; i < LEN; i++) {
  194.         n = list_new_node(i);
  195.         list_add_back(l, n);
  196.     }
  197.    
  198.     n = list_next(list_head(l));
  199.     for (int i = 1; i < LEN; i+=2) {
  200.         m = list_next(list_next(n));
  201.         ck_assert_int_eq(list_unlink_node(l, n), 0);
  202.         ck_assert_int_eq(list_node_value(n), i);
  203.        
  204.         list_free_node(n);
  205.         n = m;
  206.     }
  207.    
  208.     n = list_head(l);
  209.     for (int i = 0; i < LEN; i+=2) {
  210.         m = list_next(n);
  211.         ck_assert_int_eq(list_unlink_node(l, n), 0);
  212.         ck_assert_int_eq(list_node_value(n), i);
  213.        
  214.         list_free_node(n);
  215.         n = m;
  216.     }
  217.  
  218.     ck_assert_ptr_null(list_head(l));
  219.     list_cleanup(l);
  220. }
  221. END_TEST
  222.  
  223. /* test list cleanup */
  224. START_TEST (test_list_cleanup)
  225. {
  226.     struct list* l = list_init();
  227.     struct node* n;
  228.    
  229.     for (int i = 0; i < LEN; i++) {
  230.         n = list_new_node(i);
  231.         list_add_front(l, n);
  232.     }
  233.    
  234.     ck_assert_int_eq(list_cleanup(l),0);
  235. }
  236. END_TEST
  237.  
  238. Suite * list_suite(void) {
  239.     Suite *s;
  240.     TCase *tc_core;
  241.  
  242.     s = suite_create("List");
  243.     tc_core = tcase_create("Core");
  244.    
  245.     tcase_add_test(tc_core, test_list_init);
  246.     tcase_add_test(tc_core, test_add_front);
  247.     tcase_add_test(tc_core, test_next_multiple);
  248.     tcase_add_test(tc_core, test_add_back);
  249.     tcase_add_test(tc_core, test_prev_multiple);
  250.     tcase_add_test(tc_core, test_node_value);
  251.     tcase_add_test(tc_core, test_value_multiple);
  252.     tcase_add_test(tc_core, test_unlink_head);
  253.     tcase_add_test(tc_core, test_unlink_multiple);
  254.     tcase_add_test(tc_core, test_list_cleanup);
  255.  
  256.     suite_add_tcase(s, tc_core);
  257.     return s;
  258. }
  259.  
  260. int main(void) {
  261.     int number_failed;
  262.     Suite *s = list_suite();
  263.     SRunner *sr = srunner_create(s);
  264.  
  265.     srunner_run_all(sr, CK_VERBOSE);
  266.     number_failed = srunner_ntests_failed(sr);
  267.    
  268.     srunner_free(sr);
  269.     return number_failed ? EXIT_FAILURE : EXIT_SUCCESS;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement