From 08e48355841946cf804510a24ea1f8df72e23d2f Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sat, 22 Sep 2007 12:33:28 +0000 Subject: s/malloc.h/stdlib.h/ (malloc.h is non-standard, though common) Sprinkle some assertions about so as to catch malloc failure at the earliest opportunity. Pedantic line length and brace position changes. svn path=/trunk/dom/; revision=3563 --- test/list.c | 25 ++++++++++++++++++------- test/list.h | 6 ++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/test/list.c b/test/list.c index 3204237..f94b408 100644 --- a/test/list.c +++ b/test/list.c @@ -5,21 +5,26 @@ * Copyright 2007 James Shaw */ -#include -#include +#include + #include +#include +#include #include "list.h" -struct list* list_new(void) { +struct list* list_new(void) +{ struct list* list = malloc(sizeof(struct list)); + assert(list != NULL); list->size = 0; list->head = NULL; list->tail = NULL; return list; } -void list_destroy(struct list* list) { +void list_destroy(struct list* list) +{ struct list_elt* elt = list->head; while (elt != NULL) { struct list_elt* nextElt = elt->next; @@ -29,8 +34,10 @@ void list_destroy(struct list* list) { free(list); } -void list_add(struct list* list, void* data) { +void list_add(struct list* list, void* data) +{ struct list_elt* elt = malloc(sizeof(struct list_elt)); + assert(elt != NULL); elt->data = data; elt->next = NULL; struct list_elt* tail = list->tail; @@ -51,7 +58,9 @@ void list_add(struct list* list, void* data) { list->size++; } -bool list_contains(struct list* list, void* data, int (*comparator)(const void* a, const void* b)) { +bool list_contains(struct list* list, void* data, + int (*comparator)(const void* a, const void* b)) +{ struct list_elt* elt = list->head; while (elt != NULL) { if (comparator(elt->data, data) == 0) { @@ -62,7 +71,9 @@ bool list_contains(struct list* list, void* data, int (*comparator)(const void* return false; } -bool list_contains_all(struct list* superList, struct list* subList, int (*comparator)(const void* a, const void* b)) { +bool list_contains_all(struct list* superList, struct list* subList, + int (*comparator)(const void* a, const void* b)) +{ struct list_elt* elt = subList->head; while (elt != NULL) { if (!list_contains(superList, elt->data, comparator)) { diff --git a/test/list.h b/test/list.h index 5732032..7b5bcc0 100644 --- a/test/list.h +++ b/test/list.h @@ -30,11 +30,13 @@ void list_add(struct list* list, void* data); /** * Tests if data is equal to any element in the list. */ -bool list_contains(struct list* list, void* data, int (*comparator)(const void* a, const void* b)); +bool list_contains(struct list* list, void* data, + int (*comparator)(const void* a, const void* b)); /** * Tests if superlist contains all elements in sublist. Order is not important. */ -bool list_contains_all(struct list* superList, struct list* subList, int (*comparator)(const void* a, const void* b)); +bool list_contains_all(struct list* superList, struct list* subList, + int (*comparator)(const void* a, const void* b)); #endif -- cgit v1.2.3