]> granicus.if.org Git - check/commitdiff
Add call to check if list contains an item
authorBranden Archer <b.m.archer4@gmail.com>
Sun, 19 Jun 2016 02:37:31 +0000 (22:37 -0400)
committerBranden Archer <b.m.archer4@gmail.com>
Sun, 19 Jun 2016 02:38:10 +0000 (22:38 -0400)
src/check_list.c
src/check_list.h
tests/check_list.c

index 1fd61fe8a68df6a4330a528f2399d3c5e658f3e6..2775e25eb2bf2def9987aa8d0509abd7ec8a8d3e 100644 (file)
@@ -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;
+}
index bffffaed24259781b315b1bce5f02f72390dce63..8b7a9952fb06f26cd80b1daeab098ee44a279f33 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef CHECK_LIST_H
 #define CHECK_LIST_H
 
+#include <stdbool.h>
+
 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 */
index ff2aefb0f803765d3b56cdbeede28b15dcce75b0..d95315e3ee6201e39cb53e9b8c2130fa8b7ad385 100644 (file)
@@ -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;
 }