0

I have a void insert function and I want to test if it inserts correctly. I'm using CMocka framework for testing.

I've tried to do dpl_insert(ret, "mock_value") instead of the will_return() but it seems that no value is appended to the list.

t_list **dpl_create();
int ft_list_size(t_list *self);

void __wrap_dpl_insert(t_list **self, void *data)
{
   check_expected(self);
   check_expected(data);
}

static void test_dpl_insert_empty_list(void **state)
{
    (void) state;
    t_list **ret = dpl_create(); //Initialization of an empty struct.
    int ret_size;

    expect_value(__wrap_dpl_insert, self, ret);
    expect_value(__wrap_dpl_insert, data, "mock_value");

    will_return(__wrap_dpl_insert, XXX); //PROBLEM RESIDES HERE.

    ret_size = ft_list_size(*ret);
    fprintf(stderr, "Size of the list=>%d\n", ret_size);
}

With dpl_insert(ret, "mock_value") the fprintf() prints 0, like if no element was added.

My objective is to obtain 1 from fprintf().

Joshua
  • 57
  • 4
  • It appears, at least to me that there are too many unknowns to answer. I did find _[this converstion/answer](https://stackoverflow.com/a/42156202/645128)_ that may help with part of what you are doing. – ryyker Apr 24 '19 at 15:21

1 Answers1

0

I've came up with this solution.

static void test_dpl_insert_empty_list(void **state)                                                                                                                                                       
 {                                                                                                                                                                                                          
     (void) state; /* unused */                                                                                                                                                                             
     t_list **ret;                                                                                                                                                                                          
     t_list *beg;                                                                                                                                                                                           
     int count, data;                                                                                                                                                                                       

     count = 0;                                                                                                                                                                                             

     will_return(__wrap_dpl_create, malloc(sizeof(t_list)));                                                                                                                                                
     ret = dpl_create();                                                                                                                                                                                    
     dpl_insert(ret, (void*)1);                                                                                                                                                                             
     dpl_insert(ret, (void*)2);                                                                                                                                                                             
     dpl_insert(ret, (void*)3);                                                                                                                                                                             

     beg = *ret;                                                                                                                                                                                            
     while(beg != NULL)                                                                                                                                                                                     
     {                                                                                                                                                                                                      
         data = (intptr_t)beg->data;                                                                                                                                                                        
         assert_int_equal(++count, data);                                                                                                                                                                   
         beg = beg->next;                                                                                                                                                                                   
     }                                                                                                                                                                                                      
 } 

Instead of using the __wrap_dpl_insert() I used the original function dpl_insert(). I'm wondering if this is the best approach to do it.

Joshua
  • 57
  • 4