about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--contact_list.c26
-rw-r--r--contact_list.h1
-rw-r--r--test_contact_list.c45
3 files changed, 70 insertions, 2 deletions
diff --git a/contact_list.c b/contact_list.c
index 8522fd94..c1359d98 100644
--- a/contact_list.c
+++ b/contact_list.c
@@ -20,6 +20,7 @@
  *
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -134,7 +135,7 @@ int contact_list_add(const char * const name, const char * const show,
     }
 }
 
-contact_list_t *get_contact_list(void)
+contact_list_t * get_contact_list(void)
 {
     int count = 0;
     
@@ -163,6 +164,29 @@ contact_list_t *get_contact_list(void)
     return list;
 }
 
+char * find_contact(const char * const search_str)
+{
+    struct _contact_node_t *curr = _contact_list;
+
+    if (!curr) {
+        return NULL;
+    } else {
+        while(curr) {
+            contact_t *curr_contact = curr->contact;
+            if (strncmp(curr_contact->name, search_str, strlen(search_str)) == 0) {
+                char *result = 
+                    (char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
+                strcpy(result, curr_contact->name);
+                return result;
+            }
+
+            curr = curr->next;
+        }
+
+        return NULL;
+    }
+}
+
 static struct _contact_node_t * _make_contact_node(const char * const name, 
     const char * const show, const char * const status)
 {
diff --git a/contact_list.h b/contact_list.h
index d1c30935..00f03c9d 100644
--- a/contact_list.h
+++ b/contact_list.h
@@ -39,5 +39,6 @@ int contact_list_add(const char * const name, const char * const show,
     const char * const status);
 int contact_list_remove(const char * const name);
 contact_list_t * get_contact_list(void);
+char * find_contact(const char * const search_str);
 
 #endif
diff --git a/test_contact_list.c b/test_contact_list.c
index 09460cdf..8c205faf 100644
--- a/test_contact_list.c
+++ b/test_contact_list.c
@@ -1,5 +1,4 @@
 #include <stdio.h>
-#include <string.h>
 #include <head-unit.h>
 #include "contact_list.h"
 
@@ -314,6 +313,46 @@ static void set_status_to_null(void)
     assert_is_null(james->status);
 }
 
+static void find_first_exists(void)
+{
+    contact_list_add("James", NULL, NULL);
+    contact_list_add("Dave", NULL, NULL);
+    contact_list_add("Bob", NULL, NULL);
+
+    char *result = find_contact("B");
+    assert_string_equals("Bob", result);
+}
+
+static void find_second_exists(void)
+{
+    contact_list_add("James", NULL, NULL);
+    contact_list_add("Dave", NULL, NULL);
+    contact_list_add("Bob", NULL, NULL);
+
+    char *result = find_contact("Dav");
+    assert_string_equals("Dave", result);
+}
+
+static void find_third_exists(void)
+{
+    contact_list_add("James", NULL, NULL);
+    contact_list_add("Dave", NULL, NULL);
+    contact_list_add("Bob", NULL, NULL);
+
+    char *result = find_contact("Ja");
+    assert_string_equals("James", result);
+}
+
+static void find_returns_null(void)
+{
+    contact_list_add("James", NULL, NULL);
+    contact_list_add("Dave", NULL, NULL);
+    contact_list_add("Bob", NULL, NULL);
+
+    char *result = find_contact("Mike");
+    assert_is_null(result);
+}
+
 void register_contact_list_tests(void)
 {
     TEST_MODULE("contact_list tests");
@@ -344,4 +383,8 @@ void register_contact_list_tests(void)
     TEST(set_show_to_null);
     TEST(update_status);
     TEST(set_status_to_null);
+    TEST(find_first_exists);
+    TEST(find_second_exists);
+    TEST(find_third_exists);
+    TEST(find_returns_null);
 }