Crossfire Server, Trunk
check_alchemy.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_alchemy_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 crossfire-devel@real-time.com
27  */
28 
29 /*
30  * This is the unit tests file for server/alchemy.c
31  */
32 
33 #include <stdlib.h>
34 #include <check.h>
35 #include <global.h>
36 #include <assert.h>
37 
38 static void setup(void) {
39  /* put any initialisation steps here, they will be run before each testcase */
40 }
41 
42 static void teardown(void) {
43  /* put any cleanup steps here, they will be run after each testcase */
44 }
45 
46 /* copied from alchemy.c */
47 static float recipe_chance(const recipe *rp, const object *skill) {
48  assert(rp);
49  assert(skill);
50 
51  if (skill->level < rp->diff - 10)
52  return MAX(.01, .3 - (rp->diff - 10 - skill->level) * .03);
53 
54  if (skill->level <= rp->diff + 10)
55  return .5 + .02 * (float)(skill->level - rp->diff);
56 
57  return MIN(.95, .70 + (skill->level - rp->diff - 10) * .01);
58 }
59 
60 
61 START_TEST(test_recipe_chance) {
62  recipe rp;
63  object skill;
64  float chance;
65 
66  for (rp.diff = 0; rp.diff < 150; rp.diff++) {
67  for (skill.level = 0; skill.level < 150; skill.level++) {
68  chance = recipe_chance(&rp, &skill);
69  /* use .009 because of floating point issues */
70  fail_unless(chance >= .00999, "success can't be less than .01 but got %f for %d rp, %d skill", chance, rp.diff, skill.level);
71  fail_unless(chance <= .95, "success can't be more than .95 but got %f for %d rp, %d skill", chance, rp.diff, skill.level);
72  /*printf("%d %d => %f\n", rp.diff, skill.level, chance);*/
73  }
74  /*printf("\n");*/
75  }
76 }
77 END_TEST
78 
79 static Suite *alchemy_suite(void) {
80  Suite *s = suite_create("alchemy");
81  TCase *tc_core = tcase_create("Core");
82 
83  /*setup and teardown will be called before each test in testcase 'tc_core' */
84  tcase_add_checked_fixture(tc_core, setup, teardown);
85 
86  suite_add_tcase(s, tc_core);
87  tcase_add_test(tc_core, test_recipe_chance);
88 
89  return s;
90 }
91 
92 int main(void) {
93  int nf;
94  Suite *s = alchemy_suite();
95  SRunner *sr = srunner_create(s);
96 
97  srunner_set_xml(sr, LOGDIR "/unit/server/alchemy.xml");
98  srunner_set_log(sr, LOGDIR "/unit/server/alchemy.out");
99  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
100  nf = srunner_ntests_failed(sr);
101  srunner_free(sr);
102  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
103 }
global.h
alchemy_suite
static END_TEST Suite * alchemy_suite(void)
Definition: check_alchemy.c:79
MIN
#define MIN(x, y)
Definition: compat.h:21
teardown
static void teardown(void)
Definition: check_alchemy.c:42
MAX
#define MAX(x, y)
Definition: compat.h:24
recipe_chance
static float recipe_chance(const recipe *rp, const object *skill)
Definition: check_alchemy.c:47
recipestruct
Definition: recipe.h:10
setup
static void setup(void)
Definition: check_alchemy.c:38
recipestruct::diff
int diff
Definition: recipe.h:16
START_TEST
START_TEST(test_recipe_chance)
Definition: check_alchemy.c:61
main
int main(void)
Definition: check_alchemy.c:92
obj::level
int16_t level
Definition: object.h:356