2011-03-10 13:48:02 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 13:48:02 +03:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 16:02:01 +03:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 13:48:02 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 12:16:50 +03:00
|
|
|
*
|
2011-03-10 13:48:02 +03:00
|
|
|
***************************************************************************/
|
2011-05-21 15:22:11 +04:00
|
|
|
#include "curlcheck.h"
|
2011-01-01 19:33:42 +03:00
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "llist.h"
|
2011-01-01 19:33:42 +03:00
|
|
|
|
2020-09-02 13:06:20 +03:00
|
|
|
static struct Curl_llist llist;
|
2011-01-01 19:33:42 +03:00
|
|
|
|
2020-09-02 13:06:20 +03:00
|
|
|
static struct Curl_llist llist_destination;
|
2011-06-19 04:27:22 +04:00
|
|
|
|
2020-09-02 13:06:20 +03:00
|
|
|
static void test_Curl_llist_dtor(void *key, void *value)
|
2011-01-01 19:33:42 +03:00
|
|
|
{
|
|
|
|
/* used by the llist API, does nothing here */
|
|
|
|
(void)key;
|
|
|
|
(void)value;
|
|
|
|
}
|
|
|
|
|
2011-02-02 15:35:55 +03:00
|
|
|
static CURLcode unit_setup(void)
|
2011-01-01 19:33:42 +03:00
|
|
|
{
|
2020-09-02 13:06:20 +03:00
|
|
|
Curl_llist_init(&llist, test_Curl_llist_dtor);
|
|
|
|
Curl_llist_init(&llist_destination, test_Curl_llist_dtor);
|
2011-01-06 10:53:24 +03:00
|
|
|
return CURLE_OK;
|
2011-01-01 19:33:42 +03:00
|
|
|
}
|
|
|
|
|
2011-02-02 15:35:55 +03:00
|
|
|
static void unit_stop(void)
|
2011-01-01 19:33:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UNITTEST_START
|
2017-04-20 16:10:04 +03:00
|
|
|
{
|
2011-01-19 01:39:47 +03:00
|
|
|
int unusedData_case1 = 1;
|
|
|
|
int unusedData_case2 = 2;
|
|
|
|
int unusedData_case3 = 3;
|
2020-09-02 13:06:20 +03:00
|
|
|
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;
|
2017-04-03 11:32:43 +03:00
|
|
|
size_t llist_size = Curl_llist_count(&llist);
|
2011-01-01 19:33:42 +03:00
|
|
|
|
2011-06-19 04:27:22 +04: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
|
|
|
|
*/
|
|
|
|
|
2017-04-03 11:32:43 +03: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");
|
2020-09-02 13:06:20 +03:00
|
|
|
fail_unless(llist.dtor == test_Curl_llist_dtor,
|
|
|
|
"list dtor should initiate to test_Curl_llist_dtor");
|
2011-01-19 01:39:47 +03: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
|
|
|
|
*/
|
|
|
|
|
2017-04-20 16:10:04 +03:00
|
|
|
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");
|
2022-10-30 19:38:16 +03:00
|
|
|
/* test that the list head data holds my unusedData */
|
2017-04-20 16:10:04 +03:00
|
|
|
fail_unless(llist.head->ptr == &unusedData_case1,
|
|
|
|
"head ptr should be first entry");
|
2022-10-30 19:38:16 +03:00
|
|
|
/* same goes for the list tail */
|
2017-04-20 16:10:04 +03:00
|
|
|
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");
|
2011-01-01 19:33:42 +03:00
|
|
|
|
2011-01-29 18:33:02 +03: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
|
|
|
|
*/
|
|
|
|
|
2017-09-10 00:09:06 +03:00
|
|
|
head = llist.head;
|
2017-04-03 11:32:43 +03:00
|
|
|
abort_unless(head, "llist.head is NULL");
|
2011-01-29 18:33:02 +03:00
|
|
|
element_next = head->next;
|
2017-04-03 11:32:43 +03:00
|
|
|
llist_size = Curl_llist_count(&llist);
|
2011-01-29 18:33:02 +03:00
|
|
|
|
2017-04-03 11:32:43 +03:00
|
|
|
Curl_llist_remove(&llist, llist.head, NULL);
|
2011-01-29 18:33:02 +03:00
|
|
|
|
2017-04-03 11:32:43 +03:00
|
|
|
fail_unless(Curl_llist_count(&llist) == (llist_size-1),
|
2011-02-02 15:35:55 +03:00
|
|
|
"llist size not decremented as expected");
|
2017-04-03 11:32:43 +03:00
|
|
|
fail_unless(llist.head == element_next,
|
2011-02-02 15:35:55 +03:00
|
|
|
"llist new head not modified properly");
|
2017-04-03 11:32:43 +03:00
|
|
|
abort_unless(llist.head, "llist.head is NULL");
|
|
|
|
fail_unless(llist.head->prev == NULL,
|
2011-02-02 15:35:55 +03:00
|
|
|
"new head previous not set to null");
|
2011-01-29 18:33:02 +03: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
|
|
|
|
*/
|
2017-04-20 16:10:04 +03:00
|
|
|
Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
|
|
|
|
&case4_list);
|
2017-04-03 11:32:43 +03:00
|
|
|
llist_size = Curl_llist_count(&llist);
|
2017-04-20 16:10:04 +03:00
|
|
|
fail_unless(llist_size == 3, "should be 3 list members");
|
|
|
|
|
2017-04-03 11:32:43 +03:00
|
|
|
to_remove = llist.head->next;
|
2011-03-05 00:54:04 +03:00
|
|
|
abort_unless(to_remove, "to_remove is NULL");
|
2011-01-29 18:33:02 +03:00
|
|
|
element_next = to_remove->next;
|
|
|
|
element_prev = to_remove->prev;
|
2017-04-03 11:32:43 +03:00
|
|
|
Curl_llist_remove(&llist, to_remove, NULL);
|
2011-02-02 15:35:55 +03:00
|
|
|
fail_unless(element_prev->next == element_next,
|
|
|
|
"element previous->next is not being adjusted");
|
2011-03-05 00:54:04 +03:00
|
|
|
abort_unless(element_next, "element_next is NULL");
|
2011-02-02 15:35:55 +03:00
|
|
|
fail_unless(element_next->prev == element_prev,
|
|
|
|
"element next->previous is not being adjusted");
|
2011-01-29 18:33:02 +03: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
|
|
|
|
*/
|
|
|
|
|
2017-04-03 11:32:43 +03:00
|
|
|
to_remove = llist.tail;
|
2011-01-29 18:33:02 +03:00
|
|
|
element_prev = to_remove->prev;
|
2017-04-03 11:32:43 +03:00
|
|
|
Curl_llist_remove(&llist, to_remove, NULL);
|
|
|
|
fail_unless(llist.tail == element_prev,
|
2011-02-02 15:35:55 +03:00
|
|
|
"llist tail is not being adjusted when removing tail");
|
2011-01-29 18:33:02 +03: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
|
|
|
|
*/
|
|
|
|
|
2017-04-03 11:32:43 +03:00
|
|
|
to_remove = llist.head;
|
|
|
|
Curl_llist_remove(&llist, to_remove, NULL);
|
|
|
|
fail_unless(llist.head == NULL,
|
2011-02-02 15:35:55 +03:00
|
|
|
"llist head is not NULL while the llist is empty");
|
2017-04-03 11:32:43 +03:00
|
|
|
fail_unless(llist.tail == NULL,
|
2011-02-02 15:35:55 +03:00
|
|
|
"llist tail is not NULL while the llist is empty");
|
2011-01-29 18:33:02 +03:00
|
|
|
|
2018-10-29 02:06:01 +03:00
|
|
|
Curl_llist_destroy(&llist, NULL);
|
|
|
|
Curl_llist_destroy(&llist_destination, NULL);
|
2017-04-20 16:10:04 +03:00
|
|
|
}
|
2011-01-01 19:33:42 +03:00
|
|
|
UNITTEST_STOP
|