LCOV - code coverage report
Current view: top level - redis - test_adlist.c (source / functions) Hit Total Coverage
Test: redis_test.info Lines: 40 84 47.6 %
Date: 2021-05-15 09:19:17 Functions: 5 7 71.4 %

          Line data    Source code
       1             : #include <stdarg.h>
       2             : #include <stddef.h>
       3             : #include <setjmp.h>
       4             : #include <stdint.h>
       5             : #include <cmocka.h>
       6             : #include <stdio.h>
       7             : #include "adlist.h"
       8             : 
       9             : void *__wrap_zmalloc(size_t size);
      10             : void *__real_zmalloc(size_t size);
      11           9 : void *__wrap_zmalloc(size_t size) {
      12           9 :     return (void*) mock();
      13             : }
      14             : 
      15           1 : static void test_list_create_fail(void **state) {
      16           1 :     will_return(__wrap_zmalloc, NULL);
      17           1 :     list *l = listCreate();
      18           1 :     assert_null(l);
      19           1 : }
      20             : 
      21           1 : static void test_list_create_release(void **state) {
      22           1 :     will_return(__wrap_zmalloc, __real_zmalloc(sizeof(list)));
      23           1 :     list *l = listCreate();
      24           1 :     assert_non_null(l);
      25           1 :     listRelease(l);
      26           1 : }
      27             : 
      28           0 : static void test_list_dup_search(void **state) {
      29             : 
      30           0 :     list *l = listCreate(), *l2;
      31           0 :     int a[] = {1,2,3,4}, i;
      32             :     listNode *p;
      33             :     listIter *li;
      34             : 
      35           0 :     for (i = 0; i < 4; ++i) {
      36           0 :         listAddNodeHead(l, &a[i]);
      37             :     }
      38           0 :     assert_int_equal(listLength(l), 4);
      39             : 
      40           0 :     l2 = listDup(l);
      41           0 :     assert_int_equal(listLength(l), 4);
      42             : 
      43           0 :     p = listSearchKey(l2, &a[1]);
      44           0 :     assert_int_equal(*(int*)listNodeValue(p), 2);
      45           0 :     p = listNextNode(p);
      46           0 :     assert_int_equal(*(int*)p->value, 1);
      47             : 
      48           0 :     p = listIndex(l2, 2);
      49           0 :     assert_int_equal(*(int*)listNodeValue(p), 2);
      50           0 :     p = listNextNode(p);
      51           0 :     assert_int_equal(*(int*)p->value, 1);
      52             : 
      53           0 :     listRelease(l);
      54           0 :     listRelease(l2);
      55             : 
      56             : 
      57           0 : }
      58           1 : static void test_list_iter(void **state) {
      59           1 :     will_return(__wrap_zmalloc, __real_zmalloc(sizeof(list)));
      60           1 :     list *l = listCreate();
      61           1 :     int a[] = {1,2,3,4}, i;
      62             :     listNode *p;
      63             :     listIter *li;
      64             : 
      65           5 :     for (i = 0; i < 4; ++i) {
      66           4 :         will_return(__wrap_zmalloc, __real_zmalloc(sizeof(listNode)));
      67           4 :         listAddNodeHead(l, &a[i]);
      68             :     }
      69           1 :     assert_int_equal(listLength(l), 4);
      70             : 
      71           1 :     will_return(__wrap_zmalloc, __real_zmalloc(sizeof(listIter)));
      72           1 :     li = listGetIterator(l, 0);
      73           1 :     p = listNextElement(li);
      74           1 :     assert_int_equal(*(int*)listNodeValue(p), 4);
      75           1 :     p = listNextElement(li);
      76           1 :     assert_int_equal(*(int*)listNodeValue(p), 3);
      77           1 :     listReleaseIterator(li);
      78             : 
      79           1 :     will_return(__wrap_zmalloc, __real_zmalloc(sizeof(listIter)));
      80           1 :     li = listGetIterator(l, 1);
      81           1 :     p = listNextElement(li);
      82           1 :     assert_int_equal(*(int*)listNodeValue(p), 1);
      83           1 :     p = listNextElement(li);
      84           1 :     assert_int_equal(*(int*)listNodeValue(p), 2);
      85           1 :     listReleaseIterator(li);
      86             : 
      87           1 :     listRelease(l);
      88           1 : }
      89             : 
      90           0 : static void test_list_add(void **state) {
      91           0 :     list *l = listCreate();
      92           0 :     int a = 1, b = 2, c = 3, d = 4;
      93             :     listNode *p;
      94             : 
      95           0 :     listAddNodeHead(l, &a);
      96           0 :     listAddNodeHead(l, &b);
      97           0 :     assert_int_equal(listLength(l), 2);
      98             : 
      99           0 :     listAddNodeTail(l, &c);
     100           0 :     listAddNodeTail(l, &d);
     101           0 :     assert_int_equal(listLength(l), 4);
     102             : 
     103           0 :     assert_int_equal(*(int*)l->head->value, 2);
     104           0 :     assert_int_equal(*(int*)l->head->next->value, 1);
     105           0 :     assert_int_equal(*(int*)l->head->next->next->value, 3);
     106           0 :     assert_int_equal(*(int*)l->head->next->next->next->value, 4);
     107             : 
     108           0 :     assert_int_equal(*(int*)listFirst(l)->value, 2);
     109           0 :     assert_int_equal(*(int*)listLast(l)->value, 4);
     110             : 
     111           0 :     p = listNextNode(l->head);
     112           0 :     assert_int_equal(*(int*)p->value, 1);
     113           0 :     p = listPrevNode(p);
     114           0 :     assert_int_equal(*(int*)p->value, 2);
     115             : 
     116           0 :     assert_int_equal(*(int*)listNodeValue(p), 2);
     117             : 
     118           0 :     listDelNode(l, p);
     119           0 :     assert_int_equal(listLength(l), 3);
     120           0 :     assert_int_equal(*(int*)l->head->value, 1);
     121             : 
     122           0 :     listRelease(l);
     123           0 : }
     124             : 
     125           1 : int main(void) {
     126           1 :     const struct CMUnitTest tests[] = {
     127             :         cmocka_unit_test(test_list_create_release),
     128             :         cmocka_unit_test(test_list_create_fail),
     129             :         //cmocka_unit_test(test_list_add),
     130             :         cmocka_unit_test(test_list_iter),
     131             :         //cmocka_unit_test(test_list_dup_search),
     132             :     };
     133             : 
     134           1 :     return cmocka_run_group_tests(tests, NULL, NULL);
     135             : }
     136             : 

Generated by: LCOV version 1.14