summaryrefslogtreecommitdiff
path: root/tests/unit/unit1300.c
blob: 3e4c33cccbfeafa217bd56727840d0067864b0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
#include "curlcheck.h"

#include "llist.h"

static struct curl_llist llist;

static struct curl_llist llist_destination;

static void test_curl_llist_dtor(void *key, void *value)
{
  /* used by the llist API, does nothing here */
  (void)key;
  (void)value;
}

static CURLcode unit_setup(void)
{
  Curl_llist_init(&llist, test_curl_llist_dtor);
  Curl_llist_init(&llist_destination, test_curl_llist_dtor);
  return CURLE_OK;
}

static void unit_stop(void)
{
}

UNITTEST_START
{
  int unusedData_case1 = 1;
  int unusedData_case2 = 2;
  int unusedData_case3 = 3;
  struct curl_llist_element case1_list;
  struct curl_llist_element case2_list;
  struct curl_llist_element case3_list;
  struct curl_llist_element case4_list;
  struct curl_llist_element *head;
  struct curl_llist_element *element_next;
  struct curl_llist_element *element_prev;
  struct curl_llist_element *to_remove;
  size_t llist_size = Curl_llist_count(&llist);

  /**
   * testing llist_init
   * case 1:
   * list initiation
   * @assumptions:
   * 1: list size will be 0
   * 2: list head will be NULL
   * 3: list tail will be NULL
   * 4: list dtor will be NULL
  */

  fail_unless(llist.size == 0, "list initial size should be zero");
  fail_unless(llist.head == NULL, "list head should initiate to NULL");
  fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
  fail_unless(llist.dtor == test_curl_llist_dtor,
               "list dtor should initiate to test_curl_llist_dtor");

  /**
   * testing Curl_llist_insert_next
   * case 1:
   * list is empty
   * @assumptions:
   * 1: list size will be 1
   * 2: list head will hold the data "unusedData_case1"
   * 3: list tail will be the same as list head
   */

  Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);

  fail_unless(Curl_llist_count(&llist) == 1,
              "List size should be 1 after adding a new element");
  /*test that the list head data holds my unusedData */
  fail_unless(llist.head->ptr == &unusedData_case1,
              "head ptr should be first entry");
  /*same goes for the list tail */
  fail_unless(llist.tail == llist.head,
              "tail and head should be the same");

  /**
   * testing Curl_llist_insert_next
   * case 2:
   * list has 1 element, adding one element after the head
   * @assumptions:
   * 1: the element next to head should be our newly created element
   * 2: the list tail should be our newly created element
   */

  Curl_llist_insert_next(&llist, llist.head,
                         &unusedData_case3, &case3_list);
  fail_unless(llist.head->next->ptr == &unusedData_case3,
              "the node next to head is not getting set correctly");
  fail_unless(llist.tail->ptr == &unusedData_case3,
              "the list tail is not getting set correctly");

  /**
   * testing Curl_llist_insert_next
   * case 3:
   * list has >1 element, adding one element after "NULL"
   * @assumptions:
   * 1: the element next to head should be our newly created element
   * 2: the list tail should different from newly created element
   */

  Curl_llist_insert_next(&llist, llist.head,
                         &unusedData_case2, &case2_list);
  fail_unless(llist.head->next->ptr == &unusedData_case2,
              "the node next to head is not getting set correctly");
  /* better safe than sorry, check that the tail isn't corrupted */
  fail_unless(llist.tail->ptr != &unusedData_case2,
              "the list tail is not getting set correctly");

  /* unit tests for Curl_llist_remove */

  /**
   * case 1:
   * list has >1 element, removing head
   * @assumptions:
   * 1: list size will be decremented by one
   * 2: head will be the head->next
   * 3: "new" head's previous will be NULL
   */

  head = llist.head;
  abort_unless(head, "llist.head is NULL");
  element_next = head->next;
  llist_size = Curl_llist_count(&llist);

  Curl_llist_remove(&llist, llist.head, NULL);

  fail_unless(Curl_llist_count(&llist) ==  (llist_size-1),
               "llist size not decremented as expected");
  fail_unless(llist.head == element_next,
               "llist new head not modified properly");
  abort_unless(llist.head, "llist.head is NULL");
  fail_unless(llist.head->prev == NULL,
              "new head previous not set to null");

  /**
   * case 2:
   * removing non head element, with list having >=2 elements
   * @setup:
   * 1: insert another element to the list to make element >=2
   * @assumptions:
   * 1: list size will be decremented by one ; tested
   * 2: element->previous->next will be element->next
   * 3: element->next->previous will be element->previous
   */
  Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
                         &case4_list);
  llist_size = Curl_llist_count(&llist);
  fail_unless(llist_size == 3, "should be 3 list members");

  to_remove = llist.head->next;
  abort_unless(to_remove, "to_remove is NULL");
  element_next = to_remove->next;
  element_prev = to_remove->prev;
  Curl_llist_remove(&llist, to_remove, NULL);
  fail_unless(element_prev->next == element_next,
              "element previous->next is not being adjusted");
  abort_unless(element_next, "element_next is NULL");
  fail_unless(element_next->prev == element_prev,
              "element next->previous is not being adjusted");

  /**
   * case 3:
   * removing the tail with list having >=1 element
   * @assumptions
   * 1: list size will be decremented by one ;tested
   * 2: element->previous->next will be element->next ;tested
   * 3: element->next->previous will be element->previous ;tested
   * 4: list->tail will be tail->previous
   */

  to_remove = llist.tail;
  element_prev = to_remove->prev;
  Curl_llist_remove(&llist, to_remove, NULL);
  fail_unless(llist.tail == element_prev,
              "llist tail is not being adjusted when removing tail");

  /**
   * case 4:
   * removing head with list having 1 element
   * @assumptions:
   * 1: list size will be decremented by one ;tested
   * 2: list head will be null
   * 3: list tail will be null
   */

  to_remove = llist.head;
  Curl_llist_remove(&llist, to_remove, NULL);
  fail_unless(llist.head == NULL,
              "llist head is not NULL while the llist is empty");
  fail_unless(llist.tail == NULL,
              "llist tail is not NULL while the llist is empty");

  Curl_llist_destroy(&llist, NULL);
  Curl_llist_destroy(&llist_destination, NULL);
}
UNITTEST_STOP