cunit-testingdata-structureslinked-listunity-test-framework

Linked list insert function returns wrong while tested with unity


i'm working on a simple doubly linked list implementation in c, i've created my structures as follows.

typdef struct node{
  void *data;
  struct node *next, *prev;
}node;

typedef struct list{
  struct node *head, *tail;
  size_t size;
}list;

I'm inserting elements in my linked list using this function and everything seems to work fine. Let's assume i'm filling my list with integers calling the function 4 times to insert {2,4,6,8}. When i execute my print function it correctly returns 2,4,6,8.

void insert_node(list *l, void *elem)
{
  node *n = create_node(elem); //here i just create and initialize the new node;
  if(l->size == 0){
    l->head = n;
    l->tail = n;
  }else{
    l->tail->next = n;
    n->prev = l->tail;
    l->tail = n;
  }
  l->size++;
}

The problem rises when i try to test my function with unity, i wrote this simple unit test:

void test_list_insert(){
 list *l = list_test(); //this function creates a list and inserts in it {2,4,6,8} as values
 TEST_ASSERT_EQUAL_INT(2, *(int*)(get_node_i(l,0))->data);
 TEST_ASSERT_EQUAL_INT(4, *(int*)(get_node_i(l,1))->data); //problem seems to be here..
 TEST_ASSERT_EQUAL_INT(6, *(int*)(get_node_i(l,2))->data);
 TEST_ASSERT_EQUAL_INT(8, *(int*)(get_node_i(l,3))->data);
}

When i execute my unit test i get this output:

test.c:73:test_list_insert:FAIL Expected 4 was 1

At this point the problem seems related to the 'get_node_i' function which is used to retrieve the element in the i-th position of the list... here's the function:

node *get_node_i(list *l, int pos){
 if(pos > l->size || pos < 0){
   return NULL;
 }
 node *curr = l->head;
 int currPos = 0;
 if(pos == 0) return curr;
 while(curr != NULL){
   if(currPos == pos){
     return curr; 
   }
   currPos++;
   curr = curr->next;  
 }
 return NULL;
}

I've tried to execute my print function inside the unit test and i discovered that it prints correctly just the first two nodes (2,4) and for the other nodes it prints pointers... That for me is quite strange as if i try to execute the print function in any other part of my code it returns the list correctly..

Here's how i create lists and nodes

//create new node
node* create_node(void * elem){
  node *n = (node *)malloc(sizeof (node));
  n->data = elem;
  n->next = NULL;
  n->prev = NULL;
  return n;
}
//create an empty list
list  *create_list(){
  list *l = (list *)malloc(sizeof(list));
  l->size = 0;
  l->head = NULL;
  l->tail = NULL;
  return l;
}

Here's the list_test function and the print function,

list* list_test(){
   list *l = create_list();
   int a = 2;
   int b = 4;
   int c = 6;
   int d = 8;
   insert_node(l, &a);
   insert_node(l, &b);
   insert_node(l, &c);
   insert_node(l, &d);
   return l;

}


//print the list
void print_list(list *l){
  node *tmp = l->head;
  while(tmp != NULL){
    printf("%d\t" , *(int *)tmp->data);
    tmp = tmp->next;
  }
}

if something else needs to be clarified, let me know, thanks.


Solution

  • In your function list_test you insert the address of local variables. So node->data is assigned the address of a local variable. When the function returns, the data pointed by these address will change.

    The function list_test should be something like the following:

    list* list_test(){
       list *l = create_list();
       int a = 2, *ap = malloc(sizeof(int));
       int b = 4, *bp = malloc(sizeof(int));
       int c = 6, *cp = malloc(sizeof(int));
       int d = 8, *dp = malloc(sizeof(int));
       *ap = a;
       *bp = b;
       *cp = c;
       *dp = d;
       insert_node(l, ap);
       insert_node(l, bp);
       insert_node(l, cp);
       insert_node(l, dp);
       return l;
    }