Crossfire Server, Trunk
check_free_objects.cpp
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2009 Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
28 #include <check.h>
29 #include <global.h>
30 #include <sproto.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <toolkit_common.h>
34 
35 static object *find_arch_at(mapstruct *map, int x, int y, const char *arch_name) {
36  FOR_MAP_PREPARE(map, x, y, op) {
37  if (strcmp(op->arch->name, arch_name) == 0) {
38  return op;
39  }
40  } FOR_MAP_FINISH();
41  return NULL;
42 }
43 
44 static void setup(void) {
45 }
46 
47 static void teardown(void) {
48 }
49 
50 START_TEST(test_merge) {
51  mapstruct *map1;
52  archetype *scroll_arch;
53  archetype *food_arch;
54  object *scroll1;
55  object *scroll2;
56  object *food1;
57  object *food2;
58 
59  map1 = ready_map_name("/world/world_103_128", 0);
60  FAIL_UNLESS(map1 != NULL, "cannot load map /world/world_103_128");
61 
62  scroll_arch = find_archetype("scroll_new");
63  FAIL_UNLESS(scroll_arch != NULL, "cannot find archetype scroll_new");
64 
65  food_arch = find_archetype("food");
66  FAIL_UNLESS(food_arch != NULL, "cannot find archetype food");
67 
68  scroll1 = object_create_arch(scroll_arch);
69  FAIL_UNLESS(scroll1 != NULL, "cannot create object scroll_new");
70 
71  scroll2 = object_create_arch(scroll_arch);
72  FAIL_UNLESS(scroll2 != NULL, "cannot create object scroll_new");
73 
74  food1 = object_create_arch(food_arch);
75  FAIL_UNLESS(food1 != NULL, "cannot create object food");
76 
77  food2 = object_create_arch(food_arch);
78  FAIL_UNLESS(food2 != NULL, "cannot create object food");
79 
80  food1 = object_insert_in_ob(food1, scroll1);
81 
82  food2 = object_insert_in_ob(food2, scroll2);
83 
84  FAIL_UNLESS(find_arch_at(map1, 4, 3, "scroll_new") == NULL, "map initially contains a scroll");
85  FAIL_UNLESS(find_arch_at(map1, 4, 3, "food") == NULL, "map initially contains a food");
86 
87  scroll1 = object_insert_in_map_at(scroll1, map1, NULL, 0, 4, 3);
88  FAIL_UNLESS(scroll1 != NULL, "scroll could not be added to the map");
89 
90  FAIL_UNLESS(find_arch_at(map1, 4, 3, "scroll_new") == scroll1, "scroll disappeared");
91  FAIL_UNLESS(find_arch_at(map1, 4, 3, "food") == NULL, "map contains a food");
92 
93  scroll2 = object_insert_in_map_at(scroll2, map1, NULL, 0, 4, 3);
94  FAIL_UNLESS(scroll2 != NULL, "scroll could not be added to the map");
95 
96  FAIL_UNLESS(find_arch_at(map1, 4, 3, "scroll_new") == scroll2, "scroll disappeared");
97  FAIL_UNLESS(find_arch_at(map1, 4, 3, "food") == NULL, "map contains a food");
98 
99  FAIL_UNLESS(scroll2->nrof == 2, "scrolls didn't merge");
100  FAIL_UNLESS(QUERY_FLAG(scroll1, FLAG_FREED), "scroll wasn't freed");
101 }
102 END_TEST
103 
104 static Suite *bug_suite(void) {
105  Suite *s = suite_create("bug");
106  TCase *tc_core = tcase_create("Core");
107 
108  tcase_add_checked_fixture(tc_core, setup, teardown);
109 
110  suite_add_tcase(s, tc_core);
111  tcase_add_test(tc_core, test_merge);
112  tcase_set_timeout(tc_core, 0);
113 
114  return s;
115 }
116 
117 int main(void) {
118  int nf;
119  Suite *s = bug_suite();
120  SRunner *sr = srunner_create(s);
121 
122  srunner_set_fork_status(sr, CK_NOFORK);
123  cctk_setdatadir(SOURCE_ROOT "lib");
124  init(0, NULL);
125 
126  srunner_set_xml(sr, LOGDIR "/bugs/bugtrack/free_objects.xml");
127  srunner_set_log(sr, LOGDIR "/bugs/bugtrack/free_objects.out");
128  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
129  nf = srunner_ntests_failed(sr);
130  srunner_free(sr);
131  return nf == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
132 }
global.h
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:730
diamondslots.x
x
Definition: diamondslots.py:15
ready_map_name
mapstruct * ready_map_name(const char *name, int flags)
Definition: map.cpp:1768
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.cpp:69
find_arch_at
static object * find_arch_at(mapstruct *map, int x, int y, const char *arch_name)
Definition: check_free_objects.cpp:35
teardown
static void teardown(void)
Definition: check_free_objects.cpp:47
setup
static void setup(void)
Definition: check_free_objects.cpp:44
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.cpp:2857
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
disinfect.map
map
Definition: disinfect.py:4
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:298
archetype
Definition: object.h:483
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
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:723
bug_suite
static END_TEST Suite * bug_suite(void)
Definition: check_free_objects.cpp:104
mapstruct
Definition: map.h:313
START_TEST
START_TEST(test_merge)
Definition: check_free_objects.cpp:50
give.op
op
Definition: give.py:33
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:266
main
int main(void)
Definition: check_free_objects.cpp:117
init
void init(int argc, char **argv)
Definition: init.cpp:1087
diamondslots.y
y
Definition: diamondslots.py:16
object::nrof
uint32_t nrof
Definition: object.h:342