diff options
author | James Booth <boothj5@gmail.com> | 2012-07-01 14:47:09 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2012-07-01 14:47:09 +0100 |
commit | 30ca6cac3d7f86e4a43410ba361793cd99d97a10 (patch) | |
tree | df0c1fa32c36b1b5ce7e2129892646547a28435e /tests | |
parent | 9e686c0e01520c238b53b5620f936cebc7d5ba88 (diff) | |
download | profani-tty-30ca6cac3d7f86e4a43410ba361793cd99d97a10.tar.gz |
Added autobuild tools
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_contact_list.c | 498 | ||||
-rw-r--r-- | tests/test_prof_autocomplete.c | 399 | ||||
-rw-r--r-- | tests/test_prof_history.c | 233 | ||||
-rw-r--r-- | tests/test_util.c | 165 | ||||
-rw-r--r-- | tests/testsuite.c | 12 | ||||
-rw-r--r-- | tests/testsuite.h | 9 |
6 files changed, 1316 insertions, 0 deletions
diff --git a/tests/test_contact_list.c b/tests/test_contact_list.c new file mode 100644 index 00000000..599c8ca1 --- /dev/null +++ b/tests/test_contact_list.c @@ -0,0 +1,498 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <head-unit.h> +#include <glib.h> + +#include "contact.h" +#include "contact_list.h" + +static void setup(void) +{ + contact_list_init(); +} + +static void beforetest(void) +{ + contact_list_clear(); +} + +static void aftertest(void) +{ + contact_list_clear(); +} + +static void empty_list_when_none_added(void) +{ + GSList *list = get_contact_list(); + assert_is_null(list); +} + +static void contains_one_element(void) +{ + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + assert_int_equals(1, g_slist_length(list)); +} + +static void first_element_correct(void) +{ + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_string_equals("James", p_contact_name(james)); +} + +static void contains_two_elements(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + GSList *list = get_contact_list(); + + assert_int_equals(2, g_slist_length(list)); +} + +static void first_and_second_elements_correct(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + GSList *list = get_contact_list(); + + PContact dave = list->data; + PContact james = (g_slist_next(list))->data; + + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); +} + +static void contains_three_elements(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + GSList *list = get_contact_list(); + + assert_int_equals(3, g_slist_length(list)); +} + +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); + GSList *list = get_contact_list(); + PContact bob = list->data; + PContact dave = (g_slist_next(list))->data; + PContact james = (g_slist_next(g_slist_next(list)))->data; + + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void add_twice_at_beginning_adds_once(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + GSList *list = get_contact_list(); + PContact bob = list->data; + PContact dave = (g_slist_next(list))->data; + PContact james = (g_slist_next(g_slist_next(list)))->data; + + assert_int_equals(3, g_slist_length(list)); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void add_twice_in_middle_adds_once(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("James", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + GSList *list = get_contact_list(); + PContact bob = list->data; + PContact dave = (g_slist_next(list))->data; + PContact james = (g_slist_next(g_slist_next(list)))->data; + + assert_int_equals(3, g_slist_length(list)); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void add_twice_at_end_adds_once(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + PContact bob = list->data; + PContact dave = (g_slist_next(list))->data; + PContact james = (g_slist_next(g_slist_next(list)))->data; + + assert_int_equals(3, g_slist_length(list)); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void remove_when_none_does_nothing(void) +{ + contact_list_remove("James"); + GSList *list = get_contact_list(); + + assert_int_equals(0, g_slist_length(list)); +} + +static void remove_when_one_removes(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_remove("James"); + GSList *list = get_contact_list(); + + assert_int_equals(0, g_slist_length(list)); +} + +static void remove_first_when_two(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + + contact_list_remove("James"); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact dave = list->data; + assert_string_equals("Dave", p_contact_name(dave)); +} + +static void remove_second_when_two(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + + contact_list_remove("Dave"); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact james = list->data; + assert_string_equals("James", p_contact_name(james)); +} + +static void remove_first_when_three(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + + contact_list_remove("James"); + GSList *list = get_contact_list(); + + assert_int_equals(2, g_slist_length(list)); + PContact bob = list->data; + PContact dave = (g_slist_next(list))->data; + + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void remove_second_when_three(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + + contact_list_remove("Dave"); + GSList *list = get_contact_list(); + + assert_int_equals(2, g_slist_length(list)); + PContact bob = list->data; + PContact james = (g_slist_next(list))->data; + + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Bob", p_contact_name(bob)); +} + +static void remove_third_when_three(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + + contact_list_remove("Bob"); + GSList *list = get_contact_list(); + + assert_int_equals(2, g_slist_length(list)); + PContact dave = list->data; + PContact james = (g_slist_next(list))->data; + + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); +} + +static void test_show_when_value(void) +{ + contact_list_add("James", "away", NULL); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_string_equals("away", p_contact_show(james)); +} + +static void test_show_online_when_no_value(void) +{ + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_string_equals("online", p_contact_show(james)); +} + +static void test_show_online_when_empty_string(void) +{ + contact_list_add("James", "", NULL); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_string_equals("online", p_contact_show(james)); +} + +static void test_status_when_value(void) +{ + contact_list_add("James", NULL, "I'm not here right now"); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_string_equals("I'm not here right now", p_contact_status(james)); +} + +static void test_status_when_no_value(void) +{ + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + PContact james = list->data; + + assert_is_null(p_contact_status(james)); +} + +static void update_show(void) +{ + contact_list_add("James", "away", NULL); + contact_list_add("James", "dnd", NULL); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact james = list->data; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("dnd", p_contact_show(james)); +} + +static void set_show_to_null(void) +{ + contact_list_add("James", "away", NULL); + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact james = list->data; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("online", p_contact_show(james)); +} + +static void update_status(void) +{ + contact_list_add("James", NULL, "I'm not here right now"); + contact_list_add("James", NULL, "Gone to lunch"); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact james = list->data; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Gone to lunch", p_contact_status(james)); +} + +static void set_status_to_null(void) +{ + contact_list_add("James", NULL, "Gone to lunch"); + contact_list_add("James", NULL, NULL); + GSList *list = get_contact_list(); + + assert_int_equals(1, g_slist_length(list)); + PContact james = list->data; + assert_string_equals("James", p_contact_name(james)); + assert_is_null(p_contact_status(james)); +} + +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 *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) +{ + 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); + free(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); + free(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); +} + +static void find_on_empty_returns_null(void) +{ + char *result = find_contact("James"); + assert_is_null(result); +} + +static void find_twice_returns_second_when_two_match(void) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Jamie", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + + 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) +{ + contact_list_add("Jama", NULL, NULL); + contact_list_add("Jamb", NULL, NULL); + contact_list_add("Mike", NULL, NULL); + contact_list_add("Dave", NULL, NULL); + contact_list_add("Jamm", NULL, NULL); + contact_list_add("Jamn", NULL, NULL); + contact_list_add("Matt", NULL, NULL); + contact_list_add("Jamo", NULL, NULL); + contact_list_add("Jamy", NULL, NULL); + contact_list_add("Jamz", NULL, NULL); + + char *result1 = find_contact("Jam"); + char *result2 = find_contact(result1); + char *result3 = find_contact(result2); + 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) +{ + contact_list_add("James", NULL, NULL); + contact_list_add("Jamie", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + + char *result1 = find_contact("Jam"); + 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) +{ + contact_list_add("Jamatron", NULL, NULL); + contact_list_add("Bob", NULL, NULL); + contact_list_add("Jambo", NULL, NULL); + contact_list_add("James", NULL, NULL); + contact_list_add("Jamie", NULL, NULL); + + char *result1 = find_contact("Jam"); // Jamatron + char *result2 = find_contact(result1); // Jambo + 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"); + SETUP(setup); + BEFORETEST(beforetest); + AFTERTEST(aftertest); + TEST(empty_list_when_none_added); + TEST(contains_one_element); + TEST(first_element_correct); + TEST(contains_two_elements); + TEST(first_and_second_elements_correct); + TEST(contains_three_elements); + TEST(first_three_elements_correct); + TEST(add_twice_at_beginning_adds_once); + TEST(add_twice_in_middle_adds_once); + TEST(add_twice_at_end_adds_once); + TEST(remove_when_none_does_nothing); + TEST(remove_when_one_removes); + TEST(remove_first_when_two); + TEST(remove_second_when_two); + TEST(remove_first_when_three); + TEST(remove_second_when_three); + TEST(remove_third_when_three); + TEST(test_show_when_value); + TEST(test_show_online_when_no_value); + TEST(test_show_online_when_empty_string); + TEST(test_status_when_value); + TEST(test_status_when_no_value); + TEST(update_show); + 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); + TEST(find_on_empty_returns_null); + TEST(find_twice_returns_second_when_two_match); + TEST(find_twice_returns_first_when_two_match_and_reset); + TEST(removed_contact_not_in_search); + TEST(find_five_times_finds_fifth); +} diff --git a/tests/test_prof_autocomplete.c b/tests/test_prof_autocomplete.c new file mode 100644 index 00000000..45717abd --- /dev/null +++ b/tests/test_prof_autocomplete.c @@ -0,0 +1,399 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <head-unit.h> +#include <glib.h> + +#include "contact.h" +#include "prof_autocomplete.h" + +static void clear_empty(void) +{ + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_clear(ac); +} + +static void clear_empty_with_free_func(void) +{ + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_clear(ac); +} + +static void reset_after_create(void) +{ + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_reset(ac); + p_autocomplete_clear(ac); +} + +static void find_after_create(void) +{ + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_complete(ac, "hello"); + p_autocomplete_clear(ac); +} + +static void get_after_create_returns_null(void) +{ + PAutocomplete ac = p_autocomplete_new(); + GSList *result = p_autocomplete_get_list(ac); + + assert_is_null(result); + + p_autocomplete_clear(ac); +} + +static void get_after_create_with_copy_func_returns_null(void) +{ + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + GSList *result = p_autocomplete_get_list(ac); + + assert_is_null(result); + + p_autocomplete_clear(ac); +} + +static void add_one_and_complete(void) +{ + char *item = strdup("Hello"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item); + char *result = p_autocomplete_complete(ac, "Hel"); + + assert_string_equals("Hello", result); + + p_autocomplete_clear(ac); +} + +static void add_one_and_complete_with_funcs(void) +{ + PContact contact = p_contact_new("James", "Online", "I'm here"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact); + char *result = p_autocomplete_complete(ac, "Jam"); + + assert_string_equals("James", result); + + p_autocomplete_clear(ac); +} + +static void add_two_and_complete_returns_first(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Help"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item1); + p_autocomplete_add(ac, item2); + char *result = p_autocomplete_complete(ac, "Hel"); + + assert_string_equals("Hello", result); + + p_autocomplete_clear(ac); +} + +static void add_two_and_complete_returns_first_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact1); + p_autocomplete_add(ac, contact2); + char *result = p_autocomplete_complete(ac, "Jam"); + + assert_string_equals("James", result); + + p_autocomplete_clear(ac); +} + +static void add_two_and_complete_returns_second(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Help"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item1); + p_autocomplete_add(ac, item2); + char *result1 = p_autocomplete_complete(ac, "Hel"); + char *result2 = p_autocomplete_complete(ac, result1); + + assert_string_equals("Help", result2); + + p_autocomplete_clear(ac); +} + +static void add_two_and_complete_returns_second_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact1); + p_autocomplete_add(ac, contact2); + char *result1 = p_autocomplete_complete(ac, "Jam"); + char *result2 = p_autocomplete_complete(ac, result1); + + assert_string_equals("Jamie", result2); + + p_autocomplete_clear(ac); +} + +static void add_two_adds_two(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Help"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item1); + p_autocomplete_add(ac, item2); + GSList *result = p_autocomplete_get_list(ac); + + assert_int_equals(2, g_slist_length(result)); + + p_autocomplete_clear(ac); +} + +static void add_two_adds_two_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact1); + p_autocomplete_add(ac, contact2); + GSList *result = p_autocomplete_get_list(ac); + + assert_int_equals(2, g_slist_length(result)); + + p_autocomplete_clear(ac); +} + +static void add_two_same_adds_one(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Hello"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item1); + p_autocomplete_add(ac, item2); + GSList *result = p_autocomplete_get_list(ac); + + assert_int_equals(1, g_slist_length(result)); + + p_autocomplete_clear(ac); +} + +static void add_two_same_adds_one_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact1); + p_autocomplete_add(ac, contact2); + GSList *result = p_autocomplete_get_list(ac); + + assert_int_equals(1, g_slist_length(result)); + + p_autocomplete_clear(ac); +} + +static void add_two_same_updates(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Hello"); + PAutocomplete ac = p_autocomplete_new(); + p_autocomplete_add(ac, item1); + p_autocomplete_add(ac, item2); + GSList *result = p_autocomplete_get_list(ac); + + GSList *first = g_slist_nth(result, 0); + + char *str = first->data; + + assert_string_equals("Hello", str); + + p_autocomplete_clear(ac); +} + +static void add_two_same_updates_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("James", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + p_autocomplete_add(ac, contact1); + p_autocomplete_add(ac, contact2); + GSList *result = p_autocomplete_get_list(ac); + + GSList *first = g_slist_nth(result, 0); + PContact contact = first->data; + + assert_string_equals("James", p_contact_name(contact)); + assert_string_equals("Away", p_contact_show(contact)); + assert_string_equals("Out to lunch", p_contact_status(contact)); + + p_autocomplete_clear(ac); +} + +static void add_one_returns_true(void) +{ + char *item = strdup("Hello"); + PAutocomplete ac = p_autocomplete_new(); + int result = p_autocomplete_add(ac, item); + + assert_true(result); + + p_autocomplete_clear(ac); +} + +static void add_one_returns_true_with_funcs(void) +{ + PContact contact = p_contact_new("James", "Online", "I'm here"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + int result = p_autocomplete_add(ac, contact); + + assert_true(result); + + p_autocomplete_clear(ac); +} + +static void add_two_different_returns_true(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Hello there"); + PAutocomplete ac = p_autocomplete_new(); + int result1 = p_autocomplete_add(ac, item1); + int result2 = p_autocomplete_add(ac, item2); + + assert_true(result1); + assert_true(result2); + + p_autocomplete_clear(ac); +} + +static void add_two_different_returns_true_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("JamesB", "Away", "Out to lunch"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + int result1 = p_autocomplete_add(ac, contact1); + int result2 = p_autocomplete_add(ac, contact2); + + assert_true(result1); + assert_true(result2); + + p_autocomplete_clear(ac); +} + +static void add_two_same_returns_false(void) +{ + char *item1 = strdup("Hello"); + char *item2 = strdup("Hello"); + PAutocomplete ac = p_autocomplete_new(); + int result1 = p_autocomplete_add(ac, item1); + int result2 = p_autocomplete_add(ac, item2); + + assert_true(result1); + assert_false(result2); + + p_autocomplete_clear(ac); +} + +static void add_two_same_returns_false_with_funcs(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("James", "Online", "I'm here"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + int result1 = p_autocomplete_add(ac, contact1); + int result2 = p_autocomplete_add(ac, contact2); + + assert_true(result1); + assert_false(result2); + + p_autocomplete_clear(ac); +} + +static void add_two_same_different_data_returns_true(void) +{ + PContact contact1 = p_contact_new("James", "Online", "I'm here"); + PContact contact2 = p_contact_new("James", "Away", "I'm not here right now"); + PAutocomplete ac = + p_obj_autocomplete_new((PStrFunc)p_contact_name, + (PCopyFunc)p_contact_copy, + (PEqualDeepFunc)p_contacts_equal_deep, + (GDestroyNotify)p_contact_free); + int result1 = p_autocomplete_add(ac, contact1); + int result2 = p_autocomplete_add(ac, contact2); + + assert_true(result1); + assert_true(result2); + + p_autocomplete_clear(ac); +} + +void register_prof_autocomplete_tests(void) +{ + TEST_MODULE("prof_autocomplete tests"); + TEST(clear_empty); + TEST(clear_empty_with_free_func); + TEST(reset_after_create); + TEST(find_after_create); + TEST(get_after_create_returns_null); + TEST(get_after_create_with_copy_func_returns_null); + TEST(add_one_and_complete); + TEST(add_one_and_complete_with_funcs); + TEST(add_two_and_complete_returns_first); + TEST(add_two_and_complete_returns_first_with_funcs); + TEST(add_two_and_complete_returns_second); + TEST(add_two_and_complete_returns_second_with_funcs); + TEST(add_two_adds_two); + TEST(add_two_adds_two_with_funcs); + TEST(add_two_same_adds_one); + TEST(add_two_same_adds_one_with_funcs); + TEST(add_two_same_updates); + TEST(add_two_same_updates_with_funcs); + TEST(add_one_returns_true); + TEST(add_one_returns_true_with_funcs); + TEST(add_two_different_returns_true); + TEST(add_two_different_returns_true_with_funcs); + TEST(add_two_same_returns_false); + TEST(add_two_same_returns_false_with_funcs); + TEST(add_two_same_different_data_returns_true); +} diff --git a/tests/test_prof_history.c b/tests/test_prof_history.c new file mode 100644 index 00000000..5f08c26c --- /dev/null +++ b/tests/test_prof_history.c @@ -0,0 +1,233 @@ +#include <stdio.h> +#include <head-unit.h> +#include "prof_history.h" + +void previous_on_empty_returns_null(void) +{ + PHistory history = p_history_new(10); + char *item = p_history_previous(history, "inp"); + + assert_is_null(item); +} + +void next_on_empty_returns_null(void) +{ + PHistory history = p_history_new(10); + char *item = p_history_next(history, "inp"); + + assert_is_null(item); +} + +void previous_once_returns_last(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item = p_history_previous(history, "inp"); + + assert_string_equals("Hello", item); +} + +void previous_twice_when_one_returns_first(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item1 = p_history_previous(history, NULL); + char *item2 = p_history_previous(history, item1); + + assert_string_equals("Hello", item2); +} + +void previous_always_stops_at_first(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item1 = p_history_previous(history, NULL); + char *item2 = p_history_previous(history, item1); + char *item3 = p_history_previous(history, item2); + char *item4 = p_history_previous(history, item3); + char *item5 = p_history_previous(history, item4); + char *item6 = p_history_previous(history, item5); + + assert_string_equals("Hello", item6); +} + +void previous_goes_to_correct_element(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + p_history_append(history, "world"); + p_history_append(history, "whats"); + p_history_append(history, "going"); + p_history_append(history, "on"); + p_history_append(history, "here"); + + char *item1 = p_history_previous(history, NULL); + char *item2 = p_history_previous(history, item1); + char *item3 = p_history_previous(history, item2); + + assert_string_equals("going", item3); +} + +void prev_then_next_returns_empty(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item1 = p_history_previous(history, NULL); + char *item2 = p_history_next(history, item1); + + assert_string_equals("", item2); +} + +void prev_with_val_then_next_returns_val(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item1 = p_history_previous(history, "Oioi"); + char *item2 = p_history_next(history, item1); + + assert_string_equals("Oioi", item2); +} + +void prev_with_val_then_next_twice_returns_val(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + + char *item1 = p_history_previous(history, "Oioi"); + char *item2 = p_history_next(history, item1); + char *item3 = p_history_next(history, item2); + + assert_string_equals("Oioi", item3); +} + +void navigate_then_append_new(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + p_history_append(history, "again"); + p_history_append(history, "testing"); + p_history_append(history, "history"); + p_history_append(history, "append"); + + char *item1 = p_history_previous(history, "new text"); + assert_string_equals("append", item1); + + char *item2 = p_history_previous(history, item1); + assert_string_equals("history", item2); + + char *item3 = p_history_previous(history, item2); + assert_string_equals("testing", item3); + + char *item4 = p_history_next(history, item3); + assert_string_equals("history", item4); + + char *item5 = p_history_next(history, item4); + assert_string_equals("append", item5); + + char *item6 = p_history_next(history, item5); + assert_string_equals("new text", item6); +} + +void edit_item_mid_history(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + p_history_append(history, "again"); + p_history_append(history, "testing"); + p_history_append(history, "history"); + p_history_append(history, "append"); + + char *item1 = p_history_previous(history, "new item"); + assert_string_equals("append", item1); + + char *item2 = p_history_previous(history, item1); + assert_string_equals("history", item2); + + char *item3 = p_history_previous(history, item2); + assert_string_equals("testing", item3); + + char *item4 = p_history_previous(history, "EDITED"); + assert_string_equals("again", item4); + + char *item5 = p_history_previous(history, item4); + assert_string_equals("Hello", item5); + + char *item6 = p_history_next(history, item5); + assert_string_equals("again", item6); + + char *item7 = p_history_next(history, item6); + assert_string_equals("EDITED", item7); + + char *item8 = p_history_next(history, item7); + assert_string_equals("history", item8); + + char *item9 = p_history_next(history, item8); + assert_string_equals("append", item9); + + char *item10 = p_history_next(history, item9); + assert_string_equals("new item", item10); +} + +void edit_previous_and_append(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "Hello"); + p_history_append(history, "again"); + p_history_append(history, "testing"); + p_history_append(history, "history"); + p_history_append(history, "append"); + + char *item1 = p_history_previous(history, "new item"); + assert_string_equals("append", item1); + + char *item2 = p_history_previous(history, item1); + assert_string_equals("history", item2); + + char *item3 = p_history_previous(history, item2); + assert_string_equals("testing", item3); + + p_history_append(history, "EDITED"); + + char *item4 = p_history_previous(history, NULL); + assert_string_equals("EDITED", item4); +} + +void start_session_add_new_submit_previous(void) +{ + PHistory history = p_history_new(10); + p_history_append(history, "hello"); + + char *item1 = p_history_previous(history, NULL); + assert_string_equals("hello", item1); + + char *item2 = p_history_next(history, item1); + assert_string_equals("", item2); + + char *item3 = p_history_previous(history, "new text"); + assert_string_equals("hello", item3); + + p_history_append(history, item3); +} + +void register_prof_history_tests(void) +{ + TEST_MODULE("prof_history tests"); + TEST(previous_on_empty_returns_null); + TEST(next_on_empty_returns_null); + TEST(previous_once_returns_last); + TEST(previous_twice_when_one_returns_first); + TEST(previous_always_stops_at_first); + TEST(previous_goes_to_correct_element); + TEST(prev_then_next_returns_empty); + TEST(prev_with_val_then_next_returns_val); + TEST(prev_with_val_then_next_twice_returns_val); + TEST(navigate_then_append_new); + TEST(edit_item_mid_history); + TEST(edit_previous_and_append); + TEST(start_session_add_new_submit_previous); +} diff --git a/tests/test_util.c b/tests/test_util.c new file mode 100644 index 00000000..ffe12506 --- /dev/null +++ b/tests/test_util.c @@ -0,0 +1,165 @@ +#include <stdlib.h> +#include <string.h> +#include <head-unit.h> +#include "util.h" + +void replace_one_substr(void) +{ + char *string = "it is a string"; + char *sub = "is"; + char *new = "was"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it was a string", result); +} + +void replace_one_substr_beginning(void) +{ + char *string = "it is a string"; + char *sub = "it"; + char *new = "that"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("that is a string", result); +} + +void replace_one_substr_end(void) +{ + char *string = "it is a string"; + char *sub = "string"; + char *new = "thing"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it is a thing", result); +} + +void replace_two_substr(void) +{ + char *string = "it is a is string"; + char *sub = "is"; + char *new = "was"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("it was a was string", result); +} + +void replace_char(void) +{ + char *string = "some & a thing & something else"; + char *sub = "&"; + char *new = "&"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("some & a thing & something else", result); +} + +void replace_when_none(void) +{ + char *string = "its another string"; + char *sub = "haha"; + char *new = "replaced"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("its another string", result); +} + +void replace_when_match(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("goodbye", result); +} + +void replace_when_string_empty(void) +{ + char *string = ""; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("", result); +} + +void replace_when_string_null(void) +{ + char *string = NULL; + char *sub = "hello"; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_is_null(result); +} + +void replace_when_sub_empty(void) +{ + char *string = "hello"; + char *sub = ""; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void replace_when_sub_null(void) +{ + char *string = "hello"; + char *sub = NULL; + char *new = "goodbye"; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void replace_when_new_empty(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = ""; + + char *result = str_replace(string, sub, new); + + assert_string_equals("", result); +} + +void replace_when_new_null(void) +{ + char *string = "hello"; + char *sub = "hello"; + char *new = NULL; + + char *result = str_replace(string, sub, new); + + assert_string_equals("hello", result); +} + +void register_util_tests(void) +{ + TEST_MODULE("util tests"); + TEST(replace_one_substr); + TEST(replace_one_substr_beginning); + TEST(replace_one_substr_end); + TEST(replace_two_substr); + TEST(replace_char); + TEST(replace_when_none); + TEST(replace_when_match); + TEST(replace_when_string_empty); + TEST(replace_when_string_null); + TEST(replace_when_sub_empty); + TEST(replace_when_sub_null); + TEST(replace_when_new_empty); + TEST(replace_when_new_null); +} diff --git a/tests/testsuite.c b/tests/testsuite.c new file mode 100644 index 00000000..c3f164f9 --- /dev/null +++ b/tests/testsuite.c @@ -0,0 +1,12 @@ +#include <head-unit.h> +#include "testsuite.h" + +int main(void) +{ + register_prof_history_tests(); + register_contact_list_tests(); + register_util_tests(); + register_prof_autocomplete_tests(); + run_suite(); + return 0; +} diff --git a/tests/testsuite.h b/tests/testsuite.h new file mode 100644 index 00000000..f4bd9046 --- /dev/null +++ b/tests/testsuite.h @@ -0,0 +1,9 @@ +#ifndef TESTSUITE_H +#define TESTSUITE_H + +void register_prof_history_tests(void); +void register_contact_list_tests(void); +void register_util_tests(void); +void register_prof_autocomplete_tests(void); + +#endif |