fix allies with null faction (to be resolved later).

This commit is contained in:
Enno Rehling 2014-10-31 15:38:37 +01:00
parent f27a77d288
commit cf110d7788
2 changed files with 52 additions and 36 deletions

View file

@ -4,36 +4,38 @@
#include <stdlib.h> #include <stdlib.h>
ally * ally_find(ally *al, const struct faction *f) { ally * ally_find(ally *al, const struct faction *f) {
for (;al;al=al->next) { for (; al; al = al->next) {
if (al->faction==f) return al; if (al->faction == f) return al;
} }
return 0; return 0;
} }
ally * ally_add(ally **al_p, struct faction *f) { ally * ally_add(ally **al_p, struct faction *f) {
ally * al; ally * al;
while (*al_p) { if (f) {
al = *al_p; while (*al_p) {
if (al->faction==f) return al; al = *al_p;
al_p = &al->next; if (al->faction == f) return al;
} al_p = &al->next;
al = (ally *)malloc(sizeof(ally)); }
al->faction = f; }
al->status = 0; al = (ally *)malloc(sizeof(ally));
al->next = 0; al->faction = f;
*al_p = al; al->status = 0;
return al; al->next = 0;
*al_p = al;
return al;
} }
void ally_remove(ally **al_p, struct faction *f) { void ally_remove(ally **al_p, struct faction *f) {
ally * al; ally * al;
while (*al_p) { while (*al_p) {
al = *al_p; al = *al_p;
if (al->faction==f) { if (al->faction == f) {
*al_p = al->next; *al_p = al->next;
free(al); free(al);
break; break;
}
al_p = &al->next;
} }
al_p = &al->next;
}
} }

View file

@ -7,22 +7,36 @@
static void test_ally(CuTest * tc) static void test_ally(CuTest * tc)
{ {
ally * al = 0; ally * al = 0;
struct faction * f1 = test_create_faction(0); struct faction * f1 = test_create_faction(0);
ally_add(&al, f1); ally_add(&al, f1);
CuAssertPtrNotNull(tc, al); CuAssertPtrNotNull(tc, al);
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction); CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
ally_remove(&al, f1); ally_remove(&al, f1);
CuAssertPtrEquals(tc, 0, al); CuAssertPtrEquals(tc, 0, al);
CuAssertPtrEquals(tc, 0, ally_find(al, f1)); CuAssertPtrEquals(tc, 0, ally_find(al, f1));
}
static void test_ally_null(CuTest * tc)
{
ally *a1 = 0, *a2 = 0;
a1 = ally_add(&a1, 0);
a2 = ally_add(&a1, 0);
CuAssertPtrNotNull(tc, a1);
CuAssertPtrNotNull(tc, a2);
CuAssertPtrEquals(tc, a2, a1->next);
CuAssertPtrEquals(tc, 0, a2->next);
free(a1);
free(a2);
} }
CuSuite *get_ally_suite(void) CuSuite *get_ally_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_ally); SUITE_ADD_TEST(suite, test_ally);
return suite; return suite;
} }