forked from github/server
fix allies with null faction (to be resolved later).
This commit is contained in:
parent
f27a77d288
commit
cf110d7788
2 changed files with 52 additions and 36 deletions
|
@ -4,36 +4,38 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
ally * ally_find(ally *al, const struct faction *f) {
|
||||
for (;al;al=al->next) {
|
||||
if (al->faction==f) return al;
|
||||
}
|
||||
return 0;
|
||||
for (; al; al = al->next) {
|
||||
if (al->faction == f) return al;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ally * ally_add(ally **al_p, struct faction *f) {
|
||||
ally * al;
|
||||
while (*al_p) {
|
||||
al = *al_p;
|
||||
if (al->faction==f) return al;
|
||||
al_p = &al->next;
|
||||
}
|
||||
al = (ally *)malloc(sizeof(ally));
|
||||
al->faction = f;
|
||||
al->status = 0;
|
||||
al->next = 0;
|
||||
*al_p = al;
|
||||
return al;
|
||||
ally * al;
|
||||
if (f) {
|
||||
while (*al_p) {
|
||||
al = *al_p;
|
||||
if (al->faction == f) return al;
|
||||
al_p = &al->next;
|
||||
}
|
||||
}
|
||||
al = (ally *)malloc(sizeof(ally));
|
||||
al->faction = f;
|
||||
al->status = 0;
|
||||
al->next = 0;
|
||||
*al_p = al;
|
||||
return al;
|
||||
}
|
||||
|
||||
void ally_remove(ally **al_p, struct faction *f) {
|
||||
ally * al;
|
||||
while (*al_p) {
|
||||
al = *al_p;
|
||||
if (al->faction==f) {
|
||||
*al_p = al->next;
|
||||
free(al);
|
||||
break;
|
||||
ally * al;
|
||||
while (*al_p) {
|
||||
al = *al_p;
|
||||
if (al->faction == f) {
|
||||
*al_p = al->next;
|
||||
free(al);
|
||||
break;
|
||||
}
|
||||
al_p = &al->next;
|
||||
}
|
||||
al_p = &al->next;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,22 +7,36 @@
|
|||
|
||||
static void test_ally(CuTest * tc)
|
||||
{
|
||||
ally * al = 0;
|
||||
struct faction * f1 = test_create_faction(0);
|
||||
ally * al = 0;
|
||||
struct faction * f1 = test_create_faction(0);
|
||||
|
||||
ally_add(&al, f1);
|
||||
CuAssertPtrNotNull(tc, al);
|
||||
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
|
||||
ally_add(&al, f1);
|
||||
CuAssertPtrNotNull(tc, al);
|
||||
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
|
||||
|
||||
ally_remove(&al, f1);
|
||||
CuAssertPtrEquals(tc, 0, al);
|
||||
CuAssertPtrEquals(tc, 0, ally_find(al, f1));
|
||||
ally_remove(&al, f1);
|
||||
CuAssertPtrEquals(tc, 0, al);
|
||||
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 *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_ally);
|
||||
return suite;
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_ally);
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue