From 6c19a8e3feb7b5d305905efefbe2be424a6adde3 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Sat, 18 Jun 2016 22:37:31 -0400 Subject: [PATCH] Add call to check if list contains an item --- src/check_list.c | 13 +++++++++++++ src/check_list.h | 5 +++++ tests/check_list.c | 26 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/check_list.c b/src/check_list.c index 1fd61fe..2775e25 100644 --- a/src/check_list.c +++ b/src/check_list.c @@ -140,3 +140,16 @@ void check_list_apply(List * lp, void (*fp) (void *)) fp(check_list_val(lp)); } + +bool check_list_contains(List * lp, void *val) +{ + for(check_list_front(lp); !check_list_at_end(lp); check_list_advance(lp)) + { + if(check_list_val(lp) == val) + { + return true; + } + } + + return false; +} diff --git a/src/check_list.h b/src/check_list.h index bffffae..8b7a995 100644 --- a/src/check_list.h +++ b/src/check_list.h @@ -21,6 +21,8 @@ #ifndef CHECK_LIST_H #define CHECK_LIST_H +#include + typedef struct List List; /* Create an empty list */ @@ -52,5 +54,8 @@ void check_list_free(List * lp); void check_list_apply(List * lp, void (*fp) (void *)); +/* Return true if the list contains the value, false otherwise */ +bool check_list_contains(List * lp, void *val); + #endif /* CHECK_LIST_H */ diff --git a/tests/check_list.c b/tests/check_list.c index ff2aefb..d95315e 100644 --- a/tests/check_list.c +++ b/tests/check_list.c @@ -164,6 +164,31 @@ START_TEST(test_list_abuse) } END_TEST +START_TEST(test_contains) +{ + List *lp = check_list_create(); + + char otherData[] = "other"; + char goalData[] = "goal"; + + ck_assert_msg (check_list_contains(lp, goalData) == false, + "The goal data should not be in the list yet"); + + int index; + for(index = 0; index < 10; index++) + { + check_list_add_end (lp, otherData); + ck_assert_msg (check_list_contains(lp, goalData) == false, + "The goal data should not be in the list yet"); + } + + check_list_add_end (lp, goalData); + ck_assert_msg (check_list_contains(lp, goalData) , + "The goal data should be in the list"); + + check_list_free(lp); +} +END_TEST Suite *make_list_suite (void) { @@ -179,6 +204,7 @@ Suite *make_list_suite (void) tcase_add_test (tc, test_add_front_and_next); tcase_add_test (tc, test_add_a_bunch); tcase_add_test (tc, test_list_abuse); + tcase_add_test (tc, test_contains); return s; } -- 2.40.0