Crossfire Server, Trunk  R20513
check_living.c
Go to the documentation of this file.
1 /*
2  * This is the unit tests file for common/living.c
3  */
4 
5 #include "global.h"
6 
7 #include <check.h>
8 #include <stdlib.h>
9 
10 #include "toolkit_common.h"
11 
12 static void setup(void) {
13  cctk_setdatadir(BUILD_ROOT "lib");
14  cctk_setconfdir(BUILD_ROOT "lib/config");
15  cctk_setlog(LOGDIR "/unit/common/living.out");
18 }
19 
20 static void teardown(void) {
21  /* put any cleanup steps here, they will be run after each testcase */
22 }
23 
32 START_TEST(test_fix_object) {
33 #define TESTS 50
34 #define ARCHS 3
35  static const char *archs[ARCHS] = { "pl_dragon", "pl_half_orc", "human_player" };
36 
37  object *ob, *grace, *mana;
38  player *pl;
39  int test,i;
40 
47  int wc[3][50] = {
48  { 21, 3, 3, 22, 2, 10, 18, 10, 20, 14, 1, 14, 15, 9, 19, 4, 2, 3, 16, 17, 9, 2, 11, 14, 4, 11, 14, 12, 8, 20, 16, 21, 19, 3, 12, 6, 14, 8, 10, 14, 15, 17, 6, 21, 16, 9, 3, 19, 2, 20},
49  { 6, 23, 1, 2, 6, 14, 7, 6, 23, 13, 17, 9, 9, 19, 11, 18, 5, 15, 2, 3, 15, 9, 13, 13, 6, 19, 5, 21, 2, 12, 15, 6, 23, 19, 7, 20, 19, 3, 22, 7, 7, 12, 14, 15, 7, 15, 8, 10, 23, 11},
50  { 1, 8, 5, 16, 16, 15, 9, 21, 4, 16, 20, 17, 12, 19, 1, 3, 23, 4, 2, 10, 17, 3, 20, 6, 19, 21, 1, 7, 12, 20, 8, 2, 23, 17, 17, 18, 22, 17, 6, 3, 17, 3, 18, 13, 6, 3, 5, 17, 23, 1}
51  };
52 
53  int maxgr[3][50] = {
54  { 333, 236, 182, 225, 160, 36, 53, 78, 221, 130, 204, 246, 242, 256, 38, 67, 419, 145, 282, 245, 315, 208, 340, 81, 120, 232, 176, 260, 354, 111, 332, 251, 249, 90, 215, 62, 42, 372, 241, 196, 216, 46, 94, 47, 264, 102, 86, 126, 289, 107},
55  { 62, 119, 164, 309, 223, 236, 124, 98, 38, 302, 209, 155, 148, 134, 90, 220, 96, 262, 111, 126, 92, 236, 206, 131, 269, 255, 222, 88, 58, 102, 126, 111, 215, 170, 132, 156, 46, 153, 203, 88, 236, 43, 69, 167, 123, 36, 56, 168, 359, 52},
56  { 42, 192, 232, 110, 164, 287, 90, 238, 160, 242, 225, 258, 66, 226, 165, 114, 189, 208, 88, 255, 97, 83, 40, 254, 130, 88, 109, 238, 238, 226, 46, 231, 172, 206, 42, 148, 126, 82, 97, 162, 161, 232, 108, 152, 205, 190, 212, 80, 54, 152}
57  };
58 
59  int maxsp[3][50] = {
60  { 64, 403, 72, 106, 156, 234, 214, 194, 183, 432, 219, 70, 208, 108, 140, 224, 263, 218, 211, 178, 194, 160, 48, 106, 165, 100, 349, 245, 218, 239, 336, 247, 323, 243, 148, 37, 160, 148, 132, 362, 156, 238, 286, 217, 229, 166, 156, 178, 245, 220},
61  { 159, 218, 84, 115, 154, 159, 70, 78, 46, 243, 167, 142, 70, 130, 158, 164, 66, 180, 297, 252, 188, 142, 284, 80, 251, 61, 78, 207, 188, 210, 228, 236, 124, 32, 224, 228, 39, 246, 69, 188, 200, 210, 242, 158, 112, 184, 278, 229, 346, 220},
62  { 74, 64, 156, 240, 217, 297, 114, 276, 82, 265, 273, 212, 207, 253, 172, 146, 110, 88, 72, 68, 220, 251, 224, 94, 135, 53, 151, 181, 150, 214, 92, 218, 184, 126, 172, 170, 58, 224, 90, 50, 152, 181, 208, 79, 79, 163, 106, 31, 62, 231}
63  };
64 
65  int maxhp[3][50] = {
66  { 79, 466, 246, 36, 238, 170, 80, 218, 196, 358, 279, 112, 110, 164, 62, 300, 380, 224, 119, 117, 358, 300, 218, 162, 212, 153, 306, 136, 178, 90, 186, 73, 304, 226, 188, 260, 114, 293, 267, 352, 106, 90, 244, 150, 164, 182, 228, 90, 476, 132},
67  { 210, 34, 246, 351, 198, 254, 211, 196, 34, 188, 162, 196, 162, 140, 146, 72, 210, 158, 349, 264, 138, 164, 154, 136, 192, 91, 258, 82, 256, 278, 102, 238, 93, 64, 188, 171, 64, 420, 39, 184, 303, 202, 258, 104, 206, 121, 274, 346, 130, 142},
68  { 259, 176, 219, 100, 92, 344, 164, 100, 218, 152, 248, 90, 182, 64, 242, 226, 110, 214, 239, 152, 150, 364, 54, 194, 112, 50, 250, 268, 138, 85, 174, 240, 32, 90, 82, 74, 45, 82, 284, 226, 109, 246, 78, 154, 222, 254, 202, 82, 33, 248}
69  };
70 
71  int ac[3][50] = {
72  { 4, -7, -7, 6, -7, -7, -2, -7, 2, -7, -7, -7, -7, -7, 1, -7, -7, -7, -4, -2, -7, -7, -7, -7, -7, -7, -7, -7, -7, 2, -4, 4, 1, -7, -7, -7, -7, -7, -7, -7, -6, -3, -7, 3, -4, -7, -7, 0, -7, 3},
73  { 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13},
74  { 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}
75  };
76 
77 
82  int wis[ARCHS][TESTS] = {
83  {
84  28, 26, 25, 25, 5, 16, 21, 7, 11, 25, 12, 16, 22, 23, 20, 7, 30, 20, 30, 28,
85  26, 16, 29, 24, 27, 19, 12, 26, 29, 23, 30, 14, 15, 2, 21, 13, 9, 30, 17, 19,
86  2, 13, 9, 24, 26, 21, 8, 28, 28, 7
87  },{
88  15, 19, 16, 26, 24, 19, 2, 16, 1, 27, 19, 15, 28, 21, 20, 3, 11, 23, 8, 4, 22,
89  12, 7, 17, 27, 21, 6, 9, 12, 7, 9, 28, 9, 4, 12, 1, 15, 2, 27, 2, 13, 20, 1, 24, 15, 7,
90  3, 11, 27, 1
91 
92  }, {
93  3, 11, 14, 13, 7, 11, 3, 5, 6, 13, 15, 23, 4, 13, 29, 10, 15, 10, 7, 19, 19,
94  14, 9, 25, 9, 11, 23, 29, 27, 30, 23, 30, 7, 16, 10, 9, 18, 26, 9, 11, 28, 8, 14, 25,
95  17, 5, 19, 14, 21, 12
96  }
97  };
98 
99  int intelligence[ARCHS][TESTS] = {
100  {
101  2, 7, 21, 10, 22, 2, 21, 16, 23, 27, 8, 24, 11, 27, 3, 26, 28, 1, 11, 3, 11, 27,
102  3, 15, 25, 13, 8, 25, 14, 12, 29, 11, 30, 30, 24, 5, 23, 3, 8, 10, 22, 21, 29,
103  5, 26, 11, 7, 4, 24, 30
104  },{
105  20, 18, 16, 15, 27, 2, 3, 9, 19, 21, 10, 4, 19, 8, 14, 13, 26, 7, 28, 26, 30, 27,
106  30, 2, 27, 16, 13, 22, 5, 5, 24, 20, 19, 18, 3, 17, 23, 11, 26, 11, 19, 3, 22, 29,
107  5, 10, 22, 12, 26, 8
108  },{
109  2, 20, 17, 5, 30, 9, 28, 30, 16, 19, 4, 6, 12, 28, 2, 2, 30, 24, 13, 10, 1, 15, 27,
110  16, 3, 23, 28, 10, 27, 4, 26, 4, 13, 9, 19, 23, 19, 19, 18, 13, 1, 24, 7, 12, 22,
111  18, 14, 11, 6, 26
112  }
113  };
114  int pow[ARCHS][TESTS] = {
115  {
116  19, 30, 16, 11, 5, 14, 6, 22, 28, 30, 19, 4, 11, 1, 4, 24, 28, 5, 17, 19, 29, 22,
117  23, 21, 6, 13, 29, 6, 5, 18, 25, 17, 30, 2, 21, 22, 1, 27, 27, 30, 9, 2, 21, 26,
118  23, 16, 11, 16, 30, 24
119  }, {
120  14, 7, 12, 27, 1, 28, 17, 12, 5, 22, 24, 18, 12, 23, 3, 3, 1, 21, 27, 20, 18, 2, 18,
121  14, 7, 17, 21, 20, 16, 28, 8, 20, 27, 3, 8, 27, 9, 29, 13, 12, 27, 23, 28, 7, 16, 15,
122  26, 29, 30, 8
123  }, {
124  15, 8, 15, 12, 12, 30, 7, 21, 2, 22, 29, 5, 21, 1, 1, 12, 29, 1, 13, 12, 22, 28, 11,
125  1, 21, 12, 11, 24, 10, 17, 6, 9, 10, 8, 6, 8, 15, 4, 25, 3, 17, 16, 7, 18, 18, 18, 2,
126  11, 9, 12
127  }
128  };
129  int con[ARCHS][TESTS] = {
130  {
131  19, 30, 16, 11, 5, 14, 6, 22, 28, 30, 19, 4, 11, 1, 4, 24, 28, 5, 17, 19, 29, 22,
132  23, 21, 6, 13, 29, 6, 5, 18, 25, 17, 30, 2, 21, 22, 1, 27, 27, 30, 9, 2, 21, 26,
133  23, 16, 11, 16, 30, 24
134  }, {
135  14, 7, 12, 27, 1, 28, 17, 12, 5, 22, 24, 18, 12, 23, 3, 3, 1, 21, 27, 20, 18, 2, 18,
136  14, 7, 17, 21, 20, 16, 28, 8, 20, 27, 3, 8, 27, 9, 29, 13, 12, 27, 23, 28, 7, 16, 15,
137  26, 29, 30, 8
138  }, {
139  15, 8, 15, 12, 12, 30, 7, 21, 2, 22, 29, 5, 21, 1, 1, 12, 29, 1, 13, 12, 22, 28, 11,
140  1, 21, 12, 11, 24, 10, 17, 6, 9, 10, 8, 6, 8, 15, 4, 25, 3, 17, 16, 7, 18, 18, 18, 2,
141  11, 9, 12
142  }
143  };
144  int ob_level[ARCHS][TESTS] = {
145  {
146  12, 103, 103, 6, 109, 70, 30, 69, 18, 49, 112, 46, 45, 72, 21, 100, 110, 102,
147  37, 31, 74, 110, 64, 46, 96, 64, 48, 58, 79, 20, 38, 14, 22, 103, 59, 90, 47,
148  79, 66, 46, 43, 35, 87, 15, 37, 71, 104, 25, 108, 16
149  }, {
150  90, 4, 113, 108, 89, 47, 83, 88, 4, 54, 31, 73, 71, 25, 63, 26, 95, 44, 107,
151  102, 44, 72, 52, 53, 86, 23, 94, 11, 108, 59, 41, 89, 5, 22, 84, 18, 22,
152  105, 6, 82, 84, 56, 49, 42, 83, 43, 77, 68, 4, 61
153  }, {
154  112, 78, 92, 40, 36, 42, 72, 15, 99, 36, 19, 35, 56, 22, 111, 103, 4,
155  97, 107, 66, 35, 102, 17, 87, 21, 15, 115, 84, 59, 20, 77,
156  110, 2, 35, 31, 27, 6, 31, 87, 103, 32, 103, 29, 52, 86, 102, 91, 31, 3, 114
157  }
158  };
159  int grace_level[ARCHS][TESTS] = {
160  {
161  112, 40, 61, 85, 70, 5, 15, 29, 83, 7, 92, 113, 105, 114, 8, 23, 90, 62, 42, 68, 93, 89, 94,
162  13, 21, 103, 50, 100, 111, 34, 58, 115, 70, 35, 89, 19, 11, 71, 92, 40, 98, 13, 37, 2, 93,
163  35, 33, 10, 48, 43
164  }, {
165  21, 49, 72, 104, 93, 83, 52, 39, 8, 104, 83, 66, 23, 46, 35, 100, 38, 108,
166  35, 53, 27, 108, 93, 54, 95, 111, 101, 34, 19, 27, 53, 8, 86, 75, 56, 64, 13, 48,
167  59, 34, 93, 4, 16, 62, 51, 6, 17, 47, 92, 16
168  }, {
169  11, 86, 106, 45, 72, 93, 35, 109, 70, 109, 72, 113, 23, 103, 18, 47, 54,
170  94, 34, 115, 31, 11, 10, 104, 55, 34, 36, 41, 78, 14, 7, 20, 76, 93, 11, 64, 50,
171  12, 34, 71, 27, 106, 44, 45, 89, 85, 96, 30, 14, 66
172  }
173  };
174 
175  int mana_level[ARCHS][TESTS] = {
176  {
177  22, 107, 22, 43, 68, 107, 97, 79, 34, 101, 99, 25, 94, 40, 60, 75, 58, 99, 95, 79, 28, 46, 9,
178  37, 71, 40, 107, 111, 99, 108, 108, 113, 19, 79, 51, 4, 70, 36, 26, 85, 68, 109, 97, 79, 82,
179  73, 68, 79, 17, 41
180  }, {
181  68, 99, 32, 14, 63, 33, 25, 29, 13, 101, 61, 61, 25, 48, 69, 72, 23, 78, 84,
182  99, 37, 56, 85, 30, 106, 18, 24, 85, 84, 57, 103, 101, 17, 2, 102, 70, 9, 54, 14, 84, 55, 90,
183  65, 46, 46, 82, 101, 45, 63, 100
184  }, {
185  27, 22, 67, 110, 57, 53, 33, 78, 31, 113, 71, 96, 89, 108, 76, 63, 3, 34,
186  26, 24, 99, 73, 89, 37, 57, 14, 48, 68, 53, 97, 32, 99, 82, 53, 76, 75, 17, 102, 14, 15, 66,
187  72, 94, 28, 23, 67, 43, 1, 21, 96
188  }
189  };
190 
191 
193 #define GENERATE 0
194 
195 #if GENERATE
197  StringBuffer *smaxgr = stringbuffer_new();
198  StringBuffer *smaxsp = stringbuffer_new();
199  StringBuffer *smaxhp = stringbuffer_new();
201  const char *sep = "";
202 
203  stringbuffer_append_printf(swc, "\tint wc[%d][%d] = {\n\t", ARCHS, TESTS);
204  stringbuffer_append_printf(smaxgr, "\tint maxgr[%d][%d] = {\n\t", ARCHS, TESTS);
205  stringbuffer_append_printf(smaxsp, "\tint maxsp[%d][%d] = {\n\t", ARCHS, TESTS);
206  stringbuffer_append_printf(smaxhp, "\tint maxhp[%d][%d] = {\n\t", ARCHS, TESTS);
207  stringbuffer_append_printf(sac, "\tint ac[%d][%d] = {\n\t", ARCHS, TESTS);
208 #endif
209 
210  pl = calloc(1, sizeof(player));
211 
212  for (i = 0; i < ARCHS; i++)
213  {
214 #if GENERATE
215  stringbuffer_append_printf(swc, "%s{ ", sep);
216  stringbuffer_append_printf(smaxgr, "%s{ ", sep);
217  stringbuffer_append_printf(smaxsp, "%s{ ", sep);
218  stringbuffer_append_printf(smaxhp, "%s{ ", sep);
219  stringbuffer_append_printf(sac, "%s{ ", sep);
220  sep = "";
221 #endif
222 
223  ob = create_archetype(archs[i]);
224  fail_unless(ob != NULL, "invalid archetype %s", archs[i]);
225  if (ob->type == PLAYER)
226  ob->contr = pl;
227 
228  grace = create_archetype("skill_praying");
229  object_insert_in_ob(grace, ob);
230  mana = create_archetype("skill_pyromancy");
231  object_insert_in_ob(mana, ob);
232 
233  for (test = 0; test < TESTS; test++)
234  {
235 
236  ob->contr->orig_stats.Wis = wis[i][test];
237  ob->contr->orig_stats.Int = intelligence[i][test];
238  ob->contr->orig_stats.Pow = pow[i][test];
239  ob->contr->orig_stats.Con = con[i][test];
240 
241  ob->level = ob_level[i][test];
242  grace->level = grace_level[i][test];
243  mana->level = mana_level[i][test];
244 
245  fix_object(ob);
246 
247 #if GENERATE
248  stringbuffer_append_printf(swc, "%s%d", sep, ob->stats.wc);
249  stringbuffer_append_printf(smaxgr, "%s%d", sep, ob->stats.maxgrace);
250  stringbuffer_append_printf(smaxsp, "%s%d", sep, ob->stats.maxsp);
251  stringbuffer_append_printf(smaxhp, "%s%d", sep, ob->stats.maxhp);
252  stringbuffer_append_printf(sac, "%s%d", sep, ob->stats.ac);
253  sep = ", ";
254 #else
255  fail_unless(ob->stats.wc == wc[i][test], "wc [test %d, arch %d]: got %d instead of %d", test, i, ob->stats.wc, wc[i][test]);
256  fail_unless(ob->stats.maxgrace == maxgr[i][test], "gr: got %d instead of %d", ob->stats.maxgrace, maxgr[i][test]);
257  fail_unless(ob->stats.maxsp == maxsp[i][test], "sp: got %d instead of %d", ob->stats.maxsp, maxsp[i][test]);
258  fail_unless(ob->stats.maxhp == maxhp[i][test], "hp: [test %d, arch %d] [con %d level %d] got %d instead of %d", test, i, con[i][test], ob->level, ob->stats.maxhp, maxhp[i][test]);
259  fail_unless(ob->stats.ac == ac[i][test], "ac: got %d instead of %d", ob->stats.ac, ac[i][test]);
260 #endif
261  }
263 
264 #if GENERATE
265  stringbuffer_append_string(swc, "}\n\t");
266  stringbuffer_append_string(smaxgr, "}\n\t");
267  stringbuffer_append_string(smaxsp, "}\n\t");
268  stringbuffer_append_string(smaxhp, "}\n\t");
269  stringbuffer_append_string(sac, "}\n\t");
270 #endif
271  }
272 
273  free(pl);
274 
275 #if GENERATE
276  stringbuffer_append_string(swc, "\n}; \n");
277  stringbuffer_append_string(smaxgr, "\n}; \n");
278  stringbuffer_append_string(smaxsp, "\n}; \n");
279  stringbuffer_append_string(smaxhp, "\n}; \n");
280  stringbuffer_append_string(sac, "\n}; \n");
281  {
282  char *pwc = stringbuffer_finish(swc);
283  char *pmaxgr = stringbuffer_finish(smaxgr);
284  char *pmaxsp = stringbuffer_finish(smaxsp);
285  char *pmaxhp = stringbuffer_finish(smaxhp);
286  char *pac = stringbuffer_finish(sac);
287  printf("%s\n", pwc);
288  printf("%s\n", pmaxgr);
289  printf("%s\n", pmaxsp);
290  printf("%s\n", pmaxhp);
291  printf("%s\n", pac);
292  free(pwc);
293  free(pmaxgr);
294  free(pmaxsp);
295  free(pmaxhp);
296  free(pac);
297  }
298  fail("Here are the results.");
299 #endif
300 }
301 END_TEST
302 
303 static Suite *living_suite(void) {
304  Suite *s = suite_create("living");
305  TCase *tc_core = tcase_create("Core");
306 
307  /*setup and teardown will be called before each test in testcase 'tc_core' */
308  tcase_add_checked_fixture(tc_core, setup, teardown);
309 
310  suite_add_tcase(s, tc_core);
311  tcase_add_test(tc_core, test_fix_object);
312 
313  return s;
314 }
315 
316 int main(void) {
317  int nf;
318  Suite *s = living_suite();
319  SRunner *sr = srunner_create(s);
320 
321  /* to debug, uncomment this line */
322  /*srunner_set_fork_status(sr, CK_NOFORK);*/
323 
324  srunner_set_xml(sr, LOGDIR "/unit/common/living.xml");
325  srunner_set_log(sr, LOGDIR "/unit/common/living.out");
326  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
327  nf = srunner_ntests_failed(sr);
328  srunner_free(sr);
329  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
330 }
int8_t Int
Definition: living.h:35
One player.
Definition: player.h:92
int8_t ac
Armour Class, how hard to hit, the lower the better.
Definition: living.h:37
void cctk_setdatadir(const char *datadir)
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.c:57
int16_t maxgrace
Maximum grace.
Definition: living.h:44
#define ARCHS
void init_experience(void)
This loads the experience table from the exp_table file.
Definition: exp.c:167
#define TESTS
Global type definitions and header inclusions.
static END_TEST Suite * living_suite(void)
Definition: check_living.c:303
int16_t maxsp
Max spell points.
Definition: living.h:42
int8_t Con
Definition: living.h:35
int16_t maxhp
Max hit points.
Definition: living.h:40
#define FREE_OBJ_FREE_INVENTORY
Free inventory objects; if not set, drop inventory.
Definition: object.h:532
int main(void)
Definition: check_living.c:316
object * create_archetype(const char *name)
Finds which archetype matches the given name, and returns a new object containing a copy of the arche...
Definition: arch.c:620
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Append a string to a string buffer instance.
Definition: stringbuffer.c:95
object * object_insert_in_ob(object *op, object *where)
This function inserts the object op in the linked list inside the object environment.
Definition: object.c:2690
int8_t Wis
Definition: living.h:35
living orig_stats
Permanent real stats of player.
Definition: player.h:148
void cctk_setlog(const char *logfile)
set the logdir to use
struct pl * contr
Pointer to the player which control this object.
Definition: object.h:276
int8_t wc
Weapon Class, how skilled, the lower the better.
Definition: living.h:36
See Player.
Definition: object.h:107
START_TEST(test_fix_object)
Note that the test and results here make assumptions about certain values that can change...
Definition: check_living.c:32
living stats
Str, Con, Dex, etc.
Definition: object.h:368
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
void object_free2(object *ob, int flags)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.c:1391
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Append a formatted string to a string buffer instance.
Definition: stringbuffer.c:104
int8_t Pow
Definition: living.h:35
void cctk_init_std_archetypes(void)
Loads up to archetype initialisation using standard crossfire files in source tree.
A buffer that will be expanded as content is added to it.
Definition: stringbuffer.c:25
static void setup(void)
Definition: check_living.c:12
void cctk_setconfdir(const char *confdir)
static void teardown(void)
Definition: check_living.c:20
int16_t level
Level of creature or object.
Definition: object.h:351
void fix_object(object *op)
Updates all abilities given by applied objects in the inventory of the given object.
Definition: living.c:1120
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.c:76