~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to tests/unit/unit1300.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2011-02-28 19:35:36 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20110228193536-p3a9jawxxofcsz7o
Tags: upstream-7.21.4
ImportĀ upstreamĀ versionĀ 7.21.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include "curl_config.h"
 
3
#include "setup.h"
 
4
 
 
5
#include "llist.h"
 
6
#include "curlcheck.h"
 
7
 
 
8
struct curl_llist *llist;
 
9
 
 
10
static void test_curl_llist_dtor(void *key, void *value)
 
11
{
 
12
  /* used by the llist API, does nothing here */
 
13
  (void)key;
 
14
  (void)value;
 
15
}
 
16
 
 
17
static CURLcode unit_setup(void)
 
18
{
 
19
  llist = Curl_llist_alloc(test_curl_llist_dtor);
 
20
  if (!llist)
 
21
    return CURLE_OUT_OF_MEMORY;
 
22
  return CURLE_OK;
 
23
}
 
24
 
 
25
static void unit_stop(void)
 
26
{
 
27
  Curl_llist_destroy(llist, NULL);
 
28
}
 
29
 
 
30
UNITTEST_START
 
31
  int unusedData_case1 = 1;
 
32
  int unusedData_case2 = 2;
 
33
  int unusedData_case3 = 3;
 
34
  struct curl_llist_element *head;
 
35
  struct curl_llist_element *element_next;
 
36
  struct curl_llist_element *element_prev;
 
37
  struct curl_llist_element *to_remove;
 
38
  size_t llist_size = Curl_llist_count(llist);
 
39
  int curlErrCode = 0;
 
40
 
 
41
  fail_unless(llist->size == 0, "list initial size should be zero");
 
42
  fail_unless(llist->head == NULL, "list head should initiate to NULL");
 
43
  fail_unless(llist->tail == NULL, "list tail should intiate to NULL");
 
44
  fail_unless(llist->dtor == test_curl_llist_dtor,
 
45
               "list dtor shold initiate to test_curl_llist_dtor");
 
46
 
 
47
  /**
 
48
   * testing Curl_llist_insert_next
 
49
   * case 1:
 
50
   * list is empty
 
51
   * @assumptions:
 
52
   * 1: list size will be 1
 
53
   * 2: list head will hold the data "unusedData_case1"
 
54
   * 3: list tail will be the same as list head
 
55
   */
 
56
 
 
57
  curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
 
58
  if(curlErrCode == 1) {
 
59
    fail_unless(Curl_llist_count(llist) == 1,
 
60
                 "List size should be 1 after adding a new element");
 
61
    /*test that the list head data holds my unusedData */
 
62
    fail_unless(llist->head->ptr == &unusedData_case1,
 
63
                 "List size should be 1 after adding a new element");
 
64
    /*same goes for the list tail */
 
65
    fail_unless(llist->tail == llist->head,
 
66
                 "List size should be 1 after adding a new element");
 
67
 
 
68
    /**
 
69
     * testing Curl_llist_insert_next
 
70
     * case 2:
 
71
     * list has 1 element, adding one element after the head
 
72
     * @assumptions:
 
73
     * 1: the element next to head should be our newly created element
 
74
     * 2: the list tail should be our newly created element
 
75
     */
 
76
 
 
77
    curlErrCode = Curl_llist_insert_next(llist, llist->head,
 
78
                                         &unusedData_case3);
 
79
    if(curlErrCode == 1) {
 
80
      fail_unless(llist->head->next->ptr == &unusedData_case3,
 
81
                  "the node next to head is not getting set correctly");
 
82
      fail_unless(llist->tail->ptr == &unusedData_case3,
 
83
                  "the list tail is not getting set correctly");
 
84
    }
 
85
    else {
 
86
      printf("skipping Curl_llist_insert_next as a non "
 
87
             "success error code was returned\n");
 
88
    }
 
89
 
 
90
    /**
 
91
     * testing Curl_llist_insert_next
 
92
     * case 3:
 
93
     * list has >1 element, adding one element after "NULL"
 
94
     * @assumptions:
 
95
     * 1: the element next to head should be our newly created element
 
96
     * 2: the list tail should different from newly created element
 
97
     */
 
98
 
 
99
    curlErrCode = Curl_llist_insert_next(llist, llist->head,
 
100
                                         &unusedData_case2);
 
101
    if(curlErrCode == 1) {
 
102
      fail_unless(llist->head->next->ptr == &unusedData_case2,
 
103
                  "the node next to head is not getting set correctly");
 
104
      /* better safe than sorry, check that the tail isn't corrupted */
 
105
      fail_unless(llist->tail->ptr != &unusedData_case2,
 
106
                  "the list tail is not getting set correctly");
 
107
    }
 
108
    else {
 
109
      printf("skipping Curl_llist_insert_next as a non "
 
110
             "success error code was returned\n");
 
111
    }
 
112
 
 
113
  }
 
114
  else {
 
115
    printf("skipping Curl_llist_insert_next as a non "
 
116
           "success error code was returned\n");
 
117
  }
 
118
 
 
119
  /* unit tests for Curl_llist_remove */
 
120
 
 
121
  /**
 
122
   * case 1:
 
123
   * list has >1 element, removing head
 
124
   * @assumptions:
 
125
   * 1: list size will be decremented by one
 
126
   * 2: head will be the head->next
 
127
   * 3: "new" head's previous will be NULL
 
128
   */
 
129
 
 
130
  head=llist->head;
 
131
  element_next = head->next;
 
132
  llist_size = Curl_llist_count(llist);
 
133
 
 
134
  Curl_llist_remove(llist, llist->head, NULL);
 
135
 
 
136
  fail_unless(Curl_llist_count(llist) ==  (llist_size-1),
 
137
               "llist size not decremented as expected");
 
138
  fail_unless(llist->head == element_next,
 
139
               "llist new head not modified properly");
 
140
  fail_unless(llist->head->prev == NULL,
 
141
              "new head previous not set to null");
 
142
 
 
143
  /**
 
144
   * case 2:
 
145
   * removing non head element, with list having >=2 elements
 
146
   * @setup:
 
147
   * 1: insert another element to the list to make element >=2
 
148
   * @assumptions:
 
149
   * 1: list size will be decremented by one ; tested
 
150
   * 2: element->previous->next will be element->next
 
151
   * 3: element->next->previous will be element->previous
 
152
   */
 
153
  Curl_llist_insert_next(llist, llist->head, &unusedData_case3);
 
154
  llist_size = Curl_llist_count(llist);
 
155
  to_remove = llist->head->next;
 
156
  element_next = to_remove->next;
 
157
  element_prev = to_remove->prev;
 
158
  Curl_llist_remove(llist, to_remove, NULL);
 
159
  fail_unless(element_prev->next == element_next,
 
160
              "element previous->next is not being adjusted");
 
161
  fail_unless(element_next->prev == element_prev,
 
162
              "element next->previous is not being adjusted");
 
163
 
 
164
  /**
 
165
   * case 3:
 
166
   * removing the tail with list having >=1 element
 
167
   * @assumptions
 
168
   * 1: list size will be decremented by one ;tested
 
169
   * 2: element->previous->next will be element->next ;tested
 
170
   * 3: element->next->previous will be element->previous ;tested
 
171
   * 4: list->tail will be tail->previous
 
172
   */
 
173
 
 
174
  to_remove = llist->tail;
 
175
  element_prev = to_remove->prev;
 
176
  Curl_llist_remove(llist, to_remove, NULL);
 
177
  fail_unless(llist->tail == element_prev,
 
178
              "llist tail is not being adjusted when removing tail");
 
179
 
 
180
  /**
 
181
   * case 4:
 
182
   * removing head with list having 1 element
 
183
   * @assumptions:
 
184
   * 1: list size will be decremented by one ;tested
 
185
   * 2: list head will be null
 
186
   * 3: list tail will be null
 
187
   */
 
188
 
 
189
  to_remove = llist->head;
 
190
  Curl_llist_remove(llist, to_remove, NULL);
 
191
  fail_unless(llist->head == NULL,
 
192
              "llist head is not NULL while the llist is empty");
 
193
  fail_unless(llist->tail == NULL,
 
194
              "llist tail is not NULL while the llist is empty");
 
195
 
 
196
UNITTEST_STOP