Debian Codegraph @

test/bugs/bugtrack/check_1727944.c

raw symbols SCIP
1 /*
2 * CrossFire, A Multiplayer game for X-windows
3 *
4 * Copyright (C) 2007 Crossfire Development Team
5 * Copyright (C) 1992 Frank Tore Johansen
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * The authors can be reached via e-mail at [email protected]
22 */
23
24 /**
25 * @file
26 * This is the unit tests file for the bug #1727944: The horn of plenty
27 * location: http://sourceforge.net/tracker/index.php?func=detail&aid=1727944&group_id=13833&atid=113833.
28 *
29 * I did try different combos to generate an empty horn, so far no success...
30 * @author Nicolas Weeger
31 * @date 2007-06-04
32 */
33
34 #include <stdlib.h>
35 #include <check.h>
36 #include <global.h>
37 #include <sproto.h>
38
39 static void setup(void) {
40 /* put any initialisation steps here, they will be run before each testcase */
41 }
42
43 static void teardown(void) {
44 /* put any cleanup steps here, they will be run after each testcase */
45 }
46
47 #if 0
48 static mapstruct *get_random_map(mapstruct *map) {
49 object *exit_ob;
50 mapstruct *random;
51 RMParms rp;
52 char newmap_name[HUGE_BUF], *cp;
53 static int reference_number = 0;
54 int x, y;
55
56 exit_ob = NULL;
57 for (x = 0; x < MAP_WIDTH(map) && exit_ob == NULL; x++) {
58 for (y = 0; y < MAP_HEIGHT(map) && exit_ob == NULL; y++) {
59 for (exit_ob = GET_MAP_OB(map, x, y); exit_ob != NULL; exit_ob = exit_ob->above)
60 if (exit_ob->type == EXIT && exit_ob->msg != NULL)
61 break;
62 }
63 }
64
65 if (!exit_ob)
66 /* this means we reached the end of the random part. */
67 return NULL;
68
69 /* copied from server/server.c:enter_random_map(). */
70 memset(&rp, 0, sizeof(RMParms));
71 rp.Xsize = -1;
72 rp.Ysize = -1;
73 rp.region = get_region_by_map(exit_ob->map);
74 if (exit_ob->msg)
75 set_random_map_variable(&rp, exit_ob->msg);
76 rp.origin_x = exit_ob->x;
77 rp.origin_y = exit_ob->y;
78 strcpy(rp.origin_map, map->path);
79
80 /* If we have a final_map, use it as a base name to give some clue
81 * as where the player is. Otherwise, use the origin map.
82 * Take the last component (after the last slash) to give
83 * shorter names without bogus slashes.
84 */
85 if (rp.final_map[0]) {
86 cp = strrchr(rp.final_map, '/');
87 if (!cp)
88 cp = rp.final_map;
89 } else {
90 char buf[HUGE_BUF];
91
92 cp = strrchr(rp.origin_map, '/');
93 if (!cp)
94 cp = rp.origin_map;
95 /* Need to strip of any trailing digits, if it has them */
96 snprintf(buf, sizeof(buf), "%s", cp);
97 while (isdigit(buf[strlen(buf)-1]))
98 buf[strlen(buf)-1] = 0;
99 cp = buf;
100 }
101 snprintf(newmap_name, sizeof(newmap_name), "/random/%s%04d", cp+1, reference_number++);
102 /* now to generate the actual map. */
103 return generate_random_map(newmap_name, &rp, NULL);
104 }
105
106 static void do_run(void) {
107 mapstruct *worldmap;
108 mapstruct *random;
109 mapstruct *old;
110 int iteration, x, y, map;
111 object *check;
112 char path[150];
113
114 for (map = 1; map <= 3; map++) {
115 snprintf(path, sizeof(path), "/whalingoutpost/underwaterdungeon/level%d", map);
116 worldmap = ready_map_name(path, 0);
117 fail_unless(worldmap != NULL, "Can't load %s", path);
118
119 random = worldmap;
120 old = NULL;
121 iteration = 0;
122 while (random != NULL) {
123 random = get_random_map(random);
124 if (!random)
125 break;
126 if (old)
127 delete_map(old);
128 old = random;
129 iteration++;
130 for (x = 0; x < MAP_WIDTH(random); x++) {
131 for (y = 0; y < MAP_HEIGHT(random); y++) {
132 for (check = GET_MAP_OB(random, x, y); check; check = check->above) {
133 if (check->type == ROD && check->title && strcmp(check->title, "of Plenty") == 0)
134 fail_unless(check->inv != NULL, "Horn has empty inventory!");
135 }
136 }
137 }
138 }
139 fail_unless(iteration != 0, "did %d iterations", iteration);
140 if (old)
141 delete_map(old);
142 }
143 }
144 #endif
145
146 #if 0
147 static void do_run(void) {
148 mapstruct *map, *overlay;
149 int x, y, found = 0, test = 0;
150 object *check;
151
152 overlay = ready_map_name("../../rsc/bug_1727944_unique", MAP_PLAYER_UNIQUE);
153 fail_unless(overlay != NULL, "Couldn't load unique map ../../rsc/bug_1727944_unique");
154
155 while (found == 0 && test < 10) {
156 map = ready_map_name("../../rsc/bug_1727944", MAP_PLAYER_UNIQUE);
157 fail_unless(map != NULL, "couldn't load map ../../rsc/bug_1727944");
158
159 for (x = 0; x < MAP_WIDTH(map); x++) {
160 for (y = 0; y < MAP_HEIGHT(map); y++) {
161 for (check = GET_MAP_OB(map, x, y); check; check = check->above) {
162 if (check->type == ROD) {
163 fail_unless(check->inv != NULL, "Horn has empty inventory!");
164 fail_unless(check->inv->below == NULL, "Horn has 2 items in inventory!");
165 if (check->title && strcmp(check->title, "of Plenty") == 0) {
166 object_remove(check);
167 object_insert_in_map_at(check, overlay, NULL, 0, 2, 3);
168 found++;
169 break;
170 }
171 }
172 }
173 }
174 }
175 delete_map(map);
176 test++;
177 }
178 save_map(overlay, SAVE_MODE_OVERLAY);
179 delete_map(overlay);
180 }
181 #endif
182
183 extern int artifact_init;
184 extern int arch_init;
185
186 /* Copied from loader.l */
187 extern const char *const spell_mapping[];
188
189 static void local_check_loaded_object(object *op) {
190 int ip;
191
192 if (artifact_init)
193 /* Artifacts are special beasts, let's not check them. */
194 return;
195
196 /* We do some specialized handling to handle legacy cases of name_pl.
197 * If the object doesn't have a name_pl, we just use the object name -
198 * this isn't perfect (things won't be properly pluralized), but works to
199 * that degree (5 heart is still quite understandable). But the case we
200 * also have to catch is if this object is not using the normal name for
201 * the object. In that case, we also want to use the loaded name.
202 * Otherwise, what happens is that the the plural name will lose
203 * information (appear as just 'hearts' and not 'goblins heart')
204 */
205 if (op->arch && op->name != op->arch->clone.name && op->name_pl == op->arch->clone.name_pl) {
206 if (op->name_pl)
207 free_string(op->name_pl);
208 op->name_pl = NULL;
209 }
210 if (!op->name_pl && op->name)
211 op->name_pl = add_string(op->name);
212
213 /* objects now have a materialname. try to patch it in */
214 if (!(IS_WEAPON(op) && op->level > 0)) {
215 set_materialname(op);
216 }
217 /* only do these when program is first run - a bit
218 * excessive to do this at every run - most of this is
219 * really just to catch any errors - program will still run, but
220 * not in the ideal fashion.
221 */
222 if ((op->type == WEAPON || op->type == BOW) && arch_init) {
223 if (!op->skill) {
224 LOG(llevError, "Weapon %s lacks a skill.\n", op->name);
225 } else if ((!strcmp(op->skill, "one handed weapons") && op->body_info[1] != -1)
226 || (!strcmp(op->skill, "two handed weapons") && op->body_info[1] != -2)) {
227 LOG(llevError, "weapon %s arm usage does not match skill: %d, %s\n",
228 op->name, op->body_info[1], op->skill);
229 }
230 }
231
232 /* We changed last_heal to gen_sp_armour, which is what it
233 * really does for many objects. Need to catch any in maps
234 * that may have an old value.
235 */
236 if ((op->type == WEAPON)
237 || (op->type == ARMOUR)
238 || (op->type == HELMET)
239 || (op->type == SHIELD)
240 || (op->type == RING)
241 || (op->type == BOOTS)
242 || (op->type == GLOVES)
243 || (op->type == AMULET)
244 || (op->type == GIRDLE)
245 || (op->type == BRACERS)
246 || (op->type == CLOAK)) {
247 if (op->last_heal) {
248 LOG(llevDebug, "Object %s still has last_heal set, not gen_sp_armour\n", op->name ? op->name : "NULL");
249 op->gen_sp_armour = op->last_heal;
250 op->last_heal = 0;
251 }
252 ip = calc_item_power(op);
253 /* Legacy objects from before item power was in the game */
254 if (!op->item_power && ip) {
255 if (ip > 3) {
256 LOG(llevDebug, "Object %s had no item power, using %d\n", op->name ? op->name : "NULL", ip);
257 }
258 op->item_power = ip;
259 }
260 /* Check for possibly bogus values. Has to meet both these criteria -
261 * something that has item_power 1 is probably just fine if our calculated
262 * value is 1 or 2 - these values are small enough that hard to be precise.
263 * similarly, it item_power is 0, the first check will always pass,
264 * but not the second one.
265 */
266 if (ip > 2*op->item_power && ip > (op->item_power+3)) {
267 LOG(llevDebug, "Object %s seems to have too low item power? %d > %d\n", op->name ? op->name : "NULL", ip, op->item_power);
268 }
269 }
270 /* Old spellcasting object - need to load in the appropiate object */
271 if ((op->type == ROD || op->type == WAND || op->type == SCROLL || op->type == FIREWALL || /* POTIONS and ALTARS don't always cast spells, but if they do, update them */ ((op->type == POTION || op->type == ALTAR) && op->stats.sp))
272 && !op->inv
273 && !arch_init) {
274 object *tmp;
275
276 /* Fireall is bizarre in that spell type was stored in dam. Rest are 'normal'
277 * in that spell was stored in sp.
278 */
279 tmp = create_archetype(spell_mapping[op->type == FIREWALL ? op->stats.dam : op->stats.sp]);
280 object_insert_in_ob(tmp, op);
281 op->randomitems = NULL; /* So another spell isn't created for this object */
282 }
283
284 /* spellbooks & runes use slaying. But not to arch name, but to spell name */
285 if ((op->type == SPELLBOOK || op->type == RUNE) && op->slaying && !op->inv && !arch_init) {
286 object *tmp;
287
288 tmp = create_archetype_by_object_name(op->slaying);
289 object_insert_in_ob(tmp, op);
290 op->randomitems = NULL; /* So another spell isn't created for this object */
291 /* without this, value is all screwed up */
292 op->value = op->arch->clone.value*op->inv->value;
293 }
294
295 if (QUERY_FLAG(op, FLAG_MONSTER)) {
296 if (op->stats.hp > op->stats.maxhp)
297 LOG(llevDebug, "Monster %s has hp set higher than maxhp (%d>%d)\n", op->name, op->stats.hp, op->stats.maxhp);
298 }
299 if ((QUERY_FLAG(op, FLAG_GENERATOR) && QUERY_FLAG(op, FLAG_CONTENT_ON_GEN))
300 || op->type == CREATOR
301 || op->type == CONVERTER) {
302 /* Object will duplicate it's content as part of the
303 * generation process. To do this, we must flag inventory
304 * so it remains unevaluated concerning the randomitems and
305 * the living (a demonlord shouldn't cast from inside generator!)
306 */
307 object_set_flag_inv(op, FLAG_IS_A_TEMPLATE);
308 }
309
310 /* Here we'll handle custom monsters. In order to handle them correctly, especially in the fix_object
311 * method, we'll create a new temporary archetype containing defined values.
312 * Of course this doesn't apply when loading archetypes or artifacts.
313 */
314 if (arch_init == 0 && artifact_init == 0 && QUERY_FLAG(op, FLAG_MONSTER) && op->arch && !object_can_merge(op, &op->arch->clone)) {
315 archetype *temp = get_archetype_struct();
316
317 temp->reference_count++;
318 temp->name = add_string(op->arch->name);
319 temp->tail_x = op->arch->tail_x;
320 temp->tail_y = op->arch->tail_y;
321 object_copy(op, &temp->clone);
322 temp->clone.inv = NULL;
323 temp->clone.env = NULL;
324 temp->clone.x = 0;
325 temp->clone.y = 0;
326 temp->clone.map = NULL;
327 if (FABS(temp->clone.speed) > MIN_ACTIVE_SPEED) {
328 /* Clone has a speed, so need to clear that because it isn't on a map.
329 * But we need to keep the value, because otherwise the customized object
330 * will have no speed (fix_player() will use the 0 value). So set it
331 * to zero, call object_update_speed() to remove it from active list, then
332 * set its speed back to the original.
333 */
334 temp->clone.speed = 0;
335 object_update_speed(&temp->clone);
336 temp->clone.speed = op->speed;
337 }
338
339 temp->more = op->arch->more;
340 op->arch = temp;
341 /* LOG(llevDebug, "created temporary archetype for %s at %d,%d\n", op->name, op->x, op->y); */
342 }
343 }
344
345 START_TEST(test_randommaps) {
346 #if 0
347 int test;
348 mapstruct *overlay;
349 object *check;
350
351 for (test = 0; test < 50; test++)
352 do_run();
353
354 for (test = 0; test < 50; test++) {
355 overlay = ready_map_name("../../rsc/bug_1727944_unique", MAP_PLAYER_UNIQUE);
356 fail_unless(overlay != NULL, "Couldn't load unique map ../../rsc/bug_1727944_unique");
357 fail_unless(GET_MAP_OB(overlay, 2, 3) != NULL, "No item on spot 2,3?");
358
359 for (check = GET_MAP_OB(overlay, 2, 3)->above; check != NULL; check = check->above) {
360 fail_unless(check->type == ROD, "Found a non horn?");
361 fail_unless(check->inv != NULL, "Horn without a spell!");
362 fail_unless(check->inv->below == NULL, "Horn with 2 items in inventory.");
363 }
364 save_map(overlay, SAVE_MODE_OVERLAY);
365 delete_map(overlay);
366 }
367 #endif
368
369 #if 0
370 int test;
371 archetype *horn = find_archetype("horn");
372 fail_unless(horn != NULL, "couldn't find archetype horn.");
373 archetype *horn2 = find_archetype("horn2");
374 fail_unless(horn2 != NULL, "couldn't find archetype horn2.");
375
376 for (test = 0; test < 100000; test++) {
377 object *check = arch_to_object(RANDOM()%2 ? horn : horn2);
378
379 generate_artifact(check, RANDOM()%100);
380 fail_unless(check->inv != NULL, "horn without inventory!");
381 }
382 #endif
383
384 int test, level, found = 0;
385 object *the_chest, *check;
386 mapstruct *map;
387 treasurelist *tlist = find_treasurelist("uncommon_items");
388 fail_unless(tlist != NULL, "couldn't find treasure list uncommon_items");
389
390 for (test = 0; test < 10; test++) {
391 for (level = 1; level < 120; level++) {
392 map = get_empty_map(1, 1);
393 fail_unless(map != NULL, "failed to get empty map");
394 map->difficulty = level;
395
396 the_chest = create_archetype("chest"); /* was "chest_2" */
397 fail_unless(the_chest != NULL, "failed to get chest");
398 the_chest->randomitems = tlist;
399 the_chest->stats.hp = RANDOM()%100;
400 object_insert_in_map_at(the_chest, map, NULL, 0, 0, 0);
401 apply_auto_fix(map);
402 the_chest = GET_MAP_OB(map, 0, 0);
403 fail_unless(the_chest != NULL, "failed to recover chest?");
404 for (check = the_chest->inv; check; check = check->below) {
405 if (check->type != ROD)
406 continue;
407 local_check_loaded_object(check);
408 fail_unless(check->inv != NULL, "horn without inventory");
409 fail_unless(check->inv->below == NULL, "horn with 2 items");
410 fail_unless(check->randomitems == NULL, "horn with randomitems set");
411 found++;
412 }
413 delete_map(map);
414 }
415 }
416 fail_unless(found > 100, "didn't find 100 horn but %d??", found);
417
418 }
419 END_TEST
420
421 static Suite *bug_suite(void) {
422 Suite *s = suite_create("bug");
423 TCase *tc_core = tcase_create("Core");
424
425 /*setup and teardown will be called before each test in testcase 'tc_core' */
426 tcase_add_checked_fixture(tc_core, setup, teardown);
427
428 suite_add_tcase(s, tc_core);
429 tcase_add_test(tc_core, test_randommaps);
430 tcase_set_timeout(tc_core, 0);
431
432 return s;
433 }
434
435 int main(void) {
436 int nf;
437 Suite *s = bug_suite();
438 SRunner *sr = srunner_create(s);
439
440 srunner_set_fork_status(sr, CK_NOFORK);
441 init(0, NULL);
442
443 srunner_set_xml(sr, LOGDIR "/bugs/bugtrack/1727944.xml");
444 srunner_set_log(sr, LOGDIR "/bugs/bugtrack/1727944.out");
445 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
446 nf = srunner_ntests_failed(sr);
447 srunner_free(sr);
448 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
449 }