Debian Codegraph @

test/unit/common/check_item.c

raw symbols SCIP
1 /*
2 * static char *rcsid_check_item_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 [email protected]
27 */
28
29 /*
30 * This is the unit tests file for common/item.c
31 */
32
33 #include <global.h>
34 #include <stdlib.h>
35 #include <check.h>
36 #include <loader.h>
37 #include <toolkit_common.h>
38
39 static void setup(void) {
40 cctk_setdatadir(BUILD_ROOT "lib");
41 cctk_setlog(LOGDIR "/unit/common/item.out");
42 cctk_init_std_archetypes();
43 init_gods();
44 init_formulae();
45 }
46
47 static void teardown(void) {
48 /* put any cleanup steps here, they will be run after each testcase */
49 }
50
51 START_TEST(test_describe_item) {
52 object *test;
53 char *buf;
54 int check;
55 treasurelist *list;
56
57 static const char *archs[] = {
58 "gem",
59 "food",
60 "lantern_1",
61 "blood",
62 "ring",
63 "amulet",
64 "goblin",
65 "potion_restoration",
66 "axe_2",
67 "elven_bow",
68 "helmet_of_brilliance",
69 "holy_avenger",
70 "scimitar",
71 "shield",
72 "fl_corpse",
73 "booze",
74 "poison",
75 "deathshead",
76 "zombie",
77 "talisman_evocation",
78 "talisman_sorcery",
79 "dragon",
80 "titan",
81 "speedboots",
82 "t_star1",
83 "girdle_con",
84 "god_finger",
85 "shining_finger",
86 "high_boots_w",
87 "plate_mail",
88 "robe",
89 "scale_mail",
90 "DShieldms",
91 "holy_shield",
92 NULL };
93 static const char *arch_results[] = {
94 "",
95 "(food+600)",
96 "(glowing)",
97 "",
98 "of adornment",
99 "of adornment",
100 "(slow movement)(wield weapon)(archer)(wear armour)(wear ring)",
101 "",
102 "(dam+6)(weapon speed 10)(Attacks: physical)",
103 "(wc+1)(dam+15)(Attacks: physical)",
104 "(Int+2)(Pow+2)(ac+2)(item_power +5)(magic+1)[color=#FF15CD](armour +5)[/color]",
105 "(Str+1)(Wis+2)(Cha+2)(dam+15)(item_power +25)(weapon speed 6)(Attacks: weaponmagic, blinding)[color=#930C76](resist magic +30)[/color][ul](resist drain +100)[/ul]",
106 "(dam+8)(weapon speed 8)(Attacks: physical)",
107 "(ac+1)[color=#FF15CD](armour +5)[/color]",
108 "(food+600)",
109 "(food+65)(slay vial_poison:poison)",
110 "",
111 "(extremely fast movement)(undead)(spellcaster)(Spell abilities:)(paralyze)(fear ability)(cause black death)(cause red death)(face of death)(meteor swarm)(hellfire)(burning hands)(large fireball)(mass confusion)(negative energy bolt)(negative energy ball)(slow ability)(Attacks: physical, cold)(armour +75)(resist magic +100)(resist fire +100)(resist electricity +90)(resist cold +100)(resist confusion +100)(resist acid +90)(resist drain +100)(resist weaponmagic +80)(resist ghosthit +100)(resist poison +100)(resist slow +100)(resist paralyzation +100)(resist fear +100)(resist cancellation +65)(resist depletion +100)(resist death +100)(resist chaos +100)(resist counterspell +65)(resist god power +80)(resist blindness +100)",
112 "(slow movement)(undead)(Attacks: physical)(resist cold +50)(resist fear +100)",
113 "",
114 "",
115 "(normal movement)(see invisible)(spellcaster)(Spell abilities:)(breath flame)(medium fireball ability)(fear ability)(Attacks: physical)(resist fire +100)(resist cold -100)(resist confusion -100)(resist fear +100)(resist blindness +50)",
116 "(fast movement)(see invisible)(wield weapon)(archer)(wear armour)(wear ring)(read scroll)(fires wand/rod/horn)(spellcaster)(Spell abilities:)(paralyze)(fear ability)(small lightning)(large lightning)(slow ability)(resist magic +50)(resist electricity +100)(resist fear +100)",
117 "(speed +6)(item_power +6)[color=#FF15CD](armour +3)[/color]",
118 "(dam+3)(weapon speed 2)(Attacks: physical)",
119 "(Con+2)(item_power +1)",
120 "(Str+2)(Dex-1)(dam+3)(item_power +2)[color=#FF15CD](armour +3)[/color]",
121 "(Str+2)(dam+3)(item_power +1)[color=#FF15CD](armour +3)[/color]",
122 "(Cha+1)(ac+1)(Spell regen penalty 4)[color=#FF15CD](armour +4)[/color](resist blindness +1)",
123 "(ac+5)(Max speed 0.70)(Spell regen penalty 30)[color=#FF15CD](armour +40)[/color]",
124 "(ac+1)(Max speed 1.20)",
125 "(ac+3)(Max speed 0.90)(Spell regen penalty 10)[color=#FF15CD](armour +20)[/color]",
126 "(Cha-5)(ac+7)(item_power +10)(reflect spells)(reflect missiles)[color=#FF15CD](armour +15)[/color][color=red](resist fire +30)[/color][ul](resist drain +100)[/ul](resist ghosthit +80)",
127 "(ac+4)(item_power +6)[color=#FF15CD](armour +10)[/color][ul](resist drain +100)[/ul](resist ghosthit +50)",
128 NULL };
129
130 /* if you change the order, the result will quite certainly change, as the generation depends on the value returned
131 * by rand() - changing the order changes the item generated...
132 */
133 static const char *treasures[] = {
134 "random_knowledge",
135 "missile_weapons",
136 "random_talisman",
137 "rare_weapons",
138 "random_food",
139 "random_artifact",
140 "random_read",
141 "random_amulet",
142 "random_artifact",
143 "random_amulet",
144 "random_artifact",
145 "standard_old",
146 NULL
147 };
148 static const char *treasure_results[] = {
149 "",
150 "(wc+1)(dam+2)(Attacks: physical)",
151 "",
152 "(dam+6)(weapon speed 9)(Attacks: physical)",
153 "(food+70)",
154 "(Con+2)(Cha-1)(dam+10)(item_power +15)(weapon speed 5)(regeneration+1)(Attacks: weaponmagic)[ul](resist drain +100)[/ul][color=green](resist poison +30)[/color]",
155 "",
156 "of adornment",
157 "(Str+1)(dam+9)(item_power +20)(weapon speed 6)(regeneration+1)(Attacks: electricity, drain)[color=#930C76](resist magic +30)[/color][color=blue](resist electricity +30)[/color][ul](resist drain +100)[/ul]",
158 "of adornment",
159 "(Str+1)[color=#FF15CD](armour +3)[/color]",
160 "",
161 NULL
162 };
163
164 for (check = 0; archs[check] != NULL; check++) {
165 test = cctk_create_game_object(archs[check]);
166 fail_unless(test != NULL, "couldn't create arch %s", archs[check]);
167 SET_FLAG(test, FLAG_IDENTIFIED);
168 buf = stringbuffer_finish(describe_item(test, NULL, 0, NULL));
169
170 /* if you're adding items, uncomment that so make finding the good value easier. */
171 /*
172 if (strcmp(buf, arch_results[check]))
173 printf("describe_item(%s) returned \"%s\" instead of \"%s\"\n", archs[check], buf, arch_results[check]);
174 */
175
176 fail_unless(strcmp(buf, arch_results[check]) == 0, "describe_item(%s) returned \"%s\" instead of \"%s\"", archs[check], buf, arch_results[check]);
177
178 free(buf);
179 object_free_drop_inventory(test);
180 }
181
182 /* we initialize the random generator to always be able to reproduce, and we use rand() in case RANDOM is something else. */
183 srand(100);
184 for (check = 0; treasures[check] != NULL; check++) {
185 list = find_treasurelist(treasures[check]);
186 fail_unless(list != NULL, "couldn't find treasure list %s", treasures[check]);
187 test = generate_treasure(list, 50);
188 fail_if(test == NULL, "couldn't create item from treasure list %s", treasures[check]);
189 SET_FLAG(test, FLAG_IDENTIFIED);
190 buf = stringbuffer_finish(describe_item(test, NULL, 0, NULL));
191
192 /* if you're adding lists, uncomment that so make finding the good value easier. */
193 /*
194 if (strcmp(buf, treasure_results[check]))
195 printf("Item %d describe_item(treasure %s) returned \"%s\" instead of \"%s\"\n", check, treasures[check], buf, treasure_results[check]);
196 */
197
198 fail_unless(strcmp(buf, treasure_results[check]) == 0, "describe_item(treasure %s) returned \"%s\" instead of \"%s\"", treasures[check], buf, treasure_results[check]);
199
200 free(buf);
201 object_free_drop_inventory(test);
202
203 }
204 }
205 END_TEST
206
207 #define DESCRIBE_PATH_SAFE(retbuf, variable, name, len, maxlen) \
208 if (variable) { \
209 int i, j = 0; \
210 safe_strcat(retbuf, "(" name ": ", len, maxlen); \
211 for (i = 0; i < NRSPELLPATHS; i++) \
212 if (variable&(1<<i)) { \
213 if (j) \
214 safe_strcat(retbuf, ", ", len, maxlen); \
215 else \
216 j = 1; \
217 safe_strcat(retbuf, spellpathnames[i], len, maxlen); \
218 } \
219 safe_strcat(retbuf, ")", len, maxlen); \
220 }
221
222 #define DESCRIBE_ABILITY_SAFE(retbuf, variable, name, len, maxlen) \
223 if (variable) { \
224 int i, j = 0; \
225 safe_strcat(retbuf, "(" name ": ", len, maxlen); \
226 for (i = 0; i < NROFATTACKS; i++) \
227 if (variable&(1<<i)) { \
228 if (j) \
229 safe_strcat(retbuf, ", ", len, maxlen); \
230 else \
231 j = 1; \
232 safe_strcat(retbuf, attacks[i], len, maxlen); \
233 } \
234 safe_strcat(retbuf, ")", len, maxlen); \
235 }
236
237 static void old_describe_monster(const object *op, char *retbuf, size_t size) {
238 int i;
239 size_t len;
240
241 retbuf[0] = '\0';
242
243 /* Note that the resolution this provides for players really isn't
244 * very good. Any player with a speed greater than .67 will
245 * fall into the 'lightning fast movement' category.
246 */
247 if (FABS(op->speed) > MIN_ACTIVE_SPEED) {
248 switch ((int)((FABS(op->speed))*15)) {
249 case 0:
250 snprintf(retbuf, size, "(very slow movement)");
251 break;
252
253 case 1:
254 snprintf(retbuf, size, "(slow movement)");
255 break;
256
257 case 2:
258 snprintf(retbuf, size, "(normal movement)");
259 break;
260
261 case 3:
262 case 4:
263 snprintf(retbuf, size, "(fast movement)");
264 break;
265
266 case 5:
267 case 6:
268 snprintf(retbuf, size, "(very fast movement)");
269 break;
270
271 case 7:
272 case 8:
273 case 9:
274 case 10:
275 snprintf(retbuf, size, "(extremely fast movement)");
276 break;
277
278 default:
279 snprintf(retbuf, size, "(lightning fast movement)");
280 break;
281 }
282 }
283 if (QUERY_FLAG(op, FLAG_UNDEAD))
284 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(undead)");
285 if (QUERY_FLAG(op, FLAG_SEE_INVISIBLE))
286 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(see invisible)");
287 if (QUERY_FLAG(op, FLAG_USE_WEAPON))
288 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(wield weapon)");
289 if (QUERY_FLAG(op, FLAG_USE_BOW))
290 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(archer)");
291 if (QUERY_FLAG(op, FLAG_USE_ARMOUR))
292 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(wear armour)");
293 if (QUERY_FLAG(op, FLAG_USE_RING))
294 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(wear ring)");
295 if (QUERY_FLAG(op, FLAG_USE_SCROLL))
296 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(read scroll)");
297 if (QUERY_FLAG(op, FLAG_USE_RANGE))
298 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(fires wand/rod/horn)");
299 if (QUERY_FLAG(op, FLAG_CAN_USE_SKILL))
300 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(skill user)");
301 if (QUERY_FLAG(op, FLAG_CAST_SPELL))
302 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(spellcaster)");
303 if (QUERY_FLAG(op, FLAG_FRIENDLY))
304 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(friendly)");
305 if (QUERY_FLAG(op, FLAG_UNAGGRESSIVE))
306 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(unaggressive)");
307 if (QUERY_FLAG(op, FLAG_HITBACK))
308 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(hitback)");
309 if (QUERY_FLAG(op, FLAG_STEALTH))
310 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(stealthy)");
311 if (op->randomitems != NULL) {
312 treasure *t;
313 int first = 1;
314
315 for (t = op->randomitems->items; t != NULL; t = t->next)
316 if (t->item && (t->item->clone.type == SPELL)) {
317 if (first) {
318 first = 0;
319 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(Spell abilities:)");
320 }
321 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(%s)", t->item->clone.name);
322 }
323 }
324 if (op->type == PLAYER) {
325 if (op->contr->digestion) {
326 if (op->contr->digestion != 0)
327 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(sustenance%+d)", op->contr->digestion);
328 }
329 if (op->contr->gen_grace) {
330 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(grace%+d)", op->contr->gen_grace);
331 }
332 if (op->contr->gen_sp) {
333 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(magic%+d)", op->contr->gen_sp);
334 }
335 if (op->contr->gen_hp) {
336 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(regeneration%+d)", op->contr->gen_hp);
337 }
338 if (op->stats.luck) {
339 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(luck%+d)", op->stats.luck);
340 }
341 }
342
343 /* describe attacktypes */
344 len = strlen(retbuf);
345 if (is_dragon_pl(op)) {
346 /* for dragon players display the attacktypes from clawing skill
347 * Break apart the for loop - move the comparison checking down -
348 * this makes it more readable.
349 */
350 object *tmp;
351
352 tmp = object_find_by_type_and_name(op, SKILL, "clawing");
353 if (tmp && tmp->attacktype != 0) {
354 DESCRIBE_ABILITY_SAFE(retbuf, tmp->attacktype, "Claws", &len, size);
355 } else {
356 DESCRIBE_ABILITY_SAFE(retbuf, op->attacktype, "Attacks", &len, size);
357 }
358 } else {
359 DESCRIBE_ABILITY_SAFE(retbuf, op->attacktype, "Attacks", &len, size);
360 }
361 DESCRIBE_PATH_SAFE(retbuf, op->path_attuned, "Attuned", &len, size);
362 DESCRIBE_PATH_SAFE(retbuf, op->path_repelled, "Repelled", &len, size);
363 DESCRIBE_PATH_SAFE(retbuf, op->path_denied, "Denied", &len, size);
364 for (i = 0; i < NROFATTACKS; i++) {
365 if (op->resist[i]) {
366 snprintf(retbuf+strlen(retbuf), size-strlen(retbuf), "(%s %+d)", resist_plus[i], op->resist[i]);
367 }
368 }
369 }
370
371 START_TEST(test_describe_monster_rewrite) {
372 char buf[HUGE_BUF], *compat, *final;
373 archetype *arch;
374 object *ob;
375 player pl;
376
377 memset(&pl, 0, sizeof(pl));
378
379 for (arch = first_archetype; arch; arch = arch->next) {
380
381 if (!QUERY_FLAG(&arch->clone, FLAG_MONSTER) && arch->clone.type != PLAYER)
382 continue;
383
384 ob = object_create_arch(arch);
385 ob->contr = &pl;
386
387 old_describe_monster(ob, buf, sizeof(buf));
388 compat = stringbuffer_finish(describe_item(ob, NULL, 0, NULL));
389 fail_unless(strcmp(buf, compat) == 0, "(compat) description change:\n%s\n === vs ===\n%s", buf, compat);
390 free(compat);
391
392 final = stringbuffer_finish(describe_monster(ob, 0, NULL));
393
394 fail_unless(strcmp(buf, final) == 0, "description change: \"%s\" vs \"%s\"", buf, final);
395 free(final);
396 object_free(ob, FREE_OBJ_NO_DESTROY_CALLBACK | FREE_OBJ_FREE_INVENTORY);
397 }
398
399 } END_TEST
400
401 static Suite *item_suite(void) {
402 Suite *s = suite_create("item");
403 TCase *tc_core = tcase_create("Core");
404
405 /*setup and teardown will be called before each test in testcase 'tc_core' */
406 tcase_add_checked_fixture(tc_core, setup, teardown);
407
408 suite_add_tcase(s, tc_core);
409 tcase_add_test(tc_core, test_describe_item);
410 tcase_add_test(tc_core, test_describe_monster_rewrite);
411
412 return s;
413 }
414
415 int main(void) {
416 #ifndef __GLIBC__
417 /* This test makes calls into the common library, and that
418 * library makes calls to the random function. But the random function
419 * in different libraries return different results - GLIBC seems
420 * consistent - so far - but even that could change. But if GLIBC
421 * is not being used, almost certain in that case that the random
422 * numbers returned are different.
423 */
424 printf("Skipping item test - need glibc to get same results to check against\n");
425 return EXIT_SUCCESS;
426 #else
427 int nf;
428 Suite *s = item_suite();
429 SRunner *sr = srunner_create(s);
430
431 /* to debug, uncomment this line */
432 srunner_set_fork_status(sr, CK_NOFORK);
433
434 srunner_set_xml(sr, LOGDIR "/unit/common/item.xml");
435 srunner_set_log(sr, LOGDIR "/unit/common/item.out");
436 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
437 nf = srunner_ntests_failed(sr);
438 srunner_free(sr);
439 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
440 #endif
441 }