Debian Codegraph @

test/unit/common/check_treasure.c

raw symbols SCIP
1 /*
2 * static char *rcsid_check_treasure_c =
3 * "$Id$";
4 */
5
6 /*
7 * CrossFire, A Multiplayer game for X-windows
8 *
9 * Copyright (C) 2002,2011 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/treasure.c
31 */
32
33 #include <stdlib.h>
34 #include <check.h>
35 #include <global.h>
36 #include <toolkit_common.h>
37
38 static void setup(void) {
39 cctk_setdatadir(BUILD_ROOT "lib");
40 cctk_setlog(LOGDIR "/unit/common/object.out");
41 printf("set log to %s\n", LOGDIR"/unit/common/object.out");
42 cctk_init_std_archetypes();
43 }
44
45 static void teardown(void) {
46 /* put any cleanup steps here, they will be run after each testcase */
47 }
48
49 /* Note: This file is far from a complete test of all treasurelists.
50 * but it tests at least one function from treasure.c.
51 */
52
53 /*
54 * This tests that the treasurelist_find_matching_type()
55 * works as expected.
56 * Unfortunately, this requires on the contents of treasurelists,
57 * which sit in the arch tree and can change over time.
58 *
59 * TODO: Add more checks. Started out with basic check
60 * that was needed for skill system.
61 *
62 * The setup is this - treasurelists[] contain the list to look
63 * at. items[][] is the types that we should find.
64 * types[] is what type we are checking for.
65 *
66 * In the initial setup, we check the same treasurelist, but
67 * for different types - in particular, to make sure we
68 * properly handle case with nothing matching.
69 */
70 START_TEST(test_treasurelist_find_matching_type) {
71 #define NUM_TREASURE_LISTS 2
72
73 /* We want to use the top level treasure list - in that way,
74 * we can test that the recursion works properly
75 */
76 const char *treasurelists[NUM_TREASURE_LISTS] = {"monk_class_items",
77 "monk_class_items"};
78 const char *items[NUM_TREASURE_LISTS][100] = { {
79 #if 0
80 /* Until new skill system is commited, these do not
81 * exist.
82 */
83 "skill_missile_weapon_9",
84 "skill_sword_9", "skill_axe_9", "skill_blunt_weapon_9",
85 "skill_spear_9", "skill_unarmed_combat_3", "skill_armor_3",
86 "skill_dodge_3", "skill_air_magic_8", "skill_fire_magic_8",
87 "skill_water_magic_8", "skill_earth_magic_8", "skill_divine_power_5",
88 "skill_trap_5", "skill_thievery_5", "skill_persuasion_6",
89 "skill_acrobatics_3", "skill_literacy_4", "skill_weaponsmith_9",
90 "skill_armorer_4", "skill_jeweler_8", "skill_alchemy_8",
91 "skill_bowyer_9", "skill_thaumaturgy_8", "skill_woodsman_5",
92 #endif
93 /* Following are legacy skills */
94 "skill_sense_curse", "skill_sense_magic", "skill_meditation",
95 "skill_karate","skill_praying","skill_missile_weapon",
96 "skill_punching","skill_literacy","skill_use_magic_item",
97 "skill_remove_trap","skill_find_traps","skill_throwing",
98 NULL
99 },
100 { NULL }
101 };
102 int types[NUM_TREASURE_LISTS] = {SKILL, HOLY_ALTAR };
103 int tr;
104
105 for (tr=0; tr < NUM_TREASURE_LISTS; tr++) {
106 struct treasureliststruct *randomitems;
107 int *results=NULL, list_size;
108 objectlink *ol;
109
110 randomitems = find_treasurelist(treasurelists[tr]);
111
112 fail_unless(randomitems != NULL, "Could not find treasurelist %s",
113 treasurelists[tr]);
114
115 /* Figure out number of items that this one should
116 * return.
117 */
118 for (list_size=0; items[tr][list_size] != NULL; list_size++) ;
119
120 if (list_size)
121 results = calloc (list_size, sizeof(*results));
122
123 /* Last parameter is if we traverse treasurelists - that
124 * should probably be another test case, but I suspect
125 * that might be something that is always set.
126 */
127 ol = treasurelist_find_matching_type(randomitems, types[tr], TRUE);
128
129 fail_if(ol && (list_size == 0),
130 "On test %d, should have returned null but did not", tr);
131
132 fail_if((ol == NULL) && (list_size != 0),
133 "On test %d, should have returned results but returned NULL",
134 tr);
135
136 /* Based on above, this could be simplified to be just
137 * ol != NULL, but this is clearer and performance
138 * is not important.
139 */
140 if ((ol !=NULL) && (list_size != 0)) {
141 int i;
142 objectlink *oltmp;
143
144 for (oltmp = ol; oltmp != NULL; oltmp = oltmp->next) {
145 fail_if(oltmp->ob == NULL,
146 "On test %d, found NULL object on linked list", tr);
147
148 /* We do this loop because treasurelist_find_matching_type()
149 * specifically says that the order of results
150 * is not deterministic.
151 */
152 for (i=0; i < list_size; i++) {
153 if (!strcmp(items[tr][i], oltmp->ob->arch->name)) {
154 results[i]++;
155 break;
156 }
157 }
158
159 /* Did not find match */
160 fail_if(i == list_size,
161 "On test %d, did not find match for %s", tr,
162 oltmp->ob->arch->name);
163 }
164 /* treasurelist_find_matching_type() does state multiple
165 * objects of the same type may be returned - to properly
166 * check for this, we would need to have a result table
167 * for each test. What I am more concerned is that it
168 * finds all the objects it should, and does not find
169 * any it should not.
170 */
171 for (i=0; i < list_size; i++) {
172 fail_if(results[i] == 0, "On test %d, got 0 matches for %s",
173 tr, items[tr][i]);
174 }
175 }
176 free(results);
177 if (ol) free_objectlink(ol);
178 } /* for loop */
179 }
180
181
182
183 END_TEST
184
185 static Suite *treasure_suite(void) {
186 Suite *s = suite_create("treasure");
187 TCase *tc_core = tcase_create("Core");
188
189 /*setup and teardown will be called before each test in testcase 'tc_core' */
190 tcase_add_checked_fixture(tc_core, setup, teardown);
191
192 suite_add_tcase(s, tc_core);
193 tcase_add_test(tc_core, test_treasurelist_find_matching_type);
194
195 return s;
196 }
197
198 int main(void) {
199 int nf;
200 Suite *s = treasure_suite();
201 SRunner *sr = srunner_create(s);
202
203
204 /* to debug, uncomment this line */
205 srunner_set_fork_status(sr, CK_NOFORK);
206
207 srunner_set_xml(sr, LOGDIR "/unit/common/treasure.xml");
208 srunner_set_log(sr, LOGDIR "/unit/common/treasure.out");
209 srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
210 nf = srunner_ntests_failed(sr);
211 srunner_free(sr);
212 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
213 }