Crossfire Server, Trunk  R20513
check_c_party.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_c_party_c =
3  * "$Id: check_c_party.c 15379 2011-11-01 19:40:45Z ryo_saeba $";
4  */
5 
6 /*
7  * CrossFire, A Multiplayer game for X-windows
8  *
9  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  * Copyright (C) 1992 Frank Tore Johansen
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * The authors can be reached via e-mail at crossfire-devel@real-time.com
27  */
28 
29 /*
30  * This is the unit tests file for server/c_party.c
31  */
32 
33 #include <stdlib.h>
34 #include <check.h>
35 #include <global.h>
36 #include <sproto.h>
37 
38 static void setup(void) {
39  /* put any initialisation steps here, they will be run before each testcase */
40 }
41 
42 static void teardown(void) {
43  /* put any cleanup steps here, they will be run after each testcase */
44 }
45 
46 START_TEST(test_party) {
47  partylist *p1, *p2, *p3;
48  object *pl1;
49  object *pl2;
50  object *pl3;
51 
52  fail_unless(party_get_first() == NULL, "firstparty should be NULL!");
53 
54  pl1 = calloc(1, sizeof(object));
55  pl1->name = "player1";
56  fail_unless(pl1 != NULL, "memory allocation failure");
57  pl1->contr = calloc(1, sizeof(player));
58  fail_unless(pl1->contr != NULL, "memory allocation failure");
59  first_player = pl1->contr; /* needed because obsolete parties uses this. */
60  pl1->contr->ob = pl1;
61 
62  pl2 = calloc(1, sizeof(object));
63  pl2->name = "player2";
64  fail_unless(pl2 != NULL, "memory allocation failure");
65  pl2->contr = calloc(1, sizeof(player));
66  fail_unless(pl2->contr != NULL, "memory allocation failure");
67  first_player = pl2->contr; /* needed because obsolete parties uses this. */
68  pl2->contr->ob = pl2;
69 
70  pl3 = calloc(1, sizeof(object));
71  pl3->name = "player2";
72  fail_unless(pl3 != NULL, "memory allocation failure");
73  pl3->contr = calloc(1, sizeof(player));
74  fail_unless(pl3->contr != NULL, "memory allocation failure");
75  first_player = pl3->contr; /* needed because obsolete parties uses this. */
76  pl3->contr->ob = pl3;
77 
78  p1 = party_form(pl1, "test1");
79  fail_unless(p1 != NULL, "party_form failed.");
80  fail_unless(party_get_first() == p1, "firstparty wasn't updated");
81  fail_unless(strcmp(p1->partyname, "test1") == 0, "wrong party name");
82  fail_unless(p1 == pl1->contr->party, "player wasn't added to party");
83  fail_unless(strcmp(party_get_leader(p1), "player1") == 0, "wrong party leader");
84 
85  p2 = party_form(pl2, "test2");
86  fail_unless(p2 != NULL, "party_form failed.");
87  fail_unless(party_get_next(party_get_first()) == p2, "party incorrectly linked");
88 
89  party_remove(p1);
90 
91  fail_unless(party_get_first() == p2, "party incorrectly removed");
92 
93  p3 = party_form(pl3, "test3");
94  fail_unless(p3 != NULL, "party_form failed");
95  fail_unless(party_get_next(party_get_first()) == p3, "party p3 incorrectly linked");
96  fail_unless(pl3->contr->party == p3, "p3 incorrectly assigned to pl3");
97 
99  fail_unless(party_get_first() == p3, "party p2 wasn't removed by obsolete_parties(), party %s still there", party_get_first() ? party_get_first()->partyname : "NULL party?");
100 }
101 END_TEST
102 
103 static Suite *c_party_suite(void) {
104  Suite *s = suite_create("c_party");
105  TCase *tc_core = tcase_create("Core");
106 
107  /*setup and teardown will be called before each test in testcase 'tc_core' */
108  tcase_add_checked_fixture(tc_core, setup, teardown);
109 
110  suite_add_tcase(s, tc_core);
111  tcase_add_test(tc_core, test_party);
112 
113  return s;
114 }
115 
116 int main(void) {
117  int nf;
118  Suite *s = c_party_suite();
119  SRunner *sr = srunner_create(s);
120 
121  srunner_set_xml(sr, LOGDIR "/unit/server/c_party.xml");
122  srunner_set_log(sr, LOGDIR "/unit/server/c_party.out");
123  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
124  nf = srunner_ntests_failed(sr);
125  srunner_free(sr);
126  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
127 }
One player.
Definition: player.h:92
One party.
Definition: party.h:10
partylist * party_get_next(const partylist *party)
Returns the next party from the list of all parties.
Definition: party.c:229
const char * party_get_leader(const partylist *party)
Returns the name of the party&#39;s leader.
Definition: party.c:322
static END_TEST Suite * c_party_suite(void)
void party_remove(partylist *party)
Removes and frees a party.
Definition: party.c:165
Global type definitions and header inclusions.
char * partyname
Party name.
Definition: party.h:14
partylist * party
Party this player is part of.
Definition: player.h:186
START_TEST(test_party)
Definition: check_c_party.c:46
const char * name
The name of the object, obviously...
Definition: object.h:311
struct pl * contr
Pointer to the player which control this object.
Definition: object.h:276
partylist * party_get_first(void)
Returns the first party from the list of all parties.
Definition: party.c:217
object * ob
The object representing the player.
Definition: player.h:158
void party_obsolete_parties(void)
Remove unused parties (no players), this could be made to scale a lot better.
Definition: party.c:236
EXTERN player * first_player
First player.
Definition: global.h:117
static void setup(void)
Definition: check_c_party.c:38
int main(void)
partylist * party_form(object *op, const char *partyname)
Forms the party struct for a party called &#39;partyname&#39;.
Definition: party.c:40
static void teardown(void)
Definition: check_c_party.c:42