From: Mike D. Lowis Date: Thu, 22 Mar 2012 17:23:20 +0000 (-0400) Subject: Renamed files in preparation for new datastructures and started unit test files X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=b40f5f4631909149c9f1e7390d00557f53fd0e67;p=projs%2Flibcds.git Renamed files in preparation for new datastructures and started unit test files --- diff --git a/.gitignore b/.gitignore index 0951d20..2f153e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/*/ cscope.out +tags +project.vim diff --git a/src/hashtable/hashtable.c b/src/ht/ht.c similarity index 100% rename from src/hashtable/hashtable.c rename to src/ht/ht.c diff --git a/src/hashtable/hashtable.h b/src/ht/ht.h similarity index 100% rename from src/hashtable/hashtable.h rename to src/ht/ht.h diff --git a/src/linked_list/linked_list.c b/src/linked_list/linked_list.c deleted file mode 100644 index a0df8ac..0000000 --- a/src/linked_list/linked_list.c +++ /dev/null @@ -1,119 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2011 Michael D. Lowis - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *****************************************************************************/ -#include "linked_list.h" -#include - -/****************************************************************************** - * Public Functions - ******************************************************************************/ -LinkedList_T* LL_New( PTR_TYPE contents ) -{ - LinkedList_T* list = (LinkedList_T*)malloc( sizeof(LinkedList_T) ); - list->contents = contents; - list->next = NULL; - return list; -} - -LinkedList_T* LL_Last( LinkedList_T* list ) -{ - LinkedList_T* node = list; - while((node != NULL) && (node->next != NULL)) - { - node = node->next; - } - return node; -} - -LinkedList_T* LL_Get( LinkedList_T* list, int index ) -{ - int current = 0; - LinkedList_T* node = list; - LinkedList_T* indexed_node = NULL; - while ((node != NULL)) - { - if ( current == index ) - { - indexed_node = node; - break; - } - node = node->next; - current++; - } - return indexed_node; -} - -void LL_Add( LinkedList_T* list, PTR_TYPE contents ) -{ - LinkedList_T* node = LL_Last( list ); - node->next = LL_New( contents ); -} - -LinkedList_T* LL_Insert( LinkedList_T* list, int index, PTR_TYPE contents ) -{ - int req_index = ((index-1) < 0) ? 0 : index-1; - LinkedList_T* node = LL_Get( list, req_index ); - if(node != NULL) - { - LinkedList_T* next_next = node->next; - node->next = LL_New( contents ); - node->next->next = next_next; - node = node->next; - } - return node; -} - -void LL_Delete( LinkedList_T* list, int index, BOOL free_contents) -{ - LinkedList_T* node = LL_Get( list, (index-1)); - if((node != NULL) && (node->next != NULL)) - { - LinkedList_T* node_to_delete = node->next; - node->next = node_to_delete->next; - if (free_contents) - { - free(node_to_delete->contents); - } - free(node_to_delete); - } -} - -void LL_Free( LinkedList_T* list, BOOL free_contents) -{ - LinkedList_T* node = list; - while( node != NULL ) - { - LinkedList_T* next = node->next; - if (free_contents) - { - free(node->contents); - } - free(node); - node = next; - } -} - -U32 LL_Length(LinkedList_T* list) -{ - U32 length = 0; - LinkedList_T* item = list; - for ( item = list; item != NULL; item = item->next ) - { - length++; - } - return length; -} - diff --git a/tests/DUMMY b/src/lists/double_link/dll.c similarity index 100% rename from tests/DUMMY rename to src/lists/double_link/dll.c diff --git a/src/lists/double_link/dll.h b/src/lists/double_link/dll.h new file mode 100644 index 0000000..e69de29 diff --git a/src/lists/single_link/sll.c b/src/lists/single_link/sll.c new file mode 100644 index 0000000..bed666d --- /dev/null +++ b/src/lists/single_link/sll.c @@ -0,0 +1,121 @@ +/****************************************************************************** + * Copyright (C) 2011 Michael D. Lowis + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ +#include "sll.h" +#include + +/****************************************************************************** + * Public Functions + ******************************************************************************/ +sll_node* sll_new( void* contents ) +{ + sll_node* list = (sll_node*)malloc( sizeof(sll_node) ); + list->contents = contents; + list->next = NULL; + return list; +} + +sll_node* sll_last( sll_node* list ) +{ + sll_node* node = list; + while((node != NULL) && (node->next != NULL)) + { + node = node->next; + } + return node; +} + +sll_node* sll_get( sll_node* list, int index ) +{ + int current = 0; + sll_node* node = list; + sll_node* indexed_node = NULL; + while ((node != NULL)) + { + if ( current == index ) + { + indexed_node = node; + break; + } + node = node->next; + current++; + } + return indexed_node; +} + +sll_node* sll_add( sll_node* list, void* contents ) +{ + sll_node* node = sll_last( list ); + node->next = sll_new( contents ); +} + +sll_node* sll_insert( sll_node* list, int index, void* contents ) +{ + int req_index = ((index-1) < 0) ? 0 : index-1; + sll_node* node = sll_get( list, req_index ); + if(node != NULL) + { + sll_node* next_next = node->next; + node->next = sll_new( contents ); + node->next->next = next_next; + node = node->next; + } + return node; +} + +sll_node* sll_delete( sll_node* list, int index, int free_contents) +{ + sll_node* node = sll_get( list, (index-1)); + if((node != NULL) && (node->next != NULL)) + { + sll_node* node_to_delete = node->next; + node->next = node_to_delete->next; + if (free_contents) + { + free(node_to_delete->contents); + } + free(node_to_delete); + node = node->next; + } + return node; +} + +void sll_free( sll_node* list, int free_contents) +{ + sll_node* node = list; + while( node != NULL ) + { + sll_node* next = node->next; + if (free_contents) + { + free(node->contents); + } + free(node); + node = next; + } +} + +unsigned int sll_length(sll_node* list) +{ + unsigned int length = 0; + sll_node* item = list; + for ( item = list; item != NULL; item = item->next ) + { + length++; + } + return length; +} + diff --git a/src/linked_list/linked_list.h b/src/lists/single_link/sll.h similarity index 54% rename from src/linked_list/linked_list.h rename to src/lists/single_link/sll.h index 551a1da..1bed18e 100644 --- a/src/linked_list/linked_list.h +++ b/src/lists/single_link/sll.h @@ -17,96 +17,96 @@ #ifndef LINKED_LIST_H #define LINKED_LIST_H -#include "common.h" - -#define PTR_TYPE void * - -typedef struct LinkedList +typedef struct node { - PTR_TYPE contents; - struct LinkedList * next; -} LinkedList_T; + void* contents; + struct node* next; +} sll_node; /** - * @brief Creates a new linked list node with the supplied value. - * - * Allocates a new node on the heap and populates the node contents with the + * @brief Creates a new linked list node with the supplied value. + * + * Allocates a new node on the heap and populates the node contents with the * supplied contents pointer. * - * @param contents The contents of the newly created node. + * @param contents The contents of the newly created node. * - * @return A pointer to the newly created node. + * @return A pointer to the newly created node. * */ -LinkedList_T* LL_New( PTR_TYPE contents ); +sll_node* sll_new( void* contents ); /** - * @brief Finds and returns the last node in the supplied linked list. + * @brief Finds and returns the last node in the supplied linked list. * - * @param list The linked list to search. + * @param list The linked list to search. * - * @return Pointer to the last node in the supplied list. + * @return Pointer to the last node in the supplied list. * */ -LinkedList_T* LL_Last(LinkedList_T* list); +sll_node* sll_last(sll_node* list); /** - * @brief Return the node at the specified index in a linked list. + * @brief Return the node at the specified index in a linked list. * * Loops through the linked list and returns the node in the list at the * specified index. Returns NULL if the index is out of range. * - * @param list The list to search for the supplied index. - * @param index The index of the node to return. + * @param list The list to search for the supplied index. + * @param index The index of the node to return. * - * @return A pointer to the node and the supplied index, NULL if out of range. + * @return A pointer to the node and the supplied index, NULL if out of range. * */ -LinkedList_T* LL_Get(LinkedList_T* list, int index); +sll_node* sll_get(sll_node* list, int index); /** - * @brief Adds a new node to an existing linked list. + * @brief Adds a new node to an existing linked list. * - * @param list - * @param contents + * @param list + * @param contents + * + * @return Pointer to the newly added node. * */ -void LL_Add( LinkedList_T* list, PTR_TYPE contents ); +sll_node* sll_add( sll_node* list, void* contents ); /** - * @brief Inserts a new node in a linked list at the specified index. + * @brief Inserts a new node in a linked list at the specified index. * - * @param list - * @param index - * @param contents + * @param list + * @param index + * @param contents * - * @return Pointer to the newly inserted node, NULL if index is out of range. + * @return Pointer to the newly inserted node, NULL if index is out of range. * */ -LinkedList_T* LL_Insert( LinkedList_T* list, int index, PTR_TYPE contents); +sll_node* sll_insert( sll_node* list, int index, void* contents); /** - * @brief Deletes a node from the supplied list. + * @brief Deletes a node from the supplied list. * * Deletes the node found at the supplied index from the supplied list and frees * the memory used by the node and its contents. * - * @param list - * @param index + * @param list + * @param index * @param free_contents Whether or not to also free the contents of the node. + * + * @return Pointer to the node that is now at the supplied index. * */ -void LL_Delete( LinkedList_T* list, int index, BOOL free_contents); +sll_node* sll_delete( sll_node* list, int index, int free_contents); /** - * @brief Frees all memory used by a linked list. + * @brief Frees all memory used by a linked list. * * Loops through the supplied list and frees all nodes. Also frees contents if - * free_contents is passed TRUE. This is to avoid trying to free memory + * free_contents is passed TRUE. This is to avoid trying to free memory * allocated on the stack. * - * @param list The list to be freed. + * @param list The list to be freed. * @param free_contents Whether or not to also free the contents of each node. * */ -void LL_Free( LinkedList_T* list, BOOL free_contents); +void sll_free( sll_node* list, int free_contents); /** * @brief Returns the number of elements in the list. - * + * * Loops through the supplied list and returns a count of the number of elements * contained in the list. * @@ -114,6 +114,6 @@ void LL_Free( LinkedList_T* list, BOOL free_contents); * * @return The number of elements in the list. **/ -U32 LL_Length(LinkedList_T* list); +unsigned int sll_length(sll_node* list); #endif diff --git a/src/trees/avl/avl.c b/src/trees/avl/avl.c new file mode 100644 index 0000000..e69de29 diff --git a/src/trees/avl/avl.h b/src/trees/avl/avl.h new file mode 100644 index 0000000..e69de29 diff --git a/src/trees/binary/bt.c b/src/trees/binary/bt.c new file mode 100644 index 0000000..e69de29 diff --git a/src/trees/binary/bt.h b/src/trees/binary/bt.h new file mode 100644 index 0000000..e69de29 diff --git a/src/trees/redblack/rbt.c b/src/trees/redblack/rbt.c new file mode 100644 index 0000000..e69de29 diff --git a/src/trees/redblack/rbt.h b/src/trees/redblack/rbt.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/ht/test_ht.c b/tests/ht/test_ht.c new file mode 100644 index 0000000..2302194 --- /dev/null +++ b/tests/ht/test_ht.c @@ -0,0 +1,9 @@ +#include "unity.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} diff --git a/tests/lists/sll/test_sll.c b/tests/lists/sll/test_sll.c new file mode 100644 index 0000000..a661a81 --- /dev/null +++ b/tests/lists/sll/test_sll.c @@ -0,0 +1,121 @@ +#include "unity.h" + +// File Under Test +#include "sll.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +//----------------------------------------------------------------------------- +void test_sll_new_should_allocate_memory_for_and_return_a_pointer_to_a_new_linked_list(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_last_should_return_the_last_element_in_the_given_list(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_last_should_return_null_if_the_list_is_empty(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_get_should_return_a_pointer_to_the_node_at_the_given_index(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_get_should_return_null_if_the_list_is_empty(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_add_should_add_a_new_node_to_the_end_of_the_given_list_with_the_supplied_contents_and_return_a_pointer_to_the_new_node(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_add_should_return_a_new_list_if_the_given_list_is_null(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_insert_should_create_a_new_node_add_it_to_the_list_at_the_desired_index_and_return_a_pointer_to_it(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_insert_should_return_null_if_the_specified_index_does_not_exist(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_insert_should_return_null_if_the_supplied_list_is_null(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_delete_should_delete_the_node_at_the_specified_index_in_the_given_list_and_free_both_its_memory_and_contents_when_free_contents_is_1(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_delete_should_delete_the_node_at_the_specified_index_in_the_given_list_and_free_only_its_memory_when_free_contents_is_0(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_delete_should_do_nothing_if_the_specified_index_does_not_exist_in_the_list(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_delete_should_do_nothing_if_the_supplied_list_is_null(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_free_should_call_free_on_all_nodes_and_contents_in_the_supplied_list_when_free_contents_is_1(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_free_should_call_free_on_all_nodes_and_NOT_contents_in_the_supplied_list_when_free_contents_is_0(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_free_should_do_nothing_when_supplied_list_is_null(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +//----------------------------------------------------------------------------- +void test_sll_length_should_return_2_list_when_list_is_of_length_2(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_length_should_return_1_when_list_is_of_length_1(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} + +void test_sll_length_should_return_0_when_supplied_list_is_null(void) +{ + TEST_FAIL_MESSAGE("Not Implemented"); +} +