1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Fixed duplicated contacts

This commit is contained in:
James Booth 2012-03-08 22:43:28 +00:00
parent 78bd151c48
commit 7a9d127ae2
3 changed files with 220 additions and 55 deletions

View File

@ -1,103 +1,103 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "contact_list.h" #include "contact_list.h"
// internal contact list // contact list node
struct _contact_t { struct _contact_t {
char *contact; char *contact;
struct _contact_t *next; struct _contact_t *next;
int last;
}; };
// the contact list // the contact list
static struct _contact_t *_contact_list = NULL; static struct _contact_t *_contact_list = NULL;
static struct _contact_t * _make_contact(char *contact);
void contact_list_clear(void) void contact_list_clear(void)
{ {
if (_contact_list == NULL) { struct _contact_t *curr = _contact_list;
return;
} if (curr) {
else { while(curr) {
struct _contact_t *curr = _contact_list;
while(!curr->last) {
free(curr->contact); free(curr->contact);
curr = curr->next; curr = curr->next;
} }
free(_contact_list); free(_contact_list);
_contact_list = NULL;
} }
}
_contact_list = NULL;
int contact_list_remove(char *contact)
{
return 0;
} }
int contact_list_add(char *contact) int contact_list_add(char *contact)
{ {
if (_contact_list == NULL) { if (!_contact_list) {
// create the new one _contact_list = _make_contact(contact);
_contact_list = (struct _contact_t *) malloc(sizeof(struct _contact_t));
_contact_list->contact = return 1;
(char *) malloc((strlen(contact)+1) * sizeof(char));
strcpy(_contact_list->contact, contact);
_contact_list->last = 1;
// complete the circle
_contact_list->next = _contact_list;
} else { } else {
// go through entries
struct _contact_t *curr = _contact_list; struct _contact_t *curr = _contact_list;
while (!curr->last) { struct _contact_t *prev = NULL;
// if already there, return without adding
if (strcmp(curr->contact, contact) == 0)
return 1;
// move on while(curr) {
curr = curr->next; if (strcmp(curr->contact, contact) == 0)
return 0;
prev = curr;
curr = curr->next;
} }
// create the new one curr = _make_contact(contact);
struct _contact_t *new =
(struct _contact_t *) malloc(sizeof(struct _contact_t)); if (prev)
new->contact = (char *) malloc((strlen(contact)+1) * sizeof(char)); prev->next = curr;
strcpy(new->contact, contact);
new->last = 1;
// point the old one to it return 1;
curr->next = new;
curr->last = 0;
} }
return 0;
} }
struct contact_list *get_contact_list(void) struct contact_list *get_contact_list(void)
{ {
int count = 0;
struct contact_list *list = struct contact_list *list =
(struct contact_list *) malloc(sizeof(struct contact_list)); (struct contact_list *) malloc(sizeof(struct contact_list));
if (_contact_list == NULL) { struct _contact_t *curr = _contact_list;
if (!curr) {
list->contacts = NULL; list->contacts = NULL;
list->size = 0;
return list;
} else { } else {
struct _contact_t *curr = _contact_list;
list->contacts = (char **) malloc(sizeof(char **)); list->contacts = (char **) malloc(sizeof(char **));
int count = 0;
while(1) { while(curr) {
list->contacts[count] = list->contacts[count] =
(char *) malloc((strlen(curr->contact)+1) * sizeof(char)); (char *) malloc((strlen(curr->contact) + 1) * sizeof(char));
strcpy(list->contacts[count], curr->contact); strcpy(list->contacts[count], curr->contact);
count++; count++;
if (curr->last) curr = curr->next;
break;
else {
curr = curr->next;
}
} }
list->size = count;
return list;
} }
list->size = count;
return list;
} }
static struct _contact_t * _make_contact(char *contact)
{
struct _contact_t *new = (struct _contact_t *) malloc(sizeof(struct _contact_t));
new->contact = (char *) malloc((strlen(contact) + 1) * sizeof(char));
strcpy(new->contact, contact);
new->next = NULL;
return new;
}

View File

@ -8,6 +8,7 @@ struct contact_list {
void contact_list_clear(void); void contact_list_clear(void);
int contact_list_add(char *contact); int contact_list_add(char *contact);
int contact_list_remove(char *contact);
struct contact_list *get_contact_list(void); struct contact_list *get_contact_list(void);
#endif #endif

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include <head-unit.h> #include <head-unit.h>
#include "contact_list.h" #include "contact_list.h"
@ -46,6 +47,156 @@ static void first_and_second_elements_correct(void)
assert_string_equals("Dave", list->contacts[1]); assert_string_equals("Dave", list->contacts[1]);
} }
static void contains_three_elements(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
struct contact_list *list = get_contact_list();
assert_int_equals(3, list->size);
}
static void first_three_elements_correct(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
struct contact_list *list = get_contact_list();
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
assert_string_equals("Bob", list->contacts[2]);
}
static void add_twice_at_beginning_adds_once(void)
{
contact_list_add("James");
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
struct contact_list *list = get_contact_list();
assert_int_equals(3, list->size);
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
assert_string_equals("Bob", list->contacts[2]);
}
static void add_twice_in_middle_adds_once(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("James");
contact_list_add("Bob");
struct contact_list *list = get_contact_list();
assert_int_equals(3, list->size);
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
assert_string_equals("Bob", list->contacts[2]);
}
static void add_twice_at_end_adds_once(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
contact_list_add("James");
struct contact_list *list = get_contact_list();
assert_int_equals(3, list->size);
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
assert_string_equals("Bob", list->contacts[2]);
}
/*
static void remove_when_none_does_nothing(void)
{
contact_list_remove("James");
struct contact_list *list = get_contact_list();
assert_int_equals(0, list->size);
}
static void remove_when_one_removes(void)
{
contact_list_add("James");
contact_list_remove("James");
struct contact_list *list = get_contact_list();
assert_int_equals(0, list->size);
}
static void remove_first_when_two(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_remove("James");
struct contact_list *list = get_contact_list();
assert_int_equals(1, list->size);
assert_string_equals("Dave", list->contacts[0]);
}
static void remove_second_when_two(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_remove("Dave");
struct contact_list *list = get_contact_list();
assert_int_equals(1, list->size);
assert_string_equals("James", list->contacts[0]);
}
static void remove_first_when_three(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
contact_list_remove("James");
struct contact_list *list = get_contact_list();
assert_int_equals(2, list->size);
assert_string_equals("Dave", list->contacts[0]);
assert_string_equals("Bob", list->contacts[1]);
}
static void remove_second_when_three(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
contact_list_remove("Dave");
struct contact_list *list = get_contact_list();
assert_int_equals(2, list->size);
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Bob", list->contacts[1]);
}
static void remove_third_when_three(void)
{
contact_list_add("James");
contact_list_add("Dave");
contact_list_add("Bob");
contact_list_remove("Bob");
struct contact_list *list = get_contact_list();
assert_int_equals(2, list->size);
assert_string_equals("James", list->contacts[0]);
assert_string_equals("Dave", list->contacts[1]);
}*/
void register_contact_list_tests(void) void register_contact_list_tests(void)
{ {
TEST_MODULE("contact_list tests"); TEST_MODULE("contact_list tests");
@ -55,4 +206,17 @@ void register_contact_list_tests(void)
TEST(first_element_correct); TEST(first_element_correct);
TEST(contains_two_elements); TEST(contains_two_elements);
TEST(first_and_second_elements_correct); 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);
*/
} }