Home | History | Annotate | Download | only in unit
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.haxx.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 #include "curlcheck.h"
     23 
     24 #include "llist.h"
     25 
     26 static struct curl_llist llist;
     27 
     28 static struct curl_llist llist_destination;
     29 
     30 static void test_curl_llist_dtor(void *key, void *value)
     31 {
     32   /* used by the llist API, does nothing here */
     33   (void)key;
     34   (void)value;
     35 }
     36 
     37 static CURLcode unit_setup(void)
     38 {
     39   Curl_llist_init(&llist, test_curl_llist_dtor);
     40   Curl_llist_init(&llist_destination, test_curl_llist_dtor);
     41   return CURLE_OK;
     42 }
     43 
     44 static void unit_stop(void)
     45 {
     46   Curl_llist_destroy(&llist, NULL);
     47   Curl_llist_destroy(&llist_destination, NULL);
     48 }
     49 
     50 UNITTEST_START
     51 {
     52   int unusedData_case1 = 1;
     53   int unusedData_case2 = 2;
     54   int unusedData_case3 = 3;
     55   struct curl_llist_element case1_list;
     56   struct curl_llist_element case2_list;
     57   struct curl_llist_element case3_list;
     58   struct curl_llist_element case4_list;
     59   struct curl_llist_element case5_list;
     60   struct curl_llist_element *head;
     61   struct curl_llist_element *element_next;
     62   struct curl_llist_element *element_prev;
     63   struct curl_llist_element *to_remove;
     64   size_t llist_size = Curl_llist_count(&llist);
     65 
     66   /**
     67    * testing llist_init
     68    * case 1:
     69    * list initiation
     70    * @assumptions:
     71    * 1: list size will be 0
     72    * 2: list head will be NULL
     73    * 3: list tail will be NULL
     74    * 4: list dtor will be NULL
     75   */
     76 
     77   fail_unless(llist.size == 0, "list initial size should be zero");
     78   fail_unless(llist.head == NULL, "list head should initiate to NULL");
     79   fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
     80   fail_unless(llist.dtor == test_curl_llist_dtor,
     81                "list dtor shold initiate to test_curl_llist_dtor");
     82 
     83   /**
     84    * testing Curl_llist_insert_next
     85    * case 1:
     86    * list is empty
     87    * @assumptions:
     88    * 1: list size will be 1
     89    * 2: list head will hold the data "unusedData_case1"
     90    * 3: list tail will be the same as list head
     91    */
     92 
     93   Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
     94 
     95   fail_unless(Curl_llist_count(&llist) == 1,
     96               "List size should be 1 after adding a new element");
     97   /*test that the list head data holds my unusedData */
     98   fail_unless(llist.head->ptr == &unusedData_case1,
     99               "head ptr should be first entry");
    100   /*same goes for the list tail */
    101   fail_unless(llist.tail == llist.head,
    102               "tail and head should be the same");
    103 
    104   /**
    105    * testing Curl_llist_insert_next
    106    * case 2:
    107    * list has 1 element, adding one element after the head
    108    * @assumptions:
    109    * 1: the element next to head should be our newly created element
    110    * 2: the list tail should be our newly created element
    111    */
    112 
    113   Curl_llist_insert_next(&llist, llist.head,
    114                          &unusedData_case3, &case3_list);
    115   fail_unless(llist.head->next->ptr == &unusedData_case3,
    116               "the node next to head is not getting set correctly");
    117   fail_unless(llist.tail->ptr == &unusedData_case3,
    118               "the list tail is not getting set correctly");
    119 
    120   /**
    121    * testing Curl_llist_insert_next
    122    * case 3:
    123    * list has >1 element, adding one element after "NULL"
    124    * @assumptions:
    125    * 1: the element next to head should be our newly created element
    126    * 2: the list tail should different from newly created element
    127    */
    128 
    129   Curl_llist_insert_next(&llist, llist.head,
    130                          &unusedData_case2, &case2_list);
    131   fail_unless(llist.head->next->ptr == &unusedData_case2,
    132               "the node next to head is not getting set correctly");
    133   /* better safe than sorry, check that the tail isn't corrupted */
    134   fail_unless(llist.tail->ptr != &unusedData_case2,
    135               "the list tail is not getting set correctly");
    136 
    137   /* unit tests for Curl_llist_remove */
    138 
    139   /**
    140    * case 1:
    141    * list has >1 element, removing head
    142    * @assumptions:
    143    * 1: list size will be decremented by one
    144    * 2: head will be the head->next
    145    * 3: "new" head's previous will be NULL
    146    */
    147 
    148   head = llist.head;
    149   abort_unless(head, "llist.head is NULL");
    150   element_next = head->next;
    151   llist_size = Curl_llist_count(&llist);
    152 
    153   Curl_llist_remove(&llist, llist.head, NULL);
    154 
    155   fail_unless(Curl_llist_count(&llist) ==  (llist_size-1),
    156                "llist size not decremented as expected");
    157   fail_unless(llist.head == element_next,
    158                "llist new head not modified properly");
    159   abort_unless(llist.head, "llist.head is NULL");
    160   fail_unless(llist.head->prev == NULL,
    161               "new head previous not set to null");
    162 
    163   /**
    164    * case 2:
    165    * removing non head element, with list having >=2 elements
    166    * @setup:
    167    * 1: insert another element to the list to make element >=2
    168    * @assumptions:
    169    * 1: list size will be decremented by one ; tested
    170    * 2: element->previous->next will be element->next
    171    * 3: element->next->previous will be element->previous
    172    */
    173   Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
    174                          &case4_list);
    175   llist_size = Curl_llist_count(&llist);
    176   fail_unless(llist_size == 3, "should be 3 list members");
    177 
    178   to_remove = llist.head->next;
    179   abort_unless(to_remove, "to_remove is NULL");
    180   element_next = to_remove->next;
    181   element_prev = to_remove->prev;
    182   Curl_llist_remove(&llist, to_remove, NULL);
    183   fail_unless(element_prev->next == element_next,
    184               "element previous->next is not being adjusted");
    185   abort_unless(element_next, "element_next is NULL");
    186   fail_unless(element_next->prev == element_prev,
    187               "element next->previous is not being adjusted");
    188 
    189   /**
    190    * case 3:
    191    * removing the tail with list having >=1 element
    192    * @assumptions
    193    * 1: list size will be decremented by one ;tested
    194    * 2: element->previous->next will be element->next ;tested
    195    * 3: element->next->previous will be element->previous ;tested
    196    * 4: list->tail will be tail->previous
    197    */
    198 
    199   to_remove = llist.tail;
    200   element_prev = to_remove->prev;
    201   Curl_llist_remove(&llist, to_remove, NULL);
    202   fail_unless(llist.tail == element_prev,
    203               "llist tail is not being adjusted when removing tail");
    204 
    205   /**
    206    * case 4:
    207    * removing head with list having 1 element
    208    * @assumptions:
    209    * 1: list size will be decremented by one ;tested
    210    * 2: list head will be null
    211    * 3: list tail will be null
    212    */
    213 
    214   to_remove = llist.head;
    215   Curl_llist_remove(&llist, to_remove, NULL);
    216   fail_unless(llist.head == NULL,
    217               "llist head is not NULL while the llist is empty");
    218   fail_unless(llist.tail == NULL,
    219               "llist tail is not NULL while the llist is empty");
    220 
    221   /* @testing Curl_llist_move(struct curl_llist *,
    222    * struct curl_llist_element *, struct curl_llist *,
    223    * struct curl_llist_element *);
    224   */
    225 
    226   /**
    227    * @case 1:
    228    * moving head from an llist containing one element to an empty llist
    229    * @assumptions:
    230    * 1: llist size will be 0
    231    * 2: llist_destination size will be 1
    232    * 3: llist head will be NULL
    233    * 4: llist_destination head == llist_destination tail != NULL
    234    */
    235 
    236   /*
    237   * @setup
    238   * add one element to the list
    239   */
    240 
    241   Curl_llist_insert_next(&llist, llist.head, &unusedData_case1,
    242                          &case5_list);
    243   /* necessary assertions */
    244 
    245   abort_unless(Curl_llist_count(&llist) == 1,
    246   "Number of list elements is not as expected, Aborting");
    247   abort_unless(Curl_llist_count(&llist_destination) == 0,
    248   "Number of list elements is not as expected, Aborting");
    249 
    250   /*actual testing code*/
    251   Curl_llist_move(&llist, llist.head, &llist_destination, NULL);
    252   fail_unless(Curl_llist_count(&llist) == 0,
    253       "moving element from llist didn't decrement the size");
    254 
    255   fail_unless(Curl_llist_count(&llist_destination) == 1,
    256         "moving element to llist_destination didn't increment the size");
    257 
    258   fail_unless(llist.head == NULL,
    259       "llist head not set to null after moving the head");
    260 
    261   fail_unless(llist_destination.head != NULL,
    262         "llist_destination head set to null after moving an element");
    263 
    264   fail_unless(llist_destination.tail != NULL,
    265           "llist_destination tail set to null after moving an element");
    266 
    267   fail_unless(llist_destination.tail == llist_destination.tail,
    268             "llist_destination tail doesn't equal llist_destination head");
    269 }
    270 UNITTEST_STOP
    271