Debian Codegraph @

test/bugs/bugtrack/check_free_objects.c

raw symbols SCIP
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 [email protected]
21 */
22
23 /**
24 * @file
25 * Makes sure that inventory objects are freed correctly.
26 */
27
28 #include <check.h>
29 #include <global.h>
30 #include <sproto.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 static object *find_arch_at(mapstruct *map, int x, int y, const char *arch_name) {
35 FOR_MAP_PREPARE(map, x, y, op) {
36 if (strcmp(op->arch->name, arch_name) == 0) {
37 return op;
38 }
39 } FOR_MAP_FINISH();
40 return NULL;
41 }
42
43 static void setup(void) {
44 }
45
46 static void teardown(void) {
47 }
48
49 START_TEST(test_merge) {
50 mapstruct *map1;
51 archetype *scroll_arch;
52 archetype *food_arch;
53 object *scroll1;
54 object *scroll2;
55 object *food1;
56 object *food2;
57
58 map1 = ready_map_name("/world/world_103_128", 0);
59 fail_unless(map1 != NULL, "cannot load map /world/world_103_128");
60
61 scroll_arch = find_archetype("scroll_new");
62 fail_unless(scroll_arch != NULL, "cannot find archetype scroll_new");
63
64 food_arch = find_archetype("food");
65 fail_unless(food_arch != NULL, "cannot find archetype food");
66
67 scroll1 = object_create_arch(scroll_arch);
68 fail_unless(scroll1 != NULL, "cannot create object scroll_new");
69
70 scroll2 = object_create_arch(scroll_arch);
71 fail_unless(scroll2 != NULL, "cannot create object scroll_new");
72
73 food1 = object_create_arch(food_arch);
74 fail_unless(food1 != NULL, "cannot create object food");
75
76 food2 = object_create_arch(food_arch);
77 fail_unless(food2 != NULL, "cannot create object food");
78
79 food1 = object_insert_in_ob(food1, scroll1);
80
81 food2 = object_insert_in_ob(food2, scroll2);
82
83 fail_unless(find_arch_at(map1, 4, 3, "scroll_new") == NULL, "map initially contains a scroll");
84 fail_unless(find_arch_at(map1, 4, 3, "food") == NULL, "map initially contains a food");
85
86 scroll1 = object_insert_in_map_at(scroll1, map1, NULL, 0, 4, 3);
87 fail_unless(scroll1 != NULL, "scroll could not be added to the map");
88
89 fail_unless(find_arch_at(map1, 4, 3, "scroll_new") == scroll1, "scroll disappeared");
90 fail_unless(find_arch_at(map1, 4, 3, "food") == NULL, "map contains a food");
91
92 scroll2 = object_insert_in_map_at(scroll2, map1, NULL, 0, 4, 3);
93 fail_unless(scroll2 != NULL, "scroll could not be added to the map");
94
95 fail_unless(find_arch_at(map1, 4, 3, "scroll_new") == scroll2, "scroll disappeared");
96 fail_unless(find_arch_at(map1, 4, 3, "food") == NULL, "map contains a food");
97
98 fail_unless(scroll2->nrof == 2, "scrolls didn't merge");
99 fail_unless(QUERY_FLAG(scroll1, FLAG_FREED), "scroll wasn't freed");
100 }
101 END_TEST
102
103 static Suite *bug_suite(void) {
104 Suite *s = suite_create("bug");
105 TCase *tc_core = tcase_create("Core");
106
107 tcase_add_checked_fixture(tc_core, setup, teardown);
108
109 suite_add_tcase(s, tc_core);
110 tcase_add_test(tc_core, test_merge);
111 tcase_set_timeout(tc_core, 0);
112
113 return s;
114 }
115
116 int main(void) {
117 int nf;
118 Suite *s = bug_suite();
119 SRunner *sr = srunner_create(s);
120
121 srunner_set_fork_status(sr, CK_NOFORK);
122 init(0, NULL);
123
124 srunner_set_xml(sr, LOGDIR "/bugs/bugtrack/free_objects.xml");
125 srunner_set_log(sr, LOGDIR "/bugs/bugtrack/free_objects.out");
126 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
127 nf = srunner_ntests_failed(sr);
128 srunner_free(sr);
129 return nf == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
130 }