diff --git a/c-the-hard-way/exercises/33/liblcthw/Makefile b/c-the-hard-way/exercises/33/liblcthw/Makefile index edbb5ce..4de84ed 100644 --- a/c-the-hard-way/exercises/33/liblcthw/Makefile +++ b/c-the-hard-way/exercises/33/liblcthw/Makefile @@ -1,4 +1,6 @@ -CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS) +CC=clang +#CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS) +CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic $(OPTFLAGS) LIBS=-ldl $(OPTLIBS) PREFIX?=/usr/local diff --git a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/dbg.h b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/dbg.h index 6bee036..acfe0a4 100644 --- a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/dbg.h +++ b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/dbg.h @@ -8,7 +8,7 @@ #ifdef NDEBUG #define debug(M, ...) #else -#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n",\ +#define debug(M, ...) fprintf(stderr, "[DEBUG] %s:%d: " M "\n",\ __FILE__, __LINE__, ##__VA_ARGS__) #endif diff --git a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.c b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.c index 5e9b0be..b5c3b00 100644 --- a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.c +++ b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.c @@ -134,9 +134,10 @@ List_Remove(List * list, ListNode * node) } void -List_Print(List * list) +List_Print(List * list, const char *message) { check(list, "List is NULL"); + log_info("%s", message); LIST_FOREACH(list, first, next, current) { log_info("Node [%p] - value: %s -- prev [%p] <- * -> next [%p]", current, (char *)current->value, current->prev, current->next); diff --git a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.h b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.h index a7a9781..425fc4f 100644 --- a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.h +++ b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list.h @@ -51,7 +51,7 @@ void * List_Remove(List * list, ListNode * node); void -List_Print(List * list); +List_Print(List * list, const char *); #define LIST_FOREACH(L, S, M, V) ListNode *_node = NULL;\ ListNode *V = NULL;\ diff --git a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c index c9549bd..caea28c 100644 --- a/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c +++ b/c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c @@ -13,29 +13,39 @@ ListNode_Swap(ListNode *n1, ListNode *n2) int List_bubble_sort(List *list, List_compare list_compare_function) { + int result = -1; if (list && list_compare_function) { ListNode *node_one = list->first; ListNode *node_two = node_one->next; - int i; int j; for (i = 0; i < list->count; i++) { + node_one = list->first; + node_two = node_one->next; for (j = 0; j < list->count - i - 1; j++) { - if (list_compare_function(node_one, node_two)) + // check if node_one value is greater than node_two value + if (list_compare_function(node_one, node_two) == 1) { - //swap(node_one, node_two); + ListNode_Swap(node_one, node_two); } + node_one = node_two; + node_two = node_one->next; } } + result = 0; } - return -1; + return result; } List * List_merge_sort(List *list, List_compare list_compare_function) { + if (list && list_compare_function) + { + return list; + } return NULL; } diff --git a/c-the-hard-way/exercises/33/liblcthw/tests/list_algos_tests.c b/c-the-hard-way/exercises/33/liblcthw/tests/list_algos_tests.c index 1fc4b93..c108b4d 100644 --- a/c-the-hard-way/exercises/33/liblcthw/tests/list_algos_tests.c +++ b/c-the-hard-way/exercises/33/liblcthw/tests/list_algos_tests.c @@ -38,10 +38,13 @@ test_bubble_sort() { List *words = create_words(); - List_Print(words); + List_Print(words, "Before Bubble Sort"); // should work on a list that needs sorting int rc = List_bubble_sort(words, (List_compare) strcmp); + + List_Print(words, "After Bubble Sort"); + mu_assert(rc == 0, "Bubble sort failed."); mu_assert(is_sorted(words), "Words are not sorted after bubble sort."); diff --git a/c-the-hard-way/exercises/33/liblcthw/tests/minunit.h b/c-the-hard-way/exercises/33/liblcthw/tests/minunit.h index 22fba08..fb9b7e5 100644 --- a/c-the-hard-way/exercises/33/liblcthw/tests/minunit.h +++ b/c-the-hard-way/exercises/33/liblcthw/tests/minunit.h @@ -15,7 +15,6 @@ message = test(); tests_run++; if (message) return message; #define RUN_TESTS(name) int main(int argc, char *argv[]) {\ - argc = 1; \ debug("----- RUNNING: %s", argv[0]);\ printf("-----\nRUNNING: %s\n", argv[0]);\ char *result = name();\