Crossfire Server, Branches 1.12  R18729
check_item.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_item_c =
3  * "$Id: check_item.c 11578 2009-02-23 22:02:27Z lalo $";
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 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 void setup(void) {
40  cctk_setdatadir(BUILD_ROOT "lib");
41  cctk_setlog(LOGDIR "/unit/common/item.out");
43  init_gods();
44 }
45 
46 void teardown(void) {
47  /* put any cleanup steps here, they will be run after each testcase */
48 }
49 
50 START_TEST(test_describe_item) {
51  object *test;
52  char buf[5000];
53  int check;
54  treasurelist *list;
55 
56  static const char *archs[] = {
57  "gem",
58  "food",
59  "lantern_1",
60  "blood",
61  "ring",
62  "amulet",
63  "goblin",
64  "potion_restoration",
65  "axe_2",
66  "elven_bow",
67  "helmet_of_brilliance",
68  "holy_avenger",
69  "scimitar",
70  "shield",
71  "fl_corpse",
72  "booze",
73  "poison",
74  "deathshead",
75  "zombie",
76  "talisman_evocation",
77  "talisman_sorcery",
78  "dragon",
79  "titan",
80  "speedboots",
81  "t_star1",
82  "girdle_con",
83  "god_finger",
84  "shining_finger",
85  "high_boots_w",
86  "plate_mail",
87  "robe",
88  "scale_mail",
89  "DShieldms",
90  "holy_shield",
91  NULL };
92  static const char *arch_results[] = {
93  "",
94  "(food+200)",
95  "(glowing)",
96  "",
97  "",
98  "",
99  "(slow movement)(wield weapon)(archer)(wear armour)(wear ring)",
100  "",
101  "(dam+6)(weapon speed 10)(Attacks: physical)",
102  "(wc+1)(dam+15)(Attacks: physical)",
103  "(Int+2)(Pow+2)(ac+2)(item_power +5)(magic+1)(armour +5)",
104  "(Str+1)(Wis+2)(Cha+2)(dam+15)(item_power +25)(weapon speed 6)(Attacks: weaponmagic, blinding)(resist magic +30)(resist drain +100)",
105  "(dam+8)(weapon speed 8)(Attacks: physical)",
106  "(ac+1)(armour +5)",
107  "(food+600)",
108  "(food+100)(slay vial_poison:poison)",
109  "",
110  "(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)",
111  "(slow movement)(undead)(Attacks: physical)(resist cold +50)(resist fear +100)",
112  "",
113  "",
114  "(normal movement)(see invisible)(spellcaster)(Spell abilities:)(burning hands ability)(medium fireball ability)(fear ability)(Attacks: physical)(resist fire +100)(resist cold -100)(resist confusion -100)(resist fear +100)(resist blindness +50)",
115  "(fast movement)(see invisible)(wield weapon)(archer)(wear armour)(wear ring)(read scroll)(spellcaster)(Spell abilities:)(paralyze)(fear ability)(small lightning)(large lightning)(slow ability)(resist magic +50)(resist electricity +100)(resist fear +100)",
116  "(speed +6)(item_power +6)(armour +3)",
117  "(dam+3)(weapon speed 2)(Attacks: physical)",
118  "(Con+2)(item_power +1)",
119  "(Str+2)(Dex-1)(dam+3)(item_power +2)(armour +3)",
120  "(Str+2)(dam+3)(item_power +1)(armour +3)",
121  "(Cha+1)(ac+1)(Spell regen penalty 4)(armour +4)(resist blindness +1)",
122  "(ac+5)(Max speed 0.70)(Spell regen penalty 30)(armour +40)",
123  "(ac+1)(Max speed 1.20)",
124  "(ac+3)(Max speed 0.90)(Spell regen penalty 10)(armour +20)",
125  "(Cha-5)(ac+7)(item_power +10)(reflect spells)(reflect missiles)(armour +15)(resist fire +30)(resist drain +100)(resist ghosthit +80)",
126  "(ac+4)(item_power +6)(armour +10)(resist drain +100)(resist ghosthit +50)",
127  NULL };
128 
129  /* if you change the order, the result will quite certainly change, as the generation depends on the value returned
130  * by rand() - changing the order changes the item generated...
131  */
132  static const char *treasures[] = {
133  "random_knowledge",
134  "missile_weapons",
135  "random_talisman",
136  "rare_weapons",
137  "random_food",
138  "random_artifact",
139  "random_read",
140  "random_amulet",
141  "random_artifact",
142  "random_amulet",
143  "random_artifact",
144  "standard_old",
145  NULL
146  };
147  static const char *treasure_results[] = {
148  "",
149  "(wc+1)(dam+2)(Attacks: physical)",
150  "",
151  "(dam+6)(weapon speed 9)(Attacks: physical)",
152  "(food+200)",
153  "(Con+2)(Cha-1)(dam+10)(item_power +15)(weapon speed 5)(regeneration+1)(Attacks: weaponmagic)(resist drain +100)(resist poison +30)",
154  "",
155  "",
156  "(Str+1)(dam+9)(item_power +20)(weapon speed 6)(regeneration+1)(Attacks: electricity, drain)(resist magic +30)(resist electricity +30)(resist drain +100)",
157  "",
158  "(Str+1)(armour +3)",
159  "",
160  NULL
161  };
162 
163  for (check = 0; archs[check] != NULL; check++) {
164  test = cctk_create_game_object(archs[check]);
165  fail_unless(test != NULL, "couldn't create arch %s", archs[check]);
166  SET_FLAG(test, FLAG_IDENTIFIED);
167  describe_item(test, NULL, buf, sizeof(buf));
168 
169  /* if you're adding items, uncomment that so make finding the good value easier. */
170  /*
171  if (strcmp(buf, arch_results[check]))
172  printf("describe_item(%s) returned \"%s\" instead of \"%s\"\n", archs[check], buf, arch_results[check]);
173  */
174 
175  fail_unless(strcmp(buf, arch_results[check]) == 0, "describe_item(%s) returned \"%s\" instead of \"%s\"", archs[check], buf, arch_results[check]);
176 
177  free_object(test);
178  }
179 
180  /* we initialize the random generator to always be able to reproduce, and we use rand() in case RANDOM is something else. */
181  srand(100);
182  for (check = 0; treasures[check] != NULL; check++) {
183  list = find_treasurelist(treasures[check]);
184  fail_unless(list != NULL, "couldn't find treasure list %s", treasures[check]);
185  test = generate_treasure(list, 50);
186  fail_if(test == NULL, "couldn't create item from treasure list %s", treasures[check]);
187  SET_FLAG(test, FLAG_IDENTIFIED);
188  describe_item(test, NULL, buf, sizeof(buf));
189 
190  /* if you're adding lists, uncomment that so make finding the good value easier. */
191  /*
192  if (strcmp(buf, treasure_results[check]))
193  printf("Item %d describe_item(treasure %s) returned \"%s\" instead of \"%s\"\n", check, treasures[check], buf, treasure_results[check]);
194  */
195 
196  fail_unless(strcmp(buf, treasure_results[check]) == 0, "describe_item(treasure %s) returned \"%s\" instead of \"%s\"", treasures[check], buf, treasure_results[check]);
197 
198  free_object(test);
199 
200  }
201 }
202 END_TEST
203 
204 Suite *item_suite(void) {
205  Suite *s = suite_create("item");
206  TCase *tc_core = tcase_create("Core");
207 
208  /*setup and teardown will be called before each test in testcase 'tc_core' */
209  tcase_add_checked_fixture(tc_core, setup, teardown);
210 
211  suite_add_tcase(s, tc_core);
212  tcase_add_test(tc_core, test_describe_item);
213 
214  return s;
215 }
216 
217 int main(void) {
218  int nf;
219  Suite *s = item_suite();
220  SRunner *sr = srunner_create(s);
221 
222  srunner_set_xml(sr, LOGDIR "/unit/common/item.xml");
223  srunner_set_log(sr, LOGDIR "/unit/common/item.out");
224  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
225  nf = srunner_ntests_failed(sr);
226  srunner_free(sr);
227  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
228 }
void teardown(void)
Definition: check_item.c:46
#define SET_FLAG(xyz, p)
Definition: define.h:510
object * cctk_create_game_object(char *archname)
treasurelist * find_treasurelist(const char *name)
Definition: treasure.c:295
END_TEST Suite * item_suite(void)
Definition: check_item.c:204
int main(void)
Definition: check_item.c:217
#define FLAG_IDENTIFIED
Definition: define.h:557
START_TEST(test_describe_item)
Definition: check_item.c:50
void init_gods(void)
Definition: holy.c:63
void describe_item(const object *op, const object *owner, char *retbuf, size_t size)
Definition: item.c:1018
void cctk_setlog(char *logfile)
void cctk_init_std_archetypes(void)
void cctk_setdatadir(char *datadir)
object * generate_treasure(treasurelist *t, int difficulty)
Definition: treasure.c:524
void free_object(object *ob)
Definition: object.c:1238
void setup(void)
Definition: check_item.c:39