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:
parent
78bd151c48
commit
7a9d127ae2
110
contact_list.c
110
contact_list.c
@ -1,103 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "contact_list.h"
|
||||
|
||||
// internal contact list
|
||||
// contact list node
|
||||
struct _contact_t {
|
||||
char *contact;
|
||||
struct _contact_t *next;
|
||||
int last;
|
||||
};
|
||||
|
||||
// the contact list
|
||||
static struct _contact_t *_contact_list = NULL;
|
||||
|
||||
static struct _contact_t * _make_contact(char *contact);
|
||||
|
||||
void contact_list_clear(void)
|
||||
{
|
||||
if (_contact_list == NULL) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
struct _contact_t *curr = _contact_list;
|
||||
while(!curr->last) {
|
||||
struct _contact_t *curr = _contact_list;
|
||||
|
||||
if (curr) {
|
||||
while(curr) {
|
||||
free(curr->contact);
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
free(_contact_list);
|
||||
_contact_list = NULL;
|
||||
}
|
||||
|
||||
_contact_list = NULL;
|
||||
}
|
||||
|
||||
int contact_list_remove(char *contact)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int contact_list_add(char *contact)
|
||||
{
|
||||
if (_contact_list == NULL) {
|
||||
// create the new one
|
||||
_contact_list = (struct _contact_t *) malloc(sizeof(struct _contact_t));
|
||||
_contact_list->contact =
|
||||
(char *) malloc((strlen(contact)+1) * sizeof(char));
|
||||
strcpy(_contact_list->contact, contact);
|
||||
_contact_list->last = 1;
|
||||
|
||||
// complete the circle
|
||||
_contact_list->next = _contact_list;
|
||||
|
||||
if (!_contact_list) {
|
||||
_contact_list = _make_contact(contact);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
// go through entries
|
||||
struct _contact_t *curr = _contact_list;
|
||||
while (!curr->last) {
|
||||
// if already there, return without adding
|
||||
if (strcmp(curr->contact, contact) == 0)
|
||||
return 1;
|
||||
struct _contact_t *prev = NULL;
|
||||
|
||||
// move on
|
||||
curr = curr->next;
|
||||
while(curr) {
|
||||
if (strcmp(curr->contact, contact) == 0)
|
||||
return 0;
|
||||
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
// create the new one
|
||||
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->last = 1;
|
||||
curr = _make_contact(contact);
|
||||
|
||||
if (prev)
|
||||
prev->next = curr;
|
||||
|
||||
// point the old one to it
|
||||
curr->next = new;
|
||||
curr->last = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct contact_list *get_contact_list(void)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
struct contact_list *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->size = 0;
|
||||
return list;
|
||||
} else {
|
||||
struct _contact_t *curr = _contact_list;
|
||||
list->contacts = (char **) malloc(sizeof(char **));
|
||||
int count = 0;
|
||||
while(1) {
|
||||
|
||||
while(curr) {
|
||||
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);
|
||||
count++;
|
||||
|
||||
if (curr->last)
|
||||
break;
|
||||
else {
|
||||
curr = curr->next;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ struct contact_list {
|
||||
|
||||
void contact_list_clear(void);
|
||||
int contact_list_add(char *contact);
|
||||
int contact_list_remove(char *contact);
|
||||
struct contact_list *get_contact_list(void);
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <head-unit.h>
|
||||
#include "contact_list.h"
|
||||
|
||||
@ -46,6 +47,156 @@ static void first_and_second_elements_correct(void)
|
||||
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)
|
||||
{
|
||||
TEST_MODULE("contact_list tests");
|
||||
@ -55,4 +206,17 @@ void register_contact_list_tests(void)
|
||||
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);
|
||||
*/
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user