2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 - 2011, 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 http://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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2011-05-21 19:22:11 +08:00
|
|
|
#include "curlcheck.h"
|
2011-01-02 00:33:42 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "llist.h"
|
2011-01-02 00:33:42 +08:00
|
|
|
|
2011-05-24 23:35:08 +08:00
|
|
|
static struct curl_llist *llist;
|
2011-01-02 00:33:42 +08:00
|
|
|
|
2011-06-19 08:27:22 +08:00
|
|
|
static struct curl_llist *llist_destination;
|
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
static void test_curl_llist_dtor(void *key, void *value)
|
2011-01-02 00:33:42 +08:00
|
|
|
{
|
|
|
|
/* used by the llist API, does nothing here */
|
|
|
|
(void)key;
|
|
|
|
(void)value;
|
|
|
|
}
|
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
static CURLcode unit_setup(void)
|
2011-01-02 00:33:42 +08:00
|
|
|
{
|
2011-02-02 20:35:55 +08:00
|
|
|
llist = Curl_llist_alloc(test_curl_llist_dtor);
|
2011-06-19 08:27:22 +08:00
|
|
|
if(!llist)
|
2011-01-06 15:53:24 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-06-19 08:27:22 +08:00
|
|
|
llist_destination = Curl_llist_alloc(test_curl_llist_dtor);
|
2011-06-25 03:32:38 +08:00
|
|
|
if(!llist_destination) {
|
|
|
|
Curl_llist_destroy(llist, NULL);
|
2011-06-19 08:27:22 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2011-06-25 03:32:38 +08:00
|
|
|
}
|
2011-06-19 08:27:22 +08:00
|
|
|
|
2011-01-06 15:53:24 +08:00
|
|
|
return CURLE_OK;
|
2011-01-02 00:33:42 +08:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
static void unit_stop(void)
|
2011-01-02 00:33:42 +08:00
|
|
|
{
|
2011-02-02 20:35:55 +08:00
|
|
|
Curl_llist_destroy(llist, NULL);
|
2011-06-19 08:27:22 +08:00
|
|
|
Curl_llist_destroy(llist_destination, NULL);
|
2011-01-02 00:33:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UNITTEST_START
|
2011-01-19 06:39:47 +08:00
|
|
|
int unusedData_case1 = 1;
|
|
|
|
int unusedData_case2 = 2;
|
|
|
|
int unusedData_case3 = 3;
|
2011-01-29 23:33:02 +08:00
|
|
|
struct curl_llist_element *head;
|
|
|
|
struct curl_llist_element *element_next;
|
|
|
|
struct curl_llist_element *element_prev;
|
|
|
|
struct curl_llist_element *to_remove;
|
2011-02-02 20:35:55 +08:00
|
|
|
size_t llist_size = Curl_llist_count(llist);
|
2011-01-19 06:39:47 +08:00
|
|
|
int curlErrCode = 0;
|
2011-01-02 00:33:42 +08:00
|
|
|
|
2011-06-19 08:27:22 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
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 shold initiate to test_curl_llist_dtor");
|
2011-01-19 06:39:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
|
2011-02-02 20:35:55 +08:00
|
|
|
if(curlErrCode == 1) {
|
|
|
|
fail_unless(Curl_llist_count(llist) == 1,
|
|
|
|
"List size should be 1 after adding a new element");
|
2011-01-19 06:39:47 +08:00
|
|
|
/*test that the list head data holds my unusedData */
|
2011-02-02 20:35:55 +08:00
|
|
|
fail_unless(llist->head->ptr == &unusedData_case1,
|
|
|
|
"List size should be 1 after adding a new element");
|
2011-01-19 06:39:47 +08:00
|
|
|
/*same goes for the list tail */
|
2011-02-02 20:35:55 +08:00
|
|
|
fail_unless(llist->tail == llist->head,
|
|
|
|
"List size should be 1 after adding a new element");
|
2011-01-19 06:39:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
curlErrCode = Curl_llist_insert_next(llist, llist->head,
|
|
|
|
&unusedData_case3);
|
|
|
|
if(curlErrCode == 1) {
|
|
|
|
fail_unless(llist->head->next->ptr == &unusedData_case3,
|
|
|
|
"the node next to head is not getting set correctly");
|
2011-01-19 06:39:47 +08:00
|
|
|
fail_unless(llist->tail->ptr == &unusedData_case3,
|
|
|
|
"the list tail is not getting set correctly");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("skipping Curl_llist_insert_next as a non "
|
2011-02-02 20:35:55 +08:00
|
|
|
"success error code was returned\n");
|
2011-01-19 06:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
curlErrCode = Curl_llist_insert_next(llist, llist->head,
|
|
|
|
&unusedData_case2);
|
2011-02-02 20:35:55 +08:00
|
|
|
if(curlErrCode == 1) {
|
2011-01-19 06:39:47 +08:00
|
|
|
fail_unless(llist->head->next->ptr == &unusedData_case2,
|
2011-02-02 20:35:55 +08:00
|
|
|
"the node next to head is not getting set correctly");
|
2011-01-22 16:52:09 +08:00
|
|
|
/* better safe than sorry, check that the tail isn't corrupted */
|
2011-01-19 06:39:47 +08:00
|
|
|
fail_unless(llist->tail->ptr != &unusedData_case2,
|
2011-02-02 20:35:55 +08:00
|
|
|
"the list tail is not getting set correctly");
|
2011-01-19 06:39:47 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("skipping Curl_llist_insert_next as a non "
|
2011-02-02 20:35:55 +08:00
|
|
|
"success error code was returned\n");
|
2011-01-19 06:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("skipping Curl_llist_insert_next as a non "
|
2011-02-02 20:35:55 +08:00
|
|
|
"success error code was returned\n");
|
2011-01-19 06:39:47 +08:00
|
|
|
}
|
2011-01-02 00:33:42 +08:00
|
|
|
|
2011-01-29 23:33:02 +08:00
|
|
|
/* 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;
|
2011-03-05 05:54:04 +08:00
|
|
|
abort_unless(head, "llist->head is NULL");
|
2011-01-29 23:33:02 +08:00
|
|
|
element_next = head->next;
|
2011-02-02 20:35:55 +08:00
|
|
|
llist_size = Curl_llist_count(llist);
|
2011-01-29 23:33:02 +08:00
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
Curl_llist_remove(llist, llist->head, NULL);
|
2011-01-29 23:33:02 +08:00
|
|
|
|
2011-02-02 20:35:55 +08:00
|
|
|
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");
|
2011-03-05 05:54:04 +08:00
|
|
|
abort_unless(llist->head, "llist->head is NULL");
|
2011-02-02 20:35:55 +08:00
|
|
|
fail_unless(llist->head->prev == NULL,
|
|
|
|
"new head previous not set to null");
|
2011-01-29 23:33:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-02-02 20:35:55 +08:00
|
|
|
Curl_llist_insert_next(llist, llist->head, &unusedData_case3);
|
|
|
|
llist_size = Curl_llist_count(llist);
|
2011-01-29 23:33:02 +08:00
|
|
|
to_remove = llist->head->next;
|
2011-03-05 05:54:04 +08:00
|
|
|
abort_unless(to_remove, "to_remove is NULL");
|
2011-01-29 23:33:02 +08:00
|
|
|
element_next = to_remove->next;
|
|
|
|
element_prev = to_remove->prev;
|
2011-02-02 20:35:55 +08:00
|
|
|
Curl_llist_remove(llist, to_remove, NULL);
|
|
|
|
fail_unless(element_prev->next == element_next,
|
|
|
|
"element previous->next is not being adjusted");
|
2011-03-05 05:54:04 +08:00
|
|
|
abort_unless(element_next, "element_next is NULL");
|
2011-02-02 20:35:55 +08:00
|
|
|
fail_unless(element_next->prev == element_prev,
|
|
|
|
"element next->previous is not being adjusted");
|
2011-01-29 23:33:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2011-02-02 20:35:55 +08:00
|
|
|
Curl_llist_remove(llist, to_remove, NULL);
|
|
|
|
fail_unless(llist->tail == element_prev,
|
|
|
|
"llist tail is not being adjusted when removing tail");
|
2011-01-29 23:33:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2011-02-02 20:35:55 +08:00
|
|
|
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");
|
2011-01-29 23:33:02 +08:00
|
|
|
|
2011-06-19 08:27:22 +08:00
|
|
|
/* @testing Curl_llist_move(struct curl_llist *,
|
|
|
|
* struct curl_llist_element *, struct curl_llist *,
|
|
|
|
* struct curl_llist_element *);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @case 1:
|
|
|
|
* moving head from an llist containg one element to an empty llist
|
|
|
|
* @assumptions:
|
|
|
|
* 1: llist size will be 0
|
|
|
|
* 2: llist_destination size will be 1
|
|
|
|
* 3: llist head will be NULL
|
|
|
|
* 4: llist_destination head == llist_destination tail != NULL
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @setup
|
|
|
|
* add one element to the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
|
|
|
|
/* necessary assertions */
|
|
|
|
|
|
|
|
abort_unless(curlErrCode == 1,
|
|
|
|
"Curl_llist_insert_next returned an error, Can't move on with test");
|
|
|
|
abort_unless(Curl_llist_count(llist) == 1,
|
|
|
|
"Number of list elements is not as expected, Aborting");
|
|
|
|
abort_unless(Curl_llist_count(llist_destination) == 0,
|
|
|
|
"Number of list elements is not as expected, Aborting");
|
|
|
|
|
|
|
|
/*actual testing code*/
|
|
|
|
curlErrCode = Curl_llist_move(llist, llist->head, llist_destination, NULL);
|
|
|
|
abort_unless(curlErrCode == 1,
|
|
|
|
"Curl_llist_move returned an error, Can't move on with test");
|
|
|
|
fail_unless(Curl_llist_count(llist) == 0,
|
|
|
|
"moving element from llist didn't decrement the size");
|
|
|
|
|
|
|
|
fail_unless(Curl_llist_count(llist_destination) == 1,
|
|
|
|
"moving element to llist_destination didn't increment the size");
|
|
|
|
|
|
|
|
fail_unless(llist->head == NULL,
|
|
|
|
"llist head not set to null after moving the head");
|
|
|
|
|
|
|
|
fail_unless(llist_destination->head != NULL,
|
|
|
|
"llist_destination head set to null after moving an element");
|
|
|
|
|
|
|
|
fail_unless(llist_destination->tail != NULL,
|
|
|
|
"llist_destination tail set to null after moving an element");
|
|
|
|
|
|
|
|
fail_unless(llist_destination->tail == llist_destination->tail,
|
|
|
|
"llist_destination tail doesn't equal llist_destination head");
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-02 00:33:42 +08:00
|
|
|
UNITTEST_STOP
|