Crossfire Server, Trunk
check_monster.cpp
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_monster_c =
3  * "$Id$";
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/monster.c
31  */
32 
33 #include <stdlib.h>
34 #include <check.h>
35 
36 #include <global.h>
37 #include <sproto.h>
38 #include <toolkit_common.h>
39 
40 void setup(void) {
41 }
42 
43 void teardown(void) {
44 }
45 
46 START_TEST(test_monster_find_nearest_enemy) {
47  object *first, *second, *third, *owner, *found;
48  player pl;
49  socket_struct sock;
50 
51  // The function randomizes directions, so check 200 times to be safe.
52 
53  memset(&pl, 0, sizeof(pl));
54  memset(&sock, 0, sizeof(sock));
55  pl.socket = &sock;
56 
57  mapstruct *map = get_empty_map(3, 3);
58 
59  first = create_archetype("kobold");
60  FAIL_UNLESS(QUERY_FLAG(first, FLAG_MONSTER), "first isn't a monster");
61  object_insert_in_map_at(first, map, NULL, 0, 1, 1);
62 
63  for (uint8_t i = 0; i < 200; i++) {
64  FAIL_UNLESS(monster_find_nearest_enemy(first, NULL) == NULL, "Found something when nothing?");
65  }
66 
67  second = create_archetype("kobold");
68  object_insert_in_map_at(second, map, NULL, 0, 0, 1);
69  for (uint8_t i = 0; i < 200; i++) {
70  FAIL_UNLESS(monster_find_nearest_enemy(first, NULL) == second, "Didn't find second monster?");
71  }
72 
73  owner = create_archetype("dwarf_player");
74  owner->contr = &pl;
75  object_insert_in_map_at(owner, map, NULL, 0, 0, 0);
76  first->owner = owner;
77  first->ownercount = owner->count;
78  for (uint8_t i = 0; i < 200; i++) {
79  found = monster_find_nearest_enemy(first, owner);
80  FAIL_IF(found == owner, "Found owner?");
81  FAIL_UNLESS(found == second, "Should find second!");
82  }
83 
84  second->owner = owner;
85  second->ownercount = owner->count;
86  for (uint8_t i = 0; i < 200; i++) {
87  found = monster_find_nearest_enemy(first, owner);
88  FAIL_IF(found == owner, "Found owner?");
89  FAIL_UNLESS(found == NULL, "Shouldn't find anything since both are pets");
90  }
91 
92  pl.petmode = pet_sad;
93  for (uint8_t i = 0; i < 200; i++) {
94  found = monster_find_nearest_enemy(first, owner);
95  FAIL_UNLESS(found == NULL, "Pets shouldn't attack other pets");
96  }
97 
98  third = create_archetype("kobold");
99  object_insert_in_map_at(third, map, NULL, 0, 1, 0);
100  for (uint8_t i = 0; i < 200; i++) {
101  found = monster_find_nearest_enemy(first, owner);
102  FAIL_UNLESS(found == third, "Should find third monster");
103  }
104 
105  uint8_t co = 0, cs = 0, ct = 0;
106  for (uint16_t i = 0; i < 2000; i++) {
107  found = monster_find_nearest_enemy(first, NULL);
108  FAIL_UNLESS(found, "Should find a target!");
109  if (found == owner) {
110  co++;
111  } else if (found == second) {
112  cs++;
113  } else {
114  ct++;
115  }
116  }
117  FAIL_UNLESS(co != 0, "Should have found the owner");
118  FAIL_UNLESS(cs != 0, "Should have found second");
119  FAIL_UNLESS(ct != 0, "Should have found third");
120 }
121 END_TEST
122 
123 Suite *monster_suite(void) {
124  Suite *s = suite_create("monster");
125  TCase *tc_core = tcase_create("Core");
126 
127  /*setup and teardown will be called before each test in testcase 'tc_core' */
128  tcase_add_checked_fixture(tc_core, setup, teardown);
129 
130  suite_add_tcase(s, tc_core);
131  tcase_add_test(tc_core, test_monster_find_nearest_enemy);
132 
133  return s;
134 }
135 
136 int main(void) {
137  int nf;
138 
139  cctk_setdatadir(SOURCE_ROOT "lib");
140  init(0, NULL);
141 
142  Suite *s = monster_suite();
143  SRunner *sr = srunner_create(s);
144 
145  // Uncomment to debug
146  // srunner_set_fork_status(sr, CK_NOFORK);
147 
148  srunner_set_xml(sr, LOGDIR "/unit/server/monster.xml");
149  srunner_set_log(sr, LOGDIR "/unit/server/monster.out");
150  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
151  nf = srunner_ntests_failed(sr);
152  srunner_free(sr);
153  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
154 }
teardown
void teardown(void)
Definition: check_monster.cpp:40
global.h
object::owner
object * owner
Definition: object.h:387
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.cpp:842
player
Definition: player.h:105
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.cpp:69
START_TEST
START_TEST(test_empty)
Definition: check_monster.cpp:44
socket_struct
Definition: newserver.h:89
object::ownercount
tag_t ownercount
Definition: object.h:390
object::count
tag_t count
Definition: object.h:307
setup
void setup(void)
Definition: check_monster.cpp:36
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
monster_find_nearest_enemy
object * monster_find_nearest_enemy(object *npc, object *owner)
Definition: monster.cpp:175
object::contr
struct player * contr
Definition: object.h:284
toolkit_common.h
disinfect.map
map
Definition: disinfect.py:4
main
int main(void)
Definition: check_monster.cpp:62
monster_suite
END_TEST Suite * monster_suite(void)
Definition: check_monster.cpp:49
sproto.h
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.cpp:2100
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
pet_sad
@ pet_sad
Definition: player.h:59
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
mapstruct
Definition: map.h:313
init
void init(int argc, char **argv)
Definition: init.cpp:1087
FAIL_IF
#define FAIL_IF(expr,...)
Definition: toolkit_common.h:16
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28