Crossfire Server, Trunk
check_weight_reduction.c
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2009 Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
28 #include <check.h>
29 #include <global.h>
30 #include <sproto.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <toolkit_common.h>
34 
35 static void setup(void) {
36 }
37 
38 static void teardown(void) {
39 }
40 
41 START_TEST(test_weight_reduction) {
42  /* Weight reduction to apply, last value -1. */
43  const int reduction[] = { 0, 30, 50, 70, -1 };
44  /* Container archetypes, must be CONTAINER, last value NULL. */
45  const char *archs[] = { "sack", "luggage", "luggage", "luggage", NULL };
46  /* What to put into the containers, last value NULL. Must be mergeable items. */
47  const char *items[] = { "scroll", "crown", "gem", "tooth", NULL };
48  /* How many to put or remove from container. Sum should be 0, last value 0. */
49  const int add[] = { 2, 5, 7, 3, 3, 9, -5, -7, 11, -8, -9, -11, 0 };
50  int red, a, i, nrof;
51  uint32_t sum;
52  signed long carrying;
53  object *container, *item;
54 
55  for (red = 0; reduction[red] != -1; red++) {
56  for (a = 0; archs[a] != NULL; a++) {
57  container = create_archetype(archs[a]);
58  fail_unless(container != NULL, "couldn't find container arch %s!", archs[a]);
59  fail_unless(container->type == CONTAINER, "%s isn't a container, type %d instead of %d!", archs[a], container->type, CONTAINER);
60  fail_unless(container->carrying == 0, "%s has already some carrying?", archs[a]);
61 
62  container->stats.Str = reduction[red];
63 
64  for (i = 0; items[i] != NULL; i++) {
65  item = create_archetype(items[i]);
66  fail_unless(item != NULL, "couldn't find item %s to put", items[i]);
67  fail_unless(item->nrof == 1, "can't test item %s with nrof != 1", items[i]);
68  sum = 1;
69 
70  object_insert_in_ob(item, container);
71  carrying = (signed long)(item->weight * (100 - container->stats.Str) / 100);
72  fail_unless(container->carrying == carrying, "invalid weight %d instead of %d for %s in %s reduction %d!", container->carrying, carrying, items[i], archs[a], reduction[red]);
73 
74  for (nrof = 0; add[nrof] != 0; nrof++) {
75  sum += add[nrof];
76  if (add[nrof] > 0) {
77  item = create_archetype(items[i]);
78  item->nrof = add[nrof];
79  item = object_insert_in_ob(item, container);
80  } else {
81  object_decrease_nrof(item, - add[nrof]);
82  }
83  fail_unless(item->nrof == sum, "invalid item count %d instead of %d", item->nrof, sum);
84  carrying = (signed long)(item->nrof * item->weight * (100 - container->stats.Str) / 100);
85  fail_unless(container->carrying == carrying, "invalid weight %d instead of %d for %s in %s reduction %d after adding %d!", container->carrying, carrying, items[i], archs[a], reduction[red], add[nrof]);
86  }
88  object_free(item, 0);
89  fail_unless(container->carrying == 0, "container %s is still carrying %d from %s instead of 0!", archs[a], container->carrying, items[i]);
90  }
91  object_free(container, 0);
92  }
93  }
94 }
95 END_TEST
96 
97 static Suite *bug_suite(void) {
98  Suite *s = suite_create("bug");
99  TCase *tc_core = tcase_create("Core");
100 
101  tcase_add_checked_fixture(tc_core, setup, teardown);
102 
103  suite_add_tcase(s, tc_core);
104  tcase_add_test(tc_core, test_weight_reduction);
105  tcase_set_timeout(tc_core, 0);
106 
107  return s;
108 }
109 
110 int main(void) {
111  int nf;
112  Suite *s = bug_suite();
113  SRunner *sr = srunner_create(s);
114 
115  srunner_set_fork_status(sr, CK_NOFORK);
116  cctk_setdatadir(SOURCE_ROOT "lib");
117  init(0, NULL);
118 
119  srunner_set_xml(sr, LOGDIR "/bugs/bugtrack/weight_reduction.xml");
120  srunner_set_log(sr, LOGDIR "/bugs/bugtrack/weight_reduction.out");
121  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
122  nf = srunner_ntests_failed(sr);
123  srunner_free(sr);
124  return nf == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
125 }
126 
global.h
object_free
void object_free(object *ob, int flags)
Definition: object.c:1578
object_remove
void object_remove(object *op)
Definition: object.c:1819
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.c:69
liv::Str
int8_t Str
Definition: living.h:36
disinfect.a
a
Definition: disinfect.py:13
toolkit_common.h
CONTAINER
@ CONTAINER
Definition: object.h:231
obj::carrying
int32_t carrying
Definition: object.h:372
sproto.h
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:281
obj::type
uint8_t type
Definition: object.h:343
obj::stats
living stats
Definition: object.h:373
item
Definition: item.py:1
init
void init(int argc, char **argv)
Definition: init.c:1108
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2833
setup
static void setup(void)
Definition: check_weight_reduction.c:35
teardown
static void teardown(void)
Definition: check_weight_reduction.c:38
object_decrease_nrof
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.c:2652
bug_suite
static END_TEST Suite * bug_suite(void)
Definition: check_weight_reduction.c:97
say.item
dictionary item
Definition: say.py:149
main
int main(void)
Definition: check_weight_reduction.c:110
START_TEST
START_TEST(test_weight_reduction)
Definition: check_weight_reduction.c:41