about summary refs log tree commit diff stats
path: root/test_contact_list.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-03-20 00:05:33 +0000
committerJames Booth <boothj5@gmail.com>2012-03-20 00:05:33 +0000
commitdab37236a2e51b8ef7e6181df302adef9eeba199 (patch)
tree96dd8e07d38e9e3133b3272a3678e6aea89753b5 /test_contact_list.c
parent9a3a0840e358e1f9d6065ba034688d87a8aa0283 (diff)
downloadprofani-tty-dab37236a2e51b8ef7e6181df302adef9eeba199.tar.gz
Rewrote copying of contact list for /who
Diffstat (limited to 'test_contact_list.c')
-rw-r--r--test_contact_list.c208
1 files changed, 131 insertions, 77 deletions
diff --git a/test_contact_list.c b/test_contact_list.c
index 1f56dc04..39fbd983 100644
--- a/test_contact_list.c
+++ b/test_contact_list.c
@@ -1,4 +1,6 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <head-unit.h>
 #include "contact_list.h"
 
@@ -7,47 +9,58 @@ static void beforetest(void)
     contact_list_clear();
 }
 
+static void aftertest(void)
+{
+    contact_list_clear();
+}
+
 static void empty_list_when_none_added(void)
 {
-    contact_list_t *list = get_contact_list();
-    assert_int_equals(0, list->size);
+    struct contact_node_t *list = get_contact_list();
+    assert_is_null(list);
+    destroy_list(list);
 }
 
 static void contains_one_element(void)
 {
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    assert_int_equals(1, list->size);
+    struct contact_node_t *list = get_contact_list();
+    assert_int_equals(1, get_size(list));
+    destroy_list(list);
 }
 
 static void first_element_correct(void)
 {
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
 
     assert_string_equals("James", james->name);
+    destroy_list(list);
 }
 
 static void contains_two_elements(void)
 {
     contact_list_add("James", NULL, NULL);
     contact_list_add("Dave", NULL, NULL);
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(2, list->size);
+    assert_int_equals(2, get_size(list));
+    destroy_list(list);
 }
 
 static void first_and_second_elements_correct(void)
 {
     contact_list_add("James", NULL, NULL);
     contact_list_add("Dave", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *dave = list->contacts[0];
-    contact_t *james = list->contacts[1];
+    struct contact_node_t *list = get_contact_list();
+    
+    struct contact_t *dave = list->contact;
+    struct contact_t *james = (list->next)->contact;
     
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
+    destroy_list(list);
 }
 
 static void contains_three_elements(void)
@@ -55,9 +68,10 @@ static void contains_three_elements(void)
     contact_list_add("James", NULL, NULL);
     contact_list_add("Bob", NULL, NULL);
     contact_list_add("Dave", NULL, NULL);
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(3, list->size);
+    assert_int_equals(3, get_size(list));
+    destroy_list(list);
 }
 
 static void first_three_elements_correct(void)
@@ -65,14 +79,15 @@ static void first_three_elements_correct(void)
     contact_list_add("Bob", NULL, NULL);
     contact_list_add("Dave", NULL, NULL);
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *bob = list->contacts[0];
-    contact_t *dave = list->contacts[1];
-    contact_t *james = list->contacts[2];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *bob = list->contact;
+    struct contact_t *dave = (list->next)->contact;
+    struct contact_t *james = ((list->next)->next)->contact;
     
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void add_twice_at_beginning_adds_once(void)
@@ -81,15 +96,16 @@ static void add_twice_at_beginning_adds_once(void)
     contact_list_add("James", NULL, NULL);
     contact_list_add("Dave", NULL, NULL);
     contact_list_add("Bob", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *bob = list->contacts[0];
-    contact_t *dave = list->contacts[1];
-    contact_t *james = list->contacts[2];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *bob = list->contact;
+    struct contact_t *dave = (list->next)->contact;
+    struct contact_t *james = ((list->next)->next)->contact;
     
-    assert_int_equals(3, list->size);    
+    assert_int_equals(3, get_size(list));    
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void add_twice_in_middle_adds_once(void)
@@ -98,15 +114,16 @@ static void add_twice_in_middle_adds_once(void)
     contact_list_add("Dave", NULL, NULL);
     contact_list_add("James", NULL, NULL);
     contact_list_add("Bob", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *bob = list->contacts[0];
-    contact_t *dave = list->contacts[1];
-    contact_t *james = list->contacts[2];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *bob = list->contact;
+    struct contact_t *dave = (list->next)->contact;
+    struct contact_t *james = ((list->next)->next)->contact;
     
-    assert_int_equals(3, list->size);    
+    assert_int_equals(3, get_size(list));    
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void add_twice_at_end_adds_once(void)
@@ -115,32 +132,35 @@ static void add_twice_at_end_adds_once(void)
     contact_list_add("Dave", NULL, NULL);
     contact_list_add("Bob", NULL, NULL);
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *bob = list->contacts[0];
-    contact_t *dave = list->contacts[1];
-    contact_t *james = list->contacts[2];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *bob = list->contact;
+    struct contact_t *dave = (list->next)->contact;
+    struct contact_t *james = ((list->next)->next)->contact;
     
-    assert_int_equals(3, list->size);    
+    assert_int_equals(3, get_size(list));    
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void remove_when_none_does_nothing(void)
 {
     contact_list_remove("James");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(0, list->size);
+    assert_int_equals(0, get_size(list));
+    destroy_list(list);
 }
 
 static void remove_when_one_removes(void)
 {
     contact_list_add("James", NULL, NULL);
     contact_list_remove("James");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(0, list->size);
+    assert_int_equals(0, get_size(list));
+    destroy_list(list);
 }
 
 static void remove_first_when_two(void)
@@ -149,11 +169,12 @@ static void remove_first_when_two(void)
     contact_list_add("Dave", NULL, NULL);
 
     contact_list_remove("James");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(1, list->size);
-    contact_t *dave = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *dave = list->contact;
     assert_string_equals("Dave", dave->name);
+    destroy_list(list);
 }
 
 static void remove_second_when_two(void)
@@ -162,11 +183,12 @@ static void remove_second_when_two(void)
     contact_list_add("Dave", NULL, NULL);
 
     contact_list_remove("Dave");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(1, list->size);
-    contact_t *james = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *james = list->contact;
     assert_string_equals("James", james->name);
+    destroy_list(list);
 }
 
 static void remove_first_when_three(void)
@@ -176,14 +198,15 @@ static void remove_first_when_three(void)
     contact_list_add("Bob", NULL, NULL);
 
     contact_list_remove("James");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(2, list->size);
-    contact_t *bob = list->contacts[0];
-    contact_t *dave = list->contacts[1];
+    assert_int_equals(2, get_size(list));
+    struct contact_t *bob = list->contact;
+    struct contact_t *dave = (list->next)->contact;
     
     assert_string_equals("Dave", dave->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void remove_second_when_three(void)
@@ -193,14 +216,15 @@ static void remove_second_when_three(void)
     contact_list_add("Bob", NULL, NULL);
 
     contact_list_remove("Dave");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(2, list->size);
-    contact_t *bob = list->contacts[0];
-    contact_t *james = list->contacts[1];
+    assert_int_equals(2, get_size(list));
+    struct contact_t *bob = list->contact;
+    struct contact_t *james = (list->next)->contact;
     
     assert_string_equals("James", james->name);
     assert_string_equals("Bob", bob->name);
+    destroy_list(list);
 }
 
 static void remove_third_when_three(void)
@@ -210,107 +234,117 @@ static void remove_third_when_three(void)
     contact_list_add("Bob", NULL, NULL);
 
     contact_list_remove("Bob");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
     
-    assert_int_equals(2, list->size);
-    contact_t *dave = list->contacts[0];
-    contact_t *james = list->contacts[1];
+    assert_int_equals(2, get_size(list));
+    struct contact_t *dave = list->contact;
+    struct contact_t *james = (list->next)->contact;
     
     assert_string_equals("James", james->name);
     assert_string_equals("Dave", dave->name);
+    destroy_list(list);
 }
 
 static void test_show_when_value(void)
 {
     contact_list_add("James", "away", NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
     
     assert_string_equals("away", james->show);
+    destroy_list(list);
 }
 
 static void test_show_online_when_no_value(void)
 {
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
     
     assert_string_equals("online", james->show);
+    destroy_list(list);
 }
 
 static void test_show_online_when_empty_string(void)
 {
     contact_list_add("James", "", NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
     
     assert_string_equals("online", james->show);
+    destroy_list(list);
 }
 
 static void test_status_when_value(void)
 {
     contact_list_add("James", NULL, "I'm not here right now");
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
     
     assert_string_equals("I'm not here right now", james->status);
+    destroy_list(list);
 }
 
 static void test_status_when_no_value(void)
 {
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
-    contact_t *james = list->contacts[0];
+    struct contact_node_t *list = get_contact_list();
+    struct contact_t *james = list->contact;
     
     assert_is_null(james->status);
+    destroy_list(list);
 }
 
 static void update_show(void)
 {
     contact_list_add("James", "away", NULL);
     contact_list_add("James", "dnd", NULL);
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(1, list->size);
-    contact_t *james = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *james = list->contact;
     assert_string_equals("James", james->name);
     assert_string_equals("dnd", james->show);
+    destroy_list(list);
 }
 
 static void set_show_to_null(void)
 {
     contact_list_add("James", "away", NULL);
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(1, list->size);
-    contact_t *james = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *james = list->contact;
     assert_string_equals("James", james->name);
     assert_string_equals("online", james->show);
+    destroy_list(list);
 }
 
 static void update_status(void)
 {
     contact_list_add("James", NULL, "I'm not here right now");
     contact_list_add("James", NULL, "Gone to lunch");
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(1, list->size);
-    contact_t *james = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *james = list->contact;
     assert_string_equals("James", james->name);
     assert_string_equals("Gone to lunch", james->status);
+    destroy_list(list);
 }
 
 static void set_status_to_null(void)
 {
     contact_list_add("James", NULL, "Gone to lunch");
     contact_list_add("James", NULL, NULL);
-    contact_list_t *list = get_contact_list();
+    struct contact_node_t *list = get_contact_list();
 
-    assert_int_equals(1, list->size);
-    contact_t *james = list->contacts[0];
+    assert_int_equals(1, get_size(list));
+    struct contact_t *james = list->contact;
     assert_string_equals("James", james->name);
     assert_is_null(james->status);
+    destroy_list(list);
 }
 
 static void find_first_exists(void)
@@ -319,8 +353,13 @@ static void find_first_exists(void)
     contact_list_add("Dave", NULL, NULL);
     contact_list_add("Bob", NULL, NULL);
 
-    char *result = find_contact("B");
+    char *search = (char *) malloc(2 * sizeof(char));
+    strcpy(search, "B");
+
+    char *result = find_contact(search);
     assert_string_equals("Bob", result);
+    free(result);
+    free(search);
 }
 
 static void find_second_exists(void)
@@ -331,6 +370,7 @@ static void find_second_exists(void)
 
     char *result = find_contact("Dav");
     assert_string_equals("Dave", result);
+    free(result);
 }
 
 static void find_third_exists(void)
@@ -341,6 +381,7 @@ static void find_third_exists(void)
 
     char *result = find_contact("Ja");
     assert_string_equals("James", result);
+    free(result);
 }
 
 static void find_returns_null(void)
@@ -368,6 +409,8 @@ static void find_twice_returns_second_when_two_match(void)
     char *result1 = find_contact("Jam");
     char *result2 = find_contact(result1);
     assert_string_equals("Jamie", result2);
+    free(result1);
+    free(result2);
 }
 
 static void find_five_times_finds_fifth(void)
@@ -389,6 +432,11 @@ static void find_five_times_finds_fifth(void)
     char *result4 = find_contact(result3);
     char *result5 = find_contact(result4);
     assert_string_equals("Jamo", result5);
+    free(result1);
+    free(result2);
+    free(result3);
+    free(result4);
+    free(result5);
 }
 
 static void find_twice_returns_first_when_two_match_and_reset(void)
@@ -401,6 +449,8 @@ static void find_twice_returns_first_when_two_match_and_reset(void)
     reset_search_attempts();
     char *result2 = find_contact(result1);
     assert_string_equals("James", result2);
+    free(result1);
+    free(result2);
 }
 
 static void removed_contact_not_in_search(void)
@@ -416,12 +466,16 @@ static void removed_contact_not_in_search(void)
     contact_list_remove("James");
     char *result3 = find_contact(result2);
     assert_string_equals("Jamie", result3);
+    free(result1);
+    free(result2);
+    free(result3);
 }
 
 void register_contact_list_tests(void)
 {
     TEST_MODULE("contact_list tests");
     BEFORETEST(beforetest);
+    AFTERTEST(aftertest);
     TEST(empty_list_when_none_added);
     TEST(contains_one_element);
     TEST(first_element_correct);