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